3

In my program I created a struct called student and it has two int fields ID and classNum. If I make a 2D array of 20 students, how do I use a loop to go through the array and assign each student its values? For this the values are in numeric order, 1-20, and 30-50, so I thought using a for loop should make it easy, but I can't get it to work. I basically need it to return a pointer to the array when it's finished.

typedef struct student
{
    int ID;
    int classNum;
};

//Creates a 4x5 array of students
struct student classroom [4][5];

//Creates the function that will return the array
student **makeClass()
{
    int classNumba = 30;
    int x = 0;
    for(x = 1; x <= 20; x++)
    {
        classroom[x].ID = x;
        classroom[x].classNum = classNumba;
        classNumba++;
    }
    return classroom;
}
Zorgatone
  • 4,176
  • 4
  • 30
  • 47
Josh Burkman
  • 81
  • 1
  • 9
  • What is `students` ? And you don't use your 2-d array any where. – ameyCU Dec 04 '15 at 06:39
  • there are tons of issues in your code... `students` is never defined; neither is `board`; you are looping through `studens` as a 1D-array, ... – Chris Maes Dec 04 '15 at 06:40
  • Welcome to stack overflow. I would suggest to go through some online tutorials or books to get through the basics of C. If you find some difficulty then search on SO or Google. If it still doesn't help then let us know on SO. We love to help programmers but before asking any question you should have to show us your research effort. – haccks Dec 04 '15 at 06:41
  • Sorry guys, wrote the wrong thing there. students is supposed to be classroom, Ill fix it now – Josh Burkman Dec 04 '15 at 06:52
  • Sorry I was just reading something about a board game when I wrote that, the end was supposed to return classroom. I fixed the two mistakes you guys mentioned. – Josh Burkman Dec 04 '15 at 06:54
  • 1
    Pointer-to-pointers have nothing what-so-ever to do with 2D arrays. – Lundin Dec 04 '15 at 07:30
  • `return classroom;` isn't going to work( and is not necessary either) – M.M Dec 04 '15 at 08:12

3 Answers3

3

You need to have 2 loops instead of 1 as below,

for(i = 0; i <4; i++)
{
    for(j = 0; j <5; j++)
      {
        classroom[i][j].ID = (i+1) * (j+1);
        classroom[i][j].classNum = classNumba;
        classNumba++;
       }
 }

Also, as classroom is global you don't need to return it.

Magisch
  • 7,312
  • 9
  • 36
  • 52
Rohan
  • 52,392
  • 12
  • 90
  • 87
1

As I can see from your question you do not know how to initialize 2-D structure array . So you should refer this. To fill 2D array you always need 2 for loop so keep that in mind.

int i=1, classNumba=30;

    for(x=0; x<4; x++) {
           for(y = 0; y <5; y++) {
                classroom[x][y].ID = i;
                classroom[x][y].classNum = classNumba;
                classNumba++;
                i++;
           }
    }

Loop will execute for ((x=4)*(y=5)=20) means 20 times. 20 times i will be assigned to classroom[x][y].ID and each time it will increase +1 from its initial value i=1 . Similarly classNumba will be assigned to classroom[x][y].classNum and each time it will increase +1 from its initial value classNumba=30

Community
  • 1
  • 1
Punit Vara
  • 3,744
  • 1
  • 16
  • 30
0

There are a number of subtle issues with your code, the first and foremost -- Why in the world do you want a 2D array of structs when a simple array makes much more sense?? Regardless, you are free to use a 2D array, but as far as your problem statement goes -- there is absolutely no need to do so.

Using a simple 1D array greatly simplifies your code, as well as the passing of parameters between functions. Give the following a try for comparison:

#include <stdio.h>

typedef struct student {
    int ID;
    int classNum;
} student;

#define NSTUD 20

student *makeClass (student *classroom);

int main (void) {

    student classroom [NSTUD] = {{0}, {0}}; /* array of 20 students */
    size_t i = 0;

    makeClass (classroom);

    printf ("\nclassroom information:\n\n");
    for (i = 0; i < NSTUD; i++)
        printf ("  ID : %2d    classNum : %2d\n", 
                classroom[i].ID, classroom[i].classNum);

    return 0;
}

/* function that will return the array */
student *makeClass (student *classroom)
{
    if (!classroom) return NULL;    /* validate classroom address */

    int classNumba = 30;
    int x = 0;
    for(x = 0; x < NSTUD; x++)
    {
        classroom[x].ID = x + 1;
        classroom[x].classNum = classNumba;
        classNumba++;
    }
    return classroom;
}

Compile (with Warnings enabled)

gcc -Wall -Wextra -O3 -o bin/classroom classroom.c

Use/Output

$ ./bin/classroom

classroom information:

  ID :  1    classNum : 30
  ID :  2    classNum : 31
  ID :  3    classNum : 32
  ID :  4    classNum : 33
  ID :  5    classNum : 34
  ID :  6    classNum : 35
  ID :  7    classNum : 36
  ID :  8    classNum : 37
  ID :  9    classNum : 38
  ID : 10    classNum : 39
  ID : 11    classNum : 40
  ID : 12    classNum : 41
  ID : 13    classNum : 42
  ID : 14    classNum : 43
  ID : 15    classNum : 44
  ID : 16    classNum : 45
  ID : 17    classNum : 46
  ID : 18    classNum : 47
  ID : 19    classNum : 48
  ID : 20    classNum : 49

Like I said, you are free to use a 2D array. If there is a valid reason for it, then it may make sense, but based upon what I can see in your questions, you are much better served with a 1D array. Let me know if you have any questions.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85