Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C Programming, User input Multiplication to calculate KWHrs

Options
  • 04-11-2013 9:24pm
    #1
    Registered Users Posts: 68 ✭✭


    I need help because for the life of me I cannot figure this out,

    Basically I need to end up with C programming for this pseudocode

    "input number of rooms:

    for each room in house

    input wattage of lights:
    input number of hours use/day (average)
    calculate kWHrs for room per year
    sumkWHrs_lights

    input number of TV/Computers

    for each TV/computer/gaming device in house

    input wattage
    input number of hours/day (average)
    calculate kWHrs for year
    sumkWHrs_TV_Computers


    Total_kWHrs = sum kWHrs_lights + sum kWHrs_TV_Computers etc etc..


    I've been trying to multiply out the first part and I cannot get it to work any way at all.
    Here's My Attempt



    {int NumRooms = 0;
    int counter;
    float RoomWatts, LightHours, LightKWhrs=0;
    float SumHours=0, SumWatts=0;
    printf ("Enter number of rooms in your house: ");
    scanf ("%d", &NumRooms);


    counter = 1;
    while (counter <= NumRooms)
    {
    printf ("Enter Total Light Watts and The Hours Use of The Lights Per Day in Room %d :", counter);
    scanf ("%f %f", &RoomWatts, &LightHours);
    LightKWhrs= ((SumWatts)*(SumHours)) +((RoomWatts)*(LightHours));
    counter++;
    }

    printf("Your total Kilowatt Hours consumed by lights is %f kWhrs \n\n", LightKWhrs);

    }


Comments

  • Closed Accounts Posts: 183 ✭✭Scuba_Scoper


    matt.finn wrote: »
    float RoomWatts, LightHours, LightKWhrs=0;

    Hint: Three variables I do declare, only 1 has value

    Hint2: take a tutorial on debugging and inspecting values.


  • Registered Users Posts: 68 ✭✭matt.finn


    Cheers, Ill try that now.

    Think I have a new way figured to do it using functions anyway.


  • Closed Accounts Posts: 183 ✭✭Scuba_Scoper


    matt.finn wrote: »
    Cheers, Ill try that now.

    Think I have a new way figured to do it using functions anyway.

    good man - keep at it... the only way in the beginning is to keep trying until you get it.. then after 20 or 30 programs you realise that design is where you need to take your time with - everything else flows from that. Top Down step wise refinement... ahhh the memories.


  • Registered Users Posts: 68 ✭✭matt.finn


    How would I code so that each input is multiplied and adds to the next

    I want to add up all the kwhrs usage in the house?

    I just can't seem to figure it out and it's driving me towards insanity!!


  • Registered Users Posts: 6,111 ✭✭✭Talisman


    You start with an input variable numRooms and a subTotal (initialised to 0.0).
    int numRooms;
    float subTotal = 0.0f;
    
    for (iteration=1; iteration <= numRooms; iteration++) {
      subTotal += getKWHrs(iteration);
    }
    

    The getKWHrs function would return the total for the room and it gets added to the subTotal.

    You can build similar functionality into the code of the function to process each appliance - if you store the appliance types in an array then you iterate through the array prompting for a value for each and add it to the sub total for that room.


  • Advertisement
  • Registered Users Posts: 68 ✭✭matt.finn


    Ah cheers, You have no idea how much that helps me now!

    Appreciate it!


  • Registered Users Posts: 68 ✭✭matt.finn


    Anyone able to figure problems with this? After the 1st run through the loop it stops?
    I need to add the figures for each room to get LightKWhrs and I need to do this for every electric appliance in my house by monday so help needed asap!!


    if(choice==2) //Choice is = Bottom_Up_Approach
    {
    int NumRooms = 0;
    int counter=0;
    float RoomWatts=0.0;
    float LightHours=0.0;
    float LightKWhrs=0.0;
    //float SumHours=1.0;
    //float SumWatts=2.0;
    printf ("Enter number of rooms in your house: ");
    scanf_s ("%d", &NumRooms);
    if (NumRooms <=0)
    {
    printf ("This is impossible, Value must be >=0 \n");
    return;
    }
    for(counter=0;counter<=NumRooms;counter++)
    {
    printf ("Enter Total Light Watts and Hours Usage Of Lights in Room %d :",(counter+1));
    scanf_s("%.2f %.2f ", &RoomWatts, &LightHours);

    if (RoomWatts <0.0)
    {
    printf ("This is impossible, Value must be >=0 \n");
    return;
    }

    if (LightHours<0.0)
    {
    printf ("This is impossible, Value must be >=0 \n");
    return;
    }
    LightKWhrs= LightKWhrs+(RoomWatts*LightHours);
    printf("test...\n");
    }

    printf("Your total Kilowatt Hours consumed by lights is %f kWhrs \n\n", LightKWhrs);
    }

    }


  • Registered Users Posts: 6,111 ✭✭✭Talisman


    scanf_s("%.2f %.2f ", &RoomWatts, &LightHours);
    

    %.2f is for output : It displays 2 decimal places

    For input you use %f. It doesn't matter whether the user enters a value with a decimal point or not, the input will be treated as a float.

    Also you have a bug in your for loop, it will loop once more than required:
    for(counter=0;counter<=NumRooms;counter++)
    

    Either change the counter to initially be 1 or change the condition to be less than NumRooms.

    Another thing you should do is specify to the compiler that the initial values of the variables are of type float - put f immediately after the declared value. If you don't put the f, then the compiler may assume that the value is a double and add extra code to cast the value to a float at runtime.
    float RoomWatts=0.0f;
    float LightHours=0.0f;
    float LightKWhrs=0.0f;
    


  • Registered Users Posts: 68 ✭✭matt.finn


    I think I have it got now, Got a decent amount of help off a classmate today.


Advertisement