Question:

Having trouble writing a program in C .

by  |  earlier

0 LIKES UnLike

I'm beginning to learn C lately, and I now realize how frustrating it can be.

I am writing a program that calculates the wages of an employee if they were to receive 1 penny for the first day of work, 2 pennies for their second day, and then double that for the third day, etc, etc.

I wrote the beginning statements, asking the user to input the amount of days to be calculated, and now I have to write a looping statement. But I'm stuck at what to put as a mathematical expression that'll calculate the amount of pennies.

Also, the amounts must be displayed in dollar amount, in a chart like...

Day# Wages

--------------------------------------...

1 0.01

2 0.02

3 0.04

4 0.08

I realize that there's an exponential pattern within the penny amount, and I tried everything I could to get that working...I was then thinking about maybe putting in a nested loop to configure the dollar amount...I don't even know.

Can anyone help?

I'm just a beginner, so go easy on me.

 Tags:

   Report

3 ANSWERS


  1. What you actually want is a recursive formula.

    On the first day .01 * 2**0

    On the next day it's the first day plus .01* 2**1

    Next is the first two plus .01* 2** 2

    Each day's amount is .01 * 2 raised to the day-1.  And you have to sum up for all the days.

    So you need a recursive calc function, that takes an int with the number of days as the input and returns the salary as a double.

    Something like this:

    double calcSalary(int days)

    {

    double temp;



    if (days <= 1)

    return .01;

    else

    {

    days--;

    temp = .01 * pow(2, days);

    return (temp + calcSalary(days));

    }

    }

    Your main asks the user for the number of days, and calls the calculation function with that as the input.

    If you need to generate a chart, then you'll be writing the whole thing in a "while" loop, with something that stops the process of your asking for number of days.  Like, user input is -1, then you're done.

    Give it a try and email me if you have trouble.  However, I'm serious about "give it a try".


  2. Here i'll help you to get started with the math algrothem. The best way to learn to program is to figure it out so ia m not going to give you all the code.

    Day     No.of Pennies Given    

            1.                         1                        

            2.                 1 x 2 =  2                                    

            3.                  2 x 2 =  4                

            4.                   4 x 2 =  8              

            5.        8 x 2 = 16

    That should help you get started. Remember that Ur first case is a special case. i would do it out side of the loop first, that way you don't do that if statement every time you go in that loop.

    If you are still stuck let us know.

  3. Get numDays from user (or some other source)

    daysWage = 0.01

    output heading and 1st day's wage

    for i 2->numDays {

    daysWage *= 2

    output nth day's wage}

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.