3

I'm confused with declaring arrays. If I enter a size, ie good[200], the answer will just return 200. The program essentially takes 10 integers from user and returns which are of particular ranges of numbers. I thought of doing that by collecting the numbers through arrays and then returning the length of those arrays as the answer.

int n[10], i, j, excellent[], good[], poor[];
printf("Input 10 integers\n");
for (i = 0; i < 10; i++) {
    scanf("%d", &n[i]);
}
for (j = 0; j < 10; j++) {
    if (n[j] > 80) {
        excellent[j] = n[j];
    }
    if (n[j] >= 60 && n[j] <= 79) {
        good[j] = n[j];
    }
    if (n[j] < 60) {
        poor[j] = n[j];
    }
}
printf("%lu are excellent\n", (sizeof(excellent) / sizeof(excellent[0])));
printf("%lu are good\n", (sizeof(good) / sizeof(good[0])));
printf("%lu are poor\n", (sizeof(poor) / sizeof(poor[0])));
3
  • excellent[], good[],poor[] array size missing refers to those. You need to give it a size either explicitly or via an initializer. Commented May 3, 2020 at 1:58
  • You do need to include the size, eg. good[200]. The size will then be 200 elements. If you want to show how many you've actually used, you'll have to keep track of that separately. Commented May 3, 2020 at 1:59
  • @Piegoose Why do you not want to use int excellent[10],good[10],poor[10];? Commented May 3, 2020 at 2:04

3 Answers 3

4

Arrays in C have a fixed size that must be specified at the time the array is defined, either explicitly by giving a size between [ and ] or implicitly by the number of element the array is initialized with. They do not have a dynamic size that increases with each element added.

You should give each array a size of 10 to match the input array, and keep track of the number of valid element in each array with separate variables as counters.

int n[10],excellent[10],good[10],poor[10];
int i, j, excellent_cnt = 0, good_cnt = 0, poor_cnt = 0;

printf("Input 10 integers\n");
for(i=0;i<10;i++){
    scanf("%d",&n[i]);}
for(j=0;j<10;j++){
    if(n[j]>80){
        excellent[excellent_cnt++] = n[j];}
    if(n[j]>=60 && n[j]<=79){
        good[good_cnt++] = n[j];}
    if(n[j]<60){
        poor[poor_cnt++] = n[j];}
}

printf("%lu are excellent\n", excellent_cnt);
printf("%lu are good\n", good_cnt);
printf("%lu are poor\n", poor_cnt);
Sign up to request clarification or add additional context in comments.

Comments

0

First of all you have to initialize excelent[] good[] and poor[] with a defined size exactly the same way that n[] (write int excelent[your size], same for poor[] and good[]).

In C, arrays always had a fixed size, if you want variable size, maybe you should create a custom data structure working as list in c#.

Comments

0

For reference, you're only allowed to omit the array size in a couple of circumstances:

  • when the array is initialized char s[] = "hello"; int a[] = {1, 2, 3};
  • when it's only a declaration, not a definition extern int a[];
  • as the trailing member of a struct: `struct foo { char data };
  • in function arguments (but that's actually a pointer, not an array)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.