0

Im not sure what Ive done wrong but I'm trying to display my array but the program keeps ending so I cant see if its actually working. Im sure its a little mistake - Any help or suggestions will be appreciated.

# include <stdio.h>
# define MAX 100
int main(void)
{
    int array[MAX],i,n;

    printf("\n Enter Array Size: ",MAX);
 
    scanf("%d", &n); //Allows user to choose Array Size
 
    for(i = 0; i < n; i++)
    {
       printf("\n Enter %d value: ",i+1); 
       scanf("\n%d", &array[i]); //Allows user to enter values into array
       }
       printf("%d",&array[i]); //Trying to display the array but program keeps ending not sure what Im doing wrong.
 
       return 1;

}

Thanks
TrueCoding

Edited by TrueCoding: n/a

3
Contributors
6
Replies
8
Views
6 Years
Discussion Span
Last Post by Adak
0

There you go: i have written where i made the change

# include <stdio.h>
# define MAX 100
int main(void)
{
    int array[MAX],i,n;
 
    //printf("\n Enter Array Size: ",MAX);
 
    scanf("%d", &n); //Allows user to choose Array Size
 
    for(i = 0; i < n; i++)
    {
       printf("\n Enter %d value: ",i+1); 
       scanf("\n%d", &array[i]); //Allows user to enter values into array
       printf("%d",array[i]); // you had an "&" here.
 
    }
       
     return 1;
 
}

Also, im not sure why you had used the MAX in the printf, so i just commented it.

Edited by myk45: n/a

0

Ok thanks for that Myk but for some reason its not displaying my array it just says some random numbers?

0

Basically this is the code but its not displaying my array just some random numbers.

# include <stdio.h>
# define MAX 100
int main(void)
{
    int array[MAX],i,n;

    printf("\n Enter Array Size: ",MAX);
 
    scanf("%d", &n);
 
    for(i = 0; i < n; i++)
    {
       printf("\n Enter %d value: ",i+1); 
       scanf("\n %d", &array[i]);
       }
       printf("\n %d",array[i]);
       getch();
       return 1;
}
0

Needs a bit of touch up:

Basically this is the code but its not displaying my array just some random numbers.

Let's use MAX of 10, so you don't have to enter lots of numbers yet ;)

# include <stdio.h>
# define MAX 10
int main(void)
{
    int array[MAX],i,n;
    /* your array size is already set at MAX integers (0 through MAX-1) */
    //printf("\n Enter Array Size: ",MAX);
      
    for(i = 0; i < MAX; i++)
    {
       printf("\n Enter %d value: ",i+1); 
       scanf("%d", &array[i]);
    }
    //now print out the array
    putchar('\n');
    for(i = 0; i < MAX; i++)
    {
       printf("%3d  ",array[i]); 
    }
    getch();
    return 0;
}

By convention return 0 means run was normal. Anything else may be an error.

Edited by Adak: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.