Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

OOP BIT F16

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>

using namespace std;

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.

Abdul Mateen PUCIT


OOP BIT F16
8. Print pair of corresponding values in separate lines. Like print first value of x & y in first line, print second value
of x & y in second line and so on.
9. Print values of x which are greater than values of y at corresponding positions
10. Print values of x using values of y as index of x. Say x[y[i]], because y[i] has values 0 to 9, so these values can
be used as index as well.
Extra problem for passionate students only:
Declare array of size 10. Input unique values from user in loop, which is not repeated. This means you have to take
input from user & check with all previous values (except in first input), if value is not repeated, you will enter in array
otherwise again input. This means nested loop (loop inside loop), outer loop for size of array, inner loop k times for
checking repetition. This is somehow tricky so only for passionate students.

Abdul Mateen PUCIT

You might also like