theGrayFox
theGrayFox

Reputation: 931

Basic C programming help

Hey, I need help writing a simple program. I want to be able to demonstrate the use of integers and the remainders using a modulus. I'm stuck at how I should calculate the information. Any help would be much appreciated, but here's the general idea. The program encompasses the following:

1 week = 40 hours ($200 per week)
1 day = 7 hours   ($45 per day)
                  ($2 per hour)

Sample run:

Enter the total hours:59 (59 is just an example.)
You have:
         1week
         2day(s)
         5hr(s)
Payment: $300.00

Here's what I've come up with so far...

int main(){

   int totalWeekHrs = 0,
   totalDayHrs = 0,
   totalWorkedHrs = 0;

   float totalPayment = 0,
     payPerWeek = 0,
     payPerDay = 0,
     PayPerHr = 0;

   // Input process   
   printf("Enter the total hours :");
   scanf("%i",&totalWeekHrs,&totalDayHrs,&totalWorkedHrs);

  // Calculative process

system("pause");    
}

Upvotes: 0

Views: 247

Answers (2)

Baltasarq
Baltasarq

Reputation: 12212

I won't answer your question with code, since it seems homework. You have also stopped when you should really start coding!

The problem is another skin for the "change return" typical question. This is a eager algorithm which tries to resolve the objective with the biggest step it can take.

You have to have two paralell vectors:

{ 40,   7, 1 }  // hours worked (week, day, hour)
{ 200, 45, 2 }  // salary for each item above.

Notice that the first vector is sorted and that each position matches the same position in the second. The objective is 59 in your example.

For each position in the first vector, you have to divide by your objective, and annotate the remaining.

For example, for the first position, the biggest amount is 1, for the second, 2...

First step:
( 59 / 40 ) == 1
( 59 % 40 ) == 19

Second step:
( 19 / 7 ) == 2
( 19 % 7 ) == 5

Third step:
( 5 / 1 ) == 5
( 5 % 1 ) == 0

You'll finally get a vector as long as the first one with the results:

{ 1, 2, 5 } // one week, two days and five hours.

In order to show the results, just run over the vector, and multiply each position by the same position in the second vector:

1 week(s)  ( 1 * 200 )
2 day(s)   ( 2 * 45 )
5 hour(s)  ( 5 * 2 )

( 1 * 200 ) + ( 2 * 45 ) + ( 5 * 2 ) = 300

This way you get the result you need.

Upvotes: 0

s1n
s1n

Reputation: 1454

This smells like homework so I will explain how modulus works.

The modulus operator, %, performs integer division and returns the remainder. For example:

int foo = 6;
int bar = 4;
int remainder = foo % bar;

In that example, remainder will be set to 2.

You can read more about the modulus operator here.

Upvotes: 1

Related Questions