Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (lgbasallote):

C array help

OpenStudy (lgbasallote):

i have this code ``` #include <conio.h> #include <stdio.h> main() { int a[100]; int b, c, d; printf("How many numbers do you want? "); scanf("%d", &c); for (b=1; b<=c; b++) { printf("\nEnter number %d: ", b); scanf("%d", d); } getch(); } ``` how can i make the program print the inputted numbers in a form of a list?

OpenStudy (anonymous):

#include <conio.h> #include <stdio.h> main() { int a[100]; int b, c, d[c]; printf("How many numbers do you want? "); scanf("%d", &c); for (b=1; b<=c; b++) { printf("\nEnter number %d: ", b); scanf("%d", d[b]); } getch(); } may be this can be helpful

OpenStudy (anonymous):

#include <conio.h> #include <stdio.h> main() { int a[100]; int b, c; printf("How many numbers do you want? "); scanf("%d", &c); for (b=1; b<=c; b++) { printf("\nEnter number"); scanf("%d", &a[c]); } getch(); }

OpenStudy (anonymous):

The &a[c] lets you give an input of c numbers If you want to display it again then you can add up the following code for (b=1; b<=c; b++) { printf("\nThe numbers are\n"); printf("%d", a[c]); }

OpenStudy (anonymous):

it will give you array out of bound error ? dont you noticed?

OpenStudy (anonymous):

In C an array always starts at index 0, not 1. You can ask the user for the first number but the first position in the array is zero. And the highest index is the size of the array minus 1. 99 in this case. So you should use < instead of <= and starting at 0. Otherwise you could get an index out of range error when accessing index 100. #include <conio.h> #include <stdio.h> main() { int a[100]; int b = 0; int c = 0; // c mustn't be higher than 100 // if c is greater 100, ask the user again do { printf("How many numbers do you want (max. 100)? "); scanf("%d", &c); } while(c > 100); // input for (b=0; b<c; b++) { printf("Enter number %d: ", b+1); scanf("%d", a[b]); } // output for (b=0; b<c; b++) { printf("Number %d: %d\n", b+1, a[b]); } getch(); }

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!