Vectors

Hey pals, i've finished learning the loops( for, while, do while), switch, if, if-else-if , &&, || , increment and decrement operators and some other the very basic operators. So, please be as simple as possible when explaining or comment, as its just the beginning for me.

Help with the following code please, it has to fulfill the following conditions:

create a vector which contains "n" nr. of elements, between the x...y interval.
p.s. need your attention starting with condition nr. 5:

1. identify how many elements will contain "10..20" interval.

2. identify the max. and min. value of the vector.

3. identify the max. value on odd coordinate and min value on even coordinate.

4. max. and min. value should change places.

5. calculate the sum of the first half of the vector and calculate the sum2 for the next half of the vector.

6. first and last element should change their places, also second element and last but one should change their places too.

7. first odd element and first even element of the vector should change their places.

8. first half of the vector should change its place with the second half of the vector.

9. how many times value "p" which is defined by the user is repeated in the vector.

10. all the odd elements will be copied in another vector called "a vector" and all the even elements will be copied in the "b vector".


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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
  #include <iostream>
  #include <cstdlib> 
  #include <ctime>
  #include <conio.h>
  #include <iomanip>
  
  using namespace std;
 
  int main()
  {
        srand(time(0));

 int v[1000];
    
    int n;
    cout<<"Enter n=";
    cin>>n;
    
    int x,y;
    
    cout<<"Enter x..y"<<endl;
    cin>>x>>y;
    
for(int i=0; i<n; i++)
    {
       
        v[i]=rand()%(y-x)+x;
                
    }
    
for(int i=0; i<n; i++)
    {
       
        cout<<setw(3)<<v[i];
                
    }
    
    int c=0, max=v[10], min=v[0], imax=10, imin=0;
    
    int maxp=v[0], mini=v[1];
        
for(int i=0; i<n; i++)
    {
//1. 10 = v[] <20 contor
         
        if( v[i]>=10 && v[i]<20) c++;
      
//2. max min  
       
       if( max < v[i]) { max=v[i]; imax=i;}
       if( min > v[i]) { min=v[i]; imin=i;}
       
//3. max -> i-odd, min -> i - even
       
       if(i%2==0 && maxp < v[i]) { maxp=v[i]; }
       if(i%2!=0 && mini > v[i]) { mini=v[i]; }
    }    
      
//4. max <=> min,  v[max] <=> v[min]
    
     int temp  = v[imax];
         v[imax]  = v[imin];
         v[imin]  = temp;
    
for(int i=0; i<n; i++)
    {
      cout<<setw(3)<<v[i];
    }
    
//5. sum of the first half of the vector and calculate the sum2 for the next half of the vector..

????
	      
    cout<<endl;
	   
//6. first and last element should change their places, also second element and last but one shuold change their places too..
	
for(int i=0; i<n; i++)
    {
		
	int h = v[0];
	v[0] = v[i];
	v[i] = h;
	
	int h1 = v[1];
	v[1] = v[i-1];
	v[i-1] = h1;
	}   
	 cout<<endl;
	   
for(int i=0; i<n; i++)
    {
       
     cout<<setw(3)<<v[i];
                
    }
    
//7. first odd element and first even element of the vector should change their places.

for(int i=0, a, b, t; i<n; i++)
    {
       
        if (v[i]%2==0 || v[i+1]%2!=0) {
        	a=v[i];
			}
        	
        else if (v[i]%2==0 || v[i+1]%2!=0){
        	b=v[i];
			}
		
                   t = a;
		   a = b;
		   b = t;
		        
    }
    cout<<endl;
    
for(int i=0; i<n; i++)
    {
       
     cout<<setw(3)<<v[i];
                
    }
    
//8. both halves of the vector should change their positions.

for(int i=0; i<n; i++)
    {
	    v[i]/2 = k;
    	    k1 = v[i] ;
    	
    	l = k1;
    	k1 = k;
    	l = k;
	}
    cout<<endl;
    
for(int i=0; i<n; i++)
    {
       
     cout<<setw(3)<<v[i];
                
    }
	
//9. how many times value "p" which is defined by the user is repeated in the vector.; 

for(int i=0,p=0; i < n; i++) {
    if(v[i] == v[0]) p++;

}
cout<<endl;	

//10. all the odd elements will be copied in another vector called "a vector" and all the even elements will be copied in the "b vector".

for(int i=0; i<n; i++)
    { 
	if ( v[i]%2==0) v[i]=v[a];
        if ( v[i]%2!=0) v[i]=v[b];
    }
    
    cout<<"\n 1. c="    <<c<<endl;
    cout<<"\n 2. max="    <<max<<" imax="<<imax<<endl;
    cout<<"\n 2. min="    <<min<<" imin="<<imin<<endl;
    cout<<"\n 4. max odd="    <<maxp<<endl;
    cout<<"\n 4. min even="    <<mini<<endl;    
    cout << "First element repeats " << p << " more times." << endl;   //9 
    cout << "v[a]= " << v[a] << "\n v[b]= " << v[b]<<endl;   //10
    
      return 0;
  }


