# find the largest number, the smallest number, the average, the sum,

Page 1 of 1

## 7 Replies - 65949 Views - Last Post: 19 October 2008 - 07:51 PMRate Topic:     //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=68191&amp;s=84c79861c655c85b8e81dec8bf54f033&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 scully_67 Reputation: 0
• Posts: 3
• Joined: 19-October 08

# find the largest number, the smallest number, the average, the sum,

Posted 19 October 2008 - 02:45 PM

Hey guys,

I need some help. I need to write a program that will take a file that has a list of numbers and will find the largest number, the smallest number, the average, the sum, and the number of values on the list. It also needs a menu. This is what I have so far:

```#include <iostream>
#include <fstream>
using namespace std;

int main()
{
ifstream inputFile; //file stream object
int choice; // menu choice
int number; // to hold a value from file
int total; //to hold a value from file
int large; // to hold largest value from file
int small; // to hold smallest value from file
int sum; // to hold sum of the values from file
int average; // to hold the average values from file
int quantity; // to hold the number of values entered

inputFile.open("TopicDin.txt"); // Open the file

do
{
//display the menu and get a choice
cout << "Make a selection from the list:";
cout << "1. Get the largest value";
cout << "2. Get the smallest value";
cout << "3. Get the sum of the values";
cout << "4. Get the average";
cout << "5. Get the number of values entered";
cout << "6. End this program";
cout << "Enter your choice: ";
cin >> choice;

//Validate the menu selection
while (choice < 1 || choice > 6)
{
cout << "Please enter 1, 2, 3, 4, 5, or 6: ";
cin >> choice
}

// Respond to the user's menu selection
switch (choice)
{
case 1:
inFile << number
cout << large;
break;

case 2;
cout << small;
break;

case 3;
inFile << number
do
number += number
while(number != " ");
cout << number;
break;

case 4;
cout << average;
break;

case 5;
cout << quantity;
break;

case 6;
cout << "Program ending. \n";
break;

}
} while (choice != 6)

return 0;
}

```

I'm just stuck on how to evaluate the list to find these numbers.

Mod edit - Fixed code tags
~BetaWar

Is This A Good Question/Topic? 0

## Replies To: find the largest number, the smallest number, the average, the sum,

### #2 GWatt Reputation: 309
• Posts: 3,106
• Joined: 01-December 05

## Re: find the largest number, the smallest number, the average, the sum,

Posted 19 October 2008 - 02:54 PM

Finding the average is easy. You simply take the sum of all numbers in the list and divide by the amount of numbers in the list.
Maximum and minimum are a little less straightforward. You can iterate through the list and keep track of the largest and smallest numbers to that point and when you get to the end you know that you have the largest and smallest numbers for the entire list.
I would probably use the built in sort functions in the standard library and then take the element off the front and back of the array.

### #3 scully_67 Reputation: 0
• Posts: 3
• Joined: 19-October 08

## Re: find the largest number, the smallest number, the average, the sum,

Posted 19 October 2008 - 02:56 PM GWatt, on 19 Oct, 2008 - 02:54 PM, said:

Finding the average is easy. You simply take the sum of all numbers in the list and divide by the amount of numbers in the list.
Maximum and minimum are a little less straightforward. You can iterate through the list and keep track of the largest and smallest numbers to that point and when you get to the end you know that you have the largest and smallest numbers for the entire list.
I would probably use the built in sort functions in the standard library and then take the element off the front and back of the array.

How do you iterate through the list?

### #4 SCStudent86 Reputation: 1
• Posts: 21
• Joined: 06-October 08

## Re: find the largest number, the smallest number, the average, the sum,

Posted 19 October 2008 - 03:10 PM scully_67, on 19 Oct, 2008 - 02:56 PM, said: GWatt, on 19 Oct, 2008 - 02:54 PM, said:

Finding the average is easy. You simply take the sum of all numbers in the list and divide by the amount of numbers in the list.
Maximum and minimum are a little less straightforward. You can iterate through the list and keep track of the largest and smallest numbers to that point and when you get to the end you know that you have the largest and smallest numbers for the entire list.
I would probably use the built in sort functions in the standard library and then take the element off the front and back of the array.

How do you iterate through the list?

First you need to declare an array with the size of your list of numbers. The you can use a FOR-loop to loop around the numbers until it finds the largest or smallest number. For the average and sums, just use a for-loop again to get the sums of all the numbers. Kind of like the summation formula in mathematics. Then you can take that and divide the sum by the size of your list.
[code]
int count;
int highest;

highest = numbers;
for(count = 1; count < SIZE; i++)
{
if(numbers[count] > highest)
{
highest = numbers[count];
}
}
[code]
Same scenario for the lowest number. I wouldn't worry about creating a function that does this unless your instructor wants that. The algorithm isn't really that big. All you can do is just add it to your switch command where it asks for the highest/lowest.

### #5 scully_67 Reputation: 0
• Posts: 3
• Joined: 19-October 08

## Re: find the largest number, the smallest number, the average, the sum,

Posted 19 October 2008 - 03:12 PM SCStudent86, on 19 Oct, 2008 - 03:10 PM, said: scully_67, on 19 Oct, 2008 - 02:56 PM, said: GWatt, on 19 Oct, 2008 - 02:54 PM, said:

Finding the average is easy. You simply take the sum of all numbers in the list and divide by the amount of numbers in the list.
Maximum and minimum are a little less straightforward. You can iterate through the list and keep track of the largest and smallest numbers to that point and when you get to the end you know that you have the largest and smallest numbers for the entire list.
I would probably use the built in sort functions in the standard library and then take the element off the front and back of the array.

How do you iterate through the list?

First you need to declare an array with the size of your list of numbers. The you can use a FOR-loop to loop around the numbers until it finds the largest or smallest number. For the average and sums, just use a for-loop again to get the sums of all the numbers. Kind of like the summation formula in mathematics. Then you can take that and divide the sum by the size of your list.
[code]
int count;
int highest;

highest = numbers;
for(count = 1; count < SIZE; i++)
{
if(numbers[count] > highest)
{
highest = numbers[count];
}
}
[code]
Same scenario for the lowest number. I wouldn't worry about creating a function that does this unless your instructor wants that. The algorithm isn't really that big. All you can do is just add it to your switch command where it asks for the highest/lowest.

Thank you very much!

### #6 AfterBurner66 Reputation: 16
• Posts: 117
• Joined: 02-August 08

## Re: find the largest number, the smallest number, the average, the sum,

Posted 19 October 2008 - 04:38 PM scully_67, on 19 Oct, 2008 - 02:45 PM, said:

Hey guys,

I need some help. I need to write a program that will take a file that has a list of numbers and will find the largest number, the smallest number, the average, the sum, and the number of values on the list. It also needs a menu. This is what I have so far:

```#include <iostream>
#include <fstream>
using namespace std;

int main()
{
ifstream inputFile; //file stream object
int choice; // menu choice
int number; // to hold a value from file
int total; //to hold a value from file
int large; // to hold largest value from file
int small; // to hold smallest value from file
int sum; // to hold sum of the values from file
int average; // to hold the average values from file
int quantity; // to hold the number of values entered

inputFile.open("TopicDin.txt"); // Open the file

do
{
//display the menu and get a choice
cout << "Make a selection from the list:";
cout << "1. Get the largest value";
cout << "2. Get the smallest value";
cout << "3. Get the sum of the values";
cout << "4. Get the average";
cout << "5. Get the number of values entered";
cout << "6. End this program";
cout << "Enter your choice: ";
cin >> choice;

//Validate the menu selection
while (choice < 1 || choice > 6)
{
cout << "Please enter 1, 2, 3, 4, 5, or 6: ";
cin >> choice
}

// Respond to the user's menu selection
switch (choice)
{
case 1:
inFile << number
cout << large;
break;

case 2;
cout << small;
break;

case 3;
inFile << number
do
number += number
while(number != " ");
cout << number;
break;

case 4;
cout << average;
break;

case 5;
cout << quantity;
break;

case 6;
cout << "Program ending. \n";
break;

}
} while (choice != 6)

return 0;
}

```

