CSE 9B
FAQ


Questions from E-mail

  1. What on Earth is a "FAQ" anyway?
PA#9:
  1. My strcat is giving me a problem like wrapping around on a second go around especially with long strings like Hello San Diego. Now I am looking for the last letter then adding the new word, so like a if statement to see if there is a null then a for loop?
  2. I don't understand at the bottom of page 25, is s1+1, s2+2 just moving the string to the next letter and running it through the function again. So for a true situation a return TRUE should be outside of the if else loop. I keep getting a FALSE return so I was wondering why?
PA#8:
  1. No questions!
PA#7:
  1. In section e) can I just use the *this to do the same job without using tmp?
  2. In Strg operator+(), do I need to use a tmp also?
  3. For all the extra code we have to use for our p7.C file, should we put it in p7.C file or put it in p7.h file?
  4. Do we need to comment the p7.h file and p7main.C file, or do we just put all our Pre and Post conditions in our p7.C file?
PA#6:
  1. No questions!
PA#5:
  1. How do I set up a tmp string to use after cin.getline(...) in Arithmetic::get_expr()?
  2. Do I set up the tmp string to do all the error testing?
  3. Do I just "tmp[] = num", using the private data member like the example you gave?
  4. In Arithmetic::set(void), am I suppose to assign the input to memory?
  5. Is that ok for this assignment to use strcpy(), and those others from <string.h> ?
  6. In get_expr(), how should I approach reading in my arithmetic expression? My loop can't read more than 1 digit in so: 3 + is fine, but 33 +, it thinks the second 3 is an operator or 1.23 where it thinks . is an operator. So I know it's not reading the whole string at once then deciding what the num is and what the operator is.
  7. In get_expr(), should I look at the white space in between the num and the operator, then set up a loop to continue reading the operand until a nondigit appears then go on to do a loop for the operator?
  8. What exactly in Arithmetic::divisor (void) is being returned?
  9. In get_expr (void) definition, how do I return an "op" value to main?
  10. On the bottom of page 12 of the Programming Assignment handout, what does it mean: "If the input is other than a negative real number, then a '?' is returned."
  11. How does my program know how to call calc.add () from main ()? I can't use (if op == '+') because op is a private member...
  12. Does the error message: "ERROR! Only real numbers for operand" on all error examples, have to come from main(), in the case '?'.
  13. Can the error message: "ERROR! Only real numbers for operand" be from inside get_expr() by setting an error flag then a cout statement?
  14. How do I clean up my get_expr() so it isn't a bunch of " != " to get the operand from buf into tmp?
  15. What happens to buf[i] when there is nothing in the buf[i], not even a space?
PA#4:
  1. In the function get_seats(), I have a tough time to do the part where I have to find if the seat is available, and the replace it. I have a whole mess of if else, and do while loop going but I can't get to work correctly. Mostly because the input is one # and one char. To check if there is an 'X' gets messy (all my if else statements). Any suggestions?
  2. Can you please explain to me where the true, false return to in main. I assume that it goes back to where sold_out() is called.
  3. I ran into problems putting variable answer in the do while loop or should I use the 'break' statement.
  4. If I enter both parameters wrong in get_seats(), will the error message for the "rows" come up or "A B C or D only " come up.
PA#3:
  1. For part a), should I put the cin in main() or in get_data()?
  2. For the print out Before sort: and After sort:, is it correct to put it in main() before and after the function call sort().
  3. For part d) are we sorting the ASCII values?
PA#2:
  1. I get that the "operation double % int is illegal". This is done when I am trying to convert the seconds to normal time. How do I fix this?
  2. Should Time::difference(Time, Time) have a t1 and t2 after Time respectively, so I can distinguish the two time parameter of seconds I am subtracting eg.Time::(Time t1, Time t2)
