Thread: Dynamic Allocating Array and Function Call

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    126

    Dynamic Allocating Array and Function Call

    Clearly I'm not doing this right, what am I missing?

    Code:
    /* printing the average grade for every student in every class.
    printing out the average grade per class; printing out the average grade per student over all
    classes. Your college administration wants this task accomplished by a program which allocates
    memory for the array to hold the student grades in the classes. Then, each printing must be
    accomplished by a separate function with the array or portions thereof passed as parameters.   */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    #define NUMBER_OF_STUDENTS  11 //globals for array variables
    #define NUMBER_OF_CLASSES   4
    #define NUMBER_OF_GRADES    5
    
    float gradeAve(int ***c);
    void freeArray(int ***a);
    
    int main()
    {
    int i, j, student, class, grade;
    
       int*** array;
       
       array=malloc(NUMBER_OF_STUDENTS*sizeof(int**));
       
       for (i=0; i<NUMBER_OF_STUDENTS; i++)
       {
       
          array[i]=malloc(NUMBER_OF_CLASSES*sizeof(int*));
          
            for (j=0;i<NUMBER_OF_CLASSES;j++)
            {
            
                array[i][j]=malloc(NUMBER_OF_GRADES*sizeof(int));} }
                
    for(student=0; student<NUMBER_OF_STUDENTS; student++)
        {//for loop for populating 3 dimensional array
            for(class=0;class<NUMBER_OF_CLASSES;class++)
                {
        
            for(grade=0; grade<NUMBER_OF_GRADES; grade++)
                {
                if(grade==0){
                    printf("Enter in the grade for student %d in subject %d:\n", student+1, class+1);
                    scanf("%d", &array[student][class][grade]);
                }else
                    scanf("%d",&array[student][class][grade]);
    }
    }
    }
                
                
    
    printf("Average of all Grades: %.2f\n", gradeAve(array));
    freeArray(array);
    }
    
    float gradeAve(int ***c)
    {
      float sum=0;
        int grade;//local variables
    
            for(grade=0; grade<NUMBER_OF_GRADES; grade++)
            {
                sum+=c[NUMBER_OF_STUDENTS][NUMBER_OF_CLASSES][NUMBER_OF_GRADES];//only takes sum of the grades and divides by number of grades
            }
        
        return (sum/NUMBER_OF_GRADES);
    
    }
    
    void freeArray(int ***a) {
        int i, j;
        for (j=0; j< NUMBER_OF_CLASSES; ++j)
            free(a[j]);
        for (i = 0; i < NUMBER_OF_GRADES; ++i) {
            free(a[j][i]);
        }
        free(a);
    }
    Last edited by Sorinx; 12-13-2012 at 05:09 AM.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Sorinx View Post
    Clearly I'm not doing this right, what am I missing?
    You are missing to tell us what your problem is (compiler errors/warnings, program crash, unexpected results).

    Please provide as much information as possible and copy'n'paste error messages as plain text. Don't rewrite them!

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Quote Originally Posted by AndiPersti View Post
    You are missing to tell us what your problem is (compiler errors/warnings, program crash, unexpected results).

    Please provide as much information as possible and copy'n'paste error messages as plain text. Don't rewrite them!

    Bye, Andreas
    Just crashes when I try and run

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You do not free your memory.

    Also, do not cast malloc
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Quote Originally Posted by std10093 View Post
    You do not free your memory.

    Also, do not cast malloc
    what should I do instead of casting malloc?

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Do not cast it. Just let it as it is.
    You have something like this
    Code:
    ... = (int)malloc...
    Do it like this
    Code:
    ... = malloc...
    But this is not why your code crashes. Neither is the fact that you didn't free the memory.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Quote Originally Posted by std10093 View Post
    Do not cast it. Just let it as it is.
    You have something like this
    Code:
    ... = (int)malloc...
    Do it like this
    Code:
    ... = malloc...
    But this is not why your code crashes. Neither is the fact that you didn't free the memory.
    Ok I wrote a function to free my memory, and I won't cast it in the future. Trying to figure out what I am doing wrong

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    array[i]=(int**)malloc(NUMBER_OF_CLASSES*sizeof(int*));{
        for (j=0;i<NUMBER_OF_GRADES;j++)
    Do you see the problem?

    Code:
             
    for(student=0; student<11; student++){//for loop for populating 3 dimensional array
        for(class=0;class<4;class++){
            for(grade=0; grade<5; grade++){
    Why the magic numbers?

    Code:
    float gradeAve(int ***c)
    {
    ...
    sum+=c[NUMBER_OF_STUDENTS][NUMBER_OF_CLASSES][NUMBER_OF_GRADES];//only takes sum of the grades and divides by number of grades
    You are out of bounds.

    How about learning to use a debugger which can tell you at which line your program crashes?

    Bye, Andreas

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Check here
    Code:
    array[i]=(int**)malloc(NUMBER_OF_CLASSES*sizeof(int*));{
            for (j=0;i<NUMBER_OF_GRADES;j++)
    Change that to this
    Code:
     array[i]=malloc(NUMBER_OF_CLASSES*sizeof(int*));
     
            for (j=0;j<NUMBER_OF_CLASSES;j++)
            {
    The Indent style would tell you the truth by itself
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Quote Originally Posted by std10093 View Post
    Check here
    Code:
    array[i]=(int**)malloc(NUMBER_OF_CLASSES*sizeof(int*));{
            for (j=0;i<NUMBER_OF_GRADES;j++)
    Change that to this
    Code:
     array[i]=malloc(NUMBER_OF_CLASSES*sizeof(int*));
     
            for (j=0;j<NUMBER_OF_CLASSES;j++)
            {
    The Indent style would tell you the truth by itself
    Thanks I missed that semi colon, it's early and my spacing is horrid atm, bad habits. Still same result however

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Well, post your updated code please
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  12. #12
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Quote Originally Posted by std10093 View Post
    Well, post your updated code please
    Ahh sorry, I edit it in main, whenever I respond about the changes!

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Still on line 31 you have i instead of j!!!

    Also in the function that you calculate the average, you get out of bounds!

    You should go through every cell. You can do that the same way you filled them up in main
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  14. #14
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    The free function is also wrong, but first fix the others and then we talk about the free. I would suggest you to comment the function or not to call it yet
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  15. #15
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Bare with me I'm a bit over tired, why am I out of bounds?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 07-06-2011, 11:44 AM
  2. Replies: 3
    Last Post: 12-10-2010, 08:51 AM
  3. Dynamic Array of Object Fucntion call.
    By Spitball04 in forum C++ Programming
    Replies: 5
    Last Post: 09-15-2004, 10:40 AM
  4. dynamic memory allocating error
    By valhall in forum C Programming
    Replies: 2
    Last Post: 04-04-2003, 10:49 AM
  5. Allocating dynamic memory for structs
    By Nevyn in forum C Programming
    Replies: 4
    Last Post: 09-17-2001, 11:54 AM