Computer Science

LAB 1.3 Running a Program with a Run Time Error

Exercise 1: Bring in program runprob.cpp from the Lab 1 folder.

Exercise 2: Compile the program. You should get no syntax errors.

Exercise 3: Run the program. You should now see the first of several run time

errors. There was no syntax or grammatical error in the program; however,

just like commanding someone to break a law of nature, the program is

asking the computer to break a law of math by dividing by zero. It cannot

be done. On some installations, you may see this as output that looks very

strange. Correct this program by having the code divide by 2 instead of 0.

Exercise 4: Re-compile and run the program. Type 9 when asked for input.

Record what is printed.

Lesson 1B 9

Exercise 5: Run the program using different values. Record the output.

Do you feel that you are getting valid output?

The code of runprob.cpp is as follows:

// This program will take a number and divide it by 2.

// Place your name here

#include <iostream>

using namespace std;

int main()

{

float number;

int divider;

divider = 0;

cout << “Hi there” << endl;

cout << “Please input a number and then hit return” << endl;

cin >> number;

number = number / divider;

cout << “Half of your number is ” << number << endl;

return 0;

}

L E S S O N 1 B

LAB 1.4 Working with Logic Errors

Exercise 1: Bring in program logicprob.cpp from the Lab 1 folder. The code

follows.

// This program takes two values from the user and then swaps them

// before printing the values. The user will be prompted to enter

// both numbers.

// Place your name here

#include <iostream>

using namespace std;

continues

10 LESSON SET 1 Introduction to Programming and the Translation Process

int main()

{

float firstNumber;

float secondNumber;

// Prompt user to enter the first number.

cout << “Enter the first number” << endl;

cout << “Then hit enter” << endl;

cin >> firstNumber;

// Prompt user to enter the second number.

cout << “Enter the second number” << endl;

cout << “Then hit enter” << endl;

cin >> secondNumber;

// Echo print the input.

cout << endl << “You input the numbers as ” << firstNumber

<< ” and ” << secondNumber << endl;

// Now we will swap the values.

firstNumber = secondNumber;

secondNumber = firstNumber;

// Output the values.

cout << “After swapping, the values of the two numbers are ”

<< firstNumber << ” and ” << secondNumber << endl;

return 0;

}

Exercise 2: Compile this program. You should get no syntax errors.

Exercise 3: Run the program. What is printed?

Exercise 4: This program has no syntax or run time errors, but it certainly has a

logic error. This logic error may not be easy to find. Most logic errors

create a challenge for the programmer. Your instructor may ask you not to

worry about finding and correcting the problem at this time.

Lesson 1B 11

LAB 1.5 Writing Your First Program (Optional)

Exercise 1: Develop a design that leads to an algorithm and a program that will

read in a number that represents the number of kilometers traveled. The

output will convert this number to miles. 1 kilometer = 0.621 miles. Call

this program kilotomiles.cpp.

Exercise 2: Compile the program. If you get compile errors, try to fix them and

re-compile until your program is free of syntax errors.

Exercise 3: Run the program. Is your output what you expect from the input

you gave? If not, try to find and correct the logic error and run the pro-

gram again. Continue this process until you have a program that produces

the correct result.

Order now and get 10% discount on all orders above $50 now!!The professional are ready and willing handle your assignment.

ORDER NOW »»