PA#1:
  1. In PA#1, shouldn't "minutes" and "hours" be included under the private member data in the Time class definition?
  2. Why do we have to instantiate three objects, time1, time2, and time3.
  3. Do I cout "08:09:10 and 09:11:13" from the fn Time::prt_time() ?
  4. Do I then cout the elapsed hour, min, sec from elapsed_time() or from Time::prt_time or print everything from main()?


What on Earth is a "FAQ" anyway?

FAQ stands for Frequently Asked Questions. It is here that you will find answers to questions asked via e-mail. Before e-mailing a question to the TA and proctors, you should check here to see if it has already been asked.

This FAQ will be updated approximately once a week.

In PA#1, shouldn't "minutes" and "hours" be included under the private member data in the Time class definition?

Essentially the hours, minutes, and seconds are converted to seconds and stored in the object as seconds. Therefore seconds is a private data member. There will be NO member data "hours, minutes, seconds" for the Time class.

Why do we have to instantiate three objects, time1, time2, and time3.

time1 will hold the 1st time, time2 will the 2nd time, and time3 will hold the elapsed time between time2 and time1.

e.g.

    time2     11:30:00       (2nd time)
  - time1     09:00:00       (1st time)
  --------------------
    time3     02:30:00       (holds elapsed time)
Do I cout "08:09:10 and 09:11:13" from the fn Time::prt_time() ?

Yes. In the sample output statement: "The elapsed time from 08:09:10 to 09:11:13 is 1 hour 2 minutes 3 seconds". Time::prt_time() essentially just prints out the times in the format of hr:min:sec from long seconds.

Do I then cout the elapsed hour, min, sec from elapsed_time() or from Time::prt_time or print everything from main()?

This is how I did it:

    In main()            "The elapsed time from" 
    Time::prt_time()     "08:09:10"
    main()               " to "
    Time::prt_time()     "09:11:13"
    main()               " is "
    Time::elapsed_time() " 1 hour 2 minutes 3 seconds"
Note: Page 2 of the Programming Assignment (PA) Handout "e)" states Time::prt_time() prints the formatted hr:min:sec. Page 3 of the PA Handout "g)" states Time::elapsed_time() prints the calculated elapsed time in hours, minutes and seconds.

I get that the "operation double % int is illegal". This is done when I am trying to convert the seconds to normal time. How do I fix this?

Well either:

1) Typecast the double.  
   e.g. double seconds;    "(int) seconds % SEC_PER_HR" 
   
   since modulus operator requires integers 

2) Do not use the "%", modulus operator.  Just subtract the number of hours 
   (in seconds from the total number of seconds).
   e.g.
   sec  = seconds;
   hr   = sec / SEC_PER_HR;
   sec -= hr * SEC_PER_HR;			// deduct the number of hours
   min  = sec / ...
Should Time::difference(Time, Time) have a t1 and t2 after Time respectively, so I can distinguish the two time parameter of seconds I am subtracting eg.Time::(Time t1, Time t2)

void Time::difference(Time, Time); is the function declaration. Notice that the formal parameter names are *NOT* needed in the function declaration.

The function definition will look something like: void Time::difference(Time t1, Time t2) { ... }

Notice that the formal parameter names are *NEEDED* in the function definition so that they can be used in the function body.

The function call in main() will look something like: diff.differenc(time1, time2);

For part a), should I put the cin in main() or in get_data()?

get_data()

3rd line states get_data() will allow the user to enter 4 characters delimited by spaces.

For the print out Before sort: and After sort:, is it correct to put it in main() before and after the function call sort().

Yes, that is what I did.

For part d) are we sorting the ASCII values?

Yes. When you test "if ( *p1 > *p2 )" that is testing if ( 'B' > 'A') which is really the ASCII values testing if ( 66 > 65 ).

In the function get_seats(), I have a tough time to do the part where I have to find if the seat is available, and the replace it. I have a whole mess of if else, and do while loop going but I can't get to work correctly. Mostly because the input is one # and one char. To check if there is an 'X' gets messy (all my if else statements). Any suggestions?

