Getting started Change into the cse220 directory. Create a new directory called
ID: 3681164 • 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
#include <stdio.h> // header file
int newtemp(); // function declarations
float avgtemp();
float avg5temp();
int nooftemp();
void printall();
int temp[500]={10,10,10,10,10}; // global array with initial values
int indx=5; // index of temp array
int main() // execution starts here
{
char c; // character to recognize user option
do
{ // program contiues untill user enters F- i.e exit
printf(" Enter A to computer average temperature of all. B to compute avg of last five. C to add new temperatures. ");
printf("D to print the number of temperature records. E to print temeratures recoreded. F to exit");
scanf("%c",&c);
switch(c) // switch to do tasks according to users input character
{
case 'A': printf("Average temperature is: %f",avgtemp()); break;
case 'B': printf("Average of last five temperatures is: %f",avg5temp());break;
case 'C': newtemp(); break;
case 'D': printf("All records are: %d ",nooftemp()); break;
case 'E': printall();break;
case 'F': printf("Good bye"); break;
}
}while(c!='F'); // repeat until user enters F
return 0;
}
int newtemp() // function to add new record to temp array
{
int j,n; // local variables for the calculation of array indexes
for(int i=0;i<500;i++) // going through array to check no of records stored
{
if(temp[i]==0) // if any element is equal to 0;, i.e default value of an array
{
j=i; break; // storing that perticular index to add new values from here
}
}
printf(" Enter number of temperature records to enter: ");
scanf("%d",&n);
printf("Enter the record and press enter ");
for(int i=j;i<j+n;i++) // loop to store no of temeratures
{
scanf("%d",&temp[i]);
}
indx=j+n; // storing new index in indx variable
return j+n; // returning index- optional
}
void printall() // fun to print all elements
{
for(int i=0;i<indx;i++) // loop through all elements to print
{
printf("%d ",temp[i]);
}
}
float avgtemp() // fun to calucalte avg temp all records
{
int count=0;
float avg;
for(int i=0;i<indx;i++)
{
count=count+temp[i]; // with the help of count summing all temps
}
avg=count/indx; // avg formula
return avg; // returning avg
}
int nooftemp() // function to know no of records
{
return indx;
}
float avg5temp() // fun to calculate last 5 temps avgs
{
int count=0;
float avg;
if(indx<5) // if records are less than five calculation using other formula
{
for(int i=0;i<indx;i++)
{
count=count+temp[i];
}
avg=count/indx; // avg formula
return avg;
}
else
{
for(int i=indx-1;i<indx+5;i--)
{
count=count+temp[i];
}
avg=count/5; // avg formula for last five temps
return avg;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.