Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

c programming 2d array define problem help pls ~?

First lets view my question in below links, very thanks...

http://imageupper.com/i/?S0500010100011D1318671604...

___________________________________________________________________________________

My codes so far...

#include <stdio.h>

#define CLASS_SIZE 100

int main(void) {

char input[CLASS_SIZE][5];

int size, j;

printf("Please input class size: ");

scanf("%d", &CLASS_SIZE);

for(i=0; i<CLASS_SIZE; i++);

{

for(j=0; j<5; j++);

{

printf("Please input grade for student %d", i+1);

scanf("%c", &input[CLASS_SIZE][j]);

}

}

return 0;

}

_________________________________________________________________________________

My problem is I know that I can't change the define value, but this question request me to allow user input the class size then store into the define value. So, how can I do that? Or did I miss understand this question? Please help me.

3 Answers

Relevance
  • 10 years ago
    Favorite Answer

    #define is a preprocessor directive, which means that the C preprocessor replaces each occurrence of "CLASS_SIZE" with "100", so here's what your line looks like to the compiler:

    scanf("%d", &100);

    which will cause an error during compilation because 100 is an r-value and you can't make a reference to it.

    You need to store the class size in another variable, example:

    int cSize;

    scanf("%d", &cSize);

    and then use cSize as the size, but note that you can't create the array after getting the value since the size of the array needs to be a fixed value or a const variable.

    You can use either dynamic arrays or vectors if you want more flexibility, though those are a little more advanced.

  • 10 years ago

    You have to do one out of 2 things whereas you're doing both. First, you defined the value of class size as 100 and then your asking the user to input the same. And if the question says to use define, then you can't take an input from the user.

    Don't worry.

    Engineer's advice.

  • 10 years ago

    I can't see enough of the assignment to really clarify what what was wrong.

    However, it's common to use a define to set the maximum allowable value. You still get the class size from the user but it's not the same variable. You then use this input value for your loops. It wastes space in the array but so what.

    So, you a different variable like

    int classSize;

    get this value from the user and use it in the loops. The only place that CLASS_SIZE is used is in the declaration of your array.

Still have questions? Get your answers by asking now.