Introduction

Some students (and even some instructors) ask us to provide more help on coding challenge activities. However, many instructors ask us NOT to give more help, since much learning comes from thinking through what might be wrong. We thus leave it to instructors to decide whether and how to provide additional help, commonly via a class discussion forum.

With that said, we check which coding challenge activities students struggle with most. In response, sometimes we update the activity’s instructions and/or the section itself. Other times, we think it’s best to add a few notes to this document, to nudge students in the right direction. Such activities include a link to the appropriate note on this page. (Because zyBooks are configurable, the activity numbers differ across zyBooks, so are not listed here). We hope you find these notes helpful.

General notes

Students commonly submit bug feedback for coding challenge activities saying:

  • “The compiler reports an error on a line that I can’t edit”. A compiler may not notice an error until a later line. Ex: Omitting a semicolon in C/C++/Java yields an error message for the NEXT line. If you get such an error message, check your code’s lines just before the reported error line.
  • “The code works fine in my IDE but fails in the zyBook”. The coding challenge activities use a variety of test cases to make sure your code is really correct, which is standard software testing practice. You probably didn’t test your code as thoroughly as the activity’s test cases. Look at what test case failed, and try to find the error in your code.

Top

Output sample solutions

Challenge activities don’t normally give away solutions. But since this CA is likely your first, we decided to err on the side of providing sample solutions. For other CAs, if you get stuck, try looking at the section for help, or make use of your class’ help resources.

C:

  • Output with varying whitespace:
  • Output with a variable:

C++:

  • Output with varying whitespace:
  • Output with a variable:

Java:

  • Output with varying whitespace:
  • Output with a variable:

Python 2:

  • Output with varying whitespace:
  • Output with a variable:

Python 3:

  • Output with varying whitespace:
  • Output with a variable:

Top

Outputting text

Many of our tools use yellow highlights to show how your output text differs.  means an extra or missing newline, and  means an extra or missing space.

Some students question whether emphasizing spaces/newlines is practical, but learning to be precise is a key part of programming. Plus, many real applications (for the web, smartphones, or laptops/desktops) have carefully formatted output, where every space/newline does matter.

Top

Sphere volume

Volume of sphere = (4.0 / 3.0) π r3. A common student question: How can I compute r3 without having learned how to compute exponents? Note that r3 can be easily calculated as r * r * r.
Top

Tree height

The student is given an equation tan(angleElevation) = treeHeight / shadowLength, but then asked to compute tree height. Some very basic algebraic equation manipulation is needed here. Think back to your algebra days, and you can do this one. A common mistake is to forget the tangent part.
Top

rand function: Seed and then get random numbers

  • (C/C++) Students often call srand() multiple times, but srand should only be called once (at the start), to “seed” the random number generator. It’s like taking someone to a hiking trail area, and saying “OK, start here at trail number 5”.
  • (C/C++) Some students struggle getting the math right: Is it % 9 or % 10? Should 1 be added or not? Tracing out the possibilities on paper might help.
  • (C/C++/Java) Students sometime say “If it’s random, how can you test for specific values?” Because it’s not really random, it’s pseudorandom. The numbers *seem* random, but every time the program is run, the random number generator generates the exact same seemingly-random sequence of numbers.

Testing with seedVal = 4 results in output 1 and 3. Testing with seedVal = 55 results in output 8 and 5.
Top

If-else statement: Fix errors

One of the most common errors among new programmers is using = instead of ==. New programmers should be extra vigilant to watch out for that error.
Top

Multiple if statements: Print car info

Getting if and if-else statements right can be a bit tricky for new programmers, but they eventually get the hang of it. Sometimes students think this activity is broken, but it’s not. Note that the topic is “multiple if statements”, not “if-else statements”. If you are using if-else, your code can only output one message, but for this activity, sometimes your code should output multiple messages.

Autograder output shows that testing with CarYear = 1990 expects two output statements: "Probably has seat belts." and "Probably has anti-lock breaks.".
Top

Bool in branching statements

Students sometimes get the logic wrong on this activity. Tracing one’s own code carefully can help. Watch out for using = instead of ==, or using & instead of &&. Make sure to choose the proper branching structure (do you want if-else or multiple ifs?).

isBallon = false and isRed = false yields and output of "Not a balloon". isBallon = true and isRed = false yields and output of "Balloon". isBalloon = false and isRed = true yields and output of "Not a balloon". isBalloon = true and isRed = true yields and output of "Red balloon".
Top

Printing array elements with a for loop

This activity enters the harder world of programming with arrays and loops, which can be quite challenging. A common error from students (and even professional programmers) is trying to access an element past the end of an array. Remember that for int myArray[N], the last element is myArray[N-1]. Students should be EXTRA careful to make sure their loop bounds does not cause access of an element above N-1 (or below 0).

A common mistake on this activity is to hardcode the bounds, using 4 instead of NUM_VALS. The activity’s test system will run the student’s code in a program having an array of just size 2, so if the student hardcoded courseGrades[3], the program will fail. This activity intentionally teaches students to NOT hardcode such values, but rather to use the better practice of declaring and using a constant variable like NUM_VALS.
Top

Printing vector elements with a for loop

(See above note for arrays; the same discussion applies for vectors).
Top

Finding values in arrays

This activity’s code initializes numMatches to -99. Students sometimes ask why. This was just to ensure the student assigns numMatches with 0 before the loop. The choice of -99 was arbitrary; by initializing to a value other than 0, the student’s loop will fail unless the student assigned numMatches with 0 before the loop.
Top

Finding values in vectors

(See above note for arrays; the same discussion applies for vectors).
Top

Find 2D array max and min

The solution requires students to initialize max_miles (or maxMiles) or and min_miles (or minMiles) before writing their nested for loop. Most students, and even some instructors forget this step.

Top