Click here to Skip to main content
15,942,847 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include <iostream>
#include <iomanip>

using namespace std;

const int sentinel = 0;

const int arraysize = 10;

int main()
{
	cout << fixed << showpoint << setprecision(2); // format to 2 decimals

	int array[arraysize]; // declare an array item of 10 components
	int number;

	int count = 0;


	for ( int i = 0; i < arraysize; i++)
		array[i] = 0;
	cout <<"Please enter ints, " << sentinel << " to quit: ";
	cin >> number;


	while (number != sentinel && count < arraysize)
	{
		array[count++] = number;
		cin >> number;
	}

	cout << endl;
	cout << "The original array: ";
	for (int i = 0; i < count; i++)
		cout << array[i] << ' ';
	cout << endl;



	int temp;
	for (int i = 0; i < arraysize/2; i++)
	{

		temp = array[arraysize-1-i];
		array[arraysize - i - 1] = array[i];
		array[i] = temp;
	}

	cout << "The reverse array: ";
	for (int i = 0; i < arraysize; i++)
		cout << array[i]<< ' ';
	cout << endl;

	return 0;

}


What I have tried:

I tried to print reverse if the original value is 1 2 3 4 5 6 then I want reverse array to be 6 5 4 3 2 1 not 0 0 0 0 6 5 4 3 2 1
Posted
Updated 10-Feb-16 19:32pm
v3

Use count - the number of elements the user entered - instead of arraysize in your for loops:
C++
for (int i = 0; i < count/2; i++)
{
    temp = array[count-1-i];
    array[count - i - 1] = array[i];
    array[i] = temp;
}
cout << "The reverse array: ";
for (int i = 0; i < count; i++)
{
    cout << array[i]<< ' ';
}
 
Share this answer
 
Comments
Patrice T 11-Feb-16 1:56am    
Looks like we agree :)
+5
CPallini 11-Feb-16 3:24am    
5.
I would try something like that.
C++
int temp;
for (int i = 0; i < count/2; i++)
{

    temp = array[count-1-i];
    array[count - i - 1] = array[i];
    array[i] = temp;
}

cout << "The reverse array: ";
for (int i = 0; i < count; i++)
    cout << array[i]<< ' ';
cout << endl;
 
Share this answer
 
Comments
CPallini 11-Feb-16 3:24am    
5.
Patrice T 11-Feb-16 7:09am    
Thank you
You know, your code could be rewritten this way:
C++
 #include <iostream>
 #include <vector>
using namespace std;

int main()
{
  const int sentinel = 0;
  int n;
  vector <int> v;

  cout <<"Please enter ints, " << sentinel << " to quit: " << endl;

  do
  {
    cin >> n;
    if ( n ) v.push_back(n);
  } while (n);


  cout << "the original array : ";

  for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    cout << *it << " ";
  cout << endl;

  cout << "the reverse array : ";
  for (vector <int>:: reverse_iterator it = v.rbegin(); it != v.rend(); it++)
    cout << *it << " ";
  cout << endl;

}
 
Share this answer
 
v2
Hi.

I will do just one change in your code. At line 50, in the loop to print the reverse array, change "for (int i = 0; i < arraysize; i++)" to "for (int i = arraysize - count; i < arraysize; i++)"


C++
cout << "The reverse array: ";
for (int i = 0; i < arraysize; i++)
		cout << array[i]<< ' ';

// change above code to 
cout << "The reverse array: ";
for (int i = arraysize - count; i < arraysize; i++)
		cout << array[i]<< ' ';
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900