Lab #5 FAQ

(Frequently Asked Questions)

Updated: Monday, August 4, at 9:15 am

Here are some Questions and Answers for Lab #5. I've noticed that some students are asking questions similar to these:

******************************
Q. ispunct() doesn't seem to return a 1 if the character is a punctuation. isspace() is working correctly.
A. It turns out that the ispunct doesn't return 1 (one) if it is a punctation. It returns non-zero. On my machine, ispunct() returns 64 if it is a punctuation, otherwise, it returns zero. According to the documentation on the ispunct() routine ( according to Microsoft's C++ compiler and Borlands C++ 3.1), ispunct() returns non-zero if the input is a punctuation character. Remember, 0 means false, anything else is true, so although ispunct() does indeed return true, it's not guaranteed to be 1. You should check the online help for your compiler to see what ispunct() returns. I have a feeling it is 0 if it is NOT a character, and non-zero (meaning anything but 0) if it is a character.
You shouldn't count on isspace() to work either. It may work by coincidence, but you would be better off not to assume that it is 1.


******************************
Q. In the handout you specify that we must use an array to keep track of how many occurences there are of each letter, instead of using 26 different named variables. In my program only 1 variable is ever needed to keep track of each letter's number of occurences because I employ a for loop which repeats over and over until each letter has been dealt with. The program works fine and produces the same results, and I don't have 26 variables floating around out there. Is that o.k.? The only array I use is for the string entered by the user in the begining.
A. Without seeing your code, I'm guessing that you are looping over the string 26 times; once for each character. Your program will run 26 times slower this way. That's the point of arrays: you only have to scan the string once. You will count each letter as you find it. Also, I *want* you to use an array. That's why I said don't use 26 named variables. The point of this assignment is to use arrays. I make stipulations like that so people don't avoid the issue.


******************************
Q. strupr() appears not to work in the Unix environment at school. I included the string.h file, but the compiler still says that strupr is undefined.
A. You are right. You cannot use the strupr() function for this assignment. I consulted the book I brought into class and it is not listed. I remember this problem. I've been dealing with this with porting to the Macintosh too. Just to ease your mind, it really is not necessary to use strupr() to do the assignment. There are many other ways (functions) to solve the problem.


******************************
Q. Is the "Total characters" line on the handout, which says 37, a typo?
A. That depends. I want the total number of letters, not counting spaces, punctuation, etc. I think I should word it:
   Total letters: 37
So, the typo is the word "characters", not the number 37. There are 46 characters, but only 37 are letters (a - z), and I want the count of letters only.


******************************
Q. Can arrays be considered value parameters if they are passed as constants? I would think they would be sinced they cannot be changed (just like value parameters), and have labelled them as such in my function declaration headers.
A. Yes, anything that can't be changed is considered a value parameter. You should consider them as inputs rather than outputs and list them as such.


*****************************
Q. Do you want the pseudo code to include passed parameters? ie:
//    Call doCalculations (inputString, indesArray, letterOccurrence,
//                        letterFrequency, totalLetters)
//    Call displayTable (indexArray, letterOccurrence, letterFrequency,
//                      totalLetters)

or do you want it with out parameters? ie:

//    Call doCalculations
//    Call displayTable
A. You should include the parameters. However, if you notice in the examples in the handout and also in the examples I posted, you don't use parentheses. That's because not all languages use parentheses to pass parameters. The examples show you that you can do it like this:
  Call SomeFunction with parameterA, parameterB returning Sum
This is just an example, but the idea is that you call a function WITH some parameters and the function is RETURNING some other value. The Module Definition and Activation section (page 7 in the handout) shows it best.


****************************************
Q. When I define:
  char str[100];
  cin.get(str, 99);
  int punct = ispunct (str);
I get the message: Cannot convert '*char' to 'int'. What does this mean and how could I fix it?

A. First of all, ispunct() is function that expects a single character as input. You are trying to pass a string (an array of characters, hence, several characters). You must pass each character, one at a time, to ispunct(). It doesn't make any sense to pass multiple characters to ispunct() and expect a yes or no answer. ispunct is a function that looks at the character you passed in and says "is this a punctuation character?" If so, return non-zero (true), if not, return 0, (false).


****************************************
Q. Why do I need to use an array? Can't I just loop over the input string 26 times and count the characters that way?
A. Yes and no. Of course, you could scan the string 26 times, once for each letter in the alphabet, printing out the count after each pass. However, that is brute-force and won't get you much credit on the assignment. The idea of using arrays is so that you scan the string only once. After the one and only scan, you have all of the counts for all of the 26 letters. The assignment (as is the case with all assignments) is not lengthy if you spend time to analyze the problem. There are many solutions to the assignment. Remember, scan the string once only.


****************************
Other hints
1. Read the handout completely and follow the sample. Some students lose points because they failed to following the instructions in the handout.
2. Analyze the problem. Don't write any C++ code until you've written pseudo-code and/or structure charts and/or flow diagrams. If you can't explain in English what the program is going to do, you're not ready to write C++ code.
3. Read up on the isXXXXX functions (isalpha, isalnum, isspace, ispunct, etc..) You'll save yourself a lot of effort (and get a better grade) than using the brute-force method.
4. "Functions are your Friend" should be a motto. Not only the functions you write, but the thousands of functions that are in the Standard C++ library for you to use. We've only talked about a dozen of them or so, but that is more than enough to complete this assignment in one page (not including comments and pseudo-code.)
5. Run it under UNIX. If you don't, many of you will most definitely lose a lot of points this time.


Back to Outline