+ 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

5th Nov 2016, 2:11 PM
Laluprasad
Laluprasad - avatar
13 odpowiedzi
+ 2
rand() and srand() both are standard functions present in cstdlib header file.
6th Nov 2016, 11:02 AM
kamal joshi
kamal joshi - avatar
0
will we use 4 loops?
5th Nov 2016, 2:31 PM
arkoth 20
arkoth 20 - avatar
0
instead of taking 16 variables you can use array of size 16 and assign values to it using a for loop.
5th Nov 2016, 2:37 PM
kamal joshi
kamal joshi - avatar
0
well!! using array is ok..
5th Nov 2016, 2:50 PM
Laluprasad
Laluprasad - avatar
0
can anyone give me a prog pls!!
6th Nov 2016, 10:03 AM
Laluprasad
Laluprasad - avatar
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.
6th Nov 2016, 10:17 AM
kamal joshi
kamal joshi - avatar
0
sorry but the values are to be assigned randomly and by the compiler
6th Nov 2016, 10:30 AM
Laluprasad
Laluprasad - avatar
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; }
6th Nov 2016, 10:52 AM
kamal joshi
kamal joshi - avatar
0
what does that cstdlib do there
6th Nov 2016, 11:01 AM
Laluprasad
Laluprasad - avatar
0
I dont think this will print each value exactly 4 times
6th Nov 2016, 11:02 AM
Laluprasad
Laluprasad - avatar
0
aren't they included in iostream
6th Nov 2016, 11:05 AM
Laluprasad
Laluprasad - avatar
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; }
6th Nov 2016, 11:13 AM
kamal joshi
kamal joshi - avatar
0
no they are not included in iostream.
6th Nov 2016, 11:14 AM
kamal joshi
kamal joshi - avatar