In: Computer Science
Inside “Lab1”folder, create a project named “Lab1Ex1”. Use this
project to write and run a C++ program that produces: Define a
constant value called MAX_SIZE with value of 10. Define an array
of integers called Class_Marks with MAX_SIZE which contains
students Mark. Define a function called “Fill_Array” that takes an
array, and array size as parameters. The function fills the odd
index of the array randomly in the range of [50- 100] and fills the
even index of the array using input from user. The valid input
values are between 50-100.
Hint: To get random number you can use rand() function: min +
rand()%(max-min+1) Fill the array by calling Fill_Array
function
#include<iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
using namespace std;
void Fill_Array(int marks[],int size);
int main (){
/* initialize random seed: */
srand (time(NULL));
const int MAX_SIZE = 10;
int Class_Marks[MAX_SIZE];
Fill_Array(Class_Marks,MAX_SIZE);
cout<<"\nMarks in the array:\n";
for(int i=0;i<MAX_SIZE;i++){
cout<<Class_Marks[i]<<endl;
}
return 0;
}
void Fill_Array(int marks[],int size){
for(int i=0;i<size;i++){
if(i%2==0){
cout<<"Enter marks at index "<<i<<": ";
cin>>marks[i];
while(marks[i]<50 || marks[i]>100){
cout<<"Marks should be in the range
[50,100]!!\n";
cout<<"Enter marks at index "<<i<<": ";
cin>>marks[i];
}
} else {
marks[i] = 50 +
rand()%(100-50+1);
}
}
}