Many thanks
There don't seem to be any vectors in this code.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <vector>
#include <iostream>

using namespace std;

int main()
{
    int n;
    cout<<"Enter n=";
    cin>>n;

   vector<int> the_vector(n); // A vector of integers, size n
}



your code, properly indented (my comments between /** **/)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <conio.h>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>

using namespace std;

int main() {
  srand(time(0));

  int v[1000];

  int n;
  cout << "Enter n=";
  cin >> n;

  int x, y;
  cout << "Enter x..y" << endl;
  cin >> x >> y;

  for (int i = 0; i < n; i++) {
    v[i] = rand() % (y - x) + x;
  }

  for (int i = 0; i < n; i++) {
    cout << setw(3) << v[i];
  }

  int c = 0, max = v[10], min = v[0], imax = 10, imin = 0;
  /** max = v[10] will work only if you do have at least 11 elements
   * ¿why didn't use v[0]?
   * another option is going outside your range, in this case it's limited to [x,y]
   **/

  int maxp = v[0], mini = v[1];

  for (int i = 0; i < n; i++) {
    // 1. 10 = v[] <20 contor
    if (v[i] >= 10 && v[i] < 20) /** ¿should you include 20? [) or [] range  **/
      c++; /** use a meaningful variable name **/

    // 2. max min
    if (max < v[i]) {
      max = v[i];
      imax = i;
    }
    if (min > v[i]) {
      min = v[i];
      imin = i;
    }

    // 3. max -> i-odd, min -> i - even
    if (i % 2 == 0 && maxp < v[i]) {
      maxp = v[i];
    }
    if (i % 2 != 0 && mini > v[i]) {
      mini = v[i];
    }
  }

  // 4. max <=> min,  v[max] <=> v[min]
  int temp = v[imax];
  v[imax] = v[imin];
  v[imin] = temp;

  for (int i = 0; i < n; i++) {
    cout << setw(3) << v[i];
  }

  // 5. sum of the first half of the vector and calculate the sum2 for the next
  // half of the vector..

  /** the first half is the range [0; n/2), the second half [n/2; n)
   * for (int K=0; K<n/2; ++K)
   *    sum += v[K]; **/
  ? ? ? ?
  cout << endl;

  // 6. first and last element should change their places, also second element
  // and last but one shuold change their places too..

  /** ¿why do you need a loop? you were asked to do two swaps
   * look at your answer for (4) **/
  for (int i = 0; i < n; i++) {
    int h = v[0];
    v[0] = v[i];
    v[i] = h;

    int h1 = v[1];
    v[1] = v[i - 1];
    v[i - 1] = h1;
  }
  cout << endl;

  for (int i = 0; i < n; i++) {
    cout << setw(3) << v[i];
  }

  // 7. first odd element and first even element of the vector should change
  // their places.

  /** don't try to do everything at once
   * first identify the elements, then swap them
   * int first_even_index = -1, first_odd_index = -1;
   * for (int K=0; K<n; ++K)
   *    if (v[K] % 2 == 0){
   *       first_even_index = K;
   *       break;
   *    }
   * same for odd
   * then swap v[first_even_index] an v[first_odd_index] (¿what if the index remains -1?)
   **/
  for (int i = 0, a, b, t; i < n; i++) {
    if (v[i] % 2 == 0 || v[i + 1] % 2 != 0) {
      a = v[i];
    }
    else if (v[i] % 2 == 0 || v[i + 1] % 2 != 0) {
      b = v[i];
    }

    t = a;
    a = b;
    b = t;
  }
  cout << endl;

  for (int i = 0; i < n; i++) {

    cout << setw(3) << v[i];
  }

  // 8. both halves of the vector should change their positions.
  /** say you have 6 elements, the idea is to do these 3 swaps
   * swap(v[0], v[3])
   * swap(v[1], v[4])
   * swap(v[2], v[5])
   **/
  for (int i = 0; i < n; i++) { /** you'll do `n' swaps instead of just `n/2' **/
    v[i] / 2 = k; /** this doesn't compile **/
    k1 = v[i];

    l = k1; /** lost me on too many temporaries, may declare at point of use **/
    k1 = k;
    l = k;
  }
  cout << endl;

  for (int i = 0; i < n; i++) {
    cout << setw(3) << v[i];
  }

  // 9. how many times value "p" which is defined by the user is repeated in the
  // vector.;
  /** ¿where did the user defined p?
   * you are just comparing against the first element
   * the idea is quite there, but not what you were asked to do
   **/
  for (int i = 0, p = 0; i < n; i++) {
    if (v[i] == v[0])
      p++;
  }
  cout << endl;

  // 10. all the odd elements will be copied in another vector called "a vector"
  // and all the even elements will be copied in the "b vector".
  /** _another_ vector
   * you need int a[1000], b[1000]
   * if (v[i] % 2 == 0){
   *    a[k] = v[i]
   *    ++k
   * }
   **/
  for (int i = 0; i < n; i++) {
    if (v[i] % 2 == 0)
      v[i] = v[a];
    if (v[i] % 2 != 0)
      v[i] = v[b];
  }

  cout << "\n 1. c=" << c << endl;
  cout << "\n 2. max=" << max << " imax=" << imax << endl;
  cout << "\n 2. min=" << min << " imin=" << imin << endl;
  cout << "\n 4. max odd=" << maxp << endl;
  cout << "\n 4. min even=" << mini << endl;
  cout << "First element repeats " << p << " more times." << endl; // 9
  cout << "v[a]= " << v[a] << "\n v[b]= " << v[b] << endl;         // 10

  return 0;
}
Last edited on
8) what if the # of items is odd? are any of the other questions affected by this? Be sure if you use the swap approach that for an odd one it either swaps the middle with itself or skips doing anything at all on that element.

