Hi guys :
i'm new in programming, i'm reading c++ by dissection and for chapter 2 i encounter with a exercise that i can't solve it,please help me on it.
the program is :
Write a program that finds the maximum and minimum integer value of a sequence of inputted integers. The program should first prompt the user for how many values will be entered. The program should print this value out and ask the user to confirm this value. If the user fails to confirm the value, she must enter a new value.

i don't have the concept, please help me.

thank you

Recommended Answers

All 4 Replies

You have to first create a data structure. An array or vector of int should be the most correct data structure.
Then ask the user how may value you need to input.
Run a loop and accept all those value in a vector ( or array,) from the user. ( by using cin>>)
Now you will have a vector ( or an array) which contain the sequence.
To find the maximum, assume that the first element of the vector (or array ) is the maximum. (this can be done by defining a variable: max and setting its value to the value of the first element of the vector or array) Now iterate through the whole vector(or array) and check if max<n where n is the i-th element.
If it is, set max to n.

commented: Very good description of the algorithm :) +3

Probably it's described in a further chapter of your book, but I still want to give you an example using vectors:

/* Declare the vector */
vector<int> v;
/* Put some values in it */
v.push_back(10);
v.push_back(3);
v.push_back(30);
v.push_back(12);
v.push_back(5);
v.push_back(23);
v.push_back(28);
/* Sort the vector from the lowest to the highest numbers */
sort(v.begin(),v.end()); /* now the vector contains the elements in the following order: 3, 5, 10, 12, 23, 28, 30 */

Now you just have to read out the first and the last element of the vector and you've the lowest and the highest number of a sequence :), but probably siddhantes' way is the easiest to understand for you ...

I think you guys are reading too much into the problem. It simply asks that the program keep track of the lightest and lowest values entered, there is nothing about storing all the values.

sailorsun - once you've gotten the number of values to be examined from the user, use that as the limit on a for loop. As each value is then entered, it, compare it to both the stored high value and the stored low value, updating them as appropriate.

Oh, where do you get the initial high and low values? Before the loop, get the first input from user and that becomes both high and low. (Consider the user was going to give you just one value - isn't that by default the high and the low of the set?)

>>I think you guys are reading too much into the problem. It simply asks that the program keep track of the lightest and lowest values entered, there is nothing about storing all the values.

Right vmanes, even I was first thought of giving the same line of attack to the problem. But I asked him to store values in an vector so that he maybe later if decide to print the arithmetic mean, median, etc can do that with least modifying the code. Moreover, maybe this will help him get through more concepts (it is quite possible that OP didn't used a vector before).;)
Anyways, this approach ( what you gave in your last post) will surely be more fast and efficient(both memory and time) to do small jobs like this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.