0

I am writing a code that takes an array of 10 and collects user input. I want to print out the array after the user has finished putting all the values into it. The problem is that when I printout the array it gives me a bunch of garbage values and a weird output. I am not sure why it is doing what it is doing.

This is my code:

#include <iostream>
#include<ctime>

using namespace std;

int main(){

    int num;

    int arr[10] = {0};

    cout<<"Enter 10 numbers: "<< endl;

    for(int i = 0; i < 10; i++){
        cin>>arr[i];

        
    }

    for(int i = 0; i < sizeof(arr); i++){
        cout<<arr[i]<< " ";
    }

return 0;

}
0

3 Answers 3

2

That is because you are running the loop from 0 till sizeof(arr), here sizeof(arr) means the size of array in bytes. Which happens to be sizeof(int) times the number of elements in the array.

So if we consider sizeof(int) to be 4bytes(i.e. each int takes 4bytes in memory), and number of elements to be 10, then it would be 4*10 = 40. So the for loop would run for 40 times.

Instead do the following:

#include <iostream>
#include<ctime>

using namespace std;

int main(){

    int num;

    int arr[10];

    cout<<"Enter 10 numbers: "<< endl;

    for(int i = 0; i < (sizeof(arr)/sizeof(arr[0])); i++){
        cin>>arr[i];

        
    }

    for(int i = 0; i < (sizeof(arr)/sizeof(arr[0])); i++){
        cout<<arr[i]<< " ";
    }

return 0;

}

As pointed out by @user4581301, it's better to make the loop stop taking input at (sizeof(arr)/sizeof(arr[0])), as the size can be changed laterwards.

I have corrected it. It will work now.

Sign up to request clarification or add additional context in comments.

1 Comment

Recommendation: Make the same change to for(int i = 0; i < 10; i++). Right now the input loop is vulnerable to changing int arr[10]; to some number other than 10.
1

Why you are getting garbage values is very well explained in the other answers. You can use std::begin and std::end or range-based for-loop here for iterating through the array. C++ is smart enough to deduce the length of the array for you.

int main(){
    int arr[10]{0};

    /* Take inputs from the console and fill the array here. */

    auto start = std::begin(arr);
    auto end = std::end(arr);
    for(; start!=end; ++start)
        std::cout<<*start<<" ";

    // Or using range-based for-loop
    for(auto val: arr)
        std::cout<<val<<" ";
}

Comments

1

sizeof(arr) is the total size in bytes of the array, not the number of elements. That's probably 40 rather than 10, Because the typical compiler targeting desktop PC hardware uses a 32 bit int at this time.

If your compiler's up to date and supports at least the C++17 Standard revision, use std::size to get the number of elements in the array, but a better option (supported back to C++11) is to use range-based for loops and let the compiler figure out the bounds for you. This makes changes to the count of elements in arr self-managing.

Eg:

for(auto & val:arr){
    cin>>val;
}
for(auto & val:arr){
    cout<<arr[i]<< " ";
}

If range-based for and std::size are not available, define and use a constant value everywhere you use 10 or sizeof(arr).

Eg:

const int ARR_SIZE = 10;

and then the definition of arr becomes

int arr[ARR_SIZE] = {0};

and the control for both for loops become

for(int i = 0; i < ARR_SIZE; i++)

This allows you to vary the size of arr with only one change required to make the code function correctly. This is also less cumbersome than repeating sizeof(arr)/sizeof(arr[0]) everywhere the number of elements in arr is needed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.