Output of the array

#include
using namespace std;
int main() {

int n;
cin>>n;
int a[100];
int n1,r,q,i;
n1=n;
i=0;
while(n1!=0)
{
  r=n1%10;
  n1=n1/10;
  a[i]=r;
  i++;  
}

n=sizeof(a)/sizeof(a[0]);
for (i=0;i<n;i++)
cout<<a[i]<<’\t’;
return 0;
}

this is my code for the Chewbacca and Number, HackerBlocks Tutorial
I wanted to put all the digits of a number in an array and then interchange the digits with 9-i.
In this process I wanted to display the array I am getting the array but after that, some unwanted garbage values are being displayed.


can I know how to get rid of the garbage values.

Hello @yyashu123,

The garbage values that you are getting are the initial garbage values stored in the array.
The reason those values are being displayed:
You are iterating the loop 100 times.

As, you have declared an array of size 100:
int a[100];
So, sizeof(arr)=4*100
and sizeof(arr[0])=4;
Hence, n=sizeof(a)/sizeof(a[0])=100

I have corrected your code:

Hope, this would help.
Give a like if you are satisfied.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.