Getting started Change into the cse220 directory. Create a new directory called
ID: 3681329 • Letter: G
Question
Getting started Change into the cse220 directory. Create a new directory called lab10. Change into the new directory. Implement the program below in your lab10 directory. Temperature Records Create a program to store temperatures recorded over a period of time. The program should store the temperatures in a global array variable. Assume that the maximum number of temperatures that may be stored is 500. Add the following function to your program: A function that finds the average of all temperatures stored A function that finds the average of the last 5 recorded temperatures. If there are less than 5 temperatures recorded, return the average of all recorded temperatures. A function that records a new temperature. It must check if there is still space in the array. A function that prints the number of temperatures recorded A function that prints all the temperatures recorded Organize the program so the function prototypes are presented first, and the definitions after the main function. Your program should keep asking the user to select an option from the following: (A) computed average of all temperatures, (B) compute the average of the last five, (C) add a new temperature, (D) print the number of temperatures recorded, (E) print the temperatures recorded, or (F) exit the program. Call your program temperatures.c. Compile and test your program. Submit through the handin system the source code temperatures.cExplanation / Answer
temperatures.c
#include <stdio.h>
double temperatures[500];
int size=0;
double average();
double averageLastFive();
void record(double temp);
void printSize();
void print();
int main()
{
char choice;
while(true)
{
printf("Choose an option ");
printf("(A) compute average of all temperatures. ");
printf("(B) compute average of last five. ");
printf("(C) add a new temperature. ");
printf("(D) print the number of temperatures recorded. ");
printf("(E) print the temperatures recorded,or ");
printf("(F) exit the program ");
scanf(" %c",&choice);
switch(choice)
{
case 'A' : printf( "Average of temperatures is %lf ",average());
break;
case 'B' : printf( "Average of last five temperatures is %lf ",averageLastFive());
break;
case 'C' :
double temp;
scanf("%lf",&temp);
record(temp);
break;
case 'D' : printSize();
break;
case 'E' : print();
break;
case 'F' :
printf("Exiting the program ");
return -1;
break;
default : printf("Incorrect choice ");
break;
}
}
return 0;
}
double average()
{
if(size==0)
{
printf("No temperatures are there to calculate the average ");
return 0;
}
double sum=0,average=0;
int i;
for(i=0;i<size;++i)
{
sum+=temperatures[i];
}
average=sum/(double)size;
return average;
}
double averageLastFive()
{
if(size<5)
{
return average();
}
double sum=0,average=0;
int i;
for(i=size-5;i<size;++i)
{
sum+=temperatures[i];
}
average=sum/(double)size;
return average;
}
void record(double temp)
{
if(size<500)
{
temperatures[size++]=temp;
}
}
void printSize()
{
printf("Number of temperatures recorded are %d ",size);
}
void print()
{
int i;
printf("Recorded temperatures are ");
for(i=0;i<size;++i)
{
printf("%lf",temperatures[i]);
printf(" ");
}
printf(" ");
}
sample output:
Choose an option
(A) compute average of all temperatures.
(B) compute average of last five.
(C) add a new temperature.
(D) print the number of temperatures recorded.
(E) print the temperatures recorded,or
(F) exit the program
A
No temperatures are there to calculate the average
Average of temperatures is 0.000000
Choose an option
(A) compute average of all temperatures.
(B) compute average of last five.
(C) add a new temperature.
(D) print the number of temperatures recorded.
(E) print the temperatures recorded,or
(F) exit the program
C
12.4
Choose an option
(A) compute average of all temperatures.
(B) compute average of last five.
(C) add a new temperature.
(D) print the number of temperatures recorded.
(E) print the temperatures recorded,or
(F) exit the program
C
3.2
Choose an option
(A) compute average of all temperatures.
(B) compute average of last five.
(C) add a new temperature.
(D) print the number of temperatures recorded.
(E) print the temperatures recorded,or
(F) exit the program
C
45.6
Choose an option
(A) compute average of all temperatures.
(B) compute average of last five.
(C) add a new temperature.
(D) print the number of temperatures recorded.
(E) print the temperatures recorded,or
(F) exit the program
C
23.4
Choose an option
(A) compute average of all temperatures.
(B) compute average of last five.
(C) add a new temperature.
(D) print the number of temperatures recorded.
(E) print the temperatures recorded,or
(F) exit the program
C
11.4
Choose an option
(A) compute average of all temperatures.
(B) compute average of last five.
(C) add a new temperature.
(D) print the number of temperatures recorded.
(E) print the temperatures recorded,or
(F) exit the program
C
30
Choose an option
(A) compute average of all temperatures.
(B) compute average of last five.
(C) add a new temperature.
(D) print the number of temperatures recorded.
(E) print the temperatures recorded,or
(F) exit the program
C
32.4
Choose an option
(A) compute average of all temperatures.
(B) compute average of last five.
(C) add a new temperature.
(D) print the number of temperatures recorded.
(E) print the temperatures recorded,or
(F) exit the program
A
Average of temperatures is 22.628571
Choose an option
(A) compute average of all temperatures.
(B) compute average of last five.
(C) add a new temperature.
(D) print the number of temperatures recorded.
(E) print the temperatures recorded,or
(F) exit the program
B
Average of last five temperatures is 20.400000
Choose an option
(A) compute average of all temperatures.
(B) compute average of last five.
(C) add a new temperature.
(D) print the number of temperatures recorded.
(E) print the temperatures recorded,or
(F) exit the program
D
Number of temperatures recorded are 7
Choose an option
(A) compute average of all temperatures.
(B) compute average of last five.
(C) add a new temperature.
(D) print the number of temperatures recorded.
(E) print the temperatures recorded,or
(F) exit the program
E
Recorded temperatures are
12.400000 3.200000 45.600000 23.400000 11.400000 30.000000 32.400000
Choose an option
(A) compute average of all temperatures.
(B) compute average of last five.
(C) add a new temperature.
(D) print the number of temperatures recorded.
(E) print the temperatures recorded,or
(F) exit the program
F
Exiting the program
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.