In: Computer Science
2. Write a program in C++ that:
a) Declares a 1D array A with 30 elements
b) Inputs an integer n from 1-30 from the keyboard. If n < 1 set n = 1. If n > 30 set n = 30.
the program should keep asking the user the input n one by one, followed by printing of the value of n (n=n if bigger than 1 and smaller than 30, 1 if smaller than 1 and 30 if bigger than 30)
#include <iostream>
using namespace std;
int main()
{
int i=0, array[30];
while(i<30){
int temp=0;
cin>>temp;
if(temp>=1 && temp <=30 ){
array[i]=temp;
}
else if(temp<1){
array[i]=1;
}
else{ array[i]=30;
}
i++;
}
for(int i =0;i<30;i++){
cout<<"array["<<i<<"] =
"<<array[i]<<endl;
}
}
Code with output -