我的程序应该读取数字文本文件,这是我列出我的数字并将它们存储到数组中然后显示在 5 列中的地方。我的问题是它根本不会显示数组。我不知道为什么。
#include <stdio.h>
#include <stdlib.h>
#define ELEMENTS 100
void fillArray(FILE *, double [], double *);
void printArray(double [], double);
double findMIN(double [], double);
double findMAX(double [], double);
int main()
{
FILE *doublefp;
double values[ELEMENTS];
double elements;
int status;
status = fopen_s(&doublefp, "Numbers.txt", "r");
if (status != 0)
{
printf("Unable to open the file Numbers.txtn");
system("PAUSE");
exit(99);
}
fillArray(doublefp, values, &elements);
printArray(values, elements);
printf("The minimum value is %dn", findMIN(values, elements));
printf("The maximum value is %dn", findMAX(values, elements));
system ("PAUSE");
return 0;
}
double findMIN(double nums[], double element)
{
int i;
double min = nums[0];
for (i = 1; i < element; i++)
if (min > nums[i])
min = nums[i];
return (min);
}
double findMAX(double nums[], double element)
{
int i;
double max = nums[0];
for (i = 1; i < element; i++)
if (max < nums[i])
max = nums[i];
return (max);
}
void fillArray (FILE *fp, double nums[], int *count)
{
double number;
printf("Enter up to %d integers press the F6 key to end input. n", ELEMENTS);
*count = 0;
while (fscanf_s(fp, "%d", &nums[*count]) != EOF)
{
(*count)++;
}
}
void printArray (double nums[], double elements)
{
int count;
printf("Values in array:n");
for (count = 0; count < elements; count++)
{
printf("%5d ",nums[count]);
if ((count+1)% 10 == 0)
printf("n");
}
printf("n");printf("n");
}
我的号码在文本文件中列出如下:23.5356.812.1677.23122.09788.18123.2565.1298.18622.27366.34433.45844.56244.67544.78290.10189.28522.17321.33178,76
,double elements;
需要int elements
这里&elements
是一个double *
,函数fillArray
期望一个int *
fillArray(doublefp, values, &elements);
您是否在编译器中打开了警告?
此外,在printArray
中,elements
被定义为一个double
,当它应该是一个int
。
与findMax
和findMin
相同。
printf("%5d ",nums[count]);
应该printf("%lf ",nums[count]);
打开编译器中的所有警告,修复它们,然后再次询问是否不起作用。
两个问题:
1) 您的 "printf()" 格式语句应该使用 "%d" 打印整数,使用 "%lf" 打印双精度。 你没有始终如一地这样做。
2)你最好使用"int"来"计算数字",而不是双精度。 不幸的是,你也没有始终如一地这样做。
建议的更改:
#include <stdio.h>
#include <stdlib.h>
#define ELEMENTS 100
void fillArray(FILE * fp, double values [], int * count);
void printArray(double values[] int count);
double findMIN(double values[], int count);
double findMAX(double values[], int count);
int main()
{
FILE *doublefp;
double values[ELEMENTS];
int elements;
int status;
status = fopen_s(&doublefp, "Numbers.txt", "r");
if (status != 0)
{
printf("Unable to open the file Numbers.txtn");
system("PAUSE");
exit(99);
}
fillArray(doublefp, values, &elements);
printArray(values, elements);
printf("The minimum value is %fn", findMIN(values, elements));
printf("The maximum value is %fn", findMAX(values, elements));
system ("PAUSE");
...
double findMIN(double nums[], int count)
...
void fillArray (FILE *fp, double nums[], int *count)
{
printf("Enter up to %d integers press the F6 key to end input. n", ELEMENTS);
*count = 0;
while (fscanf_s(fp, "%f", &nums[*count]) != EOF)
{
(*count)++;
}
...