【问题标题】:My Multidimensional Array program isn't populating properly, what am I missing?我的多维数组程序没有正确填充,我错过了什么?
【发布时间】:2020-04-15 14:06:09
【问题描述】:

我正在为类编写一个程序,该程序从文本文件中对一组数字进行排序,并输出最高降雪量、第二高降雪量和总降雪范围。

大部分情况下它都在工作。但我在输出正确的英寸数和第二高降雪量的程序时遇到了问题。

那个 .txt 文件里面有这个

2017   180.00
2016   176.50
2015   181.50
2014   208.75
2013   225.75
2012   132.00
2011   178.50
2010    150.50
2009    217.00
2008    185.25
2007    162.75
2006    219.50
2005    164.71
2004    232.00
2003    228.00
2002    254.00
2001    303.60
2000    155.00
1999    231.00
1998    131.50
1997    253.10
1996    328.20
1995    175.90
1994    185.30
1993    159.60
1992    166.20
1991    165.80

//Block comment with name, date, program description

#include <stdlib.h>
#include <stdio.h>

// include other header files that you need

#define MAX_SIZE 100 //can go back 100 years if necessary
#define t 1
#define num 20

int ReadFile (int data1[], double data2[]);
int HighestSnow(double data[], int size);
int NextHighestSnow (double data[], int size, int Highest);
double Range (double data[], int size);

int main (void)

{
    int Year[MAX_SIZE];
    double Snow[MAX_SIZE],SnowfallRange;
    int FileLength, IndexHighest, IndexNextHighest;
    int i;

//Describe code
//Read file using function

    FileLength = ReadFile(Year,Snow);

    for(i = 0; i < FileLength; i++)
        printf("The year for the highest snowfall is: [%d] at %lg\n", Year[IndexHighest], Snow[IndexHighest]);

//Print out data in two columns with first being year and second
//being snowfall in inches

    IndexHighest = HighestSnow(Snow,FileLength);

        printf("The year for the highest snowfall is: [%d] at %lg\n", Year[IndexHighest], Snow[IndexHighest]);

//Print out the year and amount of the highest snowfall

    IndexNextHighest = NextHighestSnow(Snow, FileLength, IndexHighest);

        printf("The year for the second highest snowfall is: [%d] at %lg\n", Year[IndexNextHighest], Snow[IndexNextHighest]);

//Print out the year and amount of the next highest snowfall

    SnowfallRange = Range(Snow,FileLength);

        printf("The snow range is: %lg\n", SnowfallRange);

//Print out the range of snowfall (Max-Min) in inches.

    return 0;

}

//Code for part 1 goes here, include ALL error checks.
int ReadFile (int data1[], double data2[])
{
    FILE *f; //Step 1: Declaring a file pointer

    f = fopen("snowfall.txt", "r"); //Step 2: opening a file

    if(f == NULL) //Step 3: checking to see if the file exists
    {
        printf("File does not exit\n");
        exit(1);
    }

    int i;

    for(i = 0; fscanf(f,"%d %lf ", &data1[i], &data2[i]) == 2; i++); //Step 4: read the values

    fclose(f);

    return i;
}

// Code for part 2 goes here
int HighestSnow(double data[], int size)
{
    int i, index;
    double max = data [0];

    for (i = 1; i < size; i++)
    {
        if (data[i] > max)
        {
            max = data[i];
            index = i;
        }
    }
    return index;
}


/* Code for part 3 goes here*/
int NextHighestSnow(double data[], int size, int Highest)
{
    int i;
    double secmax = -1;
    int arrayindex;

        for(i = 0; i > size; i++)
    {
        if(data[i] > secmax && data[i] < Highest) {
            secmax = data[i];
            arrayindex = i;
        }

    }
    return arrayindex;
}

/* Code for part 4 goes here*/

double Range (double data[], int size)
{
    double maxval = data[HighestSnow(data, size)];
    double min = data[0];

    int i;

    for(i = 1; i < size; i++)
    {
        if(min > data[i])
            min = data[i];
    }

    return maxval - min;
}



