+ 1
Write a prog in c/c++
to assign 4 different values, each 4 times repeated, to 16 different variables, randomly... ex: a=3. e=2 b=4. f=1 c=2. g=3 d=2. h=1 A=1. E=4 B=4. F=4 C=3. G=1 d=2. H=3 where 1,2,3 and 4... each repeated exactly 4 times
13 Answers
+ 2
rand() and srand() both are standard functions present in cstdlib header file.
0
will we use 4 loops?
0
instead of taking 16 variables you can use array of size 16 and assign values to it using a for loop.
0
well!! using array is ok..
0
can anyone give me a prog pls!!
0
#include <iostream>
using namespace std;
int main() {
int a[16];
for(int i=0;i<16;i++){
cin>>a[i];
}
for(int i=0;i<16;i++){
cout<<a[i]<<" ";
}
return 0;
}
Here, you can assign whichever value like.
consider a[0] as first variable,a[1] as second and so on.
0
sorry but the values are to be assigned randomly and by the compiler
0
Sorry I didn't see "randomly" word in your question.đ
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(0));
int a[16];
for(int i=0;i<16;i++){
a[i] = rand()%4 + 1;
}
for(int i=0;i<16;i++){
cout<<"a["<<i<<"] = "<<a[i]<<endl;
}
return 0;
}
0
what does that cstdlib do there
0
I dont think this will print each value exactly 4 times
0
aren't they included in iostream
0
then you can do it like this:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(0));
int a[4],b[4],c[4],d[4];
for(int i=0;i<4;i++){
a[i] = rand()%4 + 1;
b[i] = rand()%4 + 1;
c[i] = rand()%4 + 1;
d[i] = rand()%4 + 1;
}
for(int i=0;i<4;i++){
cout<<"a["<<i<<"] = "<<a[i]<<endl;
cout<<"b["<<i<<"] = "<<b[i]<<endl;
cout<<"c["<<i<<"] = "<<c[i]<<endl;
cout<<"d["<<i<<"] = "<<d[i]<<endl;
}
return 0;
}
0
no they are not included in iostream.