【发布时间】:2021-06-20 20:28:09
【问题描述】:
对这里的编程比较陌生。我在这里很难弄清楚如何让这段代码编译,但不断收到这个错误:
我不一定认为它与函数 insert_array_ascend、get_value、read_value 或 is_in_array 有任何关系,但公平地说,你永远不知道。
/tmp/ccB0YyFX.o: In function `main':
lab.c:(.text+0xb7): undefined reference to `print_array'
collect2: error: ld returned 1 exit status
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void insert_array_ascend(int array[], int n_count);
void print_array(int array[], int size);
int get_value(char *prompt);
int read_file(int array[], char *fname);
int is_in_array(int array[], int size, int input);
int main(){
int primary[100];
int count = 0;
int i;
char choice;
count += read_file(primary, "input_numbers.txt");
do{
int user_value = get_value("Enter a value to test: ");
int result = is_in_array(primary, count, user_value);
primary[count] = user_value;
insert_array_ascend(primary, count);
count++;
print_array(primary, count);
if (result == 1){
printf("TRUE\n");
}
else{
printf("FALSE\n");
}
printf("Would you like to run again? [y/n]: ");
scanf("%c", &choice);
} while (choice == 'y' || choice == 'Y');
return EXIT_SUCCESS;
}
int read_file(int array[], char *fname){
int position = 0;
FILE *fp = fopen(fname, "r");
while (fscanf(fp, "%d", &array[position]) != EOF){
insert_array_ascend(array, position);
position++;
}
return position;
}
void insert_array_ascend(int array[], int size){
int i, key, j;
for (i = 1; i <= size; i++){
j = i;
while (j > 0 && array[j -1] > array[j]){
key = array[j];
array[j] = array[j - 1];
array[j - 1] = key;
j--;
}
}
}
int is_in_array(int array[], int size, int input){
int l = 0;
int h = size - 1;
while (l <= h){
int checker = (l + h) / 2;
if (array[checker] == input){
return 1;
}
else if (input < array[checker]){
h = checker - 1;
}
else{
l = checker + 1;
}
}
}
int get_value(char *prompt){
int amount;
printf("%s", prompt);
scanf("%d", &amount);
printf("\n Echo printing, read in the number = %d\n\n", amount);
return amount;
}
我应该怎么做才能使它正确运行?有点菜鸟,所以请帮助外行术语。非常感谢!
【问题讨论】:
标签: arrays c compiler-errors