Uploaded by Fui

Lab 1 Directions (1)

CSCI 205 – Lab 1 – Fall 2023
C++ is one more than C?
Lab Objectives
• Creating and modifying C++ projects
• Working with console inputs and outputs in C++
• Creating and manipulating variables based on user input
• Write and modify conditional statements (if/else) in C++
• Write and modify for and while loops in C++
Frequently for lab or programming assignments, I provide files for you to include or to edit.
Most of the time, once you save the files, you can start working on the lab. However, today I ask
that you wait to begin working on the lab until everyone has the files saved. As we are not using
DarkGDK (something from CSCI 110), I need to step through creating a new project with you.
Today, please save the file Lab1.cpp. You will be editing it during lab today. An IDE (such as
MS Visual Studio or XCode) allows programmers to organize projects that use multiple files. A
user may create a project (even when you are only using one file) which keeps track of header
files, implementation files, and application files and their relationships (which depends on the
other, etc.) as well as parameters such as target platform, code for debugging, and many other
useful features. Getting the details right is very important.
During today’s lab, you will work through the questions listed below. Please create a word
document (or similar) and use it to record your answers to the listed questions. Your lab report
should be a big list of answers, the first one for question 1, the second for question 2, and so on
through to the end. When a question asks you to provide a screenshot, you may use any program
you wish to capture that portion of your screen, but I strongly recommend building your report as
you go. Later questions may request that you modify earlier code and if you don’t write your
report up until the end, you will have lost some of your work.
Due: Tuesday, September 5, 2023: 11:59 am
Task: Complete an existing C++ program to make it greet the user, then convert temperatures
for them.
1. Our TAs and I will explain how to make a new project with a single file, Lab1.cpp.
Please follow those directions. Now that the project is created, please build it and run it.
Every C++ program has exactly one method called “main” (no matter how many files it
has) and that method is what gets executed when the program is run. Provide a
screenshot of the output. Ignore line 11 and everything lines 16 and below, we will need
them later. What do you think lines 6, 7, and 9 do? Ask a TA or Dr. Meyer to confirm
whether or not your guess is correct and write that in your report. Which line of code
wrote to the console?
2. If a line of code has the string // in it, all text after the slashes is ignored by the compiler.
This is called a comment and they are used in complicated programs to document what a
piece of code does, or to activate and deactivate portions of a program. Please comment
out line 15 and uncomment the lines of code that follow “Q2” and then build and run the
program again. Provide a screenshot of the output. Which line of code got your input?
How did the code use your input in the output?
3. As you have discovered, the statement “cout” is short for “console out” and writes text
out to the console. Similarly, “cin” is short for “console in” and gets text in from the
console. Modify the code in the previous section to prompt the user for two names (yours
and your lab partners) and output “Hello world, it is [person1] and [person2].” Provide
screenshots of your code and the output of one run through the program.
4. Another way to make comments is to use /* and */. Unlike // which comments only until
the end of the line, everything between /* and */ gets ignored by the compiler. We will
use these when we wish to comment out a block of code instead of just one line.
Comment out the code for Q2 and what you wrote for Q3 and uncomment the block after
“Q4”, then run the code. What is this code supposed to do? What is wrong with it? Fix
it, then provide screenshots of your code and the output.
5. Sometimes it is very cold in Wisconsin. Your instructor is unhappy when the Celsius
temperature is below 0. Boolean variables are a special type of variable which holds
either “true” or “false”. Uncomment the code after “Q5” but do not comment out the Q4
code. What does the Q5 code do? Try it with a different value of cTemp. Why do you
think it displays the way it does? Please record your initial guess as to why it behaves
this way, but then ask a TA or Dr. Meyer to explain how the computer is internally
storing a Boolean.
6. An if statement is a block of code that tests to see if a Boolean variable (as in the if) or
Boolean statement (as in the else if) is true or false and then runs the appropriate
block of code. These statements can be chained together as you see here with as many
else ifs as you want (including just writing an if or just an if and an else).
Comment out the code in Q5 and uncomment the code after “Q6” (leaving Q4
uncommented) and run it. Try several different values of cTemp by changing the value
in cTemp. Provide screenshots of 3 different outputs.
7. Modify the code in Q4 and Q6 in two ways. First, ask the user for today’s temperature in
Celsius (instead of having it hard coded to a specific value) and use the code to convert it.
Second, decide with your lab partner what appropriate temperatures would be (in
Fahrenheit!) and update the if/else if statements to reflect your choices. Again,
provide screenshots of your code and the output.
8. Comment out the code you wrote for Q7 and uncomment the code following Q8. This
code runs a function called “convertCtoF”. We will get practice writing functions next
week, but this week we will be calling our function. C++ requires you to tell it about any
functions you’re going to write above main (see line 11), but functions are almost always
implemented below the main function (see lines 81-86). You give the function an input
by putting a double in the parentheses and the function returns a double. Unfortunately,
something is not working correctly in this function. What is it? Provide a screenshot of
the fixed function and its output.
9. C++ provides two good keywords to use to write loops: for and while. They work
slightly differently. Please comment out the code for everything in the previous
questions here, but do not delete it as you will want to use it as a reference.
a. A for loop is usually set up to execute a piece of code a certain number of times.
I have provided a sketch of how the syntax works for this in 9a. Please write code
that does the following:
1. Asks the user for the number of temperature values they wish to
convert from C to F.
2. Uses a for loop to run that many times, where each time through
the loop it asks them for the C value, calls the function we used in
#8, and outputs the conversion to the screen.
b. A while loop executes a piece of code over and over until a Boolean statement
is false. I have provided a sketch of how the syntax works for this in 9b. Please
write code that does the following:
1. Write a while loop that asks the user for a temperature value in
Celsius, uses the function from #8 to convert that to Fahrenheit,
and then outputs the conversion to the screen.
2. Then asks the user if they want to convert another temperature. If
they respond with ‘y’, the loop should run again. If they respond
‘n’, the loop should end and a “Goodbye” message should be
displayed. If they respond with a different character than ‘y’ or
‘n’, the program should tell them that the input is invalid and
prompt them again.
c. Please provide screenshots of your code for part (a) and the output from a sample
run through. Do the same with part (b) but make sure that your output
demonstrates that all the parts work.
Submit 2 things
1) Electronic version of your source code Lab1.cpp
2) Lab Report – as a PDF which contains 3 things:
• Cover Page that contains your name, your lab partner’s name, course, lab number, date,
and your honor code signature (an acknowledgement that this report is your own
individual work).
• Answers to the questions posed above with screenshots as requested in the prompts.
• Lab reflection which includes what parts of this lab you found easy, which were most
troublesome, which you would like more practice with, and your thoughts on what
worked best and what could be improved. In the future, reflections will also include
analysis on the concepts and algorithms, but not today.