Write a C++ program to : 1. Create an integer array of size 20 Assign int values
ID: 3680068 • Letter: W
Question
Write a C++ program to :
1. Create an integer array of size 20
Assign int values using loops such that the first element is 3 and every element after that is the determined by the following formula:
a[i] = a[i-1] + (3*i) if a[i-1] is an even number
a[i] = a[i-1] - (2*i) if a[i-1] is an odd number
Print array elements with each element separated by a space
Compute and print the following (with formats given in double quotes):
a. average of all array elements - "Average : "(value)
b. maximum element - "Maximum value : "(value)
c. minimum element - "Minimum value : "(value)
d. number of elements divisible by 5 - "Count of numbers divisible by 5 : "(value)
Ensure every statement is printed on a new line.
#include <iostream>
#include <string>
using namespace std;
int main() {
//declare array
//assign values to array
//print array elements
//compute and print average
//compute and print maximum value
//compute and print minimum value
//compute and print count of array elements divisible by 5
return 0;
}
Explanation / Answer
#include<iostream.h>
#include<conio.h>
void main()
{
int a[20],i,sum=0,min=0,max=0;
double av;
a[0]=3;
for(i=1;i<20;i++)
{
if(a[i-1]%2==0)
a[i]=a[i-1] + (3*i);
else
a[i] = a[i-1] - (2*i);
}
cout<<"Elements of array are:"<<endl;
for(i=0;i<20;i++)
{
cout<<a[i]<<endl;
sum=sum+a[i];
}
av=sum/20;
cout<<"Average :"<<av<<endl;
for(i=0;i<20;i++)
{
if(a[i]>max)
max=a[i];
}
cout<<"Maximum value :"<<max<<endl;
for(i=0;i<20;i++)
{
if(a[i]<min)
min=a[i];
}
cout<<"Minimum value :"<<min<<endl;
cout<<"Count of numbers divisible by 5 : ";
for(i=0;i<20;i++)
{
if(a[i]%5==0)
cout<<a[i]<<" ,";
}
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.