Home > front end >  array doesn't show correct values [closed]
array doesn't show correct values [closed]

Time:09-23

I'm trying to show the values of the array after getting it from user

#include <stdio.h>
#define MAXSIZE  3
void main (void)
{
    int data[MAXSIZE];
    int n ,i ;

    for (i = 0;i < MAXSIZE ;i   )
    {
        printf("Enter array [%d]: ",i 1);
        scanf("%d",&data[n]);

    }

    printf("data before sort\n");
    printf("===================\n");
    for (i = 0; i < MAXSIZE ;i   )
        printf("Value[%d]:%d\n",i 1,data[n]);





}

input :

Enter array [1]: 3

Enter array [2]: 2

Enter array [3]: 1

output:

data before sort

===================

Value[1]:1

Value[2]:1

Value[3]:1

I want to show data entered data but it shows the last entered value

in this case it's 1

CodePudding user response:

Replace instances of n with i. Variablei is being used to track the count in each for loop and n is never set or initialized.

  • Related