here are the program below reads five numbers, find their sum, and print the numbers in reverse order.

#include <iostream>


using namespace std;


int main()
{
int  item0, item1, item2, item3, item4;
int sum;


cout<<"Enter five integers: ";
cin>>item0>>item1>>item2>>item3>>item4;
cout<<endl;


sum = item0 + item1 + item2 + item3 + item4;


cout<<"The sum of the numbers = "<<sum<<endl;
cout<<"The numbers in reverse order are: ";
cout<<item4<<" "<<item3<<" "<<item2<<" "
<<item1<<" "<<item0<<endl;
return 0;
}

How can i modify the program so that it will use array instead of using 5 different variables. Help me...plz

With the way you have it set up, you can add an array declaration of the same name, remove the declarations for your items, and then everywhere you use itemn, replace it with item[[I]n[/I]]:

#include <iostream>

using namespace std;

int main()
{
  int item[5];
  int sum;

  cout<<"Enter five integers: ";
  cin>>item[0]>>item[1]>>item[2]>>item[3]>>item[4];
  cout<<endl;

  sum = item[0] + item[1] + item[2] + item[3] + item[4];

  cout<<"The sum of the numbers = "<<sum<<endl;
  cout<<"The numbers in reverse order are: ";
  cout<<item[4]<<" "<<item[3]<<" "<<item[2]<<" "
    <<item[1]<<" "<<item[0]<<endl;

  return 0;
}

But to make the code better, you should use loops instead of hardcoding each item:

#include <iostream>

using namespace std;

int main()
{
  const int n = 5;
  int item[n];
  int sum;

  cout<<"Enter "<< n <<" integers: ";
  for ( int i = 0; i < n; i++ )
    cin>> item[i];
  cout<<endl;

  sum = 0;
  for ( int i = 0; i < n; i++ )
    sum += item[i];

  cout<<"The sum of the numbers = "<<sum<<endl;
  cout<<"The numbers in reverse order are: ";
  for ( int i = n - 1; i >= 0; i-- )
    cout<< item[i] <<' ';
  cout<<'\n';

  return 0;
}

It's longer, but now you can work with a list of any length without making more than a single change (the value of n).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.