帮我改以下代码的错#include <stdio.h> int main() { int mem_page_num=3; int seq_len=17; int page_seq[] = { 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1 }; int result = opt_missing_page_num(page_seq, seq_len, mem_page_num); printf("%d", result); return 0; } int opt_missing_page_num(int page_seq[], int seq_len, int mem_page_num) { int mem_page[mem_page_num]; int page_fault = 0; int next_use[mem_page_num]; int i, j, k, max_next_use; for (i = 0; i < mem_page_num; i++) { mem_page[i] = -1; next_use[i] = seq_len; } for (i = 0; i < seq_len; i++) { int page = page_seq[i]; int found = 0; for (j = 0; j < mem_page_num; j++) { if (mem_page[j] == page) { found = 1; next_use[j] = seq_len; break; } } if (!found) { int victim = -1; for (j = 0; j < mem_page_num; j++) { int use_time = next_use[j]; if (use_time < seq_len && (victim == -1 || use_time < next_use[victim])) { victim = j; } } mem_page[victim] = page; next_use[victim] = seq_len; page_fault++; } for (j = 0; j < mem_page_num; j++) { int page2 = mem_page[j]; if (page2 != -1) { found = 0; max_next_use = -1; for (k = i + 1; k < seq_len; k++) { if (page_seq[k] == page2) { found = 1; max_next_use = k; break; } } if (!found) { max_next_use = seq_len; } next_use[j] = max_next_use; } } } return page_fault; }
时间: 2024-02-14 21:11:33 浏览: 78
c语言函数库-第一章(C标准库)
这段代码中的问题是缺少函数的声明,需要在main函数前面加上函数声明:
```c
#include <stdio.h>
int opt_missing_page_num(int page_seq[], int seq_len, int mem_page_num);
int main() {
int mem_page_num=3;
int seq_len=17;
int page_seq[] = { 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1 };
int result = opt_missing_page_num(page_seq, seq_len, mem_page_num);
printf("%d", result);
return 0;
}
int opt_missing_page_num(int page_seq[], int seq_len, int mem_page_num) {
int mem_page[mem_page_num];
int page_fault = 0;
int next_use[mem_page_num];
int i, j, k, max_next_use;
for (i = 0; i < mem_page_num; i++) {
mem_page[i] = -1;
next_use[i] = seq_len;
}
for (i = 0; i < seq_len; i++) {
int page = page_seq[i];
int found = 0;
for (j = 0; j < mem_page_num; j++) {
if (mem_page[j] == page) {
found = 1;
next_use[j] = seq_len;
break;
}
}
if (!found) {
int victim = -1;
for (j = 0; j < mem_page_num; j++) {
int use_time = next_use[j];
if (use_time < seq_len && (victim == -1 || use_time < next_use[victim])) {
victim = j;
}
}
mem_page[victim] = page;
next_use[victim] = seq_len;
page_fault++;
}
for (j = 0; j < mem_page_num; j++) {
int page2 = mem_page[j];
if (page2 != -1) {
found = 0;
max_next_use = -1;
for (k = i + 1; k < seq_len; k++) {
if (page_seq[k] == page2) {
found = 1;
max_next_use = k;
break;
}
}
if (!found) {
max_next_use = seq_len;
}
next_use[j] = max_next_use;
}
}
}
return page_fault;
}
```
阅读全文