"Write a program where the user inputs several integers.
Output sum,max,secmax,and average in object oriented class.
thus can't get the average to work.
On Fri, 28 Apr 2006 20:28:17 -0700, Fao wrote:
Sorry for the long wait on the response.
Okay I have stripped down the program to just do SUM and MAX.
My problem is I don't know how to "pull" the input data from SumType
into MaxType.
As osmium says, it's hard to help you because we don't know exactly what
you're trying to accomplish. Are you working off a specific assignment or
exam question? If so, post the assignment. If you don't have an
assignment, try and state simply what you're trying to do - don't say
(yet) how you're trying to accomplish it, because your difficulties may
be due to you approaching the problem in the wrong way.
To give you an idea of the sort of information we need, and because this
may help you anyway, I'll take a guess at what you're trying to do and
show you how I would approach the problem.
Let's say you want to input a list of numbers and then print out the
maximum and the sum (this is the statement of what I'm trying to
accomplish - my assignment, if you like).
To begin, think about how you would do this if you were working it out
manually, rather than using a computer. What you would do is write down
all the numbers, then go through them looking for the maximum, then go
through them again and work out the sum.
Now, we can try and break down this method into parts that we can convert
into a program. What you have is one object (the place where you write
down the numbers) and three operations (input the numbers, find the
maximum and calculate the sum). For the place you are writing down the
numbers, you can use an array:
const int max_numbers=10;
int numbers[max_numbers];
For the three operations, you can use functions:
/* Input up to |max| numbers until the user inputs |sentinel|, storing the
numbers in |store| and returning how many numbers where input */
int input(int store[], int max, int sentinel);
/* Return the maximum from the array |the_numbers| of |length| numbers */
int find_maximum(int the_numbers[], int length);
/* Return the sum of array |the_numbers| of |length| numbers */
int calculate_sum(int the_numbers[], int length);
Putting all this together in a program:
void main()
{
const int sentinel = -999
const int max_numbers=10;
int numbers[max_numbers];
int how_many;
how_many = input(numbers, max_numbers, sentinel);
std::cout << "Maximum: " << find_maximum(numbers, how_many) << std::endl;
std::cout << "Sum: " << calculate_sum(numbers, how_many) << std::endl;
}
Note that I'm not using inheritance or polymorphism here - the assignment
I set myself doesn't require them. Maybe you don't need to use them either?