Click here to Skip to main content
15,922,894 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
C++
#include <iostream>
using namespace std;
main(){
	int i,j;
	int arr[i];
	int new_arr[i];
	int number,store=0;
	for(i=0;i<5;i++){
		cout<<"enter the 5-digit element of array : ";
		cin>>number;
		arr[i]=number;
		for(j=0;j<5;j++){
			store=store*10+(number%10);
			number=number/10;
		}
		new_arr[i]=store;
	}
	cout<<"the 5 digit element array is : "<<arr[0]<<" "<<arr[1]<<" "<<arr[2]<<" "<<arr[3]<<" "<<arr[4]<<endl;
	cout<<"the 5 digit element reverse order array is : "<<new_arr[0]<<" "<<new_arr[1]<<" "<<new_arr[2]<<" "<<new_arr[3]<<" "<<new_arr[4]<<endl;
	return 0;
}
Posted
Updated 17-Dec-15 2:51am
v3
Comments
PIEBALDconsult 17-Dec-15 8:31am    
Did you have a question?
zeib 17-Dec-15 8:37am    
its not working the above code. every time i run it it gives two or three true values and the rest garbage values
zeib 17-Dec-15 8:37am    
what's the error in above code???

1 solution

You are passing the uninitialised variable i to specify the array sizes:
int i,j;
int arr[i];
int new_arr[i];

As a result, you don't know how large your arrays are actually. The compiler may initialise i with zero so that your arrays did not really exist and you will get random values from the stack when reading items out.

To avoid such mistakes you should set the compiler warning level to maximum and fix your code until you got no warnings.

[EDIT/UPDATE]
In your case I would define a size:
const int max_items = 5;
int arr[max_items];
int new_arr[max_items];
// ...
for (i = 0; i < max_items; i++)
// ...


[EDIT/UPDATE 2]
Once the above has been fixed, you should also reset store before the inner loop. Otherwise the value is cumulated over all five input values:
// Reset store before calculation 
store = 0;
for(j=0;j<5;j++){
 
Share this answer
 
v3
Comments
zeib 17-Dec-15 9:31am    
^^ have tried this before but same result ..
Jochen Arndt 17-Dec-15 9:45am    
See my updated answer.
zeib 18-Dec-15 2:30am    
yeah!! Thanks it worked.. thank you very much

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