0

Hi i had to write the program that prompts the user for integer values. Place these values into an array.
What i cant figure out is the following i will be highly thankful to you if you can help me on these
Compute Sum – add up all of the values in the array.
Compute Average – divide the sum by the number of elements to compute the
average.
Maximum Value – iterate through the array to find the largest value.

#include <iostream>
#include <string>
using namespace std;

	int main() {

		
		int choice = 0;
		int value;
		int array1 [10];
		int i;
		int location;
		bool test1= false;
		bool found = false;
		bool test4 = false;
		bool test6 = false;

		for ( i = 0; i< 10; i++)
		{
			bool done = false; 
			while(!done){
			cout<< " Enter a value: ";
			if(cin>>value)
				{
					array1 [i] = value;
					done = true;
				}
				
			else{
				cin.clear();
				string failv;
				getline(cin, failv);
				cout<< "Invalid Value"<<endl;
			}
			
			}
		}
		do
		{	bool test  = false;
			while(!test){

				cout<<endl;
				cout<< "1."<< "Display Numbers -"<<endl;
				cout<< "2."<< "Display Reversed Numbers -"<<endl;
				cout<< "3."<< "Display Specific Value -"<<endl; 
				cout<< "4."<< "Find Value -"<<endl; 
				cout<< "5."<< "Insert Value -"<<endl; 
				cout<< "6."<< "Input New Values -"<<endl; 
				cout<< "7."<< "Quit -"<<endl;

				cout<<"Enter Choice:"<<endl;
			if(cin >>choice){
				if(choice<1 || choice>7){
					cout<< " Invalid entry "<<endl;	 
				}else{
					test=true;
				}
			}else{
				cin.clear();
				string quit1;
				getline(cin,quit1);
				cout<< "Invalid Entry"<<endl;
			}
		}

			switch(choice)
		{
			case 1: for ( i =0; i<10; i++)
					{
						cout<<array1[i]<<", ";
					}
					break;
									
			case 2: for (i =9; i>=0; i--)
					{
						cout<<array1[i]<<", ";
					}
					break; 
									

			case 3: while(!test1)
					{
						cout<< " Enter Location"<<endl;
						if( cin>> i)
						{
							if( i<0 || i>9 )
							{
								cout<< " i is out of bounds of the array "<<endl;
							}else{
								cout<< " value "<<i<< ": "<< array1 [i]<<endl;
								test1 = true;
							}
						}
						else
							{
							cin.clear();
							string LocF;
							getline(cin,LocF);
							cout<< "Invalid i entry"<<endl;
							}
						
					}
					break;

			case 4: while (!test4)
						{
							cout<< "Please Enter a Value"<<endl; 
							if(cin>> value)
							{
								test4 = true; 
							}else
								{
								cin.clear();
								string valF;
								getline(cin,valF);
								cout<< "Invalid value entry"<<endl;
								}
							}
						for ( i= 0; i <10; i++)
							{
								if ( array1[i] = value )
								{
									i = 11; 
									found = true;
								}
							}
						if (found)
							{
								cout<<" location of this value is: "<<i<<endl;
							}
							else
							{
								cout<<" value does not exist"<<endl;
							}

			                break;
			
					/*case 5: cout<< "Please Enter a Location"<<endl;
					cin>> location;
					cout<< "Please Enter a value"<<endl; 
					cin>> value;
					
					array1[location] = value;
					
			break;*/

			case 6:	for ( i = 0; i< 10; i++)
					{
						while(!test6){
						cout<< " Enter a value: ";
						if(cin>>value)
						{
							array1 [i] = value;
							test6 = true;
						}
						else{
							cin.clear();
							string Failv;
							getline(cin, Failv);
							cout<< "Invalid Value"<<endl;
						}
						}
					}
				break; 

			case 7: cout<< "Quit the Program"<<endl; 
					break;

			default: cout<< "Invalid Option"<<endl; 
					break;
			}
			
	
	}while( choice !=7);
	int r;
	cin>> r;
	return 0;
	}
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by Clinton Portis
0

I tried to give ye' some guidance with pseudocode... but trying to explain the loop conditions and array indicies seemed to be awkward and would have only added to your confusion. So I will give you one free of charge. Give the other 2 problems a chance on your own and show us what you come up with.

Compute Sum:

int compute_sum(int array[])
{
     int var = 0;

     for(int i=0; i<size; i++)
     {
          var += array[i];
     }

     return var;
}

Edited by Clinton Portis: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.