I try to modify the below code by using the Arrays but unable to success , would someone help me please. Thanks

//Outputs the lowest, highest, and average of inputted temperatures
//using an array structure 

#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
 	//input variables
 	float num = 0; // number of temps to be inputted
 	float temp = 0;
 	
 	//processing variables
 	int counter = 0; // keeps track of index in array (outside loop)
 	float lowest = 0;
 	float highest = 0;
 	float sum = 0;
 	float avg = 0;
 	float temps[50] = {0}; // array of temperatures
 	
 	//executable statements
 	cout << "How many number of temperature would you like to input: ";
 	cin >> num;
 	cout << "Please input temperature: ";
 	cin >> temp;
 	
 	if (temp != -999)
 	{
	 	  	  while (counter <= num - 1)
	 	  	  {
			   		temps[counter] = temp;
			   		
  					// Calculating lowest and highest temp
                    lowest = temps[counter];
                    highest = temps[counter];
			   		int loop = 0; // counter for loop through indices in array 
			   		while (loop < num - 1)
			   		{
					 	  if(temps[loop] < lowest)
					 	  {
                                  lowest = temps[loop];
						  }
						  else // temps[t] >= lowest
						  {
						   	   if (temps[loop] > highest)
						   	   	  highest = temps[loop];
					      }
					      loop++;
			 	  	}// end while
			 	  					   	                    
                    sum = sum + temp; 
                    
					//Input another temperature
					if (counter < num - 1)
					{
		 	  	       cout << "Please input temperature: ";
 					   cin >> temp;
                    }
					               
                    counter++;
 					
			  }//end while
			 	  	
			  //Calculating average
			  avg = sum/num;
			  
			  //counter == num
 			  cout << "The lowest temperature is: " << fixed;
	 	  	  cout << setprecision(2) << lowest << endl;
	 	  	  cout << "The highest temperature is: " << fixed;
	 	  	  cout << setprecision(2) << highest << endl;
	 	  	  cout << "The average temperature is: " << fixed;
	 	  	  cout << setprecision(2) << avg << endl;
	}//end if   
	
 	system("PAUSE");
 	return EXIT_SUCCESS;
}

Move lines 27 and 28 inside the while loop so that it asks for a temperature on every iteration of the loop.


Delete the if statement on lines 30 and 31 because the program already knows how many temps to enter.

The while statement on lines 40 and 41 is not needed. You just need the if statements so keep track of the lowest and highest temps as you enter them.

This article has been dead for over six months. Start a new discussion instead.