CS401 FAQs
Please check this list semi-regularly.  I may send you here if your question has already been answered.  Individual assignment questions will not show up here, but rather on those assignment pages.  The newest questions will be placed on top.

(the newest questions are at the top)

Project 6 Q&A

WELL, I HATE TO SEE PEOPLE SO FRUSTRATED AND STRUGGLING, SO THE ANSWER BELOW SHOWS A LITTLE MORE THAN I EVER INTENDED ON GIVING YOU.  PLEASE REALIZE THAT IF YOU HAVE DONE THINGS DIFFERENTLY, THIS DOESN'T MEAN YOU'RE WRONG!  YOU HAD A LOT OF FREEDOM IN THIS ASSIGNMENT, SO DON'T THINK FOR A SECOND THAT THIS IS THE ONLY WAY TO GO ABOUT IT, OR THAT IF YOU DID IT DIFFERENTLY YOU ARE SOMEHOW WRONG.  I JUST WANT YOU TO LEARN ABOUT CLASSES, AND WRESTLE WITH CREATING ONE YOURSELF.  

>Anyway, I am utterly stuck on this project.  I have gotten next to nothing finished because I simply can’t organize the
> information.  I don’t know what needs to be in the class
> and further I don’t have any idea what the public & private portions of
> the class should contain.  What’s more, I’m having a terrible time
> trying to figure out what belongs in the
> header file, what belongs in the cpp file and how those are integrated
> into a main…..Last but not least, I can’t figure out how this is going
> to unfold into anything where there
> could be more than one fighter….(sort of my experience with this class;
> flogging and kicking one’s self to death)

Ok, it seems like classes are bugging you completely.  Here's what to do to get somewhere.  It's not the most elegant approach in the world, but just take the Date class and change it (use the monolithic file to start with, you can break it up later).  Change the field names to the things you need to represent for a fighter (strength, punchPower, kickPower, and block).  Then write yourself accessors for each of these, a read method, and a set method.  These are all very short and just like the Date class, but they work on Fighters instead. 

Next, write a little block of code that uses the methods you've done.  I was hoping you guys would come up with this kind of thing, so realize, this is only a SUGGESTION.  Here is a small segment that reads in fighter information, then has f1 try to punch f2.  
 

  Fighter f1, f2;

  f1.read();
  f2.read();

  cout << f2.getName() << " strength = " << f2.getStrength() << endl;

  // let f1 punch f2
  if (f2.block()) 
    cout << f2.getName() << " blocked!\n";
  else {

    // get the power of the punch
    int pow = f1.getPunchPower();

    // tell f2 to take this damage
    f2.takeDamage(pow);

  }

  cout << f2.getName() << " strength = " << f2.getStrength() << endl;

  cout << f2.getName() << " is ";
  if (f2.dead()) 
    cout << "dead.\n";
  else
    cout << "still alive.\n";

  
Thus, you can see f2's strength before and after the punch (to know it worked).  If the block was successful, his strength will be the same, if
it wasn't successful, then his strength will be lower (by the amount of the punch power of f1).  It also sees if f2 died from the hit.

NOTE:  You could replace the two lines in the else with this, if you wanted (this is equivalent):

     f2.takeDamage(f1.getPunchPower());

You are probably getting caught up also on how strength, punch power, and blocking all work.  This is where you have to make something up and
go with it.   Ints are probably the easiest way to go and all your methods do simple things to these values.

>As I said, utterly lost and again frustrated…..  Perhaps it’s the fault of the text (hence, your reference to another
> book).  With the text’s high brow example of making a
> rectangle class, I just don’t see much correlation with the project that
> we’re working on now.  The level of complexity of the project that we’ve
> been assigned seems to be 10
> fold the complexity of the examples that we’ve been given.  Perhaps not
> but it sure seems like it.

