Structure, where is one array inside the struct... 
Author Message
 Structure, where is one array inside the struct...

Hi!

I have one problem with structures, so could someone help me, please.

My problem is the following.

I dont know how to make a structure,
where is student number (integer), student name (string) and
student grades included subject (char) and grade (integer).

I was thinking somethng like this

struct STUDENT
{
    char  stunum[5];
    int stuname[30];
    char  grades[5][10]; // this is my problem
  };

struct STUDENT array[100];

Allowed subjects are O, V, L, S and T and grade from 0 to 5.

Could someone show me what is the best way to do this and how I should
call grades.

Thanks in advance!

Regards

--
Jarmo Tiittanen
Svanstr?minkuja 5-7 B22
00870 HELSINKI
http://www.*-*-*.com/ ~jacxs




Sun, 02 Dec 2001 03:00:00 GMT  
 Structure, where is one array inside the struct...

Quote:

> Hi!

> I have one problem with structures, so could someone help me, please.

> My problem is the following.

> I dont know how to make a structure,
> where is student number (integer), student name (string) and
> student grades included subject (char) and grade (integer).

> I was thinking somethng like this

> struct STUDENT
> {
>     char  stunum[5];

       Student number is integer; why do you declare it as char array?

Quote:
>     int stuname[30];

       Student name is a string; why do you declare it as array of
integers?

Quote:
>     char  grades[5][10]; // this is my problem

       Grades are pairs of (char; int); why do you declare it as an
array of arrays of chars?

Quote:
>   };

> struct STUDENT array[100];

> Allowed subjects are O, V, L, S and T and grade from 0 to 5.

> Could someone show me what is the best way to do this and how I should
> call grades.

   #define MAX_STUDENT_COUNT 100
   #define MAX_NAME_LENGTH 30
   #define SUBJECTS_COUNT 5

   struct grade { char subject_code;
                   int subject_grade
                };
   struct student { int student_number;
                    char student_name[MAX_NAME_LENGTH];
                    struct grade grades[SUBJECTS_COUNT];
                   };

   ....
   struct student students[MAX_STUDENT_COUNT];
   ....
   students[0].student_number = 1111;
   strcpy(students[0].student_name,"Tiittanen");
   students[0].grades[0].subject_code = 'V';
   students[0].grades[0].subject_grade = 5;
--
        Regards,
                Alex Krol
Disclaimer: I'm not speaking for Scitex Corporation Ltd

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.



Sun, 02 Dec 2001 03:00:00 GMT  
 Structure, where is one array inside the struct...
Hi

Quote:



> > struct STUDENT
> > {
> >     char  stunum[5];

>        Student number is integer; why do you declare it as char array?

Hups, I declared it by mistake in a hurry, but it seems that my declarations
was gone wrong places ;^) It should be int and...

Quote:

> >     int stuname[30];

>        Student name is a string; why do you declare it as array of
> integers?

... this should be char.... apuva. I was so hurry to go to play inline
hockey.

Quote:

> >     char  grades[5][10]; // this is my problem

>        Grades are pairs of (char; int); why do you declare it as an
> array of arrays of chars?

I was wondering if there is some way to declare there int too.

Quote:

>    #define MAX_STUDENT_COUNT 100
>    #define MAX_NAME_LENGTH 30
>    #define SUBJECTS_COUNT 5

>    struct grade { char subject_code;
>                    int subject_grade
>                 };
>    struct student { int student_number;
>                     char student_name[MAX_NAME_LENGTH];
>                     struct grade grades[SUBJECTS_COUNT];
>                    };

>    ....
>    struct student students[MAX_STUDENT_COUNT];
>    ....

In that moment I have this same structure, so I thing this is the best way
to do this structure, isn't it?

Quote:
>         Regards,
>                 Alex Krol

Thanks Alex, you were very helpful.
--
Jarmo Tiittanen
Svanstr?minkuja 5-7 B22
00870 HELSINKI
http://www.dlc.fi/~jacxs




