1

我的程序假设读取我列出我的数字的数字文本文件并将它们存储到一个数组中,然后显示在 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.txt\n");
        system("PAUSE");
        exit(99);
    }

    fillArray(doublefp, values, &elements);
    printArray(values, elements);


    printf("The minimum value is %d\n", findMIN(values, elements));

    printf("The maximum value is %d\n", 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.53 56.8 12.1 677.23 122.09 788.18 123.25 65.12 98.18 622.27 366.34 433.45 844.56 244.67 544.78 290.10 189.28 317.7628 312.732

4

2 回答 2

2

基本上,double elements;需要int elements

&elements是 a double *,该函数fillArray需要一个int *

fillArray(doublefp, values, &elements);

您是否在编译器中打开了警告?

此外, in printArray,elements被定义为 adouble当它应该是a 时int

findMax和相同findMin

并且printf("%5d ",nums[count]);应该是printf("%lf ",nums[count]);

打开编译器中的所有警告,修复它们,然后再次询问它是否不起作用。

于 2013-10-27T02:18:10.600 回答
1

两个问题:

1) 您的“printf()”格式语句应该使用“%d”打印整数,使用“%lf”打印双精度数。你没有始终如一地这样做。

2)你最好使用“int”来“计数”,而不是double。不幸的是,您也没有始终如一地这样做。

建议更改:

#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.txt\n");
        system("PAUSE");
        exit(99);
    }

    fillArray(doublefp, values, &elements);
    printArray(values, elements);


    printf("The minimum value is %f\n", findMIN(values, elements));
    printf("The maximum value is %f\n", 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)++;
    }
    ...
于 2013-10-27T02:23:54.180 回答