Vectors. I do not understand why my program is not right. help me out please:)

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  #include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector <int> nata (10);
    vector <int> natadadebiti(10);
    int max=0,temp=0;
    
    
    for (int i=0;i<10;i++){
        
        cin>>nata[i];
    }
    
    double min=nata[0];
    
    
    for (int i=0;i<10;i++){
        
        if(nata[i]>0){
            if(nata[i]>max){
                
                
                max=nata[i];
                
                
            }
            
            else if (nata[i]<min){
                
                
                min=nata[i];
                
                
            }
            
            
            natadadebiti[i]=nata[i];
            
            
            cout<<natadadebiti[i]<<endl;
        }
        
        
    }
    
    cout<<endl<<endl;
    cout<<max<<" "<<min<<endl;
    
    
}
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.
Can you show the code for the program that doesn't compile? And copy and paste the exact errors?
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).


0 : 24                  INPUT
1 : 1                    
2 : 0                    
3 : 34                  
4 : 3                    
5 : 4                    
6 : 5                    
7 : 2                    
8 : 6                    
9 : 93                  
0 : 24                  OUTPUT
1 : 1
3 : 34
4 : 3
5 : 4
6 : 5
7 : 2
8 : 6
9 : 93

93 1                    MAX and MIN



After a few minutes I did this for you to pick apart (with comments):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#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.
Last edited on
Why iterate through the data after you have sorted it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

using namespace std;
int main()
{
    vector<int> data(10);
    copy(istream_iterator<int>(cin), istream_iterator<int>(), data.begin());
    sort(data.begin(), data.end());
    
    for(const auto i : data)
    {
    	cout << i << ' ';
    }
    cout << "\nmin= " << data.front() <<" max= " << data.back() << endl;
}
naraku9333 wrote:
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.
Fair enough. I suppose I could have also commented a bit.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

using namespace std;

int main()
{
    vector<int> data;
    
    // copy data from input stream into the vector using back_inserter to push into vector
    // http://www.cplusplus.com/reference/iterator/istream_iterator/
    // http://www.cplusplus.com/reference/iterator/back_inserter/
    copy(istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(data));
    
    // sort data in ascending order (defautl)
    // http://www.cplusplus.com/reference/algorithm/sort/
    sort(data.begin(), data.end());
    
    // copy data from vector to the output stream (cout) with space delimiter
    // http://www.cplusplus.com/reference/iterator/ostream_iterator/
    copy(data.begin(), data.end(), ostream_iterator<int>(cout," "));
    
    // data is sorted, so front is smallest and back is largest
    // http://www.cplusplus.com/reference/vector/vector/front/
    // http://www.cplusplus.com/reference/vector/vector/back/
    cout << "\nmin= " << data.front() <<" max= " << data.back() << endl;
}
Thank you very much!!!
Topic archived. No new replies allowed.