问题:密歇根州过去 20 年的总降雪量(以英寸为单位)在 文件“snowfall.txt”。文件被格式化,第一列是年份,第二列是 那年的降雪量(英寸)。编写程序获取以下信息: 1. 哪一年降雪量最大,那一年降了多少(英寸)? 2. 降雪量第二高的年份是哪一年,那一年降了多少(英寸)? 3. 过去 20 年的总降雪量范围是多少? 除非另有说明,否则所有 printf 语句都必须在 main() 中。 注意:请记住,您可以使用 EXCEL 验证您的结果。 请遵循以下详细说明: 1. 首先,完整阅读本文档。 2. 包括通常的(详细的)注释块,包括程序名称、作者、日期、输入、输出和 描述,然后是您的预处理器指令。 3. 源代码的大纲(Skeleton-code)如下所示,您需要遵循此 大纲。你可以编写其他支持函数,但你必须至少有 4 个函数原型。 在需要的地方插入适当的 printf 语句以获得所需的输出。大纲有四个部分 您将在哪里输入代码并完成程序。 一种。第 1 部分:您需要编写函数 ReadFile 以打开“snowfall.txt”进行输入和读取 将该文件中的数据放入 2 个数组中。记得检查文件是否存在并且没有读过 EOF。这 如果文件不存在,则错误消息可以在函数中。该函数将返回的数量 文件中的数据点。注意:请记住,数据是成对的,第一个数字是年份, 第二个是以英寸为单位的降雪记录。从 main() 中打印出两列中的数据。 湾。第 2 部分:您需要编写名为 HighestSnow 的函数,该函数接受数组 降雪量,以及一个名为 size 的参数,它指示数组的大小。这个功能 返回一个整数,指示与最高降雪量对应的数组索引号。打印 从 main() 中取出最高降雪的年份和数量。 C。第 3 部分:您需要编写函数调用 NextHighestSnow 它接受数组 降雪量,一个名为 size 的参数,指示数组的大小,以及一个整数 命名为 Highest 表示与最高降雪量对应的索引。这个函数返回 一个整数,指示与第二大降雪量相对应的数组索引号。打印 main() 中第二大降雪的年份和数量。 d。第 4 部分:您需要编写函数调用 Range,它接受降雪数组 数量,以及一个名为 size 的参数,它指示数组的大小。该函数返回 过去 20 年最大降雪量和最小降雪量之间的差异。记住,你可以 从 Range 函数调用 HighestSnow 函数。打印降雪范围 距离 main() 英寸。 e.第 5 部分(额外学分):让您的文件名成为用户输入,即询问用户输入的文件名。

OUTLINE OF THE SOURCE CODE:
//Block comment with name, date, program description
#include <stdio.h>
// include other header files that you need
#define MAX_SIZE 100 //can go back 100 years if necessary
int ReadFile (int data1[], double data2[]);
int HighestSnow (double data[], int size);
int NextHighestSnow (double data[], int size, int Highest);
double Range (double data[], int size);
int main (void) {
int Year[MAX_SIZE];
double Snow[MAX_SIZE],SnowfallRange;
int FileLength, IndexHighest, IndexNextHighest;
//Describe code
//Read file using function
FileLength = ReadFile(Year,Snow);
//Print out data in two columns with first being year and second
//being snowfall in inches
IndexHighest = HighestSnow(Snow,FileLength);
//Print out the year and amount of the highest snowfall
IndexNextHighest = NextHighestSnow(Snow, FileLength,IndexHighest);
//Print out the year and amount of the next highest snowfall
SnowfallRange = Range(Snow,FileLength);
//Print out the range of snowfall (Max-Min) in inches.
return 0;
}
//Code for part 1 goes here, include ALL error checks.
// Code for part 2 goes here
// Code for part 3 goes here
// Code for part 4 goes here

【问题讨论】:

  • 您不能创建一个包含一个int 和一个double 的二维数组。您需要两个一维数组或一个结构。例如:struct snowfall_data {int year; double amount;}
  • 另外,如果您先对数据进行排序,您的程序可以大大简化。你可以使用qsort()
  • 这在技术上是一个作业,我应该用特定的方式来写它。
  • 这是以前的作业留下的,我用它来帮助我找到我的代码问题所在。

标签: c arrays multidimensional-array


【解决方案1】:

我已经通过并指出了我发现的错误。

// Missing 3rd parameter
int NextHighestSnow (double data[], int size, int Highest);
// There is no 3rd parameter to range
double Range (double data[], int size);

int main (void)

{
    // This should not be 2d
    int Year[MAX_SIZE];
    ...
    // Don't use %d to print a double. Use %lf, %le, %lg
    printf("The year for the highest snowfall is: [%d] at %lg\n", Year[IndexHighest], Snow[IndexHighest]);
    ....
}

// Changed for 1d array
// Also, this should take a 3rd parameter for size....
int ReadFile (int data1[], double data2[])
{
    ....
    // Changed test to == 2. fscanf returns # of items successfully scanned
    // or EOF 
    for(i = 0; fscanf(f,"%d %lf ", &data1[i], &data2[i]) == 2; i++);
    ....
}

int HighestSnow(double data[], int size)
{
    ...
    double max = data [0];
    // Since you set max = data[0], loop can start at 1
    for (i = 1; i < size; i++)
    ....
}
// Added 3rd param
int NextHighestSnow(double data[], int size, int Highest)
{
    ...
    // Why search for min?

    // Can't initialize to data[0] in case data[0] is max
    // Use -1 since can't have negative snowfall
    double secmax = -1;  
    int arrayindex;

    for(i = 0; i > size; i++)
    {
        if(data[i] > secmax && data[i] < Highest) {
            secmax = data[i];
            arrayindex = i;
        }
    }

    return arrayindex;
}


double Range (double data[], int size)
{
    // Already have code to find max
    double maxval = data[HighestSnow(data, size)];
    double min = data[0];

    int i;

    for(i = 1; i < size; i++)
    {
        if(min > data[i])
            min = data[i];
    }

    return maxval - min;
}

【讨论】:

  • 所以,我使用了您的建议,并更新了我的代码,但现在我遇到了一些新错误。我在主帖中更新了新代码。
  • 更新了我的代码,现在我得到了重复的输出,直到我得到最高雪的年份。我在主帖中更新了新代码,不确定是什么导致了这个重复循环......
  • @Galahad main() 中的 for(i = 0; i &lt; FileLength; i++) printf("The year for the highest..... 循环应该被删除。
猜你喜欢
  • 1970-01-01
  • 2011-06-29
  • 1970-01-01
  • 2020-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多