I'm sorry it feels like that, trust me in that the frustration is from figuring out classes and not from this project.  Most of the methods you will write are one liners, with a few perhaps 4 or 5 lines.  The function that let's the two fighters go at it will be more lengthy, but still not that bad.

Good luck, I hope this helps.
 
 



> If a player takes a hit, how do we know if the person
> is taking a punch or a kick? Can you help me with the
> expression, thanks.

All that matters is that a player takes damage, it should not matter if it comes from a punch, kick, or an anvil falling from the sky.  Just have a method that handles when damage is done.  The paramter to this method would be the amount of damage done (which does depend on the other fighter, of course).  Here's the idea:

  Fighter 1 does a kick
  Fighter 2 tries to block
  If the block was not successful
     figure out fighter 1's kick strength
     tell fighter 2 to take that much damage

The details are up to you, for example, fighter 1 could choose to punch instead, or fighter 2 could choose to fight back instead of blocking, it's all up to you!  Usind random numbers to make these decisions is the best way to go, however, since this is a simulation and not a game.
 



> Just need you to clarify something for me.  Am I supposed to have the user 
> of the program press buttons to make the fighters punch, kick, block or are 
> we supposed to write a random function and let the user of the program sit 
> back and watch the fight on the screen?

I suggest you just go with a simulation.  While it would be cool to let two people decide on punches & kicks, we don't have the graphics and/or real time control to make this very realistic.  So, use random numbers to control these decisions, when to punch, when to kick, when to block, etc.  Just try to make it as realistic as possible within the framework we have set up.  Comment a lot to explain your choices.



> can you give any hints for doing project 6? please!

Try not to get overwhelmed, there really isn't anything horribly tricky about the assignment.  You have to make some decisions, though, so just start simple and make sure you understand the date class inside and out.  

Start by writing your class header, write a few simple methods, then write a main() that uses the class.  Once you get some simple things working, you can then add features one at a time.  I can't really give more than that because then I'd be making the decisions I want you to make!  



> I have a question about the testing file (stage 1) of
> project 6. After throwing a punch, you want to perform
> a kick and also print out the damage would be done to
> the opponent. So what you want is the separate damage
> from a punch and from a kick, or the total damage
> after a punch and a kick? If you look at the project
> page, in the stage 1, are number 3 and 4 related to
> each other? Do you want to update the amount of damage
> after performing a kick by adding the damage from a
> punch? Or just print the damage of each case
> separately? Please clarify, thanks.

I think it is more realistic to keep punches and kicks separate.  Your stage 1 is just there to test your methods and make sure your class works like you expect it to for the later stages, so do what you need to do to verify its correctness.  

I also don't think you should complicate matters by letting a fighter do more than one move per round, but if you want to, go for it.  


Exam 2 Q&A


> Recursion and the factorial function are two discreetly different items?
> Obviously, you would need a recursive function to derive factorials but is
> recursion distinctly different and can recursion be used without performing
> a factorial?

> If that's the case, why does the book muddy the waters by tying two fairly
> complicated items together and present it as one section with what seems
> like absolutely no discussion of recursion as a separate matter from
> factorials.

Well, you're not quite approaching it the right way.  Think of it this way:

Factorial is a problem.  It's defined as follows:

  n! = n * (n-1)!
  1! = 1

For example,

  4! = 4 * 3!
     = 4 * 3 * 2!
     = 4 * 3 * 2 * 1!
     = 4 * 3 * 2 * 1
     = 24

Now, if you want to implement factorial in C++ you have to decide HOW TO DO IT.  There are really only two ways to go about it:

  1.  use a loop (iterative)
  2.  use recursion

Thus, recursion is a WAY of solving a problem and factorial IS a problem.  Nose around in the book, you'll find the iterative solution to factorial in there as well.

The book didn't muddy up the waters, they had to pick some example to show how recursion worked!