I'm just stuck on how to evaluate the list to find these numbers.

Mod edit - Fixed code tags
~BetaWar

Generally,in whatever programming language you use(talking about imperative languages in this context),you can take the following approach,as for finding min,max in a list etc.:

-You can use an array to hold your values, so if you want to find whatever you want after iteration has ended, to be able to have these values stored in your array.

-You can arbitrarily assign min value to the first element of the array,that is if for example your array is called arr,arr.You can assign the max value to the same element.So:
min = arr;
max = arr;
As iteration goes on,with a simple comparison,the real min and max elements will show off:

for (j = 0;j < sizeOfArray;j++) {
if arr[j] > max
max = arr[j];
if arr[j] < min
min = arr[j];
}

-Running another loop like the previous for, you can find the sum of array elements:
sum = 0; /* give an initial value to sum,in order not to hold previous "garbage" values */

for (j = 0;j < sizeOfArray;j++) {

sum += arr[j];

}
-After this stage,you can easily find average:
avg = (float) sum / numberOfListElements; //casting to hold fractional part

-All previous hold, with the assumption that you know in advance the number of values in the list, so that's why I used a for loop.If you don't , no problem at all.You can use a while loop, like this:

while(!EOF) { //while you have not reached the end of file

//do your whatever ( e.g transfer values from file to the array )

}

Then with another while you can traverse the array and find the min,max just like before etc.

Hope this helps This post has been edited by AfterBurner66: 19 October 2008 - 04:48 PM

### #7 Jayman Reputation: 423
• Posts: 9,532
• Joined: 26-December 05

## Re: find the largest number, the smallest number, the average, the sum,

Posted 19 October 2008 - 06:54 PM

Topic renamed to be more descriptive of the problem.

### #8 SCStudent86 Reputation: 1
• Posts: 21
• Joined: 06-October 08

## Re: find the largest number, the smallest number, the average, the sum,

Posted 19 October 2008 - 07:51 PM scully_67, on 19 Oct, 2008 - 02:45 PM, said:

Hey guys,

I need some help. I need to write a program that will take a file that has a list of numbers and will find the largest number, the smallest number, the average, the sum, and the number of values on the list. It also needs a menu. This is what I have so far:

```#include <iostream>
#include <fstream>
using namespace std;

int main()
{
ifstream inputFile; //file stream object
int choice; // menu choice
int number; // to hold a value from file
int total; //to hold a value from file
int large; // to hold largest value from file
int small; // to hold smallest value from file
int sum; // to hold sum of the values from file
int average; // to hold the average values from file
int quantity; // to hold the number of values entered

inputFile.open("TopicDin.txt"); // Open the file

do
{
//display the menu and get a choice
cout << "Make a selection from the list:";
cout << "1. Get the largest value";
cout << "2. Get the smallest value";
cout << "3. Get the sum of the values";
cout << "4. Get the average";
cout << "5. Get the number of values entered";
cout << "6. End this program";
cout << "Enter your choice: ";
cin >> choice;

//Validate the menu selection
while (choice < 1 || choice > 6)
{
cout << "Please enter 1, 2, 3, 4, 5, or 6: ";
cin >> choice
}

// Respond to the user's menu selection
switch (choice)
{
case 1:
inFile << number
cout << large;
break;

case 2;
cout << small;
break;

case 3;
inFile << number
do
number += number
while(number != " ");
cout << number;
break;

case 4;
cout << average;
break;

case 5;
cout << quantity;
break;

case 6;
cout << "Program ending. \n";
break;

}
} while (choice != 6)

return 0;
}

```

I'm just stuck on how to evaluate the list to find these numbers.

Mod edit - Fixed code tags
~BetaWar

Why do you keep re-posting help for this problem? Do you still not know the process of the program?????

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }