Thread: Help with returning arrays using pointers

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    11

    Help with returning arrays using pointers

    I'm writing a program that needs to have several functions. Other than main, one of them is a function called get_inputs. In get_inputs, the program will get information from the user, and then return all 4 of the values recieved to main.

    I know that pointers need to be used here, so here is what I think needs to be done, but I know that it wont work.

    First of all, I need to properly call the function in main. I also need to find out if the way that I've implemented the pointers is correct. I'm not sure what I need to put inside the braces in the userInput array. I don't even know if the format is correct. I've done some research, but it is really confusing for me. Could I get some help with both of these?

    NOTE: there are more functions in the program, but they dont apply to this.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    /* Rows represent the months of the year, Columns represent the hours in a typical day */
    #define NROWS 11
    #define NCOLS 23
    #define TRUE 1
    #define FALSE 0
    #define INSTALLATION_COST 2000
    
    float panelEfficiency = 0.18, inverterEfficiency = 0.90;
    
    int main()
    {
          int houseEnergyREQ, numBatteries, numInverters;
          int flag = TRUE;
          float userInput[4], panelCost, inverterCost, batteryCost,
                panelArea, batteryRating = 4.8, inverterRating = 5.0,
                insolDayMin, maxHourPanelOutput, insolHourMax, totalCost;
          
          float *ptr_to_userInput;
          
          float insolation_Matrix[NROWS][NCOLS];
    
          
          while(flag)
          {
                float get_inputs(float userInput[]);
                
                /* Switch userInput array to meaningful terms */
                userInput[0] = houseEnergyREQ;
                userInput[1] = panelCost;
                userInput[2] = inverterCost;
                userInput[3] = batteryCost;
                
                /* Equation to calculate panelArea */
                panelArea = houseEnergyREQ/(insolDayMin*panelEfficiency*inverterEfficiency);
                
                /* Equation to calculate maxHourPanelOutput */
                maxHourPanelOutput = insolHourMax*panelArea*panelEfficiency;
                
                /* Equation to calculate numbatteries */
                numBatteries = ceil(houseEnergyREQ/(inverterEfficiency*batteryRating));
                
                /* Equation to calculate numInverters */
                numInverters = ceil(maxHourPanelOutput/(inverterEfficiency*inverterRating));
                
                /* Equation to calculate total cost */
                totalCost = panelArea*panelCost + numBatteries*batteryCost + numInverters*inverterCost
                             + INSTALLATION_COST;
                
                printf("The total cost of your Solar panel system is %.2f $. \n", totalCost);
                
                /* Give user an option to quit program */
                printf("Do you wish to continue? (y/n) \n");            
                if(getchar() == 'n')
                {
                      flag = FALSE;
                }
          }
    
          system("PAUSE");
    }
    /*---------------------------------------*/
    /* Function to get inputs houseEnergyREQ, panelCost, inverterCost, batteryCost */
    float get_inputs(float userInput[4], *ptr_to_userInput)
    {
          *ptr_to_userInput = &userInput[]
          
          printf("This program will calculate the total cost of your solar panel system.");
          printf("Enter the daily required amount of energy for your house, in kWh : \n");
          scanf("%f", &userInput[0]);
    
          printf("Enter the cost of solar panels (per m^2), power inverters and batteries: \n");
          scanf("*f %f %f", &userInput[1], &userInput[2], &userInput[3]);
    
          return *ptr_to_userInput;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Since arrays are always passed as "the address of the first element" in C, you don't actually need to add another argument. Just use the array as input.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    11
    How would I do that?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    void get_inputs(float userInput[4])
    {
          printf("This program will calculate the total cost of your solar panel system.");
          printf("Enter the daily required amount of energy for your house, in kWh : \n");
          scanf("%f", &userInput[0]);
    
          printf("Enter the cost of solar panels (per m^2), power inverters and batteries: \n");
          scanf("*f %f %f", &userInput[1], &userInput[2], &userInput[3]);
    }
    Like that - now whatever array you pass in will be modified by the input from the user.

    Obviously, you have no checking to see if the user entered "a" as the first input and all the input is "garbage". You should check that the return value from scanf is equal to the number or input values you expect (that is 1 and 3 respectively), and if not, you should clear the input and try again. But if you don't care about error handling, just leave it as above.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    11
    Oh, so if I change the function from float to void, I don't need a return statement?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cuba06 View Post
    Oh, so if I change the function from float to void, I don't need a return statement?
    Correct. And since the array is passed as a pointer in the first place, you can change it inside the function. And of course, you can't return multiple values anyways, so it's no loss to remove the return statement.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Reading your code a bit further:
    Code:
                float get_inputs(float userInput[]);
                
                /* Switch userInput array to meaningful terms */
                userInput[0] = houseEnergyREQ;
                userInput[1] = panelCost;
                userInput[2] = inverterCost;
                userInput[3] = batteryCost;
    
    This is just declaring that a function exists, not calling it - you probably want to call the function. Just remove the float and float keywords

    This is "back to front" - you are setting the array entries to the values from the meaningful names - I think you want the other way around

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    11
    Sweet. Thanks Mats.

    Another thing. I've got another function called fill_In_Insolation that fills in a 2D matrix that has 24 columns and 12 rows. Would I return the matrix in the same way as the 1D array? With no return statement and a void function instead of a float?


    Also, do you know of a way in which to find the largest number in a 2D array? I'm having problems trying to figure that out.

    Thank you for all your help thus far. Of all the replies I've received about C on various forums, yours has been the only one that makes sense to a beginner in C.

    EDIT: Oh ok, I realise what I did with the meaningful names things. Thanks.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, all arrays are passed as the address of the very first element of the array.

    Finding the largest element is just a question of walking through the array and tracking the largest one [start by setting "largest" to for example 0 or -9999999 or whatever you can be sure is GUARANTEED to be smaller than at least the largest number].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    11
    Okay, here is the code so far, in its entirety:

    I'm getting errors everywhere, mainly "subscripted value is neither array nor pointer". Can anyone offer any help? I'm getting desperate.

    Code:
    #define NROWS 11
    #define NCOLS 23
    #define TRUE 1
    #define FALSE 0
    #define INSTALLATION_COST 2000
    
    float panelEfficiency = 0.18, inverterEfficiency = 0.90;
    
    int main()
    {
          int houseEnergyREQ, numBatteries, numInverters;
          int flag = TRUE;
          float userInput[4], panelCost, inverterCost, batteryCost,
                panelArea, batteryRating = 4.8, inverterRating = 5.0,
                insolDayMin, maxHourPanelOutput, insolHourMax, totalCost;
          
          float insolation_Matrix[NROWS][NCOLS];
    
          void get_inputs(float userInput[4]);
          
          while(flag)
          {
                get_inputs(userInput[4]);
                
                /* Switch userInput array to meaningful terms */
                houseEnergyREQ = userInput[0];
                panelCost = userInput[1];
                inverterCost = userInput[2];
                batteryCost = userInput[3];
                
                /* Equation to calculate panelArea */
                panelArea = houseEnergyREQ/(insolDayMin*panelEfficiency*inverterEfficiency);
                
                /* Equation to calculate maxHourPanelOutput */
                maxHourPanelOutput = insolHourMax*panelArea*panelEfficiency;
                
                /* Equation to calculate numbatteries */
                numBatteries = ceil(houseEnergyREQ/(inverterEfficiency*batteryRating));
                
                /* Equation to calculate numInverters */
                numInverters = ceil(maxHourPanelOutput/(inverterEfficiency*inverterRating));
                
                /* Equation to calculate total cost */
                totalCost = panelArea*panelCost + numBatteries*batteryCost + numInverters*inverterCost
                             + INSTALLATION_COST;
                
                printf("The total cost of your Solar panel system is &#37;.2f $. \n", totalCost);
                
                /* Give user an option to quit program */
                printf("Do you wish to continue? (y/n) \n");            
                if(getchar() == 'n')
                {
                      flag = FALSE;
                }
          }
    
          system("PAUSE");
    }
    /*---------------------------------------*/
    /* Function to get inputs houseEnergyREQ, panelCost, inverterCost, batteryCost */
    void get_inputs(float userInput[4])
    {      
          printf("This program will calculate the total cost of your solar panel system.");
          printf("Enter the daily required amount of energy for your house, in kWh : \n");
          scanf("%f", &userInput[0]);
    
          printf("Enter the cost of solar panels (per m^2), power inverters and batteries: \n");
          scanf("%f %f %f", &userInput[1], &userInput[2], &userInput[3]);
    }
    /*---------------------------------------*/
    /* Function with matrix to determine insolDayMin */
    float fillInInsolation(float insolation_Matrix[NROWS][NCOLS])
    {
          /* Declare Variables : i represents a row (month), j represents a column (year) */
          int i, j;
          float insolation, Fs_h;
          
          for(i == 0; i <= 11; i++)
          {
                /* Declare insolation values for different months */
                switch (i)
                {
                       case 0:
                            insolation = 0.142;
                            break;
                       case 1:
                            insolation = 0.207;
                            break;
                       case 2:
                            insolation = 0.298;
                            break;
                       case 3:
                            insolation = 0.389;
                            break;
                       case 4:
                            insolation = 0.456;
                            break;
                       case 5:
                            insolation = 0.483;
                            break;
                       case 6:
                            insolation = 0.467;
                            break;
                       case 7:
                            insolation = 0.410;
                            break;
                       case 8:
                            insolation = 0.326;
                            break;
                       case 9:
                            insolation = 0.234;
                            break;
                       case 10:
                            insolation = 0.158;
                            break;
                       case 11:
                            insolation = 0.124;
                            break;
                }
                for (j == 0; j <= 4; j ++)
                {
                    insolation_Matrix[i][j] = 0;
                }
                for (j == 5; j <= 17; j ++)
                {
                    Fs_h = 0.1316 * insolation * cos (0.2618 * (j - 12));
                    scanf("%f", &insolation_Matrix[i][j]);
                }
                for (j == 18; j <= 23; j ++)
                {
                    insolation_Matrix[i][j] = 0;
                }
          }
          return insolation_Matrix[NROWS][NCOLS];          
    
    }
    /*---------------------------------------*/ 
    /* A function to calculate insolDayMin   */
    float calculate_insolDayMin(float insolation_Matrix, float insolDayMin)
    {
          int i, j;
          float insolDaySum[12], temp;
          
          for(i == 0; i <= 11; i++)
          {
                for (j == 5; j <= 17; j ++)
                {
                    insolDaySum[i] += insolation_Matrix[i][j];            
                }
          }
          while (1)
          {
                if(insolDaySum[i] > insolDaySum[i + 1])
                {
                      insolDaySum[i] = temp;
                      insolDaySum[i + 1] = insolDaySum[i];
                      temp = insolDaySum[i + 1];
                }
          }
          insolDaySum[0] = insolDayMin;
          
          return insolDayMin;
    }
    /*---------------------------------------*/ 
    /* A function to calculate insolHourMax  */
    float calculate_insolHourMax(float insolation_Matrix, float insolHourMax)
    {
          int i, j;
          insolHourMax = 0;
          
          for(i == 0; i <= 11; i++)      
          {
                for (j == 5; j <= 17; j ++)
                {
                    if(insolation_Matrix[i][j] > insolHourMax)
                    {
                           insolHourMax = insolation_Matrix[i][j];
                    }
                }
          }
          return insolHourMax;
    }
    Last edited by cuba06; 11-23-2007 at 11:38 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing and returning array pointers?
    By thealmightyone in forum C Programming
    Replies: 26
    Last Post: 03-26-2009, 03:38 PM
  2. Pointers to 2D arrays
    By bertazoid in forum C Programming
    Replies: 16
    Last Post: 02-26-2009, 04:46 PM
  3. Functions returning char arrays
    By turmoil in forum C Programming
    Replies: 3
    Last Post: 05-27-2003, 01:43 AM
  4. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM
  5. Hello all and pointers to multi-dimensional arrays
    By Mario in forum C++ Programming
    Replies: 16
    Last Post: 05-17-2002, 08:05 AM