Sun, 02 Dec 2001 03:00:00 GMT  
 Structure, where is one array inside the struct...
Groovy hepcat Jarmo Tiittanen was jivin' on Wed, 16 Jun 1999 16:29:41
+0300 in comp.lang.c.
Structure, where is one array inside the struct...'s a cool scene! Dig
it!

Quote:
>I dont know how to make a structure,
>where is student number (integer), student name (string) and
>student grades included subject (char) and grade (integer).

  I'm afraid your grasp of English seems to be quite poor, so this is
hard to understand. But fortunately I could understand from the code
you posted.

Quote:
>struct STUDENT
>{
>    char  stunum[5];

  I thought this was supposed to be an int. What's with the array of
char?

Quote:
>    int stuname[30];

  And this should be an array of char, not array of int.

Quote:
>    char  grades[5][10]; // this is my problem

  Now, here you're way off base. What you need is an array with 5
elements, but an array of what? Well, each element must be able to
hold two items of data: a letter representing the subject, and a grade
(presumeably from 0 to 100). Sounds like another stuct to me.

Quote:
>  };

>struct STUDENT array[100];

>Allowed subjects are O, V, L, S and T and grade from 0 to 5.

  Define the struct with an inner nested struct, something like this:

struct STUDENT
{
  int num;
  char name[30];
  struct
  {
    char subject;
    int grade;
  }grade_data[5];

Quote:
};

You can then read input into members of the struct something like
this:

#include <stdio.h>
#include <ctype.h>

#define MAX_NUM_STU 100

int yn_prompt(char *prompt);
int get_grade(char subject);

struct STUDENT
{
  int num;
  char name[30];
  struct
  {
    char subject;
    int grade;
  }grade_data[5];

Quote:
};

int main(void)
{
  struct STUDENT stu[MAX_NUM_STU];
  int i, j;
  char subject[5] = "OVLST";

  i = 0;
  do
  {
    stu[i].num = i;

    printf("\nName of student: ");
    fflush(stdout);
    fgets(stu[i].name, sizeof stu[i].name, stdin);

    for(j = 0; j < 5; j++)
    {
      stu[i].grade_data[j].grade =
get_grade(stu[i].grade_data[j].subject = subject[j]);
    }

    i++;
  }while(i <= 100 && yn_prompt("\nEnter another?"));

  return 0;

Quote:
}

int yn_prompt(char *prompt)
{
  char buf[20];
  int c;

  do
  {
    printf("%s (Y|N): ", prompt);
    fflush(stdout);
    fgets(buf, sizeof buf, stdin);
  }while((c = tolower(buf[0])) != 'y' && c != 'n');
  return c == 'y';

Quote:
}

int get_grade(char subject)
{
  char buf[20];
  int n;

  do
  {
    printf("Grade for subject %c: ", subject);
    fflush(stdout);
    fgets(buf, sizeof buf, stdin);
  }while(sscanf(buf, "%d", &n) != 1 || n < 0 || n > 100);
  return n;

Quote:
}

--

----- Dig the EVEN NEWER, MORE IMPROVED news sig!! -----

-------------- Shaggy was here! ---------------
    http://aardvark.apana.org.au/~phaywood/
============= Ain't I'm a dawg!! ==============



Fri, 07 Dec 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Problem with struct/array inside a struct using fread/fwrite

2. problem realloc array inside array of struct

3. problem realloc array inside array of struct

4. Array inside of a struct gets allocated statically?

5. Timers - Inside and array of structs

6. float array inside struct

7. Accessing arrays of char pointers inside of structs.

8. Defining MIDL structs that contain more than one conformant arrays

9. pls help to initialise a struct inside a struct

10. a struct inside a struct....

11. An array in a structure Or an Array Of Structures

12. How to bring one window inside another one?

 

 
Powered by phpBB® Forum Software