arrays with and without zeros

In this article, we show two programs. In the first program, the array is not initialized. In the second program it is initialized to a value 0.

#include<iostream>
using namespace std;
int main(){
	const int size = 10;
	int a[10];
	cout<<"The elements are "<<endl;
	for(int i=0;i<size;i++){
		cout<<a[i]<<endl;
	}
	return 0;
}
output of the above program. giving garbage values
output of the above program. giving garbage values

The output of the above program gives garbage values.

In the second program,one value is initialized to 0 and hence all the values are 0.

#include<iostream>
using namespace std;
int main(){
	const int size = 10;
	int a[10]={0};
	cout<<"The elements are "<<endl;
	for(int i=0;i<size;i++){
		cout<<a[i]<<endl;
	}
	return 0;
}
output of the above program. all values are 0
output of the above program. all values are 0

Leave a Reply