Programming Challenges Programming Challenges 1-5 allow you to practice working
ID: 3826508 • Letter: P
Question
Programming Challenges Programming Challenges 1-5 allow you to practice working with arrays without using structures or classes. Programming Challenges 5-14 use arrays with structures and classes. 3 1. Largest/smallest Array values te a program that lets the user enter at least 10 values into an array The program should then display the largest and smallest values stored in the 2. Rainfall Statistics myCodelllale write a program that lets the user enter the total rainfall for each of 12 months into an ld calculate and display the total rainfall for the yearExplanation / Answer
//Answer of Number 1.Largest/smallest array values-----------------------------------------
#include <iostream>
using namespace std;
int main ()
{
int myArray[ 10 ]; //delcares array with atleast 10 elements
int small,big;
int n=10; // number of elements of array
cout << "Enter the elements of array :"<<endl;
for ( int i = 0; i <n; i++ )
{
cout << "Input" << i+1 << ": ";
cin >> myArray[i];
}
big=small=myArray[0]; //it assigns the first element of array to be largest or smallest value for comparison
for (int i = 0; i < n; i++) //
{
if(myArray[i]>big) //compares largest value with current element
{
big=myArray[i];
}
if(myArray[i]<small) //compares smallest value with current element
{
small=myArray[i];
}
}
cout << "The largest number is " << big << endl; //prints the largest number
cout << "The smallest number is " << small << endl; //prints the smallest number
return 0;
}
//------------------------------------------------------------------------------------------------------
//Answer of number 2.Rainfall statistics of a year----------------------------------------------------------
#include <iostream>
using namespace std;
int main()
{
int rainFall[12],i,total;// to make simple it is assumed that input rainfall amounts are positive integers
double avgRain;
total=0;
cout<<"input rain falls for each month of the year ";
cout<<endl;
for(i=0;i<12;i++)
{
cout<< "rainfall of month "<<i+1<<":"<<endl;
cin>>rainFall[i];// inputs are stored in array
total=total+ rainFall[i];
}
avgRain =( (double)total)/12;
cout<< "The total rainfall of the year : "<<total;
cout<<endl;
cout<< "The average rainfall of the year "<<avgRain;
cout<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.