Thread: Need help returning Arrays,

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    25

    Exclamation Need help returning Arrays,

    I need help getting my latest program to return arrays. I now know I'm supposed to use pointers, because you can't directly return an array, but I have no idea how to convert my program to do this. If someone could help, I would greatly appreciate it.


    // Seperate files for functions //

    Code:
    #include "constants.h"#include <stdio.h>
    
    
    float *StudentAverage(int input[NUMBERofSTUDENTS][PGMSCORES]) {
    
    
    int i, j, sum=0;
    
    
    static float *SAverages[NUMBERofSTUDENTS];
    
    
    for(i=0; i < NUMBERofSTUDENTS; i++) {
    
    
        for(j=0; j < PGMSCORES; j++) {
            
            sum+= input[i][j];
        }
        
        SAverages[i] = (sum / PGMSCORES);
        sum = 0;
    }
    
    
    
    
    
    
    
    
    return SAverages;
    }
    
    
    
    
    
    
    
    
    float *ProgramAverage(int input[NUMBERofSTUDENTS][PGMSCORES]) {
    
    
    int i, j, sum=0;
    static float *PAverages[PGMSCORES];
    
    
    for(i=0; i< PGMSCORES; i++) {
    
    
        for(j=0; j< NUMBERofSTUDENTS; j++) {
            
            sum+= input[j][i];
        }
        PAverages[i] = (sum / NUMBERofSTUDENTS);
        sum=0;
    }
    
    
    
    
    
    
    
    
    
    
    return PAverages;
    }
    Code:
    #include <stdio.h>#include "constants.h"
    
    
    
    
    
    
    int GradeDist (int input[NUMBERofSTUDENTS][PGMSCORES] ) {
    
    
    int i, j, A,B,C,D,F, temp;
    
    
    static int Grades[PGMSCORES][5];
    
    
    for(i=0; i< PGMSCORES; i++) {
    
    
        for(j=0; j< NUMBERofSTUDENTS; j++) {
            
            A=B=C=D=F=0;
    
    
            temp = input[j][i];
    
    
            if(temp > 89) A++;
            if(temp <= 89 && temp >=80) B++;
            if(temp <= 79 && temp >= 70) C++;
            if(temp <=69 && temp >=60) D++;
            if(temp < 60) F++;
        }
        Grades[i][0]=A;
        Grades[i][1]=B;
        Grades[i][2]=C;
        Grades[i][3]=D;
        Grades[i][4]=F;
    }
    
    
    return Grades;
    }
    Code:
    #include <stdio.h>#include "constants.h"
    #include <math.h>
    
    
    
    
    int Median(int input[NUMBERofSTUDENTS][PGMSCORES])
    {
    
    
        int i, j, temp, z=0;
        static int medians[PGMSCORES];
        
        input = BubbleSort(input);
    
    
        for(i=0; i< PGMSCORES; i++) {
            
            for(j=0; j<NUMBERofSTUDENTS; j++) {
            
            if(NUMBERofSTUDENTS % 2 == 1) 
            {
                medians[i] = input[(NUMBERofSTUDENTS + 1)/2 -1][i];
            }
            else
            {
                medians[i] = (input[NUMBERofSTUDENTS/2] + input[NUMBERofSTUDENTS/2 -1]) /2;
            }
    
    
    
    
            }
    
    
    
    
    
    
        }
    
    
    
    
    
    
        
        
        
    
    
    
    
    retun medians;
    
    
    }

    Code:
    #include <stdio.h>#include "constants.h"
    
    
    void dashes()
    {
    int i;
    
    
    for(i=0; i < (PGMSCORES * 10) + 20; i++) printf("-");
    }
    
    
    
    
    
    
    
    
    int main() {
    
    
    int mainarr[NUMBERofSTUDENTS][PGMSCORES];
    float programavg[1][PGMSCORES];
    int i, j;
    for(i=0; i<NUMBERofSTUDENTS; i++)
        for(j=0; j < PGMSCORES; j++)
            scanf("%d", &mainarr[i][j]);
    
    
    float StudentAvgs[NUMBERofSTUDENTS] = StudentAverage(mainarr);
    float ProgramAvgs[PGMSCORES] = ProgramAverage(mainarr);
    
    
    int ProgramMedians[PGMSCORES] = Median(mainarr);
    
    
    int Grades[NUMBERofSTUDENTS][PGMSCORES] = GradeDist(mainarr);
    
    
    
    
    printf("CSE 1030 Program 4 - Alexander Hollis - email - csp_03\n\n");
    
    
    printf("Program Results:\n");
    
    
    printf("Student    |");
    for(i=0; i< PGMSCORES; i++)
    {
    printf("  PGM %d |",i+1);
    }
    printf(" Std Avg\n");
    
    
    dashes();
    
    
    printf("\n");
    
    
    for(i=0; i< NUMBERofSTUDENTS; i++)
    {
        printf("Student %d  |",i+1);
        for(j=0; j < PGMSCORES; j++)
        {
        printf("%6d  |", mainarr[i][j]);
        }
        printf("%6.1f \n",StudentAvgs[i]);
    }
    dashes();
    
    
    printf("PGM Avg     |");
    for(i=0; i< PGMSCORES; i++)
    {
        printf("%5.1f  |",ProgramAvgs[i]);
    }
    printf("\n");
    
    
    printf("PGM Median |");
    for(i=0; i < PGMSCORES; i++)
    {
        printf("%6d  |", ProgramMedians[i]);
    }
    printf("\n\n\n\n");
    printf("Program Grade Distribution:\n\n");
    printf("Program | As | Bs | Cs | Ds | Fs\n");
    for(i=0; i < 32; i++) printf("-");
    
    
    printf("\n");
    
    
    for(i=0; i< PGMSCORES; i++) 
    {
        printf("PGM %d   |",i+1);
        
        for(j=0; j<5; j++) {
            printf("%3d |", Grades[i][j]);
        }
        printf("\n");
    
    
    }
    printf("\n\n");
    
    
    return 0;
    }

    Code:
    #include <stdio.h>#include "constants.h"
    
    
    
    
    int BubbleSort(static int input[NUMBERofSTUDENTS][PGMSCORES]) {
    
    
    int i, j, temp;
    
    
    
    
    for(i=0; i < PGMSCORES; i++) {
    
    
        for(j=0; j< NUMBERofSTUDENTS; j++) {
    
    
            if(input[j][i] > input[j+1][i]) {
                
                temp = input[j+1][i];
                input[j+1][i] = input[j][i];
                input[j][i] = temp;
    
    
        }
    
    
    
    
    
    
    
    
        }
    
    
    
    
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    return input;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    static float *SAverages[NUMBERofSTUDENTS];
    You're correctly using static, but you're declaring an array of pointers to floats. You just want just an array of floats, e.g. static static float SAverages[NUMBERofSTUDENTS];. Your compiler should be complaining about this, if not, turn up the warnings. The name of the array serves as a pointer to the first element, which is what you are really returning when you do return SAverages; at the end. Similar change for PAverages. This wont work for your 2-d arrays however, or if you need to make multiple calls to a function that uses/returns a static variable, or if you use threads, etc. A better approach would probably be to pass in the array to fill as a parameter, and return void (nothing) from your function:
    Code:
    void StudentAverage(int input[NUMBERofSTUDENTS][PGMSCORES], float SAverages[NUMBERofSTUDENTS]) {
    
    
        int i, j, sum=0;
    
    
        for(i=0; i < NUMBERofSTUDENTS; i++) {
            for(j=0; j < PGMSCORES; j++) {
                sum+= input[i][j];
            }
            SAverages[i] = (sum / PGMSCORES);
            sum = 0;
        }
    }

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    25

    Compiles, but there's a problem with the output.

    The program now compiles, but either an array isn't storing properly, or handing over properly.
    Here's the updated code:


    Code:
    #include "constants.h"#include <stdio.h>
    
    
    int StudentAverage(int input[NUMBERofSTUDENTS][PGMSCORES]) {
    
    
    int i, j, sum=0;
    
    
    float SAverages[NUMBERofSTUDENTS];
    
    
    for(i=0; i < NUMBERofSTUDENTS; i++) {
    
    
        for(j=0; j < PGMSCORES; j++) {
            
            sum+= input[i][j];
        }
        
        SAverages[i] = (sum / PGMSCORES);
        return SAverages[i];
        sum = 0;
    }
    
    
    
    
    
    
    
    
    
    
    }
    
    
    
    
    
    
    
    
    int ProgramAverage(int input[NUMBERofSTUDENTS][PGMSCORES]) {
    
    
    int i, j, sum=0;
    static float PAverages[PGMSCORES];
    
    
    for(i=0; i< PGMSCORES; i++) {
    
    
        for(j=0; j< NUMBERofSTUDENTS; j++) {
            
            sum+= input[j][i];
        }
        PAverages[i] = (sum / NUMBERofSTUDENTS);
        return PAverages[i];
        sum=0;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    }
    Code:
    #include <stdio.h>#include "constants.h"
    
    
    
    
    
    
    int GradeDist (int input[NUMBERofSTUDENTS][PGMSCORES] ) {
    
    
    int i, j, A,B,C,D,F, temp;
    
    
    int Grades[PGMSCORES][5];
    
    
    for(i=0; i< PGMSCORES; i++) {
    
    
        for(j=0; j< NUMBERofSTUDENTS; j++) {
            
            A=B=C=D=F=0;
    
    
            temp = input[j][i];
    
    
            if(temp > 89) A++;
            if(temp <= 89 && temp >=80) B++;
            if(temp <= 79 && temp >= 70) C++;
            if(temp <=69 && temp >=60) D++;
            if(temp < 60) F++;
        }
        Grades[i][0]=A;
        Grades[i][1]=B;
        Grades[i][2]=C;
        Grades[i][3]=D;
        Grades[i][4]=F;
    }
    
    
    for(i=0; i< PGMSCORES; i++)
       for(j=0; j< 5; j++) return Grades[i][j];
    
    
    
    
    
    
    
    
    
    
    }
    Code:
    #include <stdio.h>#include "constants.h"
    #include <math.h>
    
    
    
    
    int Median(int input[NUMBERofSTUDENTS][PGMSCORES])
    {
    
    
        int i, j, temp, z=0;
        int x, y;
        static int medians[PGMSCORES];
        int inputSorted[NUMBERofSTUDENTS][PGMSCORES];    
        
        for(i=0; i< NUMBERofSTUDENTS; i++)
           for(j=0; j<PGMSCORES; j++) inputSorted[i][j] = BubbleSort(input);
        
    
    
        
        for(i=0; i< PGMSCORES; i++) {
        
            
            
            for(j=0; j<NUMBERofSTUDENTS; j++) {
            x = inputSorted[sizeof(inputSorted) /2][i];
            y = inputSorted[sizeof(inputSorted) /2 -1][i];
            if(NUMBERofSTUDENTS % 2 == 1) 
            {
                medians[i] = inputSorted[(NUMBERofSTUDENTS + 1)/2 -1][i];
            }
            else
            {
                medians[i] = (x + y) /2;
            }
            
    
    
            }
            return medians[i];
    
    
    
    
        }
    
    
    
    
    
    
        
        
        
    
    
    
    
    
    
    
    
    }
    Code:
    #include <stdio.h>#include "constants.h"
    #define ProgramMedians medians
    
    
    #define StudentAvgs SAverages
    #define ProgramAvgs PAverages
    
    
    void dashes()
    {
    int i;
    
    
    for(i=0; i < (PGMSCORES * 10) + 20; i++) printf("-");
    }
    
    
    
    
    
    
    
    
    int main() {
    
    
    int mainarr[NUMBERofSTUDENTS][PGMSCORES];
    float programavg[1][PGMSCORES];
    int i, j;
    for(i=0; i<NUMBERofSTUDENTS; i++)
        for(j=0; j < PGMSCORES; j++)
            scanf("%d", &mainarr[i][j]);
    
    
    float SAverages[NUMBERofSTUDENTS];
    float PAverages [PGMSCORES];
    int medians[PGMSCORES];
    int Grades[PGMSCORES][5];
    
    
    
    
    
    
    for(i=0; i< NUMBERofSTUDENTS; i++)
       SAverages[i] = StudentAverage(mainarr);
    
    
    for(i=0; i< PGMSCORES; i++)
       PAverages[i] = ProgramAverage(mainarr);
    
    
    for(i=0; i< PGMSCORES; i++)
       medians[i] = Median(mainarr);
    
    
    for(i=0; i < PGMSCORES; i++)
       for(j=0; j< 5; j++)
       Grades[i][j]= GradeDist(mainarr);
    
    
    
    
    printf("CSE 1030 Program 4 - Alexander Hollis - Email - csp_03\n\n");
    
    
    printf("Program Results:\n");
    
    
    printf("Student    |");
    for(i=0; i< PGMSCORES; i++)
    {
    printf("  PGM %d |",i+1);
    }
    printf(" Std Avg\n");
    
    
    dashes();
    
    
    printf("\n");
    
    
    for(i=0; i< NUMBERofSTUDENTS; i++)
    {
        printf("Student %d  |",i+1);
        for(j=0; j < PGMSCORES; j++)
        {
        printf("%6d  |", mainarr[i][j]);
        }
        printf("%6.1f \n",StudentAvgs[i]);
    }
    dashes();
    
    
    printf("\nPGM Avg     |");
    for(i=0; i< PGMSCORES; i++)
    {
        printf("%5.1f  |",ProgramAvgs[i]);
    }
    printf("\n");
    
    
    printf("PGM Median |");
    for(i=0; i < PGMSCORES; i++)
    {
        printf("%6d  |", ProgramMedians[i]);
    }
    printf("\n\n\n\n");
    printf("Program Grade Distribution:\n\n");
    printf("Program | As | Bs | Cs | Ds | Fs\n");
    for(i=0; i < 32; i++) printf("-");
    
    
    printf("\n");
    
    
    for(i=0; i< PGMSCORES; i++) 
    {
        printf("PGM %d   |",i+1);
        
        for(j=0; j<5; j++) {
            printf("%3d |", Grades[i][j]);
        }
        printf("\n");
    
    
    }
    printf("\n\n");
    
    
    return 0;
    }
    Code:
    #include <stdio.h>#include "constants.h"
    
    
    
    
    int BubbleSort(int inputSorted[NUMBERofSTUDENTS][PGMSCORES]) {
    
    
    int i, j, temp;
    
    
    
    
    for(i=0; i < PGMSCORES; i++) {
    
    
        for(j=0; j< NUMBERofSTUDENTS; j++) {
    
    
            if(inputSorted[j][i] > inputSorted[j+1][i]) {
                
                temp = inputSorted[j+1][i];
                inputSorted[j+1][i] = inputSorted[j][i];
                inputSorted[j][i] = temp;
    
    
        }
    
    
    
    
    
    
    
    
        }
    
    
    
    
    }
    
    
    for(i=0; i < PGMSCORES; i++)
       for(j=0; j< NUMBERofSTUDENTS; j++) return inputSorted[i][j];
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    }

    "Constants.h File"

    Code:
    #define NUMBERofSTUDENTS 5#define PGMSCORES 4
    Here's the test Data:

    100 97 94 87
    90 68 77 81
    100 100 100 100
    95 90 92 88
    87 88 79 81

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I take it you've not yet figured out that C cannot return arrays from functions.

    Yeah I know ... WTF??? ... but that's how it is.

    For example...
    Code:
    int* MyFunction(int info)
      { 
         int x;
         int* array[10];
    
         for (x = 0; x < 10; x++)
           array[x] =  info;
         
        return array;
    }
    By the time control is returned from this function array has been deleted from the program's stack and no longer contains valid data, so what you get back is a pointer to nothing. This leaves you to create the array in the parent function and pass in a pointer to the array so it is updated in place...

    Code:
    void MyFunction(int info, int *update, int size)
      { 
         int x;
         for (x = 0; x < size; x++)
           update[x] = info;
    }
    
    
    // in parent function
    int array[10];
    
    MyFunction(7,array,10);
    Since there is no way to determine the size of the array once it's out of scope, you have to pass in both a pointer to the array and it's size.

    Here's a little copy and paste example you can run to demonstrate the problems...
    Code:
    #include <stdio.h>
    
    int* MyFunction(int a, int b, int c)
      {  static int array[3];
         array[0] = a;
         array[1] = b;
         array[2] = c;
         return array;  } // return a pointer.
    
    
    int main (void)
      { int *a1, *a2;  // int pointers
    
        printf("calling a1 = MyFunction(10,20,30);\t");
        a1 = MyFunction(10,20,30);
        printf("a1 has %d %d %d\n",a1[0],a1[1],a1[2]);
    
        printf("calling a2 = MyFunction(100,200,300);\t");
        a2 = MyFunction(100,200,300);
        printf("a2 has %d %d %d\n",a2[0],a2[1],a2[2]);
    
        printf("\nLooks good, except...\t"); 
        printf("a1 now has %d %d %d\n",a1[0],a1[1],a1[2]);
    
        getchar();
        return 0; }

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Why is your code so horribly formatted? You have obscene amounts of blank lines and shoddy indentation. Pick one of the top 3 style you see here: Indent style - Wikipedia, the free encyclopedia.

    Here's your StudentAverage function properly indented and spaced, with comments to follow:
    Code:
    int StudentAverage(int input[NUMBERofSTUDENTS][PGMSCORES]) {
        int i, j, sum=0;
        float SAverages[NUMBERofSTUDENTS];
    
    
        for(i=0; i < NUMBERofSTUDENTS; i++) {
            for(j=0; j < PGMSCORES; j++) {
                sum+= input[i][j];
            }
    
    
            SAverages[i] = (sum / PGMSCORES);
            return SAverages[i];
            sum = 0;
        }
    }
    1. SAverages is now just a plain local array. Without the 'static' keyword, it ceases to exist when the function is done, so you can't use it to return a whole array. It either needs to be static, and your function needs to return a float *, or you need to declare the array in main and pass it in to be filled.
    2. The return statement is in the middle of your code, causing you to return only after calculating the first student's average. Instead of returning the whole array, it returns only one (the first) student's average. Moreover, your function returns an int, but you return a float.

    I strongly suggest passing in the array as a parameter to have it filled in by the function, as I showed in my previous post. You call it from main like so:
    Code:
    StudentAverage(mainarr, SAverages);
    No return statement necessary. No calculating only one student's averages, no complicated return types/statements. The array looks and feels like a local variable, but you're actually changing the data back in main.

    I'm not going through all of your code/files. You wrote way too much code without planning or testing. You need to plan this out on paper first. Figure out and test your calculations by hand. Then start coding in small chunks, testing frequently. No moving to the next bit until the current bit is complete and error free. Are you even sure you can read in and print out mainarr properly? If that doesn't work, your averages will never be correct.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    25
    I modified the code to now ask for the array to fill in, and all compiles, but I'm thinking somewhere along the way the mainarr is being changed, because at output all of the scores are memory addresses instead of their respective data.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Following up on Anduril's suggestion that your code is rather poorly thought out (no offense intended)...

    Would it not be easier to design a struct (one per student) and work on that instead of multiple arrays?

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    25
    I managed to find the problem after re reading over my code, I had two variables flipped, so it was changing my main array to memory allocations. The only difficulty I'm having now if getting everything to print out in a table format. Here's the assignment, I feel like I'm making the output be way more code than necessary, because right now I'm just guessing and checking to see if everything lines up. The only part that isn't lining up is the PGM Avg and medians at the bottom.

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    27
    I looked through your code but did not see any variables flipped that might cause a problem. Which two variables did u identify that were switched? As for lining everything up, are u using a format like 2.2%lf for each number to make sure they are all taking the same amount of space.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning Arrays
    By jimmyjamesii in forum C++ Programming
    Replies: 3
    Last Post: 12-04-2005, 10:02 PM
  2. returning arrays?
    By hoangvo in forum C++ Programming
    Replies: 7
    Last Post: 07-22-2005, 08:57 PM
  3. returning 2D arrays
    By ... in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 12:28 PM
  4. Returning Arrays
    By mr_spanky202 in forum C Programming
    Replies: 2
    Last Post: 04-06-2003, 02:57 PM

Tags for this Thread