c语言 - 我的多维数组程序未正确填充,我缺少什么?



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

它大部分都在工作。但是我对程序输出正确的英寸数和第二高的降雪量有问题。

那个.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 %lgn", 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 %lgn", 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 %lgn", Year[IndexNextHighest], Snow[IndexNextHighest]);
//Print out the year and amount of the next highest snowfall
SnowfallRange = Range(Snow,FileLength);
printf("The snow range is: %lgn", 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 exitn");
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 年的总降雪量(以英寸为单位)已在 文件"降雪.txt"。文件的格式设置为第一列为年份,第二列为 那一年的降雪量以英寸为单位。编写程序以获取以下信息: 1. 哪一年的降雪量最大,那一年降了多少(英寸)? 2. 哪一年的总降雪量第二高,那一年降了多少(以英寸为单位)? 3. 过去20年的总降雪量是多少? 除非另有说明,否则所有 printf 语句都必须在 main() 中。 注意:请记住,您可以使用EXCEL验证结果。 请遵循以下详细说明: 1. 首先,完整阅读本文档。 2.包括通常(详细)的注释块,包括程序名称,作者,日期,输入,输出和 说明,后跟预处理器指令。 3. 源代码的大纲(骨架代码)如下所示,您需要遵循此大纲 大纲。您可以编写其他支持函数,但您必须至少拥有 4 个函数的原型。 在需要时插入适当的 printf 语句以获得所需的输出。大纲有四个部分 您要在其中输入代码并完成程序。 一个。第 1 部分:您需要编写函数 ReadFile 以打开"降雪.txt"进行输入和读取 该文件中的数据分成 2 个数组。请记住检查文件是否存在,而不是读取 EOF。这 如果文件不存在,则错误消息可以在函数中。该函数将返回 文件中的数据点。注意:请记住,数据是成对的,第一个数字是一年,并且 第二个是以英寸为单位的降雪记录。从 main() 中打印出两列中的数据。 b.第 2 部分:您需要编写名为 HighestSnow 的函数,该函数接受 降雪量,以及一个名为 size 的参数,该参数指示数组的大小。此函数 返回一个整数,指示与最高降雪量对应的数组索引号。打印 从 main() 中得出最高降雪量的年份和数量。 c.第 3 部分:您需要编写函数调用 NextHighestSnow,它接受 降雪量、指示数组大小的名为 size 的参数以及一个整数 命名为"最高",表示与最高降雪量对应的指数。此函数返回 一个整数,指示对应于第二高降雪量的数组索引号。打印输出 来自 main() 的第二高降雪量的年份和数量。 d.第 4 部分:您需要编写接受降雪数组的函数调用范围 数量,以及一个名为 size 的参数,该参数指示数组的大小。此函数返回 过去20年最大和最小降雪量之间的差异。记住,你可以 从 Range 函数调用函数 HighestSnow。打印出降雪范围 距离主()的英寸。 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

我已经检查并指出了我发现的错误。

// 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 %lgn", 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;
}

最新更新