Write a program that first reads at most 30 numbers of type

Write a program that first reads at most 30 numbers of type double from the user (the user will type character quit when finished), and stores these values in an array in the same order that they were entered by the user. Your program should then call the function reverse_array with this array as an argument, and finally displays the new contents of the array to the screen, 5 values per line. Sample Input/Output: (the user’s input is shown in bold) Please enter at most 30 numbers; type quit when finished. 1396 470 -9 -1 9180 -5011 0 2 -275 13 216 15 -7 quit The array in reverse order (5 values per line) is : -7 15 216 13 -275 2 0 -5011 9180 -1 -9 470 1396

( I am using visual studio 2015 c++)

Solution

//code has been tested on gcc compiler

#include <iostream>

using namespace std;
int index=0; //global variable for storing total no of numbers entered by user
void reverse_array(double a[]){ //function to print reverse array
  
  
cout<<endl<<\"The array in reverse order (5 values per line) is : \";
int line=0; //variable used for printing only 5 values per line
for(int i=index-2;i>=0;i--) //printing in reverse order   
{
if(line % 5 != 0) //after 5 input go to the next line
{ cout<<a[i]<<\" \";
line++;
}
else{
cout<<endl<<a[i]; //next line
line++;
}
}
  
}
int main()
{
double a[30]; //initializing double array
cout <<\"Please enter at most 30 numbers; type quit and press enter when finished. \" << endl; //enter atmost //30 numbers enter quit to exit

while(cin.good() && index<30) //while quit not enetered
{
string s;
s = cin.peek();

if( s == \"quit\" )
break;
else{
cin>>a[index]; //storing numbers in double array
index++;
}
}

  

reverse_array(a);
return 0;
}
//end of code


**********OUTPUT************
The array in reverse order (5 values per line) is :   
-7 15 216 13 -275   
2 0 -5011 9180 -1   
-9 470 1396

**********OUTPUT************

Please let me know in case of any doubt,Thanks.

Write a program that first reads at most 30 numbers of type double from the user (the user will type character quit when finished), and stores these values in a
Write a program that first reads at most 30 numbers of type double from the user (the user will type character quit when finished), and stores these values in a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site