Q1) Write ONE program that initializes an array of integers of size 10. The inte
ID: 655695 • Letter: Q
Question
Q1) Write ONE program that initializes an array of integers of size 10. The integers are entered by the user and does the below: a) Print the elements. b) Find and print the sum of the integers in the array. c) Find and print the largest and the smallest of the integers. d) Change the value of the fourth element in the array to the product of the fifth and the seventh elements and print the result. Q2) Write a program to intialize an array of size 8 with floating point numbers entered by the user. e) Print the array. f) Find the average of the elements and print it. Find and print all the elements that has a value higher than the averageExplanation / Answer
Q1)
#include <cstdlib>
#include<iostream>
using namespace std;
int main()
{
int i,a[10],sum=0,small,large,fourth;
cout<<"Enter the intergers"<<endl;
for(i=0;i<10;i++)
cin>>a[i];
//a
cout<<"Printing the elements"<<endl;
for(i=0;i<10;i++)
cout<<a[i]<<endl;
for(i=0;i<10;i++)
sum=sum+a[i];
//b
cout<<"Sum of the integers: ";
cout<<sum<<endl;
//c
small=a[0];
for(i=1;i<10;i++)
{
if(a[i]<small)
small=a[i];
else
continue;
}
cout<<"Smallest integer in the given array: "<<small<<endl;
large=a[0];
for(i=1;i<10;i++)
{
if(a[i]>large)
large=a[i];
else
continue;
}
cout<<"largest integer in the given array: "<<large<<endl;
//d
cout<<"Value of fourth element before modification: "<<a[3]<<endl;
a[3]=(a[4]*a[6]);
cout<<"Value of fourth element after modification: "<<a[3]<<endl;
return 0;
}
Q2)
#include <cstdlib>
#include<iostream>
using namespace std;
int main()
{
int i;
float a[8],avg,sum=0;
cout<<"Enter floating numbers"<<endl;
for(i=0;i<8;i++)
cin>>a[i];
//e
cout<<endl;
cout<<"Printing the number"<<endl;
for(i=0;i<8;i++)
cout<<a[i]<<endl;
//f
cout<<endl;
for(i=0;i<8;i++)
sum=sum+a[i];
avg=(sum/8);
cout<<"Average: "<<avg<<endl<<endl;
cout<<"Printing the numbers greater than the average...."<<avg<<endl;
for(i=0;i<8;i++)
{
if(a[i]>avg)
cout<<a[i]<<endl;
else
continue;
}
cout<<"Exiting...."<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.