EECS168 09:Lab4
| Navigation | 
|---|
| Home | 
| Information | 
| Classwork | 
Objectives
- More logic errors
 - Tracing for loops
 
Find and fix logic errors
In previous labs you learned how to deal with syntax errors. You also fixed one logic error. In this lab we will learn how to deal with some more common logic errors. Look at each of the following programs, try to compile and run them, find out what is wrong with them, and fix any logic errors.
The following code is supposed to ask if a user has any dependants, only if the answer is y, then the program should ask for the number of dependants. Copy the code into a file named dependants.cpp , compile and run the program, fix any logic errors.
- Note: Make sure you test your programs with all possible answers. In this case make sure you try both y and n.
 
#include <iostream>
using namespace std;
int main()
{
  char answer;
  int num_dependents;
  cout<< "Do you have any dependants? (y/n): ";
  cin>> answer;
  if (answer == 'n') 
    cout<< "This user has no dependants. "<< endl;
  else
    cout<< "This user has some dependants. "<< endl;
    cout<< "Please enter the number of dependants: ";
    cin>> num_dependents;
  return 0;
}
Copy the following code into a file named compare.cpp, compile it and run it with the values of 2 and -2 for x and y. Fix any logic errors.
#include <iostream>
using namespace std;
int main()
{
  int x, y;
  cout << "Enter the value of X: ";
  cin>> x;
  cout<< "Enter the value of Y: ";
  cin>> y;
  if (x > 0)
    if (y > 0)
      cout<< "X and Y are bigger than zero! \n";
  else
    cout<< "X is less than zero \n";
 
 return 0;
}
The following code with the for loop has no logic errors. Run the code and see how it works.
#include <iostream>
using namespace std;
int main()
{
 int counter;
 
 cout<< "Enter a number between 1 to 100 to count down from: ";
 cin>> counter;
 for (int i = counter; i > 0; i --)
   cout<< "The current value of counter is: "<< i << endl;
 
 return 0;
}
The following while loop is supposed to create the exact same output as the previous program with the for loop. Copy the following code into a file called forever.cpp, compile it, run it, find and fix any logic errors in it.
#include <iostream>
using namespace std;
int main()
{
  int counter = 0;
  
  cout<< "Enter a number between 1 to 100 to count down from: ";
  cin>> counter;
 
  while (counter > 0)
   cout<< "The count is: "<< counter << endl;
 return 0;
}
Copy the following code into a file called accounts.cpp, compile and run the code, find and fix any logic errors in the code.
#include <iostream>
using namespace std;
int main()
{
  const int N_ACCOUNTS = 5;
  double i;
  cout << "Enter the interest rate, as a percent: ";
  cin >> i;
  // Input the current balance of all 5 bank accounts
  cout << "\nYou have " << N_ACCOUNTS << " bank accounts.\n";
  cout << "Please enter the balance of each account.\n";
  int current_account_balance;
  int total_balance = 0;
  int total_interest = 0;
  for (int i=1; i <= N_ACCOUNTS; i++) {
	cout << "Enter the next balance: ";
	cin >> current_account_balance;
	total_balance += current_account_balance;
	// Compute the interest on this account and add it to the total
	double interest_on_this_account = current_account_balance * (i/100);
	total_interest += interest_on_this_account;
  }
  cout << "Your total balance is $" << total_balance << endl;
  cout << "You made $" << total_interest << " in interest this year.\n";
  return 0;
}
The following program is supposed to get the number of hit pins in the game of bowling from the user, if the number of hits equals to 10, print the message That's a strike :) you hit 10 pins!, in all the other cases, show the following message with the number of hit pins. For example: That's NOT a strike :( you hit 7 pins! . Copy the following code into a file named strike.cpp, compile the code, run it (make sure you test your program with several different numbers), find and fix the two logic errors in it.
#include <iostream>
using namespace std;
int main()
{
  int num_hits; // variable for holding the number of hit pins in bowling
  cout<< "Please enter the number of hit pins (a number between 0 to 10): ";
  cin>> num_hits;
  do {
      cout<< "Please enter a number between 0 to 10: ";
      cin>> num_hits;
     } while (num_hits >= 0 && num_hits <= 10);  // keep looping if the user enters a number out of the specified range
  if (num_hits = 10)
    cout<< "That's a strike :) you hit "<< num_hits <<" pins! \n";
  else 
    cout<< "That's NOT a strike :( you hit "<< num_hits << " pins! \n";
  return 0;
}
Trace For Loops
Trace the following for loop by predicting and writing the output result of the code on a piece of paper, hand in the paper to your TA.
for (int i = 1; i <= 5; i++)
  {
    for (int j = 9; j <= i; j--)
	cout<< i*j;
	cout<< ", \n";
  }
Lab Submission
Submit the following files following the submission criteria as the previous labs:
- dependants.cpp
 - compare.cpp
 - accounts.cpp
 - forever.cpp
 - strike.cpp