Scanf in Array not working in my code

int main()
{
    int n, i, count;
    int numbers[3];

    printf("Enter the numbers: ");
    scanf("%d", &numbers[3]);




    for (n = 0; n <= 100; n++) {
        count = 0;
        for (i = 0; i < 3; i++) {
            if (numbers[i] == n) {
                count ++;
            }
        }
        printf("Numbers %d\t Count %d\n", n, count);
    }
    return 0;
}

Note: I am learing C in my own there is no one to guide me…Kindly help me why my code is not working.

what you want to do in this program,
kindly share your targeted output i.e. the output that you want…

I want how many numbers for how many time are there.

Eg:
If I enter 1 1 3 3 1 1 2 2

so I want the output as:

Numbers 1 count 4
Numbers 2 count 2
Numbers 3 count 3

And so on.

Thanks in advance.

Dear @imran,
This is not the way to count frequency of array elements.
Kindly check that, you have written wrong code to get array elements input from user.
Also, when you are counting the frequency you are running your loop till out of bound.

1 Like

Although your code will not count frequency coz it is not the right way of counting occurence of particular no. Other than this , ur code has many logical and other errors… as of now , ignoring ur other errors , to make ur sacnf to work with Arrays… Don’t use & sign in front of numbers[3];

Corrected code : scanf("%d" , numbers[3]);

Reason : we need not to use & with array names coz array name is itself a pointer which doesn’t require &…

I hope this solved ur scanf issue…!!

1 Like