Now, the test problem is a different problem and much simpler than factorial.  Here's how the CallMe example pans out:

  CallMe(8) = 3 * CallMe(6)
           = 3 * 3 * CallMe(4)
           = 3 * 3 * 3 * CallMe(2)
           = 3 * 3 * 3 * 3 * CallMe(0)
           = 3 * 3 * 3 * 3 * 1

CallMe(0) hits the base-case, which forces the recursion to stop.  We can look at this and see 81.  But, the computer has to "unwind" all the calls to CallMe(), so it is better to see it like this:

           = 3 * 3 * 3 * 3 * 1
           = 3 * 3 * 3 * 3
           = 3 * 3 * 9
           = 3 * 27
           = 81

Btw, you may want to review lab 4, it introduced recursion and was pretty gentle, in my opinion.


Project 5 Q&A


> 1. I don't know how to print to the screen the name of the
> player who is leading a particular category. I can get the
> leading numbers in those categories but I don't understand
> how to tie-in the leader's name to the number. Can you give
> me some guidance on this problem?

Sure... you'll need another variable to hold that player's name.  When you find a new leader in a category, update that variable at the same time.


> 2. Your sample screen display shows RBI as the 2nd item, but
> you ask for batting avg as the 2nd item in the instructions.
> Do you want RBI or Batting Average printed to the screen?

Good point, that was an oversight by me.  Let's use batting average.  I'll update the example on the web.


> I had a question about the vars used in project 5...it would be much easier
> to use the abbreviated baseball lingo that is provided in the project
> guidelines, but if we use that, then we have to add comments to describe
> what everything means, right?  So, would you recommend using AB, 2B, etc,
> or using names like, doubles, triples, etc?

Oh, I think the abbreviations are sufficient.  Be careful, though, some of them are not valid C++ identifiers.  Minimal commenting would be good, yes, for each abbreviation.


I was working on project 5 and it's in the very beginning stages but i was 
wondering if you explain what you mean by "ABs does not include walks and 
HBPs automatically.....". I was wondering if this is something to do with 
the problem i'm having so far in the program. My calculations for AVG and 
SLG are fine but OBP & OBS are wrong although the formula's seem to be 
right.

It just means that a players listed at-bats is not the true number of times that player has gone up the plate because walks and hbps are not included in that total.  That way your batting average doesn't suffer when you get a walk (because it's a good thing).  It was a clarification anyway, you can just use the formula exactly as it appears without understanding this.

My suggestion for testing is to use a very simple file (make one up and just put one line it, maybe the Babe example?).  It is always easiest to test on something small early on.



> I have a couple of questions about the output file:

> 1. Every time the user enters any output file name (exists or
> non-exists), the program either update the existing file or create a new
> file. So when will this if statement be activated:
>         if (!outfile)
>                 cout << "Error occurs while creating file";

It won't update the file, it will erase it and start a new one with the same name.  In other words, it "overwrites" the existing file. 

This error message would pop up if you were on a computer system that did not allow you to create new files, or at least you were in an area where you don't have write access.


> 2. Is there a way to warn the user, if he/she enters the file name that
> already exists, if he/she wants to overwrite the file. Where can I get
> information about how to do this? Does this project require this?

Sure, write a small function to try and open the file as an INPUT file, then if that fails, you know the file doesn't exist.  You don't have to do this in your program, but it would be cool.


Comparing the sample output file and the sample screen display, I’m seeing there is a mismatch. Based on the result in the output file, the SLG team leader is supposed to be Drew (0.688). Yet the sample screen display you have on the project 5 webpage shows that Edmonds (0.539) is the SLG team leader. I was just wondering if your plan was to put the wrong result because it’s just a sample display or there is a reason for selecting Edmonds over Drew? Please clarify.

No, it was a mistake.  I fixed the problem and the project page.  Good job.
 

Another question is: when 2 people have the same record high (e.g Drew and Pujols have 21 home runs), who should be a leader?

Just print either one as the leader.  In order to properly handle this (by printing all players with 21 home runs), we would need arrays, which we haven't covered in class yet.


