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.
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?
#include <algorithm> // min_element, max_element
#include <iostream> // cin, cout
#include <numeric> // accumulate
#include <ostream> // endl
std::list<int> li; // the list can grow as needed, as opposed to an array
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<float> (li.size()) << std::endl;
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.
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
int set, size = 0;
int i; // iterator used in for loops
int minimum, maximum, sum;
printf("Enter up to 20 numbers.\n");
printf(" Terminate with a negative number if you wish to end early: \n");
while (size < 20)
if (set[size] < 0)
printf("%d numbers have been entered\n", size);
minimum = set;
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;
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
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.
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 :)