CSE100 Principles of Programming with C++
Lab 8 :: 5 pts
1 Instructions
You may work in pairs (that is, as a group of two) with a partner on this lab project if you wish or you may work
alone. If you work with a partner, only submit one lab project with both of your names in the source code file to
Blackboard for grading; you will each earn the same number of points. What to hand in, and by when, is discussed in
Section 5; read it now.
2 Lab Objectives
After completing this assignment the student should be able to:
●
●
●
●
●
Complete all of the objectives of the previous lab projects.
Effectively use if, if-else, and if-elseif-... statements to alter flow of control.
Write programs involving multiple functions.
Effectively use the pass-by-value parameter passing technique.
Write programs involving multiple source code files.
3 Prelab Exercises
3.1 Prelab Exercise 3: Read Software Requirements
Read §4 of this lab project document, which describes what the lab project program will do, i.e., the software requirements. Then come back here and complete the remaining prelab exercises.
3.2 Prelab Exercise 1: Create Code::Blocks Project and Add Template Files to the Project
Create a new Code::Blocks project named Lab08. Remove the automatically-created main.cpp file from the project. Download the Lab08.zip archive, extract the files contained within it, and add Lab08.cpp, Kalk.cpp, and Kalk.hpp t your project.
Edit the header comment blocks of each file, see §4.1, so the AUTHOR section contains your name and your email ad dress, and—if you work with a partner—your partner's name and email address. Include the date/time of your lab and
your lab TA in the comment block.
3.3 Prelab Exercise 4: Study the Software Design
The program shall consist of three source code files,
Lab08.cpp – Contains the definition of main(). main() is very simple in this program, it simply calls kalk_init() to initialize the interpreter (by initializing the global variables allocated for the registers to 0) and then calls kalk_interpret() to
run the interpreter.
Kalk.hpp – Contains function prototypes for the public functions kalk_init() and kalk_interpret() which are defined in
Kalk.cpp. Kalk.hpp must be included in Lab08.cpp by writing a preprocessor directive.
Kalk.cpp – Contains functions which implement the interpreter. kalk_init() should be called to initialize the interpreter before starrting it. Then, kalk_interpret() is called to run the interpreter. This file contains functions that implement each of
the 16 operators. For example, the atob operator is implemented by a function named atob().
And here is the pseudocode which you are to implement. Each "module" corresponds to a .cpp source code file.
module Lab08
function main() → int
call kalk_init()
call kalk_interpret()
return 0
end function main
end module Lab08
Burger :: Computer Science & Engineering :: Arizona State University
Page 1
CSE100 Principles of Programming with C++
Lab 8 :: 5 pts
module Kalk
-- In C++, a variable is a global variable if it is defined outside the functions of the file. The scope of a local variable is
-- from the location where it is defined to the end of the source code file, so these three global variables are accessible
-- from every function in this file.
define global double variable g_reg_a -- this is the a register
define global double variable g_reg_b -- this is the b register
define global double variable g_reg_c -- this is the c register
function kalk_add() -- implements the add operator
g_reg_a ← g_reg_a + g_reg_b
end function kalk_add
function kalk_atob() -- implements the atob operator
g_reg_b ← g_reg_a
end function kalk_atob
function kalk_atoc() -- implements the atoc operator
g_reg_c ← g_reg_a
end function kalk_atoc
function kalk_btoa() -- implements the btoa operator
g_reg_a ← g_reg_b
end function kalk_btoa
function kalk_btoc() -- implements the btoc operator
g_reg_c ← g_reg_b
end function kalk_btoc
function kalk_ctoa() -- implements the ctoa operator
g_reg_a ← g_reg_c
end function kalk_ctoa
function kalk_ctob() -- implements the ctob operator
g_reg_b ← g_reg_c
end function kalk_ctob
function kalk_display() -- implements the d operator
define char variable reg
reg ← read char from keybd -- read the register name to display from the user
if reg is 'a' then
send g_reg_a, endl to cout
else if reg is 'b' then
send g_reg_b, endl to cout
else if reg is 'c' then
send g_reg_c, endl to cout
else
send "Invalid register operand: ", reg, endl to cout
end if
end function kalk_display
function kalk_div() -- implements the / operator
g_reg_a ← g_reg_a / g_reg_b
end function kalk_div
function kalk_init() -- initializes the global variables for the registers to 0's
g_reg_a ← 0
g_reg_b ← 0
g_reg_c ← 0
end function kalk_init
Burger :: Computer Science & Engineering :: Arizona State University
Page 2
CSE100 Principles of Programming with C++
Lab 8 :: 5 pts
function kalk_interpret()
define a string object named oper
oper ← kalk_read_operator("? ")
while oper ≠≠ "q" do
if oper is "+" then
kalk_add()
else if oper is "atob" then
kalk_atob()
else if oper is "atoc" then
kalk_atoc()
else if oper is "btoa" then
kalk_btoa()
else if oper is "btoc" then
kalk_btoc()
else if oper is "ctoa" then
kalk_ctoa()
else if oper is "ctob" then
kalk_ctob()
else if oper is "d" then
kalk_display()
else if oper is "/" then
kalk_div()
else if oper is "l" then -- this is an ell not a one
kalk_load()
else if oper is "*" then
kalk_mul()
else if oper is "^" then
kalk_power()
else if oper is "&" then
kalk_sqrt()
else if oper is "-" then
kalk_sub()
else
send "Invalid operator: ", oper, endl to cout
end if
oper ← kalk_read_operator("? ")
end while
end function kalk_interpret
function kalk_load()
define char variable reg
reg ← read char from keybd
if reg is 'a' then
g_reg_a ← read real number from keybd
else if reg is 'b' then
g_reg_b ← read real number from keybd
else if reg is 'c' then
g_reg_c ← read real number from keybd
else
send "Invalid register operand: ", reg, endl to cout
end if
end function kalk_load
Burger :: Computer Science & Engineering :: Arizona State University
Page 3
CSE100 Principles of Programming with C++
Lab 8 :: 5 pts
function kalk_mul()
g_reg_a ← g_reg_a * g_reg_b
end function kalk_mul
function kalk_power()
g_reg_a ← g_reg_a g_reg_b
end function kalk_power
function kalk_read_operator(prompt : string)
send prompt to cout
define string object named oper
oper ← read string from keybd
return oper
end function kalk_read_operator
function kalk_sqrt()
g_reg_a ← √g_reg_a
end function kalk_sqrt
function kalk_sub()
g_reg_a ← g_reg_a - g_reg_b
end function kalk_sub
end module Kalk
4 Lab Exercise: Software Requirements
A computer program which interactively reads programming language statements, executes the statements, and displays
results is called an interpreter. This lab project is to implement a small interpreter for a simple calculator-like language
named Kalk (which I made up). Kalk is a language for performing arithmetic operations on real numbers. It is imple mented as a rudimentary computing machine with three storage registers named a, b, and c, each of which can store a
double value. The language consists of 16 operators, which are symbols that represent an operation to be performed on
values in the register. The operators are,
atob
atoc
btoa
btoc
ctoa
ctob
d reg
l reg value
q
+
*
/
^
&
q
Copy the contents of register a to register b
Copy the contents of register a to register c
Copy the contents of register b to register a
Copy the contents of register b to register c
Copy the contents of register c to register a
Copy the contents of register c to register b
Display the contents of register reg (a, b, or c) on the output window
Load register reg (a, b, or c) with the numerical value value (note: l is an "ell" character, not a "one")
Quit the interpreter
a←a+b
a←a-b
a←a*b
a←a/b
a←a^b
a ← sqrt(a)
Quit the program
Note that Kalk is case-sensitive (atob is an operator, ATOB is not) and that an operator cannot contain spaces. Here is
a sample Kalk program which computes the distance between the points (12, 5) and (7, 3) in the Cartesian plane. The
italicized comments are not part of the program but are provided to help you understand the program.
l a 12
l b 7
Loads coord x1 = 12 into a
Loads coord x2 = 7 into b
Burger :: Computer Science & Engineering :: Arizona State University
Page 4
CSE100 Principles of Programming with C++
l b 2
^
atoc
l a 5
l b 3
l b 2
^
ctob
+
&
d a
q
Lab 8 :: 5 pts
a ← x1 - x2 = 12 - 7 = 5
b←2
a ← (x1 - x2)2 = 52 = 25
c ← (x1 - x2)2 = 25
Loads coord y1 = 5 into a
Loads coord y2 = 3 into b
a ← y1 - y2 = 2
b←2
a ← (y1 - y2)2 = 22 = 4
b ← (x1 - x2)2 = 25
a ← (y1 - y2)2 + (x1 - x2)2 = 4 + 25 = 29
a ← sqrt((y1 - y2)2 + (x1 - x2)2) = sqrt(29) = 5.385164807
Displays the distance between (12, 5) and (7, 3) which is 5.385164807
Sample Output (User Input is in Bold)
? l a 12
? l b 7
? ? l b 2
? ^
? atoc
? l a 5
? l b 3
? ? l b 2
? ^
? ctob
? +
? &
? d a
5.38516
? q
This is our first project involving more than one source code file. Lab08.cpp contains the main() function, which simply
calls kalk_ init() and then kalk_interpret() to start the interpreter. kalk_interpret() returns to main() when the q (quit)
operator is entered. Kalk.hpp is a header file that contains function prototypes for the public functions of Kalk.cpp,
void kalk_init();
void kalk_interpret();
Lab08.cpp #includes Kalk.hpp so the compiler will see the prototypes for kalk_init() and kalk_interpret() before it sees the
lines of code that call the two functions. Remember, the prototype gives the compiler all of the information it needs to
know about a function so that it can generate the code to call the function. The three things are: (1) the name of the
function; (2) the data type of the return value; and (3) the data types of the function parameters.
Each of the functions in Kalk.cpp is quite short with the exception of kalk_display(), kalk_interpret(), and kalk_load()
because those three function use if-elseif-... statements.
4.1 Additional Programming Requirements
1. Edit the header comment block at the top of each source code file so it contains the correct author and lab information.
2. Format your code neatly and use proper indentation as shown in the example programs of the textbook and the
source code the instructor posts online.
Burger :: Computer Science & Engineering :: Arizona State University
Page 5
CSE100 Principles of Programming with C++
Lab 8 :: 5 pts
3. Testing: When you run your program, test it using the Kalk program shown above that calculates the distance
between two points. The grading rubric section below has a few more test cases.
5 What to Submit for Grading and by When
When you are finished with the program, create a new empty folder named lab08-asurite, where asurite is your ASU
RITE user id, e.g., mine is kburger2. Copy all three of the source code files to this folder; there should only be three files in
this folder: Lab08.cpp, Kalk.hpp, and Kalk.cpp. Then, compress this folder creating a zip archive named lab08auriteid.zip. If you worked with a partner then name the zip archive lab08-auriteid1-asurite2.zip where asurite1 and
asurite2 are the user names of both partners. Then upload the .zip archive file to Blackboard using the lab submission link
by the deadline. If your program does not compile or run correctly, upload what you have completed for grading anyway
(you will generally receive some partial credit for effort). The deadline for the complete lab project is 4:00am Sat 1 Apr.
Consult the online syllabus for the late and academic integrity policies.
6 Grading Rubric
1. Testing
Test the student's program by running it against these three test cases, where user input is in bold.
Test Case 1
? l x 5
Invalid register operand: x
? Invalid operator: 5
try to load register x with the constant 5
since register x does not exist, should print an error message
Test Case 2
? l a 5
? d x
Invalid register operand: x
load register a with the constant 4
try to display the contents of register x
since register x does not exist, should print an error message
Test Case 3
? l a 1
? l b 2
? +
? d a
3
? l b 5
? ? d a
-2
? l b 4
? *
? d a
-8
? l b 6
? /
? d a
-1.3333
? atob
? l a 3.14159
? &
? ^
? d a
0.466194
load register a with 1
load register b with 2
a=a+b=1+2=3
displays a, which is 3
load register b with 5
a = a - b = 3 - 5 = -2
displays a, which is -2
load register b with 4
a = a * b = -2 * 4 = -8
displays a, which is -8
load register b with 6
a = a / b = -8 / 6 = -1.33333
displays a, which is -1.33333
copy a to b, b = -1.33333
load register a with 3.14159
a = sqrt(a) = sqrt(3.14159) = 1.77245
a = a ^ b = 1.77245 ^ -1.33333 = 0.466194
displays a, which is 0.466194
Burger :: Computer Science & Engineering :: Arizona State University
Page 6
CSE100 Principles of Programming with C++
Lab 8 :: 5 pts
2. Lab Program (0 to 5 pts)
Build the student's program. If it does not compile due to syntax errors, then assign partial credit for effort as explained
below. If the program does build, then run the program using the three provided test cases. Then assign pts per this
rubric.
a.
b.
c.
d.
e.
Program does not compile due to syntax errors; little of the required code was implemented: 1 pt
Program does not compile due to syntax errors; most of the required code was implemented: 2 pts
Program compiles; most of the required code was implemented; it crashes or works very poorly: 3 pts
Program compiles; it passes some of the test case inputs but fails on some as well: 4 pts
Program compiles; it passes all test cases: 5 pts
3.
a.
b.
c.
d.
Deadline was 4:00am Sat 1 Apr
Assign 20% bonus calculated on the earned pts for a submission prior to 4:00am Thu 30 Mar.
Assign 10% bonus calculated on the earned pts for a submission between 4:00am Thu 30 Mar and 3:59am Fri 31 Mar.
Deduct 0.5 pt for a submission between 4:00am Sat 1 Apr and 4:00am Sun 2 Apr.
Deduct 1 pt for a submission after 4:00am Sun 2 Apr.
Burger :: Computer Science & Engineering :: Arizona State University
Page 7
© Copyright 2026 Paperzz