Csce1030lab2 Fall2019

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

CSCE 1030 Lab 02 Fall 2019

General Guidelines: (for ALL of your programming assignments and labs)


• Use meaningful variable names.
• Use appropriate indentation.
• Use comments, especially for the header, variables, and blocks of code. Please
make sure that your submitted code has comments as points may be deducted for
a lack of comments.
Example Header:
/* Author: Jane Doe (Jane.Doe@my.unt.edu)
Date:
Instructor:
Description: A small description in your own words
that describe what the program does. Any
additional flags needed to allow the
program to compile should also be placed
here.
*/

In this course you will learn to write C++ programs to solve different problems. From
time-to-time this task may become frustrating and difficult. However, with patience and
practice, you will gain more experience and things will get easier. The difficulty in using
a computer to solve problems comes from the fact that you need to somehow ask a
computer to do things for you. Imagine, sitting in front of the computer and asking it to
add two numbers, as in “Hey, you add 2 and 4 and tell me what the answer is.” But we
do not normally talk directly to a computer (at least, not yet). Instead, we communicate
through an operating system (OS). The communication with the OS is done via the
program that we write. Our program is a set of instructions that a computer will
follow. These instructions are not in English, but we understand them very well,
because they are written in a high-level language called C++. These instructions will, at
some point, get to the machine much differently – in machine language. The machine
language is a low-level language.
In general, you will go through several steps before you can get an output from your
program:
1) First you will use an editor such as nano or vim to type your program;
2) then you will use a compiler (in our case, a C++ compiler) to compile your program
and to get an executable file; and
3) at last, you will run the executable file to obtain the output.

1
CSCE 1030 Lab 02 Fall 2019

The most important part of a programmer's job is solving the problem first. It is much
harder to solve a problem than to translate your ideas to a specific programming
language. Thus, you should first think about a method and develop an algorithm to
solve the problem. An algorithm is a sequence of precise instructions, which results in a
solution. The key concept here is precision. If your algorithm has ambiguity in it, then
you will not get the correct, or expected, result.
1. Fixing Compilation Errors
Download and open the file errorfile.cpp provided to you. Copy and paste the
contents into a file called Lab2A.cpp, and then compile the program. You will get
some syntax errors. Pay attention to the type of syntax error and note the line
number in which the error has occurred. Fix each error one-by-one, starting with
the first error, and then recompile until there are no more errors in the program.
After fixing the syntax errors, run the program by running the a.out file and make
sure it produces the five lines of output (shown below) to the screen. If you are
getting a different output, then there is a logical error in the code, fix that as well,
recompile and run the program again to get the correct output.
Once you find and correct all the errors (you should get a new file called a.out
when you compile), add your EUID as indicated and then compile and execute your
program. Your output should look something like the following (except with your
EUID instead of pls0112):
This code contains multiple syntax errors
and 1 logical error.
These need to be fixed before you submit it.
data=15
Fixed by pls0112.
Even though it may compile, if it does not print out all the five lines, then it is not
working completely as expected. Go back as needed to figure out what error still
exists if it doesn’t work exactly as specified. Feel free to reference the Lab 01
programs you worked with last week or the class lecture notes to see what a good
program looks like. Note that you will submit this file to Canvas.

2
CSCE 1030 Lab 02 Fall 2019

2. Writing a Simple C++ Program


Write a small, but complete C++ program called Lab2B.cpp that calculates the
area of the following shape:

The shape is a rectangle with a triangle on top.


Do the following steps:
a) Declare five floating-point (float or double) variables named:
• length for strong the length of the rectangle
• width for storing the width of the rectangle
• height for storing the height of the triangle
• base for storing the base of the triangle
• area for storing the area of the shape
b) Using cin and cout, prompt the user for and read in a floating-point number for
variable length.
c) Using cin and cout, prompt the user for and read in a floating-point number for
variable width.
d) Using cin and cout, prompt the user for and read in a floating-point number for
variable height.
e) Note that base and width are equal and so assign width to base.
𝑏𝑎𝑠𝑒 = 𝑤𝑖𝑑𝑡ℎ
f) Compute the area of the shape and assign it to area.
𝑎𝑟𝑒𝑎 = (𝑤𝑖𝑑𝑡ℎ ∗ 𝑙𝑒𝑛𝑔𝑡ℎ) + (0.5 ∗ 𝑏𝑎𝑠𝑒 ∗ ℎ𝑒𝑖𝑔ℎ𝑡)
g) Using cout, display the computed area on the screen using a suitable message
for the user.
h) A sample output is given to you to help you write the code.

3
CSCE 1030 Lab 02 Fall 2019

SAMPLE OUTPUT
The data entered by the user is in bold green.
$ ./a.out
Enter the length of the rectangle (in inches): 2.5
Enter the width of the rectangle (in inches): 3
Enter the height of the triangle (in inches): 2
The area of the shape is:10.5 square inches
A floating-point number is simply a real number with a decimal point. C++ supports
two very common floating-point numbers: float and double. Instead of int (for
integer), you would use either float or double as the data type when declaring
your variable so that your result can (and most likely will) have a decimal point as
seen in the example above. A data type of float would be used for numbers
where the precision is not very large (e.g., dollar amounts with cents is usually done
using a float variable) whereas double would be used for numbers requiring
greater precision, as might be seen in NASA calculations.
Before writing the code, you may want to write out the algorithm, or steps, on paper
of how you propose to solve this problem. Feel free to reference the Lab 01
programs you worked with last week or the class lecture notes to see how you might
accomplish this. Note that you will submit this file to Canvas.

Now that you have completed this lab, it’s time to turn in your results. Once you've
moved the files to your windows machine (using WinSCP), you may use the browser to
submit them to Canvas for the Lab 02 dropbox.
You should submit the following files:
• Lab2A.cpp
• Lab2B.cpp
Ask your TA to check your results before submission.
Now that you've finished the lab, use any additional time to practice writing simple
programs out of the textbook, lectures, or even ones you come up with on your own to
gain some more experience.

You might also like