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