and the number of values on the list

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:

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

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.

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.

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[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.

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[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.

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:

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:

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: