Professional Documents
Culture Documents
Assignment 01 (Due in Next Lab, Viva Will Be Conduct Therefore Do Yourself.)
Assignment 01 (Due in Next Lab, Viva Will Be Conduct Therefore Do Yourself.)
Assignment 01
(Due in next lab, Viva will be conduct therefore do yourself.)
Run this program 4-5 times and note different output.
#include <iostream>
#include <cstdlib>
#include <ctime>
int main(){
int i, x[20], sum=0;
srand(time(0));
for (i=0;i<20;i++){
x[i]=rand()%9+1;
sum=sum+x[i];
cout<<x[i]<<' ';
}
float avg=sum/20.0;
cout<<"\nAverage:"<<avg<<'\n';
for (i=0;i<20;i++)
if (x[i]>avg)
cout<<x[i]<<' ';
cout<<'\n';
return 0;
}
Extend following C++ code in each program. By extend means add code inside main function without
modifying existing code:
int main(){
int i, x[20];
srand(time(0));
for (i=0;i<20;i++)
x[i]=rand()%9+1;
return 0;
}
1. Print pair of each consecutive values. Print first & second value in first line. Print second & third value in
second line. So on print nineteenth & twentieth value in 19th line.
2. Print pair of each consecutive values where first value is smaller than second value.
3. Print all values except first & last in single line.
4. Print all values (except first & last) which are smaller than both left & right neighbors.
5. Print all consecutive pairs have sum equal to 10.
For next programs use following code:
int main(){
int i, x[20], y[20];
srand(time(0));
for (i=0;i<20;i++){
x[i]=rand()%10;
y[i]=rand()%10;
}
return 0;
}
6. Print both x & y values in single but separate lines.
7. Extend task 6. Run loop again and print positions where corresponding values are same. Means x & y have
same value at same position, like if x[3] & y[3] has 23, print 3.