there is a lot of sense in doing one thing at a time and keeping the code readable and pretty. However there are times when you need to not waste all day doing stuff. There are multiple places where you can do more than one of your tasks in the same loop. I am not saying to change the code to do it now; but look at the problems and think to yourself "if I had a trillion items to go through, where can I combine tasks". Spot them, think about it, and always look at that in your requirements ... after a while its second nature to spot unnecessary / excess iteration and cut it.

--------------
an example:
finding the sum of all items and finding the max, min, first odd, first even etc can all be done in one pass through the data.
thank you guys, your advice really help me!

@jonnin, maybe its harder for me 'cause I am just at the beginning and the conditions were given one by one, not all together so I was doing them one by one.

thanks again, waiting for more comments
Hi Max!

Did I saw that your professor sucks? If not, I should have. These assignments are convoluted and mostly just waste your time without teaching you much.

I looked over the code and comments and I don't think you should wait for more comments on the forum. Make the changes that have been suggested and post your results. The biggest question is whether you're supposed to use vector<int> for this assignment. In C++, the word "vector" has a very specific meaning.

Have you learned about functions? If so, then you might create these:
bool isEven(int num) { return num % 2 == 0; }
bool isOdd(int num) { return !isEven(num); }

Then use isEven() and isOdd() where appropriate. This will make the code clearer.

