I'm a beginner with this whole programming thing I got my self a new programming book with a a CD with so many questions and I'm stuck with this one could some one show me how the solution would be using stdio.h and please don't make the answer so complicated for a beginner :\

Produce a program which allows the user to up to twenty positive integers. The program should store a maximum of twenty numbers and the entry of a negative number should terminate. Functions should be used to:

a.Prompt the user for numbers, store them in the array and report the size of the array when the numbers have been entered.

b.Calculate the minimum of the entered numbers

c.Calculate the maximum of the entered numbers

d.Calculate the average of the entered numbers

Last edited on
If we write this for you, you won't learn anything which defeats the purpose of the exercise. To do this, you'll need to use scanf() to input everything. You'll need to store the data in an array of 20. You'll also need to use a few loops. Which part are you having trouble with?
I could tell you about std::min_element(), std::max_element(), std::list.
But what would be the point, if yours is a C assignment, and not C++?
will tell me my point is also to expand my knowledge
The C++ standard has some useful built-in things that you can use, without needing to reinvent the wheel.

You need to let go of certain things that C++ inherited from C, such as arrays, and use the new things C++ provides. Why? Because the new things in C++ work better with the rest of the things in C++.

Finally, the C++ references are your friends.
http://cplusplus.com/reference/
http://en.cppreference.com/w/

 ``1234567891011121314151617181920212223`` ``````#include // min_element, max_element #include // cin, cout #include #include // accumulate #include // endl int main() { std::list li; // the list can grow as needed, as opposed to an array int temp; std::cout << "Enter numbers, or \"stop\" to stop." << std::endl; while (std::cin >> temp) // when std::cin fails to extract a number, the while() exits, li.push_back(temp); // otherwise `temp', which is a number, is added to the list // this part may look messy, but notice how little work we actually do // C++ components do most of the work for us std::cout << "\n\nSummary:\n" << li.size() << " numbers where given.\n"; std::cout << "Largest number: " << *std::max_element(li.begin(), li.end()) << '\n'; std::cout << "Smallest number: " << *std::min_element(li.begin(), li.end()) << '\n'; std::cout << "Average: " << std::accumulate(li.begin(), li.end(), 0) / static_cast (li.size()) << std::endl; }``````

closed account (D80DSL3A)
Hi. I think you may be experiencing something which happens when you skip all the "easy" problems in the early chapters.

When I arrive at a problem during self study where I am totally stumped it means that I have missed crucial concepts and must back up for review. Once I am lost I am not going to get any less lost going forward, so trying to do it anyway is pointless.

You may need to back up and then take it slower. Try to work out all end of chapter exercises. It may be "boring" but it is the best way to move your skills forward solidly.
Last edited on
You wont believe this but I spent 9 hours and 38 min trying to solve the question I give up like seriously could some one just write the answer for me and show me the technique :| by brain is hanging upside down 0.o
Here you go, but really... why didn't you post what you had spent 9 hours and 38 minutes on? We could have told you what you were doing wrong. I really have no idea what concept was hurting you.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344`` ``````#include int main() { int set[20], size = 0; int i; // iterator used in for loops int minimum, maximum, sum; double average; printf("Enter up to 20 numbers.\n"); printf(" Terminate with a negative number if you wish to end early: \n"); while (size < 20) { scanf("%d",&set[size]); if (set[size] < 0) break; size++; } printf("%d numbers have been entered\n", size); minimum = set[0]; for (i = 1; i < size; i++) if (set[i] < minimum) minimum = set[i]; printf("The minimum of these numbers is %d\n", minimum); maximum = set[0]; for (i = 1; i < size; i++) if (set[i] > maximum) maximum = set[i]; printf("The maximum of these numbers is %d\n", maximum); sum = 0; for (i = 0; i < size; i++) sum += set[i]; average = double(sum) / double(size); printf("The average of these numbers is %f\n", average); }`````` ```Enter up to 20 numbers. Terminate with a negative number if you wish to end early: 1 2 3 4 5 -1 5 numbers have been entered The minimum of these numbers is 1 The maximum of these numbers is 5 The average of these numbers is 3.000000```
Last edited on
 show me the technique

The recommended technique is to break down a large problem into a series of smaller steps.Don't just look at the whole thing and see a mountain. Break it into stages and tackle them one at a time. Some steps may be less obvious than others, and may take longer. By all means ask for help with the tricky parts.

The main thing is to consider the design first. Don't think in terms of lines of code, or syntax. Think in terms of what actions need to be performed in order to carry out each step. Write those down in words, in ordinary English.

Only after you have at least some sort of design should you begin to write any code.
Last edited on
Thank you for youre time, the hard part was how can I allow the user to up to twenty positive integers I had know I idea how can I make that work i've never done it but Ill start analyzing the answer now so i can understand the whole concept :)
Topic archived. No new replies allowed.