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

问题描述 投票:0回答:2

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

主要在正常工作。但是我在输出正确的英寸数和第二大降雪量的程序时遇到问题。

该.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

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


#define MAX_SIZE 100 
#define t 1

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

int main (void)

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


    FileLength = ReadFile(Year,Snow);

    for(i = 0; i < FileLength; i++)
        printf("[%02d] %g\n", Year[i][0], Snow[i]);


    IndexHighest = HighestSnow(Snow,FileLength);

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


    IndexNextHighest = NextHighestSnow(Snow, FileLength);

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


    SnowfallRange = Range(Snow,FileLength,IndexHighest);

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


    return 0;

}

int ReadFile (int data1[][t], double data2[0])
{
    FILE *f;

    f = fopen("snowfall.txt", "r");

    if(f == NULL)
    {
        printf("File does not exit\n");
        exit(1);
    }

    int i;

    for(i = 0; fscanf(f,"%d %lf ", &data1[i][0], &data2[i]) != EOF; i++);

    fclose(f);

    return i;
}

int HighestSnow(double data[], int size)
{
    int i, index;
    double max = data [0];

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


int NextHighestSnow(double data[], int size)
{
    int i;
    double min = data[0];
    double secmin = data[0];
    int arrayindex;

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

    for(i = 0; i > size; i++)
    {
        if(secmin <= data[i] && min != data[i])
        {
            secmin = data[i];
            arrayindex = i;
        }

    }
    return arrayindex;
}


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

    int i;

    for(i = 0; 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.下面显示了源代码的大纲(骨架代码),您必须遵循此大纲大纲。您可以编写其他支持功能,但必须至少具有4个原型函数。在需要的地方插入适当的printf语句以获得所需的输出。大纲分为四个部分输入代码并完成程序的位置。一种。第1部分:您需要编写函数ReadFile来打开“ snowfall.txt”以进行输入和读取该文件中的数据分为2个数组。切记要检查文件是否存在,并且不能通过EOF读取。的如果文件不存在,则会出现错误消息。该函数将返回文件中的数据点。注意:请记住,数据是成对出现的,第一个数字是年份,第二个是以英寸为单位的降雪记录。从main()的两列中打印出数据。b。第2部分:您需要编写名为HighestSnow的函数,该函数接受数组降雪量和一个名为size的参数,该参数指示数组的大小。这个功能返回一个整数,该整数指示与最高降雪相对应的数组索引号。打印从main()中找出最高降雪的年份和数量。C。第3部分:您需要编写函数调用NextHighestSnow,该函数接受以下数组:降雪量,一个名为size的参数(指示数组的大小)和一个整数命名为最高,指示与最高降雪相对应的索引。该函数返回一个整数,指示与第二大降雪相对应的数组索引号。打印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
c arrays multidimensional-array
2个回答
0
投票

首先; t定义为1,因此,您不必为year创建一个二维数组; int Year[MAX_SIZE]就足够了。

返回您的问题;您可以选择前两个值的索引,例如:

main() {
    ...
    int hiFrstNdx = 0;
    int hiScndNdx = 0;
    ...
    selectIndexes(data, n, &hiFrstNdx, &hiScndNdx);
    ...

}

void selectIndexes(double data[], int n, int *hiFrstNdx, int *hiScndNdx) {
    double hiFrstVal = 0.0f;
    double hiScndVal = 0.0f;
    double tmpVal;
    int tmpNdx;

    hiFrstNdx = 0;
    hiScndNdx = 0;

    for (int i = 0; i < n; i++) {
        if (data[i] > hiScndVal) {
            hiScndVal = data[i];
            *hiScndNdx = i;

            if (hiScndVal > hiFrstVal) {
                tmpVal = hiFrstVal;
                hiFrstVal = hiScndVal;
                hiScndVal = tmpVal;
                tmpNdx = *hiFrstNdx;
                *hiFrstNdx = *hiScndNdx;
                *hiScndNdx = tmpNdx;
            }
        }
    }

    return;
}


0
投票

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

// 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;
}
© www.soinside.com 2019 - 2024. All rights reserved.