The standard library has a swap() function. Are you allowed to use it?
1
2
3
#include <algorithm>
...
swap(a,b);  // swaps the values of a and b 


I would print the vector after each of steps 7-9. Also print the "a" and "b" vectors after step 10.
@jonnin: ¿are iterations so wasteful?
kiss
if you have a lot of data relative to the cpu's speed, or a hard time constraint, yes.
there is nothing wrong with one by one and some waste for homework, but the sooner you learn to spot waste, the better you will be.
Here's a start - so tedious - maybe finish later:
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <vector>

#include <cstdlib>
#include <iomanip>

int main()
{
    // CREATE A VECTOR
    std::vector<int> v, a, b;
    
    // GET n
    int n{0};
    std::cout << "Enter n: ";
    std::cin >> n;
    
    // GET RANGE x ... y
    int x{5}, y{30};
    std::cout << "Enter range x ... y: ";
    std::cin >> x >> y;
    
    int p{0};
    std::cout << "Enter search p: ";
    std::cin >> p;
    
    
    // POPULATE VECTOR & PROCESS
    int random_number{0};
    srand(time(0));
    
    int count_10_20{0};
    int max{x}, i_max{0}, min{y}, i_min{0};
    
    int max_odd{x - 1}, i_max_odd{0};
    int min_even{y + 1}, i_min_even{0};
    
    int temp{0};
    
    int halfway = n/2;
    int sum_first_half{0}, sum_second_half{0};
    
    int count_p{0};
    
    for(int i = 0; i < n; i++)
    {
        random_number = rand() % (y - x + 1) + x;
        v.push_back(random_number);
        if( i % 10 == 0)
            std::cout << '\n';
        std::cout << std::setw(5) << v[i];
        
        // 1. NO. ELEMENTS BETWEEN 10 AND 20
        if(v[i] >= 10 and v[i] <= 20)
            count_10_20++;
        
        // 2. MAX & MIN
        if(v[i] >= max)
        {
            max =  v[i];
            i_max = i;
        }
        
        if(v[i] <= min)
        {
            min =  v[i];
            i_min = i;
        }
        
        // 3. ODD MAX AND EVEN MIN
        if(i % 2 == 1)
        {
            if(v[i] >= max_odd)
            {
                max_odd =  v[i];
                i_max_odd = i;
            }
        }
        
        if(i % 2 == 0)
        {
            if(v[i] <= min_even)
            {
                min_even =  v[i];
                i_min_even = i;
            }
        }
        
        // 5. SUM OF FIRST AND SECOND HALVES
        if(i <= halfway)
            sum_first_half += v[i];
        else
            sum_second_half += v[i];
        
        // 9. COUNT p
        if(v[i] == p)
            count_p++;
        
        // 10. POPULATE VECTORS a & b
        if( v[i] % 2 == 1)
            a.push_back(v[i]);
        else
            b.push_back(v[i]);
            
    }
    
    // 4. MAX AND MIN SWAP PLACES
    temp = v[i_max];
    v[i_max] = v[i_min];
    v[i_min] = temp;
    
    // 6. FIRST & LAST CHANGE PLACES
    temp = v[n - 1];
    v[n - 1] = v[0];
    v[0] = temp;
    
    temp = v[n - 2];
    v[n - 2] = v[1];
    v[1] = temp;
    
    // 7. SWAP FIRST ODD & FIRST EVEN
    temp = v[0]; // OR 2??
    v[0] = v[1];
    v[1] = temp;
    
    // 8. SWAP FIRST HALF WITH SECOND HALF
    for(int i = 0; i < halfway; i++)
    {
        temp = v[i];
        v[i] = v[n - i];
        v[n - i] = temp;
    }
    
    // OUTPUT
    std::cout << '\n';
    std::cout << "    Count 10-20: " << count_10_20 << '\n';
    std::cout << "            Max: " << max << "\ti: " << i_max << '\n';
    std::cout << "            Min: " << min << "\ti: " << i_min << '\n';
    
    std::cout << "        Max_odd: " << max_odd << "\t  i_odd: " << i_max_odd << '\n';
    std::cout << "       Min_even: " << min_even << "\t i_even: " << i_min_even << '\n';
    
    std::cout << " Sum first half: " << sum_first_half << '\n';
    std::cout << "Sum second half: " << sum_second_half << '\n';
    
    std::cout << "        Count p: " << count_p << '\n';
    
    std::cout << '\n';
    
    
    return 0;
}


