C++ Very quick question

911paramedic

Diamond Member
Jan 7, 2002
9,448
1
76
I was asked to do a trace table and tell if the algorithm was valid or not, and the instructor never really said what would make it invalid.

I did the trace table, came up with the numbers, then I actually copied the program from the books example to check my results. they were the same, but I don't know if they are VALID. (This is from week 1 class)

Program: for calculating an employees wages for the week.

Input: employeeId, payRate, hoursWorked
Output: pay

Very simple. but the identifiers are floats, and no rounding off has been added or set width, so the output is a raw float.

My Questions:
a)No pause at the end, you never see the pay.
b)Numbers, because they are floats, drop trailing zeros, so 500.50 becomes 500.5.
c)Same situation but it carried the number to three places 500.875.

It never said you would SEE the pay, it would just calculate it, so I think that if fine.
I don't think those are valid outputs for this type of program, even though we didn't cover integers and floats until last week. Am I wrong in assuming this?

TIA

P.S. The main part of the exercise was writing the trace tables, but I have "sleepy" notes and it's scribbled "are these valid algorithms" near the bottom.

It's a five hour class with three straight hours of lecture, two in the lab, ending at 10PM, and I am taking 22 units. So, I was a bit tired when he wrote this on the board at the end of lab and not even sure if they go with this program. :confused:



 

911paramedic

Diamond Member
Jan 7, 2002
9,448
1
76
No love...makes me sad.

I'm not asking for my homework to be completed for me, just clarification of "valid". (That's even if I need it, damn sleepy notes.)

My homework is finished, all nine pages, two chapters of questions, and five written/compiled programs.

Thanks if you can clear that "valid" part up for me. Personally, I would say the program needs to be written for only two decimal places and round up, even though three of the six employees got correct output.

I guess I have to wait until tomorrow night and try to catch the instructor before class and ask him.

EDIT: Yes, I'm inpatient because I need to run off for todays classes and I need to print the homework out at school. (my printer died, newegg has my new one on the way)
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
No love...makes me sad. EDIT: Yes, I'm inpatient because I need to run off for todays classes and I need to print the homework out at school.

If you'd posted this yesterday more people would have seen it and possibly helped.

I was asked to do a trace table and tell if the algorithm was valid or not, and the instructor never really said what would make it invalid.

Which of these is a valid algorithm for calculating the square of x?

a) sq = x + x ;
b) sq = x * x ;

One is valid, one is invalid.

That's about as much as anyone can say from the problem description you've given.

