Q1. Initialize an array to zeros

Solution to Q1

The lesson to learn from this program is to know what happens when the arrays are not initialized before use

Run the program and look at the output carefully to understand what is happening before and after array initialization

/* ArrayInitToZeros.cpp */
 
#include <iostream>
using namespace std;
 
int main(){
    //create an array of 10 elements
    int arr[10];
 
    cout << "values of arr elements before initialization " << endl;
    for (int i = 0; i < 10; i++)
        cout << arr[i] << endl;
 
    //initialize the array elements to zeros
    for (int i = 0; i < 10; i++)
        arr[i] = 0;
 
    cout << "values of arr elements after initialization " << endl;
    for (int i = 0; i < 10; i++)
        cout << arr[i] << endl;
 
    cin.get();
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License