> I was trying to open the sample output file for
> project 5 using the campus computer and was
> unsuccessful. I got this message " This file can not
> be opened directly using pro/Engineer. Please open it
> from the application." What does this mean and how can
> I open it? Please help, thanks.

The computer you are on has a file association for .dat files, it appears.  You need to right click on the file and do a "save link as" and specify a location.  From there you can open it up in notepad, or in a borland editor window, or even word.
 

Project 4 Q&A

(6/30)

> I was wondering if there is a command to clear the screen.  I have
> all my
> functions working properly and want to clean up the look of the
> program.
> With all the output writing it would be nice to start with a clean
> screen
> from time to time.  Please let me know.  Thanks.

answering his own question...

> I guess I should have checked the help first.  I found the command.
> It is in the <conio> library and looks like this

> #include <conio>
> void clrscr(void);

> When I placed it in the code I had to write it as

> clrscr();

> Thanks for the help.



> 1. I can't find any reference in my notes or on the web for
> "acceptableRange() function". Can you tell me where its at?

Here you go.  It was from 6/18.


> 2. I read FAQ about how main() in some programs are very
> long. Mind is outrageously short!!!
> (see attached c++ file)
> Am I doing something wrong?

Nope!  What you did is totally fine, you put your primary loop and switch a separate function. 


> 3. I would like some help on QUITTING the program when Q is
> entered. Should this be one of the switch-case options as I
> have it in my program? What command would I use to end the
> program and closed the MSDOS window. The text that was in my
> program before this veriosn I'm sending you was this "Hit
> the enter key to end the program", it worked, but I wanted
> to know if there was something that required less from the
> user (more automatic when Q is entered from the menu).

I'd suggest simplifying the condition on your loop.  The general layout could be something like this:

  do
      menu
      switch stmt
           handle cases
      print result if necessary
  while (user did not choose to quit)

Thus, when the quit choice is seleted, the loop (and program) will end.


> Let me just say, this project is way too hard for a
> introduction c++ class. I will be surprised if I get it to
> work and if 3 other people get it to work in the both class.

I'll have to respectfully disagree.  Intro to CS courses are like this all around the country, it's 4 credits and they were serious about the prior-programming background in the course description.

Take a look at an assignment from a previous semester:

It was the third assignment out of six, no less.  Now, if this was CS7 (intro to Pascal), I'd totally agree with you.  But this is an intro for CS majors with some programming experience.



> Based on the requirement for giving the user the option to reuse the
> inputs and the previous calculated results, I found it extremely long
> for the main function. Perhaps there is a better way to do it, I just
> don't see it right now.

> To make sure that my program meets the requirement, I drew a general
> structure for each case consider all the possible inputs and available
> results that can be reused if the user wants. I attached here the
> structure of Case 1 in main(). Please review it and let me know if I'm
> heading the right direction. If this is the right structure, then in my
> program, there are more than 3 pages of code just for Case 1. Does it
> sound right?

3 pages is definitely way too long for one case.  I could possibly see all of main() taking close to 3 pages, but still that seems a bit much.

Also, I want to make sure you know that this should be one of your very last modifications!  It's tricky and you should definitely get a core program working that always asks for new input before you work on this part. 

I certainly don't want people struggling this much, so I'm going to go ahead and suggest a direct approach.  Consider some function f with two inputs. Let's deal with each argument individually. Pseudo-code to recycle previous input would look something like this:
 

// obtain first input (x)
if (x holds a previous value)
    ask user if x should be reused
if (x needs a new value)
    read in a new value

// obtain second input (y)
if (y holds a previous value)
    ask user if y should be reused
if (y needs a new value)
    read in a new value

// now x and y hold the inputs, call the function
result = f(x,y) 

