Must be done with C language. Functions may not be used. Temperatures.txt is inc
ID: 3592544 • Letter: M
Question
Must be done with C language. Functions may not be used. Temperatures.txt is included on the bottom of the page.
Temperatures.txt:
15
63.4
66.1
67.2
58.5
61.9
68.2
65.3
70.0
72.9
71.8
70.1
70.3
69.5
66.1
70.8
5
56.5
57.1
55.0
58.0
66.2
31
54.0
53.2
46.6
47.4
56.7
62.6
53.7
57.5
60.7
65.7
66.0
67.8
62.3
58.3
55.6
60.2
62.8
70.5
72.9
71.0
61.8
61.6
71.2
73.0
68.6
56.6
51.2
56.0
59.7
62.1
69.2
20
72.3
72.8
71.5
71.3
69.4
64.6
68.7
70.6
72.5
72.2
78.0
78.6
77.4
75.4
69.3
77.3
74.2
75.7
79.9
72.2
30
72.8
70.9
72.7
73.0
71.8
69.6
61.3
71.7
73.4
76.6
77.5
77.3
76.1
71.5
72.4
73.7
75.0
77.8
77.1
77.3
73.5
74.7
78.2
78.8
78.2
73.6
69.3
76.9
76.0
78.5
30
79.2
80.3
78.9
78.7
75.8
75.6
78.4
80.0
78.1
80.7
79.4
79.9
81.9
82.2
82.0
81.0
80.8
81.3
82.4
80.7
78.0
82.3
77.7
81.6
81.7
80.7
79.0
80.4
79.1
78.2
31
76.4
76.2
78.1
78.9
80.4
81.8
82.4
82.4
82.1
80.8
80.3
78.7
78.2
77.5
77.5
78.1
79.3
78.6
77.3
77.6
80.1
79.3
78.6
79.7
79.9
81.6
78.0
79.6
79.9
83.0
82.2
31
81.1
81.9
81.6
80.0
82.7
81.2
83.6
83.0
82.0
82.3
83.6
83.1
83.6
82.5
81.7
82.2
82.8
82.3
81.8
83.0
83.1
80.4
81.8
80.2
79.0
79.9
80.8
80.3
81.6
82.6
81.2
30
79.9
78.8
79.6
80.9
81.3
80.6
77.8
80.9
81.7
81.8
81.8
79.7
79.7
82.5
81.7
80.2
80.5
78.1
79.3
78.7
78.7
79.1
77.1
77.5
76.7
80.6
76.0
76.8
78.0
77.8
31
79.1
78.5
78.7
79.5
79.5
79.2
77.1
78.9
75.5
74.6
74.9
75.8
76.1
76.0
73.2
76.9
77.4
78.4
79.0
76.5
79.8
79.6
76.8
67.8
68.3
67.5
67.1
68.5
71.8
74.1
75.3
30
77.1
71.3
67.2
68.7
73.2
74.2
70.1
70.8
70.5
72.0
72.1
71.1
64.8
62.7
68.1
70.7
75.1
75.9
73.8
71.7
73.1
73.9
73.7
67.9
67.1
72.7
69.7
50.6
59.5
64.5
31
66.1
66.1
65.8
69.6
71.4
74.1
73.2
72.8
73.1
71.8
72.0
65.9
60.0
69.7
70.9
59.3
57.1
58.4
59.1
65.8
72.7
75.0
74.7
68.6
58.8
64.6
65.2
70.1
73.4
65.2
62.3
Assignment #5 Introduction to C Programming COP 3223 1. To reinforce the use of loops 2. To learn how to read information from files. Introduction: Pirate Ti Your friend has not stopped talking about how cool pirates and how awesome it would be if they could be a real-life pirate captain! To amuse your friend, you have decided to create a series of programs about pirates Your friend is almost ready to leave Spain to search for treasure in the Caribbean! To make sure that the maximum number of crew members make it through the trip safely, your friend will carefully select which months to travel the ocean during In this program, you will examine a file of weather data. Ask the user for the name of this file. Then, ask the user for the temperature range that their crew sails best in. Calculate the percentage of days in each month that fall into that range. Pirates are not the most skilled at recording date, so a file of weather data may not contain information for every day in every month. Instead, calculate the percentage by dividing the number of days in range by the number of days that there is data for The month with the highest percentage is the month that your friend should pick to begin their journey for treasure! Arrays are not expected to be a part of this program, except for the name of the input file. Do not attempt to store all the values in the file in your program. 1. 2. 3. The lower bound temperature will be an integer between 0 and 80 The upper bound temperature will be an integer higher than the lower bound and less than 120 The file name will be a string less than 30 characters in length fication There will be 12 months of weather data available. Since months may not have complete information, each month will begin with a header number (n) to indicate how manytemperatures are available for that month. The month will then have n real numbers representing the temperature for each day utput Specification Output to the screen. Show the user the percentage of days in range for each month. Then, print the best month for your friend to begin their voyage Month X: YY.Y percent of days in range. You should leave for the Caribbean in month ZExplanation / Answer
Please find below code.
#include <stdio.h>
int main(){
float cold,hot;
char fname[30];
int line;
printf("Tell me about your crew's preffered temperature for sailing: ");
printf("What is the coldest temperature they can sail in? ");
scanf("%f",&cold);
printf("What is the hottest temperature they can sail in? ");
scanf("%f",&hot);
printf("Please enter the name of input data file: ");
scanf("%s",&fname);
printf(" ");
FILE *fp;
fp = fopen(fname,"r"); // Opening the file in read mode
if (!fp)
return 1;
int month=1,highest_month=0,inRange=0,i;
float temp = 0,percent = 0,highest_per = 0;
fscanf(fp,"%d",&line); // reading the first line of file as an integer
while(!feof(fp)){ // looping until file pointer fp reaches end of file
for(i=0;i<line;i++){ // looping until n (number of days) in the file. As there will only be n subsequent lines for each month.
fscanf(fp,"%f",&temp); // reading temperature as floating point value.
if(temp>=cold && temp<=hot){ // checking if read temperature is within the range. If so increment inRange value
inRange++;
}
}
percent = (float)inRange/line; // Calculating percentage of days in range.
if(percent>highest_per){ // condition to keep track of highest percentage of days in range and its month.
highest_month = month;
highest_per = percent;
}
printf("Month %d: %.1f percent of days in range ",month,percent*100);
fscanf(fp,"%d",&line); // reading next month header (number of days) value.
month++;
inRange = 0;
}
printf(" You should leave for the Carribbean in month %d!",highest_month);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.