The algorithm for calculating the pay may or may not be correct (we aren't given your code), and making the result display properly may or may not be part of the program requirements.
 

smack Down

Diamond Member
Sep 10, 2005
4,507
0
0
Don't add a pause just tell the professor to run it from the command line.

If you wish to show a fixed number of digits use the setprecision and fixed modifiers to cout. This will ensure you print out xxx.00 ever time.

cout << fixed;
cout << setprecision (2)

I would say any program that runs is valid
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
I would say any program that runs is valid

I've had many programs run that weren't valid, but as Dave said, there is no accepted definition for what "valid" is. A program is valid if it executes, processes input and produces correct output according to its logic. But what constitutes correct output is domain-specific.
 

smack Down

Diamond Member
Sep 10, 2005
4,507
0
0
Originally posted by: Markbnj
I would say any program that runs is valid

I've had many programs run that weren't valid, but as Dave said, there is no accepted definition for what "valid" is. A program is valid if it executes, processes input and produces correct output according to its logic. But what constitutes correct output is domain-specific.

You defined a correct program which is a sub set of a valid program.
 

911paramedic

Diamond Member
Jan 7, 2002
9,448
1
76
What a collection of responses, lol. OK, this is the code for those that want to see it:

//******************************************************************
// Payroll program
// This program computes each employee's wages and
// the total company payroll
//******************************************************************
#include <iostream>

using namespace std;

void CalcPay( float, float, float& );

const float MAX_HOURS = 40.0; // Maximum normal work hours
const float OVERTIME = 1.5; // Overtime pay rate factor


int main()
{
float payRate; // Employee's pay rate
float hours; // Hours worked
float wages; // Wages earned
int empNum; // Employee ID number


cout << "Enter employee number: (0 to exit) ";
cin >> empNum;
cout << "Enter pay rate: ";
cin >> payRate;
cout << "Enter hours worked: ";
cin >> hours;

CalcPay(payRate, hours, wages);

cout << empNum << " " << payRate << " " << hours << " " << wages<< endl;


system ("pause");
return 0;
}

//******************************************************************

void CalcPay( /* in */ float payRate, // Employee's pay rate
/* in */ float hours, // Hours worked
/* out */ float& wages ) // Wages earned


{
if (hours > MAX_HOURS) // Is there overtime?
wages = (MAX_HOURS * payRate) + // Yes
(hours - MAX_HOURS) * payRate * OVERTIME;
else
wages = hours * payRate; // No
}

As you can see, I did add a pause so I could see the actual output, even though it's not required according to the comments.

It does what it says, but not in a format that would work well on a spreadsheet or for pay purposes. "Here's your $500.875 sir." Something about that just doesn't work in my book.
 

smack Down

Diamond Member
Sep 10, 2005
4,507
0
0
Is there any reason you decided to pass a reference to the return value instead of making CalcPay just return wages. Doing it your way makes the code harder to read and you won't need the in/out comments. I'm guessing you where told to do it that way and in an OOP that syntax is just about never used. For readability you should at least pass a pointer to wages in so that it is clear to a person reading the main function that they are looking at a pass by reference instead of pass by value.

<-- Disgruntled maintenance programmer
 

911paramedic

Diamond Member
Jan 7, 2002
9,448
1
76
Putting the smack Down on code.

I copied it right out of the book, sans comments. It is from "Programming and problem solving with C++ by nell dale, chip weems, and mark headington"
 

stash

Diamond Member
Jun 22, 2000
5,468
0
0
This is kind of useless: cout << "Enter employee number: (0 to exit) ";

since there is no test of the input. Entering zero doesn't alter the progression of the program.
 

911paramedic

Diamond Member
Jan 7, 2002
9,448
1
76
I have no clue why you guys are picking apart the code, it has nothing to do with my question and was taken directly from the textbook. The only thing I did was enter it so I could check my trace tables.

Wowzers.

Thanks, but I'll just catch the instructor before class tonight and make any needed changes to my homework then.
 

911paramedic

Diamond Member
Jan 7, 2002
9,448
1
76
Originally posted by: stash
This is kind of useless: cout << "Enter employee number: (0 to exit) ";

since there is no test of the input. Entering zero doesn't alter the progression of the program.

Entering zero will end the program though, correct? So it does serve a purpose...I think.

Like I said, this was week 1 homework, and tonight is only my third class. (five hour, once a week class)
 

stash

Diamond Member
Jun 22, 2000
5,468
0
0
Entering zero will end the program though, correct?
How? There's no check of the employee ID.

Did you run the program?

I'm not sure what questions you have, since it looks like the ones in your original post were answered.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Originally posted by: smack Down
Originally posted by: Markbnj
I would say any program that runs is valid

I've had many programs run that weren't valid, but as Dave said, there is no accepted definition for what "valid" is. A program is valid if it executes, processes input and produces correct output according to its logic. But what constitutes correct output is domain-specific.

You defined a correct program which is a sub set of a valid program.

That's as good a definition of valid as any, I guess.
 

911paramedic

Diamond Member
Jan 7, 2002
9,448
1
76
Answer is in: Spoke with the instructor last night and he said they were valid. I said "But a bank would refuse a check issued for $500.6" and he said, "Well, of course there would be a trailing zero added. :confused:

stash: Of course I ran the program, how do you think I pasted the contents here? But, you are correct, entering zero just assigns that as the employee number.

I didn't write the program, I just copied it so I could check my trace tables.

Thanks for all the help people, even though I kind of disagree with the final answer. For a payroll program you need to do a bit more formatting, IMH(noob C++'er)O.