Altogether I have several ifs and one switch.

    a) Try it first with NO error input.  
    b) Then match the character input 'A" with the j index.
    c) Next with lowercase input.
    d) Next with not A B C D input
    e) Next with 'X' already
Ok there a several small problems here. Let's tackle each separately. First let's say our array is indexed as b[i][j]:
1) Input is entered as row number and seat character.
a) No problem about row number directly associating to the index i.
   Just one little if to test in range 1-7.  If true, then set errorflag.
b) The character may be 'a' or 'A'.  Well we can deal with either case
   now or later.  How about now.  Just convert the character variable, ch, to
   all uppercase.  Now we need to associate the character variable, ch, with 
   the index number, j.
   Hmm...   'A' only goes into column 0, 'B' into column 1, 'C' into column 2
   And the index, j, is either 0, 1, 2, or 3!
   So how about a switch statement?
      switch ( ch )
      {
        case 'A':
             j = 0; ...  }
   Or, you could map the character to a value that corresponds
   with the j value as follows:
      if ((ch - 'A') == j)
         ...
   So if ch == 'B', then (ch - 'A') will equal to 1, this is
   the value of j that allows a 'B' to be valid


2) The 2nd problem is if it's already 'X'ed out.  So we only want to
   check "if" there is no error input and the seat is 'X'ed out to 
   print an error message and set the error flag.  Else there is no
   error input and 'X' that seat out.
3) Then loop this whole thing again while an error exists.
Can you please explain to me where the true, false return to in main. I assume that it goes back to where sold_out() is called.

Yes. This is in the do loop in main().

I ran into problems putting variable answer in the do while loop or should I use the 'break' statement.

Remember the value returned is Boolean. Boolean sold_out( char b[][COL]); Therefore "Boolean answer;" will be the declaration. Do not need a break statement.

If I enter both parameters wrong in get_seats(), will the error message for the "rows" come up or "A B C or D only " come up.

I ran the program using the sample object code and it prints the error message for the row #. That means it gets error checked first as input. You can try this too.

         " cp ../public/assignments/p4  p4.test "
         " p4.test "
This last program is my executable code for you to test with all kinds of input and see if if works the same with your program.

How do I set up a tmp string to use after cin.getline(...) in Arithmetic::get_expr()?

In get_expr() you will have the code:

  char buf[SIZE];	// String to convert into arith. expr
  char tmp[SIZE];	// Tmp string to convert into each operand and operator
  cin.getline(buf, SIZE, '\n');		// Read entire input line

Do I set up the tmp string to do all the error testing?

You test "buf" string and if it is a real number, then assign into "tmp" string.

Do I just "tmp[] = num", using the private data member like the example you gave?

Since "tmp" is a character array, must convert into a floating point number by using "atof()" and assigning that converted value into private data "num".

In Arithmetic::set(void), am I suppose to assign the input to memory?

Yes.

Is that ok for this assignment to use strcpy(), and those others from <string.h> ?

Yup, use those string manipulating functions. This is the lab to use everything in that "Character Strings" handout.

In get_expr(), how should I approach reading in my arithmetic expression? My loop can't read more than 1 digit in so: 3 + is fine, but 33 +, it thinks the second 3 is an operator or 1.23 where it thinks . is an operator. So I know it's not reading the whole string at once then deciding what the num is and what the operator is.

No, This will not work. (See Q7)

In get_expr(), should I look at the white space in between the num and the operator, then set up a loop to continue reading the operand until a nondigit appears then go on to do a loop for the operator?

Yes. Looks good. Remember that operator is a single character so it will not need a loop. Code in these steps: First just a nondigit. Test for whole numbers. Second just a real numbers. Test for real numbers. Third a negative real numbers. Test for negative real numbers.

What exactly in Arithmetic::divisor (void) is being returned?

Just returns the private operand, num.

In get_expr (void) definition, how do I return an "op" value to main?

"return op; "

On the bottom of page 12 of the Programming Assignment handout, what does it mean: "If the input is other than a negative real number, then a '?' is returned."

