8 Replies - 578 Views - Last Post: 06 September 2013 - 03:55 PM Rate Topic: -----

#1 Honey_GaBee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-September 13

Running out of ideas from Class assignment

Posted 03 September 2013 - 06:30 PM

My assignment is,
"Write a C++ console application that accepts up to 5 numbers from the user. Display
all numbers, the highest, the lowest, and the average of the numbers.
The user does not have to enter all 5 numbers. They can enter fewer. However,
of course, you need at least two numbers to be able to look for the highest and
the lowest. Make sure you explain this for the user.
Create three functions to perform the following tasks:
Calculate the highest number.
Calculate the lowest number.
Calculate the average.
All three functions receive an array and return a single value.
Keep track of the number of the values the user enters. Remember, they do
not have to enter all 5 numbers."

Im having trouble with the part where they DONT HAVE TO ENTER THE FIVE NUMBERS. can any one suggest me or point me to what it is required?? .. This is my Code



 
//This proggram calculates the average, the lowest, and the hights of five numbers.
//Gabriel Barrera
//CSIT 743 8/26/2013 

#include <iostream>

using namespace std;

const int ARRAY_SIZE = 5;	//Creat A const Array
int numbers_entered[ARRAY_SIZE]; // Arrary SIZE and Declare array

void calculate_highest() //Calculates the Highest 
{
	int highest = numbers_entered[0];

	for (int i = 1; i < ARRAY_SIZE; i++)// loop to catch the highest
	{
		if (numbers_entered[i] > highest)
			highest = numbers_entered[i];
	}
	
	cout << "The Highest of the numbers is " << highest << endl;
}

void calculate_lowest() // Calculates the lowest
{
		int lowest = numbers_entered[0];

	for (int i = 1; i < ARRAY_SIZE; i++)// Loop to catch the lowest 
	{
		if (numbers_entered[i] < lowest)
			lowest = numbers_entered[i];
	}
	
	cout << "The lowest of the numbers is " << lowest << endl;
}

void calculate_average()
{
	double total = 0;
	double average = 0; //Initiaize the accumulator
	
	for (int counter = 0; counter < ARRAY_SIZE; counter++) // loop for the average
	{
		total += numbers_entered[counter];
		average = total / numbers_entered[counter];
	}
	
	cout << "The average of the numbers is : " << average << endl;
	

}

int main() //Main function are better at the bottom no lousy PROTOTYPEs
{
	



	cout << "This proggram calculates the average, the lowest, and the hights of five numbers." << endl;
	cout << "Please Enter up to 5 numbers. ";
	
	for (int counter = 0; counter < ARRAY_SIZE; counter++) // loop for the array
		
		{
			cout << endl << "Please Enter a number : ";
			cin >> numbers_entered[counter];

		}
		
	//Summon all the previous functions
	calculate_average();
	calculate_highest();
	calculate_lowest();

	
	
	
	system("pause");
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Running out of ideas from Class assignment

#2 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Running out of ideas from Class assignment

Posted 03 September 2013 - 07:26 PM

Does cout return the number of items it stores in a variable or object?

If it does then, use that return from count as an additional test to see if your program should loop back for yet another number.

In C, it could be:
int numbers[5]=0;
int i=0;
while((scanf("%d", &numbers[i)) > 0) {
   ++i;
   if(i>4) //since 0 to 4 equals 5
      break;
}


Was This Post Helpful? 0
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1344
  • View blog
  • Posts: 4,608
  • Joined: 19-February 09

Re: Running out of ideas from Class assignment

Posted 03 September 2013 - 07:37 PM

It might be easiest just to ask the user how many numbers they wish to input. Otherwise you could ask them if they are finished after each input, or tell them to enter a special value to finish (sentinel value).
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3574
  • View blog
  • Posts: 11,112
  • Joined: 05-May 12

Re: Running out of ideas from Class assignment

Posted 05 September 2013 - 05:43 AM

Add an elementCount parameter to your calculate_average(), calculate_highest(), and calculate_lowest() functions, and inside those functions, instead of looping to ARRAY_SIZE, simply loop to elementCount. While you are at it, you should stop using globals, and might as well also pass in the array as well.

Then you can change your main for loop to something like this psuedo code:
for(int counter = 0; counter < ARRAY_SIZE; counter++)
{
    cout << "Enter a number. ";
    if (counter > 1)
        cout << "Press Ctrl-C if you don't want to enter any more numbers.";
    cout << endl;

    cin >> numbers_entered[counter];

    if (counter >= 1)
    {
        calculate_average(numbers_entered, counter + 1);
        calculate_lowest(numbers_entered, counter + 1);
        calculate_highest(numbers_entered, counter + 1);
    }
}


Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Running out of ideas from Class assignment

Posted 05 September 2013 - 11:05 AM

the return value of the std::cin >> operator is a std::istream, however, it decays to a boolean when used a condition (true = good state, false = bad state (or EOF)). You can signal EOF on windows with ctrl-c I believe.

i.e.
#define MAX_SIZE 5

int main() {
   int arr[MAX_SIZE];
   int size = 0
   
   /* get input while input is not EOF and size is less than 5 */
   while(std::cin>>arr[size] && size < MAX_SIZE) {
      ++size; /* input was valid if you end up here */
   }

   return 0;
}


Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3574
  • View blog
  • Posts: 11,112
  • Joined: 05-May 12

Re: Running out of ideas from Class assignment

Posted 05 September 2013 - 10:04 PM

Actually, you want to reverse the conditions in the while() boolean expression:
size < MAX_SIZE && std::cin>>arr[size]



If you kept it the other way, you would have a buffer overrun.
Was This Post Helpful? 1
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Running out of ideas from Class assignment

Posted 06 September 2013 - 11:52 AM

Nice catch!
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 4070
  • View blog
  • Posts: 12,557
  • Joined: 25-December 09

Re: Running out of ideas from Class assignment

Posted 06 September 2013 - 12:13 PM

Also Ctrl-C doesn't signal EOF it terminates the program. You want either Ctrl-Z or Ctrl-D depending on your operating system.

Jim
Was This Post Helpful? 1
  • +
  • -

#9 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 659
  • View blog
  • Posts: 2,270
  • Joined: 31-December 10

Re: Running out of ideas from Class assignment

Posted 06 September 2013 - 03:55 PM

I'm pretty sure you're calculating the average wrong:
void calculate_average()
{
	double total = 0;
	double average = 0; //Initiaize the accumulator
	
	for (int counter = 0; counter < ARRAY_SIZE; counter++) // loop for the average
	{
		total += numbers_entered[counter];
		average = total / numbers_entered[counter];
	}
	
	cout << "The average of the numbers is : " << average << endl;
	

}


You don't need to compute the average inside the for-loop. You can do it right after the loop ends, and you're using the wrong value to divide the total amount by. You need to be using the number of items that make up the total. So in this case, you would use the size of the array.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1