In: Computer Science
Can't figure out what I am doing wrong.
Please let me know.
Sample output:
Please enter the name of the weather data file: temperatures.txt
Tell me about your crew’s preferred temperature for sailing:
What is the coldest temperature they can sail in? 60
What is the hottest temperature they can sail in? 80
Month 1: 93.3 percent of days in range.
Month 2: 20.0 percent of days in range.
Month 3: 58.1 percent of days in range.
Month 4: 100.0 percent of days in range.
Month 5: 100.0 percent of days in range.
Month 6: 50.0 percent of days in range.
Month 7: 64.5 percent of days in range.
Month 8: 9.7 percent of days in range.
Month 9: 60.0 percent of days in range.
Month 10: 100.0 percent of days in range.
Month 11: 93.3 percent of days in range.
Month 12: 83.9 percent of days in range.
You should leave for the Caribbean in month 4!
Numbers for the file:
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
#include <stdio.h>
#include <stdlib.h>
#define MONTHS 12
#define DAYS 31
// Main Function
int main(void){
FILE* f;
// Declared Variables
int month, day, low, high, sailmonth[12], temperature, besttime, i,
j;
float array[MONTHS][DAYS],num, sum, average;
char data[30];
printf("Please enter the name of the weather data
file:/n");
scanf("%s", data);
printf("Tell me about your crew’s preferred temperature for sailing:/n");
printf("What is the coldest temperature they can sail
in?/n");
scanf("%d", &low);
printf("What is the hottest temperature they can sail
in?/n");
scanf("%d", &high);
if((f = fopen(data, "r")) == NULL)
exit(1);
for(months = 0; months < 12; months++){
for (day = 0; day ,31; day++){
if(fscanf(f, "%f", &array[months][days]) != 1)
exit(1);
fclose(f);
}
}
for (months = 0; months < 12; months++)
{
sum = 0
num = array[months][0];
for (days = 1; days <31; days++)
{
if (array[months][days] >= low && array[months][days]
<= high)
{
printf("%f", array[months][months]);
sum = sum + array[months][months];
}
}
}
average = sum/num:
printf("Month %d: %f percent of days in range.", average,
month);
sailmonth[month] = int (average);
for (i = 0; i < 12; i++)
{
besttime = 0;
for (j = i + 1; j < 12; j++)
{
if (sailmonth[i] > sailmonth[j])
{
temperature = sailmonth[i];
sailmonth[i] = sailmonth[j];
sailmonth[j] = temperature;
besttime = i;
}
}
}
printf("You should leave for the Carribean in month %d.",
besttime);
return 0;
}
Importing data from a text file and then working on it ,
definitely a nice work and code has been nicely written too
....
But what you missed was the few typing errors , and to sort out
that , Indenting the code is the best approach
1. At line no. 20 :-> In the for loop it was used "day" while "days" was initialized because whatever indexing variable one is using the array only that variabe should be used . Also it was written "day=30;day,31;day++" but it should be "days=0;days<31;days++".
2. At line 28 :-> A semicolon was missed . "sum = 0" typo error as "sum = 0;" was required
3. At line 40. :-> Same mistake of typo error . "average = sum/num:"
4. At one place the closing bracket was placed 3 lines earlier
resulting in "printf("Month %d: %f percent of days in range.",
average, month);" printing only once , while it was intended to
print in the loop
Hence, At line 39 :-> The closing braacket was placed "}" while
this bracket should have come after line 42.
Code For refrencing line numbers : (I have indented the code as
it always helps me out)
1. #include <stdio.h>
2. #include <stdlib.h>
3. #define MONTHS 12
4. #define DAYS 31
// Main Function
5. int main(void){
6. FILE* f;
// Declared Variables
7. int month, day, low, high, sailmonth[12], temperature, besttime,
i, j;
8. float array[MONTHS][DAYS],num, sum, average;
9. char data[30];
10. printf("Please enter the name of the weather data
file:/n");
11. scanf("%s", data);
12. printf("Tell me about your crew’s preferred temperature for
sailing:/n");
13. printf("What is the coldest temperature they can sail
in?/n");
14. scanf("%d", &low);
15. printf("What is the hottest temperature they can sail
in?/n");
16. scanf("%d", &high);
17. if((f = fopen(data, "r")) == NULL)
18. exit(1);
19. for(months = 0; months < 12; months++){
20. for (day = 0; day ,31; day++){ ******************
21. if(fscanf(f, "%f", &array[months][days]) != 1)
********************
22. exit(1);
23. fclose(f);
24. }
25. }
26. for (months = 0; months < 12; months++)
27. {
28. sum = 0 *******************
29. num = array[months][0];
30. for (days = 1; days <31; days++)
31. {
32. if (array[months][days] >= low &&
array[months][days] <= high)
33.
34. {
35. printf("%f", array[months][months]);
36. sum = sum + array[months][months];
37. }
38. }
39. } ****************
40. average = sum/num: ***************
41. printf("Month %d: %f percent of days in range.", average,
month);
42. sailmonth[month] = int (average);
****************
43. for (i = 0; i < 12; i++)
44. {
45. besttime = 0;
46. for (j = i + 1; j < 12; j++)
47. {
48. if (sailmonth[i] > sailmonth[j])
49. {
50. temperature = sailmonth[i];
51. sailmonth[i] = sailmonth[j];
52. sailmonth[j] = temperature;
53. besttime = i;
54. }
55. }
56. }
57. printf("You should leave for the Carribean in month %d.",
besttime);
58. return 0;
59. }
Hope , It helps you out. If still stuck do comment please