What is wrong with this program?

This is a discussion on What is wrong with this program? within the C Programming forums, part of the General Programming Boards category; I've been working on this program for a while now and it isnt doing anything and there arent any errors ...

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

    What is wrong with this program?

    I've been working on this program for a while now and it isnt doing anything and there arent any errors at all. i've run a debugger but nothing happened. HELP!!

    Code:
    #include <stdio.h>
    #include<math.h>
    #include<stdlib.h>
    
    #define NROWS 12
    #define NCOLS 24
    #define INSTALLATION_COST 2000
    
    /* Declare global variables */
    float panelEfficiency = 0.18, inverterEfficiency = 0.90;
    
    /* Function parameters */
    void get_inputs(float *ptr_to_dailyEnergy, float *ptr_to_panelCost, float *ptr_to_batteryCost, float *ptr_to_inverterCost);
    float fillInInsolation(float insolation_Matrix[NROWS][NCOLS], float *ptr_to_row);
    float calculate_insolHourMax(float insolation_Matrix[NROWS][NCOLS], float insolHourMax);
    
    void get_inputs(float *ptr_to_dailyEnergy, float *ptr_to_panelCost, float *ptr_to_batteryCost, float *ptr_to_inverterCost)
    {
         float dailyEnergy, panelCost, batteryCost, inverterCost;
    
         while(dailyEnergy = 0 || panelCost >= 0 || batteryCost >= 0 || inverterCost >= 0)
         {
                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", dailyEnergy);
                *ptr_to_dailyEnergy = dailyEnergy;
                printf("Please enter the cost for solar panels, ($/m^2): \n");
                scanf("%f", panelCost);
                *ptr_to_panelCost = panelCost;
                printf("Please enter the cost for one battery, ($): \n");
                scanf("%f", batteryCost);
                *ptr_to_batteryCost = batteryCost;
                printf("Please enter the cost for one inverter, ($): \n");
                scanf("%f", inverterCost);
                *ptr_to_inverterCost = inverterCost;
                
                if(dailyEnergy < 0 || panelCost < 0 || batteryCost < 0 || inverterCost < 0)
                {
                       printf("Your numbers must all be positive\n");
                }
         }
         return;
    }
    
    float fillInInsolation(float insolation_Matrix[NROWS][NCOLS], float *ptr_to_row)
    {
          float insolationConstant[NROWS] = {0.142, 0.207, 0.298, 0.389, 0.456, 0.483, 0.467, 0.410, 0.326, 0.234, 0.158, 0.124};
          float insolDaySum = 0, insolDayMin = 100000000000000000000.0;
          int i = *ptr_to_row, j;
          
          for(j = 0; j < NCOLS; i++)
          {
                if((j > 6) && (j < 18))
                {
                    *(ptr_to_row + j) = 2.0 * insolationConstant[i] * cos (0.2618 * (j - 12.0));
                }
                else
                {
                    *(ptr_to_row + j) = 0;
                }
                insolDaySum += *(ptr_to_row + j);
          }
          
          if(insolDaySum < insolDayMin)
          {
                insolDayMin = insolDaySum;
          }
          return insolDayMin;
    }
          
    float calculate_insolHourMax(float insolation_Matrix[NROWS][NCOLS], float insolHourMax)
    {
          int i, j;
          insolHourMax = 0;
          
          for(i = 0; i < NROWS; i++)      
          {
                for (j = 0; j < NCOLS; j ++)
                {
                    if(insolation_Matrix[i][j] > insolHourMax)
                    {
                           insolHourMax = insolation_Matrix[i][j];
                    }
                }
          }
          return insolHourMax;
    }
    
    int main()
    {
          int numBatteries, numInverters, quit = 0;
          float ptr_to_dailyEnergy, ptr_to_panelCost, ptr_to_batteryCost, ptr_to_inverterCost,
                ptr_to_row, panelArea, batteryRating = 4.8, inverterRating = 5.0,
                insolDayMin, maxHourPanelOutput, insolHourMax, totalCost,
                houseEnergyREQ, panelCost, batteryCost, inverterCost;
          
          float insolation_Matrix[NROWS][NCOLS];
          
          while(quit != 1)
          {
                get_inputs(&ptr_to_dailyEnergy, &ptr_to_panelCost, &ptr_to_batteryCost, &ptr_to_inverterCost);
                
                /* Call upon function fillInInsolation */
                fillInInsolation(insolation_Matrix, &ptr_to_row);
                
                /* Call upon function calculate_insolHourMax */
                calculate_insolHourMax(insolation_Matrix, insolHourMax);
                
                /* Give meaningful names to pointer variables */
                houseEnergyREQ = ptr_to_dailyEnergy;
                panelCost = ptr_to_panelCost;
                batteryCost = ptr_to_batteryCost;
                inverterCost = ptr_to_inverterCost;
                
                /* Equation to calculate panelArea */
                panelArea = houseEnergyREQ/(insolDayMin*panelEfficiency*inverterEfficiency);
                printf("panelArea = %f\n", panelArea);
                
                /* Equation to calculate maxHourPanelOutput */
                maxHourPanelOutput = insolHourMax*panelArea*panelEfficiency;
                printf("maxHourPanelOutput = %f\n", maxHourPanelOutput); 
                
                /* Equation to calculate numbatteries */
                numBatteries = ceil(houseEnergyREQ/(inverterEfficiency*batteryRating));
                printf("numBatteries = %d\n", numBatteries);
                
                /* Equation to calculate numInverters */
                numInverters = ceil(maxHourPanelOutput/(inverterEfficiency*inverterRating));
                printf("numInverters = %d\n", numInverters);
                
                /* 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? (hit 1 to quit) \n");            
                scanf("%d", &quit);
          }
    
          system("PAUSE");
    
    }

  2. #2
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Everything is wrong. (In fact i didn't check, i don't like to look at pages of code asked with an unspecific question).

    When you say "i've run a debugger but nothing happened", what do you mean ? If your program doesn't run properly, then the debugger will help you find what's wrong, soon or later.

    And if you just don't know how to use the debugger (i'm a Visual Studio guy, can only help you with them), well, you might want to look for information on the net or ask a question here, someone might answer you.

    Knowing how to use a debugger is really helpful. It's a powerful tool.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Calm down and start making some sense so we can help you.
    What compiler are you using?
    What is the warning level set to?
    What errors or warnings do you get when compiling?
    Have you read the FAQ?

    Code:
         while(dailyEnergy = 0 || panelCost >= 0 || batteryCost >= 0 || inverterCost >= 0)
    That should be: dailyEnergy == 0

    You also don't need an explicit return statement at the end of a void function.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
     while(dailyEnergy = 0 || panelCost >= 0 || batteryCost >= 0 || inverterCost >= 0)
    Values not intialized

    Code:
    scanf("&#37;f", &dailyEnergy);
    scanf("%f", &panelCost);
    ..
    ..
    ..
    Need an &

    Code:
    insolDayMin = 100000000000000000000.0
    This is really a very huge number to be sorted

    I would prefer to use double instead if float. Since you are working with the some more precise values.

    Code:
    system("PAUSE");
    use getchar not system.

    And there are lot more error, i dint look at the logic of your code. Can you please tell us what are u trying to do. Be more specific to the problem which you have and post the just the small pices if code where u think might go wrong.

    Posting the whole code wouldnt help either us or you unless asked for it.

    ssharish

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    > I've been working on this program for a while now
    How much of it did you try to write before running it for the first time? All of it?

    Read this - A development process
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maze Program - What am I doing wrong?
    By Gipionocheiyort in forum C++ Programming
    Replies: 20
    Last Post: 08-02-2007, 02:31 PM
  2. Replies: 5
    Last Post: 01-13-2007, 02:14 AM
  3. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  4. Whats wrong with this program - Output of a series
    By duffmckagan in forum C Programming
    Replies: 2
    Last Post: 07-26-2006, 10:57 AM
  5. Whats wrong with my program?
    By Ruflano in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2002, 05:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21