Notice that the second if statement (for each) can be true if (1) the user requested a new value, or (2) the user has not entered a value for that variable yet.  Thus, if the user does indeed want to give a new value for x, then somehow it needs to be "reset" so that the if statement immediately after it is true.  To do this in C++, you may need to set up some other boolean variables to keep track of the status of your variables, or something.

Also, I take back what I said about functions being used here.  It looks like you could write a nice function with a reference parameter that reads in a variable if the user says s/he wants to enter a new one. This would clean up your cases dramatically.



> The wet bulb thing is screwing me up and so is the manipulators.  what you
> said in class isnt working -- about setiosflags and setprecision.  are these
> functions?  or you just type them in or what?? I keep getting an error call
> to non function so...!!  also reusing the value already computed... thats
> another tricky part!  can you help at all??????

I can try.  Let me say, though, that the frustration you are feeling is a part of programming.  Sometimes things just don't work the way you want and you just have to keep fighting it. 

About this project... First, I strongly suggest not messing around with too much at once.  For example, don't worry about the floating point output until you have some more basic things working, like the wet bulb function.  Here are the rough steps you should take to convert the Wetbulb program into a function:

  1. In your project 4 window, write an outline (comment at top, #include lines, and an empty main().
  2. Write a prototype for your wet bulb function (figure out the inputs to the function, name, and return type).
  3. Write an empty function definition (this goes below main) for the wetbulb function.  The function definition header should match the prototype, of course, but you'll have to give the parameters names.  Use the same names you did in project 2 to make it easy (this corresponds to the 3 variables you read in).
  4. Cut and paste the main block from your project 2 (Wet-bulb) into your new project 4 editor window, but put it inside the empty function definition you just created.
  5. Now edit/delete out everything that should not be there... all the print and read statements (the function gets its input from the call now, so you don't read anything in from the keyboard in the function).
  6. Add the appropriate return statement at the bottom of the function so the function will return the answer.
  7. Now go to your empty main() and write a few test calls to the wet-bulb function and make sure it works.  Just call it with constants because you'll be deleting the test line anyway after you know it works.  You'll need to print the result, or something, to verify it. 
About the floating point issue.  I promise what I gave you in class does work.  Again, I urge you to not throw too many goals in the fire at once, they just get in the way of each other and you get frustrated.  I just ran this code segment in Borland and it worked exactly as it should:
 
#include <iostream>
#include <iomanip>
#include <string>
#include <conio>
using namespace std;

int main() {

   double x = 10000000.000455;
   double y = 0.04467;
   double z = 100.2;

   cout << setiosflags(ios::fixed) << setprecision(4);
   cout << x << endl;
   cout << y << endl;
   cout << z << endl << endl;
   cout << setw(20) << x + y << endl;
   cout << setw(20) << x - z * y << endl;
   cout << setw(20) << 0.000000000000000001 << endl;

   getch();
}

I urge you to copy and paste this into an empty Borland text-edit and run it.  Play around with it and observe your changes, it's the best way to learn.

Finally, about recycling the variables.  Yes, it is tricky and something you should not attempt until you've got everything else working smoothly.  If you don't get this ultimately to work, yes you'll lose points, but it is tons better than turning in a dog that attempts to do it, but screws up everything else.  I suggest saving your program under different names at different stages too.  As far as help goes on how to do it, I can't really tell you much... I want you to figure it out.  Just ask yourself how you can detect if a value has been seen before at each time input is obtained.  Do what you gotta to do detect this.  Also, start small:  get it to work for just one of the inputs, like RH.  Then expand your solution to the other variables.



I am working on project 4 right now. So far so good, except the program seems long. Is it supposed to be this long? So far, what I have in my program are 6 functions (5 for calculation purpose and 1 for the menu), and one "long" main function. The main function wasn't that long until I added the option of reusing the temperature value. I don't know if there is a way to create another function for the reuse option. I'm still thinking about this.

That's good.  Of course, there are lots of ways to write the program, but you should definitely have 5 functions, one for each of the weather formulas, and another to print the menu.  All of these are called from main(), which can get quite long given the amount of detail it has to handle.  In this case, it is totally fine for it to be long.  I can't think of a clean way to use functions to help you in the reuse of previous values.
 

One problem I ran into is: For the default case: if an invalid number is entered, then the error message appears. But along with it, the last result also appears before the menu. How do I fix this? 

Therefore, you don't want to print for every menu choice, you only want to print for some of them.  Thus, you need to "protect" the print statement, much like you did with printing in the hailstone program, with an appropriate if statement.
 

On the project page where you said "do not use any global variables for any reason", can you please explain this? 

Check out section 6.6 in your book.  In a nutshell, any variables you use in your program need to be declared either inside of main(), or inside of one of your functions.  Global means it is accessible from everywhere.  For example, your prototypes are in a global location, they can be called from anywhere in your whole program (main, functions, etc.).  The problem with global variables (prototypes are not variables, of course) is that it makes programs very difficult to follow (and modify, and debug, etc.), so we won't be using them in our programs.
 

General Questions


Will the final exam be comprehensive?
At least a week before each exam, I'll post an exam preview on the web giving you information about the exam (coverage, style of questions, length, etc.).  To answer the question, however, the exam will be partially comprehensive, but most of the exam (roughly 60%) will focus on material from the last third of the course.  To give you a rough overall picture:

  • exam 1 will cover chapters 1 - 4
  • exam 2 will cover chapters 5 - 8
  • exam 3 will cover 9, 10, and 13 (about 60%), as well as chapters 1-8 (about 40%).
Of course, material covered in labs, projects, and lectures are testable material as well (the adjusted lab schedule will be kept in mind).

Can you recommend any supplemental C++ books?
Just about any book that meets the ANSI/ISO standards could be useful to you in some way.  I've had good luck with books written by Herbert Schildt, he's very straightforward and uses lots of examples.  Of course, our textbook should be your primary source of information.  Only get a supplemental book if you have to time to read it on top of the reading from our book.

What labs can I use?  Do I need an account?
There are 8 campus labs for your use (here is a list of the labs), and you will need a university account if you do not have one.  You should check out the students technology page, as well as Information Technology's main page for further information.  You should be able to take your Pitt ID to any lab and have them activate your account. 

Can I use a different compiler?
You can use any C++ compiler you wish, but you should realize that we will use Borland C++ V5.02 in the labs, and will only provide compiler specific help for that environment. This means if you have non-Borland compiler problems, you'll be on your own to figure them out.  This relates to the next question...

Can I do the work at home?
Absolutely!  If you have a computer and access to Borland C++ (or your preferred compiler), go for it.

Where can I get Borland C++ V5.02?
It is available in every campus computing lab, or if you want to buy a copy for use at home, you can do that too.  I think the bookstore has it (at least they used to), or you might check with SLS (Software Licensing Services) to see if they have a special student price (please let me know if they do).

How do I turn in assignments?
To turn in an assignment, we need an executable file made for a PC running Windows, an electronic copy of all of your source code, and a printed copy of the source code.  The electronic files should be put on a floppy disk (I recommend a newly formatted disk).  More details are provided with project 1.

Can I attend either lecture?
Both lectures will be the same, but you can only attend the lecture you signed up for due to space.  If you absolutely cannot attend your section for some day and want to come to the other one a time or two, please check with me and if it looks like there will be room, I'll let you know.  As long as it happens rarely, I'm sure it will be ok.

Can I attend any of the labs?  Can I safely skip the labs?
No and no.  Each lab is run by a different TA, so you must always go during the same time.  Attendence in labs is required (although you may leave early if you finish your work).  We encourage you to take advantage of this time with the TAs, they are there to help, and will even work with you on non-lab related work if possible (e.g., projects). 
 

Last Updated: 6/28/01 by H. Chad Lane, hcl@cs.pitt.edu
© 2000-2001 Jim Skrentny, University of Wisconsin