Question

***HELP FINISH CODE*** Problem: The total snowfall (in inches) in the state of Michigan over the...

***HELP FINISH CODE***

Problem: The total snowfall (in inches) in the state of Michigan over the last 20 years has been provided in the file “snowfall.txt”. The file is formatted so that the first column is the year and the second column is the snowfall in inches for that year. Write a program to obtain the following information:

  1. What year was the greatest amount of snowfall and how much fell that year (in inches)?
  2. What year was the 2nd highest total of snowfall and how much fell that year (in inches)?
  3. What is the range of total snowfall in the last 20 years?

#include <stdio.h>
#define MAX_SIZE 100

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;

FileLength = ReadFile(Year,Snow);

if (FileLength > 0)

{

printf("%d\n", FileLength);

printf("year\tsnowfall\n");

for (i = 0; i < FileLength; i++)

{

printf("%d\t%lf\n", Year[i], Snow[i]);

}

IndexHighest = HighestSnow(Snow,FileLength);

printf("%d had the highest total snow fall, with %lf inches.\n", Year[IndexHighest], Snow[IndexHighest]);

IndexNextHighest = NextHighestSnow(Snow, FileLength,IndexHighest);

printf("%d had the 2nd highest total snow fall, with %lf inches.\n", Year[IndexHighest], Snow[IndexHighest]);

SnowfallRange = Range(Snow,FileLength);

printf("The snowfall ranged by %d inches, Snow[IndexHighest])

return 0;

}
int ReadFile(int year[], double data[])

{

FILE *ptr = fopen("snowfall.txt", "r");

if (ptr == NULL)

{

printf("no such file.");

return 0;

}

else

{

int Year;

double snow;

int total = 0, i = 0, index = 0;

while (fscanf(ptr, "%d %lf", &year[i], &data[i]) == 2)

{

i++;

total++;

}

return total;

}

**snowfall.txt file***

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

#include <stdio.h>
#define MAX_SIZE 100

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;

FileLength = ReadFile(Year,Snow);

if (FileLength > 0)

{

printf("%d\n", FileLength);

printf("year\tsnowfall\n");

for (i = 0; i < FileLength; i++)

{

printf("%d\t%lf\n", Year[i], Snow[i]);

}

IndexHighest = HighestSnow(Snow,FileLength);

printf("%d had the highest total snow fall, with %lf inches.\n", Year[IndexHighest], Snow[IndexHighest]);

IndexNextHighest = NextHighestSnow(Snow, FileLength,IndexHighest);

printf("%d had the 2nd highest total snow fall, with %lf inches.\n", Year[IndexNextHighest], Snow[IndexNextHighest]);

SnowfallRange = Range(Snow,FileLength);

printf("The snowfall ranged by %f inches", SnowfallRange);
}
return 0;

}
int ReadFile(int year[], double data[])

{

FILE *ptr = fopen("snowfall.txt", "r");

if (ptr == NULL)

{

printf("no such file.");

return 0;

}

else

{

int Year;

double snow;

int total = 0, i = 0, index = 0;

while (fscanf(ptr, "%d %lf", &year[i], &data[i]) == 2)

{

i++;

total++;

}

return total;

}
}
int HighestSnow (double data[], int size)
{
int ind=0;
double max=data[0];
int i;
for(i=1;i<size;i++)
{
if(max<data[i])
{
ind=i;
max=data[i];
}
}
return ind;
}

int NextHighestSnow (double data[], int size, int Highest)
{
int ind=0;
double max=-10;
int i;
for(i=0;i<size;i++)
{
if(max<data[i]&&i!=Highest)
{


ind=i;
max=data[i];
}
}
return ind;
}

double Range (double data[], int size)
{
double min=data[0],max=data[0],range;
int i;
for(i=1;i<size;i++)
{
if(data[i]>max)
{
max=data[i];
}
if(data[i]<min)
{
min=data[i];
}
}
range=max-min;
return range;

}

FAcheckimain.exe 2014 2013 2012 2811 201e 2009 2808 2807 2806 2805 2884 2803 2882 2881 225.75080e 132.88080e 178.58080e 150.5

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
***HELP FINISH CODE*** Problem: The total snowfall (in inches) in the state of Michigan over the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with this C code Can you explain line by line? Also can you...

    I need help with this C code Can you explain line by line? Also can you explain to me the flow of the code, like the flow of how the compiler reads it. I need to present this and I want to make sure I understand every single line of it. Thank you! /* * Converts measurements given in one unit to any other unit of the same * category that is listed in the database file, units.txt. * Handles...

  • Please help in C: with the following code, i am getting a random 127 printed in...

    Please help in C: with the following code, i am getting a random 127 printed in front of my reverse display of the array. The file i am pulling (data.txt) is: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 when the reverse prints, it prints: 127 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 not sure why...please...

  • PROGRAMING IN C. PLEASE HELP FIX MY CODE TO FIT THE FOLLOWING CRITERIA: 1.)Read your stocks...

    PROGRAMING IN C. PLEASE HELP FIX MY CODE TO FIT THE FOLLOWING CRITERIA: 1.)Read your stocks from your mystocks.txt file. You can use this information for the simulator. 2.)The stock price simulator will return the current price of the stock. The current prices is the prices of the requested ticker + a random number. 3.)We will assume that the stock price remains constant after the price check till your trade gets executed. Therefore, a buy or a sell order placed...

  • Hello i have code for parameter estimation in C language. The problem is that the code is developed for floating point numbers and i want it for integer values The code is #include "stdio.h" #...

    Hello i have code for parameter estimation in C language. The problem is that the code is developed for floating point numbers and i want it for integer values The code is #include "stdio.h" #include "conio.h" #include "math.h" #define N 30 double g(double b, double s[], double sn, double sk); double gdash(double b, double s[], double sn); main() { int i; double sk=0; double s_avg,g1,g2,a; double s[N]={30.02,31.46,53.93,55.29,58.72,71.92,77.07,80.9,101.9,114.87,115.34, 121.57,124.96,134.07,136.25,151.78,177.5,180.29,182.21,186.34,256.81, 273.88,277.87,453.93,535.0,537.27,552.9,673.68,704.49,738.68}; double b[N]; clrscr(); sk=0, s_avg=0.0; printf("\n Input observations :\n"); for(i=0;i<N;i++) sk=sk+s[i];...

  • C code program help. I am trying to find the total parallel resistive load. This is...

    C code program help. I am trying to find the total parallel resistive load. This is what I have so far. It does run, it does ask for numbers. But it doesn't calculate. It just comes out "The parallel is: 0.00". I have tried 3, 4, 5 and 3.2, 4.3, 5.4. Still only shows 0.00. I have tried several combinations of putting different data types in both main.c and the rest of it. Any help or ideas would be helpful....

  • Please change this 2 thread program to a 4 thread program. /*compile as "gcc thread.c -pthread"...

    Please change this 2 thread program to a 4 thread program. /*compile as "gcc thread.c -pthread" */ #include <pthread.h> #include <stdio.h> #include <time.h> #define SIZE 100000000 //100 M //#define SIZE 10000 #define NUMBER_OF_TIMES 100 float a[SIZE]; typedef struct {        int start;        int end;        double sum;        int pid;    } range; void *thread_function(void *arg) //define a function that will be executed from //within a thread {    range *incoming = (range *) arg;...

  • Problem: The average annual water level of Lake Michigan since 1860 has been provided in the...

    Problem: The average annual water level of Lake Michigan since 1860 has been provided in the file “LMWL.txt”. The file is formatted so that the first column is the year and the second column is the average water level in feet above sea level for that year. Write a program to obtain the following information: What year had the lowest average water level, and what was the level (in feet)? What has the average water level of Lake Michigan been,...

  • Please I need help in C language, I am trying to modify the code per the...

    Please I need help in C language, I am trying to modify the code per the below instructions, but I am getting errors. Can't fgure it out. The attempted code modification and data file is privided below, after the instructions. Thank you. Instructions:                  1.      First, add a function named printMsg that displays a message, a greeting, or an introduction to the program for the user. Add a statement that calls that function from the main function when your program starts....

  • URGENT. Need help editing some C code. I have done most of the code it just...

    URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...

  • please complete the missing function only to figure out how many numbers fall within the range...

    please complete the missing function only to figure out how many numbers fall within the range of 90 through 99 total of 29 values in C 6 finclude "lab5.h" 8 const char *FILENAME() - / array of the data file names * ("lab5a.dat", "lab5b.dat", NULL); 12 int main(void) 13 int file count = 0; keeps track of which file we are on/ int check overflow - 0; / counter to prevent array overflow int real filesize = 0; /actual count...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT