I should input cin n integer, which will be stored in a vector. From positive numbers I should cout max and min numbers. and also I should copy positive numbers in a new vector and cout them in an ascending order. I have not started yet to put them in an ascending order, because I cannot put the positive numbers in a new vector and I do not understand why. Here is the code. Would you please be so kind to look at it and tell me what's wrong.
We don't understand why your program is not right either. Does it fail to compile? Does it not generate the correct output? Does it attract cats to your window? Does it cause the next Hurricane Sandy in your home town? You ned to be specific.
hahaha:)))) No, like it does not output what I want it to. I mean max and min is working, but when I try to cout natadadebiti, it says error and i cannot run program.
What it does is clear. User enters ten random numbers and it then prints out the ten numbers and tells the min and max. Problem is that it doesn't work because if you put 0 it doesn't print it but instead the next lower one. So nothing is really working like it is supposed to (at least on my computer).
#include <iostream> // for input and output
#include <vector> // for making our vector
#include <algorithm> // for using sort()
int main(int argc, char *argv[])
{
// create our vector for the numbers
std::vector<int> numberList(10);
// variables to hold the max and min numbers of the list
int min = 0, max = 0;
// loop to get the random numbers to put in our list
for(int i = 0; i < 10; i++)
{
std::cout << "Enter a random number: ";
std::cin >> numberList[i];
}
// set min and max to the first element in the list
min = max = numberList[0];
std::cout << "Now I will make them print out in ascending order and the min and max\n\n";
// sort the list in ascending order
sort(numberList.begin(), numberList.end());
// loop through the list one more time
for (int i = 0; i < 10; i++)
{
// compare min to the element
// if min is greater than the element
// set min to element
if(min > numberList[i])
{
min = numberList[i];
}
// compare max to the element
// if max is lower than the element
// set max to element
if(max < numberList[i])
{
max = numberList[i];
}
// print out the list in ascending order
std::cout << numberList[i] << ' ';
}
std::cout << std::endl;
// print the min and max numbers
std::cout << "Min in list is: " << min << std::endl;
std::cout << "Max in list is: " << max << std::endl;
return 0;
}
Output
Enter a random number: 2
Enter a random number: 0
Enter a random number: 3432
Enter a random number: 34320
Enter a random number: 4
Enter a random number: 9
Enter a random number: 90
Enter a random number: 100
Enter a random number: 405
Enter a random number: 500
Now I will make them print out in ascending order and the min and max
0 2 4 9 90 100 405 500 3432 34320
Min in list is: 0
Max in list is: 34320
This may not bet the most effective way to do it, and others will say if it isn't, but if you have any questions just ask and one of us will try our best to answer them.
Why iterate through the data after you have sorted it?
I always run on the assumption that if a person is posting a question in beginner forum they may not fully understand the thing they are doing that made the question spring up. Running on this assumption makes me write code so I don't throw too much at them at once. With my code I only add begin(), end(), and sort() so he only has three things to look up or ask questions about if he doesn't know it. Your code throws a lot at him with copy(), istream_iterator, sort(), begin(), end(), front(), back(), and if he isn't using C++11 look up what auto is. That could be a lot for someone to take in if they are battling a fairly simple program.