Enter n: 500
Enter range x ... y: 5 78
Enter search p: 56

   47   72   62   56   50   60   19   40   47   66
   53   12   66   58   75   53   46   36   54   48
   56   30   13   64   50   40   16   31    6   27
   20    6   12   29   45   53   59   62   40   22
   46   32   14   75   77   22   71   62   48   27
   44   54   62   45   54   14   58   49   60   15
   34   52   46   40   43   49   55   49   30   42
   32   55   59   77   38   76   23   47   31   72
   36   39   13   61   64   77   48   10   29   35
   76   52   26   20   66   25   35   43   58   10
   21   68   58   58   67   66   19   77   61   41
   66   13   33   29   22   45   40   34   56   28
   78   16   21    8    8   23   48   70   60   36
   57   72   36   22   30   59   51   10   28   39
   18   10   78   14   39   27   42   30   71   41
   63   71   30   11    8   51   17   33   70   31
   76   38   20   28   75   78   40   45   66   74
   78   52    6   68   13    9   23   61   32   66
   51   18   31   78   36   74   24   17   75   64
   57   41   27    7   20   10   31   34   60   38
   29   19   18   12    8   54   40   37   57    5
   11   78   41   33   47   18   61   59   31   40
   76   25    9   33   76   72   66   76   38    9
   52   47   31   75   62   18   51    6   78    9
   28   41   62   22   32   41   21   32   27   70
   28   32   22   28   56   69   14   27   24   75
   62   39   48   78   73   51   77    8   36   36
   35   77   24   68   29   18   72   43   76   41
   37    5    5   20   44   18   49   67   21    5
   68   23   32   38   44   50    9   51    9   35
   72   58   65   61   76   33   45   22   37   54
   72   36   35   70   10   26   68   39   28   26
   49   73   43   53   49   39   76   29   66   23
   65   28   51   36   11   69   74   71   39   54
   33    6    6   32   21   62   51    6   58   31
   68   44   31   56   56   27   54   29   45   65
   55   14   43   28    9   13   75   13    8   32
   15   29   28   55   57   32   33   11   21   76
   32   38   43   46   26   52   29   78   71   25
   13    6    8   68   72   14   60   45   22   39
   62   63   28   66   50   22   28   49   51   29
   52   43   52   29   76   60   26   65   34   69
   45   46   57   53   36   41   53   13   55   25
   66   59   70   60   24   45   53    8   78   26
   22   66   77   58   63   69   52   57   55   74
   68   75   57   69   75   31   30   55   63   31
   14   13   40   51   70   64   12   14    6   28
   72   29   45    9   28   37   31   72   57   63
   16   19   78   75    5   10   72   61   68   32
   62    9   44   66   31   27   62   27   78   69
    Count 10-20: 55
            Max: 78	i: 498
            Min: 5	i: 484
        Max_odd: 78	  i_odd: 387
       Min_even: 5	 i_even: 484
 Sum first half: 10584
Sum second half: 10794
        Count p: 6

Program ended with exit code: 0
Last edited on
@dhayden -
Have you learned about functions?
- not yet, unfortunately, it is only the second month of learning for me, hope to learn them asap.

@jonin -
but the sooner you learn to spot waste, the better you will be.
- its gonna happen not sooner than this year for me, i guess...

@againtry - thank you mate for all the patience and help, great job.

cheers to everyone !
I used to merge againtry's and ne555's code and it seems legit.
So much help from all of you guys, a really good bunch of teachers in front of me, great forum, nice people.
Need to improve as much as possible and as fast as it goes
Topic archived. No new replies allowed.