Write a C++ program that reads 6 real numbers from the user (negative and positive numbers) into an array, then it does the following:  Display all the values of array in reverse order.  Find the sum and average of positive numbers of the array and display them.  Count negative numbers of the array and display it.  Find the minimum value of the array and display it.  Find the index of the minimum value of the array and display it.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter8: Arrays And Strings
Section: Chapter Questions
Problem 11PE
icon
Related questions
Question

Write a C++ program that reads 6 real numbers from the user (negative and positive
numbers) into an array, then it does the following:
 Display all the values of array in reverse order.
 Find the sum and average of positive numbers of the array and display them.
 Count negative numbers of the array and display it.
 Find the minimum value of the array and display it.
 Find the index of the minimum value of the array and display it.

Expert Solution
Step 1: code
#include<iostream>
#include<iomanip>
using namespace std;

void reverse(int arr[], int size)
{
cout<< "\nArray in Reverse: ";
for(int j =0;j<6;j++)
{
cout<< arr[j]<<" ";
}
cout << endl;
}

void sumavgneg(int arr[], int size)
{
int poscount = 0;
int negcount = 0;
int sum = 0;

cout<< "\nAll positive numbers in array are: ";
for(int j =0;j<6;j++)
{
if(arr[j]>0)
{
sum += arr[j];
cout<< arr[j]<<" ";
poscount += 1;
}
}
cout<< "\nsum and average of positive numbers of the array : ";
cout << sum<<" and " << (sum/poscount)<<endl;
cout<< "\nAll Negative numbers in array are: ";
for(int j =0;j<6;j++)
{
if(arr[j]<0)
{
cout<< arr[j]<<" ";
negcount += 1;
}
}
cout << "\nTotal negative numbers are: "<<negcount<<endl;
}


int indexofSmallestElement(int array[], int size)
{
int index = 0;

for(int i = 1; i < size; i++)
{
if(array[i] < array[index])
index = i;
}

return index;
}


int main(){
int array[6],i=0,index;
cout << "\nEnter 6 number seperated by space: ";
for (int j = 5;j>=0;j--)
{
cin >> array[j];
}

reverse(array,6);
sumavgneg(array,6);
index = indexofSmallestElement(array,6);
cout<<"\nIndex of minimum element is: "<<index<<" and element is: "<<array[index]<<endl;

return 0;
}
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning