85.0k views
1 vote
Write a program that will: 1. Call a function to input temperatures for consecutive days in an array. The temperatures are to be integer numbers. 2. The user will input the number of temperatures. There will be no more than 10 temperatures. 3. Call a function to sort the array by ascending order. You can use any sorting algorithm you wish as long as you can explain it. 4. Call a function that will return the average of the temperatures. The average should be displayed to two decimal places.

User Cristo
by
4.3k points

1 Answer

6 votes

Answer:

Following are the program to the given question:

#include<iostream>//defining header file

using namespace std;

float temperatures()//defining a method temperatures

{

int a[10],i,j,n,min,max,sum=0,temp;//defining integer variable

float avg;//defining floating point variable

cout<<"Please input the number of temperatures to be read: ";//print message

cin>>n;//input integer value

if(i>10)//use if to check array length greater than 10

return -1;//return integer value -1

for(j=0;j<n;j++)//defining loop to input value

{

cout<<"Input temperature: "<<endl;//print message

cin>>a[j];//holding value in loop

}

for(i=0;i<n-1;i++)//defining loop to compare array value

{

for(j=i+1;j<n-1;j++)//defining loop to compare array value

{

if(a[i]>a[j])//use if to compare array values

{

//swapping

temp=a[j];//holding array value in temp

a[j]=a[i];//holding value in first index of array

a[i]=temp;//holding temp value

}

}

}

max=min=a[0];//holding max value

for (i = 0; i < n; i++)//defining loop to compare array value

{

if (a[i] > max)//use if to compare max array value

{

max = a[i];//holding max array value

}

else if (a[i] < min)//use else if to compare min array value

{

min = a[i];//holding min array value

}

}

for(i=0;i<n;i++)//use loop to add array value

sum=sum+a[i];//adding array value

avg=(float) sum/n;//calculating avg of the array

std::cout.setf(std::ios::fixed);//using namespace

std::cout.precision(2);//using precision method

cout<<"The average temperature is: "<<avg<<endl;//print avg value with message

cout<<"The highest temperature is: "<<max<<endl;//print max value with message

cout<<"The lowest temperature is: "<<min<<endl;//print min value with message

return avg;//return avg value

}

int main()//defining main method

{

float k;//defining float variable

k=temperatures();//calling method

}

Output:

Please find the attached file.

Step-by-step explanation:

In this code, a method "temperatures" is declared that defines the integer and float variable in which "n, array" is used to store the array value and inside the method min and max variable is declared that use the conditional statement to store its value.

Outside the main method, a floating-point variable "k" is declared that calls the method and stores its value.

Write a program that will: 1. Call a function to input temperatures for consecutive-example-1
User Awesoon
by
4.0k points