(Note Q9). get_expr() returns a character. That character will be the "op", operator IF it is a correct input. IF it is *NOT* correct input the character returned is '?'. Note on page 13, in the box for the last case label. This will match the character value returned from get_expr().

How does my program know how to call calc.add () from main ()? I can't use (if op == '+') because op is a private member...

Notice on page 13 in the box of sample code. See how the case label in the switch statement determines which member function to call. For the '/' operator returned from get_expr() that the calc.div() function is called.

Does the error message: "ERROR! Only real numbers for operand" on all error examples, have to come from main(), in the case '?'.

Yes. Therefore only ONE "cout << "ERROR! Only real numbers for operand" is in main() in the case '?':

Can the error message: "ERROR! Only real numbers for operand" be from inside get_expr() by setting an error flag then a cout statement?

No. Therefore get_expr() returns a character to main(). That one character may be "op" (the operand) OR '?'. This '?' is returned from get_expr() on error input.

How do I clean up my get_expr() so it isn't a bunch of " != " to get the operand from buf into tmp?

A14: Rethink.

1st character: You are only testing a single character for a negative number. 
    Also may be a digit or period.  Therefore test for ONLY '-' digit '.'  
    If true then assign that single character to "tmp[0] = buf[i++];"
    If false " return '?'; "
2nd, 3rd, ... characters: Copy only the digits and periods from "buf" to "tmp", 
    If digits or periods assign that single character from buf to tmp 
    ELSE " return '?'; ".
Upon seeing a space in "buf", then "tmp" is finished. "tmp" is assigned the operand (-1.23). Append a '\0'. Now, convert "tmp" to a number, atof() Skip white space to now get the opeerator.

What happens to buf[i] when there is nothing in the buf[i], not even a space?

After you get the operand into tmp, then all you have left is an operator. Assign that single character to "op". "return op; " You don't even have to test what kind of operator it is because in main() the switch statement tests what is returned from get_expr(). Forget the rest in buf.

In section e) can I just use the *this to do the same job without using tmp?

Yes, need to "return *this;" AND need tmp to hold new bigger string.

In Strg operator+(), do I need to use a tmp also?

Yes, a tmp is important to use here.

For all the extra code we have to use for our p7.C file, should we put it in p7.C file or put it in p7.h file?

Do *NOT* edit p7.h (except update "const int SIZE = 78;"). Therefore *ONLY* type your code into p7.C

Do we need to comment the p7.h file and p7main.C file, or do we just put all our Pre and Post conditions in our p7.C file?

Yes, put pre and post conditions *ONLY* in p7.C just above the function header.

My strcat is giving me a problem like wrapping around on a second go around especially with long strings like Hello San Diego. Now I am looking for the last letter then adding the new word, so like a if statement to see if there is a null then a for loop?

Now for strcat. First you do a strcpy right? Then add (append/concatenate) to the end of the 1st string.

    e.g.  s1 = Santa
          s2 = Claus        s3 = s1 + s2;
The strcpy on page 25 looks like:
      for ( i = 0, p = rs.str ; i < len ; ++i )
        str[i] = p[i];
      str[i] = '\0';
      //  STRCAT part
So when strcpy finishes, "i" should be holding the value of "len" the length of Santa ( s1[5] ) and str[i] is now positioned to hold the '\0'. So this is where you want to start the appending. You really do NOT need to search for the NULL, you have it!

I don't understand at the bottom of page 25, is s1+1, s2+2 just moving the string to the next letter and running it through the function again. So for a true situation a return TRUE should be outside of the if else loop. I keep getting a FALSE return so I was wondering why?

Your strategy is right. It seems that your FALSE is getting returned FIRST all the time and the TRUE never is looked at AFTER. SOOO... the hint is WHEN are you testing and returning TRUE?


Home || Course Description || Course Outline || Lab Hours || FAQ


Webmaster: Leslie Franklin <franklin@cs.ucsd.edu>
Last Updated: June 3, 1997