computer programming c++
1
CSCE 1030 Lab 2 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 ([email protected]) 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. */
A. Fixing Compilation Errors
Copy and paste the following program 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. Finally, run the program and make sure it produces three lines of output to the screen.
/*
This is a block comment.
The block comment ends after this line.
/This is a single line comment. The program starts after this.
include <stream>
using namespace std
int main( )
}
out < "There are multiple syntax errors in this code./n";
out << "These need to be fixed before you submit it.\n';
count >> "Fixed by [ENTER YOUR EUID HERE].\n"
return 0;
}
2
Once you find and correct all the syntax 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):
There are multiple syntax errors in this code.
These need to be fixed before you submit it.
Fixed by pls0112.
Even though it may compile, if it does not print out all three 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.
B. Declaring, Initializing, and Printing Variables
The following program contains missing code. Specifically, you are to fill in the missing code as directed below:
• Declare variable var1 as an integer, var2 as a character, and var3 and var4 as floating-point numbers, but do not initialize these variables yet.
• Next, assign 40 as the value of var1.
• Then, assign var2 to be the uppercase first letter of your last name. Now, assign
var3 to be the floating-point number 4.6.
• Finally, seeing as the var4 contains the result of an arithmetic expression, print out the result of var4 to the terminal. Be sure to print a newline after the result.
3
Complete the missing code and save the file as Lab2B.cpp, making sure it compiles and check your result that is output to the screen, making sure the operation result is correct. Note that you will submit this file to Canvas.
C. Writing a Simple C++ Program
Write a small, but complete C++ program called Lab2C.cpp that calculates the volume of a sphere as follows:
a. Declare a floating-point constant named pi and initialize it to the value of 3.14159.
b. Prompt for and read in a floating-point number variable for the radius of the sphere in cm and store in the variable radius.
c. Compute the volume using the formula:
𝒗𝒐𝒍𝒖𝒎𝒆 = 𝟒
𝟑 ∗ 𝒑𝒊 ∗ 𝒓𝒂𝒅𝒊𝒖𝒔𝟑
and save the result in the variable named volume.
Use the C++ power function to compute the cube.
d. Finally, print out a statement that provides the user the calculated volume of the sphere in cubic cm.
For example, my output might look like this:
$ ./a.out
Enter the radius of the sphere (in cm): 1
The volume of the sphere is:4.18879 cubic cm
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 Lab2 dropbox.
You should submit the following files:
• Lab2A.cpp
• Lab2B.cpp
• Lab2C.cpp
Ask your TA to check your results before submission. The above two files MUST be submitted to Canvas by the end of your lab section.
4
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.