# Running out of ideas from Class assignment

Page 1 of 1

## 8 Replies - 881 Views - Last Post: 06 September 2013 - 03:55 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=328279&amp;s=2f9e5cf5b32aed85f4ef4bcfc56acad9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Honey_GaBee

Reputation: 0
• 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

• D.I.C Lover

Reputation: 331
• 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;
}

```

### #3 #define

• Duke of Err

Reputation: 1743
• Posts: 6,200
• 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).

### #4 Skydiver

• Code herder

Reputation: 4818
• Posts: 15,931
• 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);
}
}

```

### #5 jjl

• Engineer

Reputation: 1169
• Posts: 4,785
• 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;
}

```

### #6 Skydiver

• Code herder

Reputation: 4818
• Posts: 15,931
• 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.

### #7 jjl

• Engineer

Reputation: 1169
• Posts: 4,785
• Joined: 09-June 09

## Re: Running out of ideas from Class assignment

Posted 06 September 2013 - 11:52 AM

Nice catch!

### #8 jimblumberg

Reputation: 4910
• Posts: 15,338
• 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

### #9 vividexstance

• D.I.C Lover

Reputation: 783
• Posts: 2,767
• 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.