7 Replies - 29916 Views - Last Post: 19 October 2008 - 07:51 PM Rate Topic: -----

#1 scully_67  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • member icon

Reputation: 274
  • View blog
  • Posts: 3,074
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 scully_67  Icon User is offline

  • New D.I.C Head

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

View PostGWatt, 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?
Was This Post Helpful? 0
  • +
  • -

#4 SCStudent86  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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

View Postscully_67, on 19 Oct, 2008 - 02:56 PM, said:

View PostGWatt, 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[0];
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.
Was This Post Helpful? 1
  • +
  • -

#5 scully_67  Icon User is offline

  • New D.I.C Head

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

View PostSCStudent86, on 19 Oct, 2008 - 03:10 PM, said:

View Postscully_67, on 19 Oct, 2008 - 02:56 PM, said:

View PostGWatt, 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[0];
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!
Was This Post Helpful? 0
  • +
  • -

#6 AfterBurner66  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 116
  • Joined: 02-August 08

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

Posted 19 October 2008 - 04:38 PM

View Postscully_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[0].You can assign the max value to the same element.So:
min = arr[0];
max = arr[0];
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

Was This Post Helpful? 0
  • +
  • -

#7 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#8 SCStudent86  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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

View Postscully_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?????
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1