Starting from:
$35

$29

Starting Computing Homework 3


All 3 components (Cloud9 workspace, Moodle CodeRunner attempts, and zip file) must be completed and submitted by Saturday, February 2nd, 6:00 pm for your homework to receive points.



                    1. Objectives


    • Understanding C++ data types and functions (parameter passing and return values)

    • Writing and testing C++ functions

        ◦ Understand problem description

        ◦ Design your function:

            ▪ come up with a step by step algorithm,

            ▪ convert the algorithm to pseudocode

            ▪ imagine many possible scenarios and corresponding sample runs or outputs

        ◦ Convert the pseudocode into a program written in the C++ programming language

        ◦ Test it in the Cloud9 IDE and submit it for grading on Moodle




                2. Background



Functions in C++

A function is a block of code which is used to perform a particular task. ​Depending on whether a function is predefined or created by programmer; there are two types of function:

    1. Library Function

    2. User-defined Function
Library functions are the built-in functions in C++ programming. For example, C++ library provides ​sqrt()​function to calculate the square root of a number.

C++ allows programmers to define their own functions. These are called user-defined functions. Every valid C++ program has at least one function, that is, ​main()​function. Other user-defined functions are called from the ​main()​function, or from within ​other user-defined functions.



























Here is the syntax for the function definition:



return_type function_name(parameter_list)

{

//function_body

}

    • return_type​is the data type of the value that the function returns.
    • function_name​is the actual name of the function.

    • parameter_list refers to the type, order, and number of the parameters of a function. A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument.
    • function_body contains a collection of statements that define what the function does. The statements inside the function body are executed when a function is called.


When we don’t have user-defined functions, all statements are executed sequentially, one after another. When we have used defined functions, the ​flow of execution changes. Let’s follow in the example below.


When a program begins running, the system calls the ​main()​function. When control of the program reaches the ​sum()​function inside the ​main() ​(in the example below, at line ​13​), it moves to function​ sum()​and all code inside the function is executed (lines ​6 and ​7)​. After the ​sum()​function finishes, the execution returns in the ​main()​function and the following statement is executed (line ​14​).































Data Types in C++


When programming, we store the variables in our computer's memory, but the computer needs to know what kind of data we want to store in them, since it is not going to occupy the same amount of memory to store a simple number than to store a single
letter or a large number, and they are not going to be interpreted the same way. Some commonly used data types in C++ are:

    1. int​(for integers)

        ◦ int​ myInt = 5;
    2. char​(for characters)

        ◦ char myChar = 'c';

    3. float​(for floating-point numbers)

        ◦ float myFloat = 4.4531;

    4. double​(for double precision floating-point numbers)

        ◦ double myDouble = 4.4531;







3. Submission Requirements


All three steps must be fully completed by the submission deadline for your homework to be graded.

    1. Share your Cloud 9 workspace with your TA: Your recitation TA will review your code by going to your Cloud9 workspace. ​TAs will check the last version that was saved before the submission deadline​.

        ◦ Create a directory called ​Hmwk3 and place all your file(s) for this assignment in this directory.

        ◦ Share your workspace by clicking Share in the upper right hand corner and inviting your TA using their Cloud9 username.
        ◦ Make sure to ​save the final version of your code (File > Save). Verify that this version displays correctly by going to File > File Version History.

        ◦ The file(s) should have all of your functions, test cases for the functions in main() function(s), and adhere to the style guide. Please read the ​Test Cases​and ​Style and Comments​sections for more details.

    2. Submit to the Moodle CodeRunner: ​Head over to Moodle to the link ​Homework 3 CodeRunner​. You will find one programming quiz question for each problem in the assignment. Submit your solution for the first problem and press the Check button.
You will see a report on how your solution passed the tests, and the resulting score for the first problem. You can modify your code and re-submit (press ​Check ​again) as many times as you need to, up until the assignment due date. Continue with the rest of the problems.

    3. Submit a .zip file to Moodle: ​After you have completed all 10 questions from the Moodle assignment, zip all 10 solution files you compiled in Cloud9 (one cpp file for each problem), and submit the zip file through the ​Homework 3 (File Submission) link on Moodle.

TA Name
Cloud 9 username
TA Name
Cloud 9 username




Shipra Behera
beherashipra
Telly Umada
TetsumichiUmada




Josh Ladd
joshladd
Ashwin
ashwinsankaralingam


Sankaralingam





Chu-Sheng Ku
chusheng
Supriya Naidu
supriyanaidu




Punith Patil
variable314
Sebastian
slaudens


Laudenschlager





Karthik Palavalli
karthikpalavalli
Dhanendra Soni
dhanendra




Thanika Reddy
thanika
Shudong Hao
shudonghao











4. Rubric


Aside from the points received from the ​Homework 3 CodeRunner quiz problems, your TA will look at your solution files (zipped together) as submitted through the ​Homework 3 (File Submission)​link on Moodle and assign points for the following:

Comments​(5 points):
    • Your code should be well-commented. Use comments to explain what you are doing, especially if you have a complex section of code. These comments are intended to help other developers understand how your code works. These
comments should begin with two backslashes (//) or the multi-line comments (/*
        ◦ ​comments here…​*/) .
    • Please also include a comment at the top of your solution with the following format:

            ▪ CS1300 Spring 2019

            ▪ Author: my name

            ▪ Recitation: 123 – Favorite TA

            ▪ Cloud9 Workspace Editor Link: https://ide.c9.io/…

            ▪ Homework 3 - Problem # ...




Algorithm ​(5 points)
    • Before each function that you define, you should include a comment that describes the inputs and outputs of your function and what algorithms you are using inside the function.
    • This is an example C++ solution. Look at the code and the algorithm description for an example of what is expected.

Example 1:


/*

    • Algorithm: convert money from U.S. Dollars (USD) to Euros.

    • 1. Take the value of number of dollars involved in the transaction.

    • 2. Current value of 1 USD is equal to 0.86 euros

    • 3. Multiply the number of dollars got with the currency exchange rate to get Euros value
    • 4. Return the computed Euro value

    • Input parameters: Amount in USD (double)

    • Output (prints to screen): nothing

    • Returns: Amount in Euros (double)

*/


Example 2:


double convertUSDtoEuros(double dollars)

{

double exchange_rate = 0.86; //declaration of exchange rate double euros = dollars * exchange_rate; //conversion return euros; //return the value in euros

}

The algorithm described below does not mention in detail what the algorithm does and does not mention what value the function returns. Also, the solution is not commented. This would work properly, but would not receive full credit due to the lack of documentation.

/*

    • conversion */

double convertUSDtoEuros(double dollars)

{

double euros = dollars * 0.86;

return euros;

}


Test Cases ​(20 points)
    1. Code compiles and runs​(​6 points​):
        ◦ The zip file you submit to Moodle should contain ​10 ​full programs (with a main() ​function), saved as .cpp files. It is important that your programs can be compiled and run on Cloud9 with no errors. The functions included in these programs should match those submitted to the CodeRunner on Moodle.

    2. Test cases​(​14 points​):
For this week’s homework, 7 out of the 10 problems are asking you to create a function (Problems 3 and 5-10). In your Cloud9 solution file for each function, you should have 2 test cases present in their respective ​main() function, for a total of 14 test cases (see the diagram on the next page). Your test cases should follow the guidelines, ​Writing Test Cases​, posted on Moodle under Week 3.

5. Problem Set


Note: To stay on track for the week, we recommend to finish/make considerable progress on problems 1-6 by Wednesday. Students with recitation on Thursday are encouraged to come to recitation with questions and have made a start on ​all of the problems​.

Problem 1 (5 points): ​Hello World program
The first program that we usually write in any language we’re learning is ​Hello, World​. Your task is to write a ​Hello, World​program just prints “​Hello, World!​” to the screen (the console window in Cloud9).

Here are some suggested steps:

Step 1: Open an Empty File

In Cloud9, select ​File -> New File​. A new, blank file called Untitled1 will be opened.

Step 2: Your First Code

Starting on line 1 in Untitled1, type the following code.



















Step 3: Saving Your File

Save the file: go to ​File -> Save As... ​A dialog box will open. Name it ​helloWorld.cpp and save it in the ​hmwk3​folder.
Note: ​make sure you save it with the .cpp extension or it will not compile correctly!

The ​.cpp​extension on the filename tells Cloud9 that the file should be read in the C++ programming language. Once you save it, the lines in the file should be color-coded to reflect what they do in the program. This is called syntax highlighting.





















Important: You should save your work ​frequently in Cloud9 to avoid losing your work in the event of the program crashing.

Step 4: Running Your Code


To run the program, click on the icon with the green arrow next to the word Run. If it works, you should see new terminal tab window open at the bottom. The title of the tab shows the file

being run (hmwk3/ helloWorld.cpp), and inside the window you should see “Running ....”

(again the name and full path of the file), and underneath it, the output of our program:
“Hello,​ World!”​















Step 5: Running Your Code from Command Line

Move to the “bash” tab (the first tab in the bottom panel). Right-click again and Clear the
Buffer. Make sure you are inside the hmwk3​​directory. Type:

$ g++ helloWorld.cpp -g -std=c++11

the ​-g option turns on debugging, which we will use later in the semester, so we should get used to it.

the ​-std=c++11 option makes sure that the c++ version used to run the program is c++ 11. If you don’t give this option then default version(which is usually C++98) is used.

This creates an executable called "a.out" (see figure below). You can run it by typing

$ ./a.out

Since no executable name was specified to g++, a.out is chosen by default. You can alternatively use the "-o" option to change the name :

$ g++ helloWorld.cpp -g -std=c++11 -o hello

creates an executable called "​hello"​ (see figure below). You can run it by typing

$ ./hello

Notice the output in the same: ​Hello, world!​, followed by the return of the prompt, for new commands.




























Step 6: Submit to Moodle CodeRunner

Head over to Moodle to the link ​Homework 3 CodeRunner​. Submit your solution for the first problem and press the Check button. You will see a report on how your solution
passed the tests, and the resulting score for the first problem. You can modify your code and re-submit (press Check again) as many times as you need to.




















































Step 7: The zip file submission

Remember that the file ​helloWorld.cpp​will be one of 10 files you need to zip together for the ​Homework 3 (File Submission)​on Moodle.
Problem 2 (5 points): ​Hello Class program
Write a program that takes as input from the user the integer value of a CS course number. Then, your program should print “​Hello, CS [course number] World!​” to the screen. ​You will first need to prompt the user to enter a CS course number with a statement requesting them to “​Enter a CS course number: ​”. Once the user enters the course number, print the required output.

For example: If the user enters the input ​1300​, the output of the program should be,


















Here are some suggested steps:

Step 1: Create a new file

Just like we did in Problem 1, create a file called ​helloClass.cpp​.

Step 2: Write the program
Create a ​main()​function, just like for Problem 1. You will need to modify the Problem

1 solution as follows:

    • Create an integer variable to store the value of course number

    • Prompt the user to enter a course number using the output: “​Enter a CS
course number: ​”
    • Generate the final output as a combination of text (strings) and the value of the variable holding the course number entered by the user. The output should match exactly the example below.

















Step 3: Submit to the Moodle CodeRunner

Head over to Moodle to the link ​Homework 3 CodeRunner​. Submit your solution for Problem 2 and press the Check button. You can modify your code and re-submit (press Check again) as many times as you need to.

Important​: the ​cout​formats provided for each problem are not suggestions – they MUST be followed precisely, word-for-word and including all punctuation marks, otherwise the CodeRunner will not recognize your results and you will not receive credit.

Step 4: The zip file submission

Remember that the file ​helloClass.cpp​will be one of 10 files you need to zip together for the ​Homework 3 (File Submission)​on Moodle.


Problem 3 (5 points): ​classGreeting function
Write a function called ​classGreeting that takes a single integer parameter and prints a greeting to the screen. If ​1300 is the given argument value, the function should print:

Hello, CS 1300 World!

Specifications:
    • Your function should have ​one ​input argument as an integer:

        ◦ An integer parameter representing the course number

    • Your function should not return anything.

    • Your function should ​print ​the course number in the following format
        ◦ Hello, CS **** World!​, where **** should be replaced by the value of the input argument

    • Your function ​MUST​be named ​classGreeting​.

Here are some suggested steps:

Step 1: Create a new file

Just like we did in Problems 1 and 2, create a file called ​classGreeting.cpp​.

Step 2: Write the program

Write the pre-processor directives (​#include <...> ​and using ​namespace std;​), followed by the ​main()​​​function. Above the ​main()​​​function, you need to write the code for the ​classGreeting ​function. Use the information provided above in the Specifications, ​and follow the syntax for a function definition provided in the Background section.

    • Start with the return type

    • Then the function name

    • Then, in parenthesis, the type and name of the input parameter

Write the solution for the function body, in between the {}.

Step 3: Calling the function

After writing a function, it’s very important to check if your function is working as we expect. In the ​main()​​function, call the ​classGreeting ​function to test if it accomplishes the required output. Remember you will need to pass a value as an argument to the function.

Note: the submission requirements are to have at least 2 tests for each function. Follow the ​Writing Test Cases​examples posted on Moodle

Your test cases should look like in the example below, where the name of the function is sayHello​, and the ​main()​function has two function calls.


void sayHello(string name)

{

cout << "Hello " << name << endl;

}

int main()

{

    • test 1

    • expected output

    • Hello Bob

sayHello("Bob"); // first function call to sayHello()

    • test 2

    • expected output

    • Hello Mary

sayHello("Mary"); // second function call to sayHello()

}




Step 4: Submit to the Moodle CodeRunner

Head over to Moodle to the link ​Homework 3 CodeRunner​. Go to Problem 3.






















In the Answer Box, paste ​only your function definition, not the entire program.


















Press the Check button. You can modify your code and re-submit (press Check again) as many times as you need to.

You must name the functions as indicated in each problem description. ​Importantly​, the ​cout formats provided for each problem are not suggestions – they ​MUST be
followed precisely, word-for-word and including all punctuation marks​, otherwise the CodeRunner will not recognize your results and you will not receive credit.

If there are errors in your solution to a particular problem, a button labeled “Show differences” will appear below the table of tests after you hit “check”. This can be a very useful tool in helping you find small typos, especially in cout statements.




















For example, below we hit “check” for a solution to problem 3 of this homework and have failed all the test cases despite getting the correct values. Hitting “Show differences”, we can see that a comma (​,​) is missing. When characters are in the expected output but not in your output they are highlighted in the “Expected” column.




















On the other hand, when we include extra, unexpected characters in output they are highlighted in the “Got” column. Below we added additional exclamation points (!) to the output.




















Step 5: The zip file submission

Remember that the file ​classGreeting.cpp​will be one of 10 files you need to zip together for the ​Homework 3 (File Submission)​on Moodle.


Problem 4 (5 points): ​Calculating sphere volume and area
Alter the provided ​main() in ​sphereVolumeArea.cpp file (on Moodle) to print both the volume and surface area of a sphere with given radius. For a radius of 5, the output of the program should look like this:

















Here are some suggested steps:

Step 1: Download sphereVolume.cpp from Moodle
You can find ​sphereVolumeArea.cpp​, and upload it to your workspace on cloud9.

Step 2: Add new statements for the surface area calculation

Add statements to compute the surface area of a sphere, which is ​4πr​2​, where ​r​is the radius.

Step 3: Submit to the Moodle CodeRunner

Head over to Moodle to the link ​Homework 3 CodeRunner​. Submit your solution for Problem 4 and press the Check button. You can modify your code and re-submit (press Check again) as many times as you need to.

Step 4: The zip file submission

Remember that the file ​sphereVolumeArea.cpp​(after your modifications) will be one of 10 files you need to zip together for the ​Homework 3 (File Submission)​on Moodle.


Problem 5 (5 points): ​sphereVolume function
Write a function called ​sphereVolume ​that determines the volume of a sphere with a given radius and prints the result to the screen.

    • Your function ​MUST​be named ​sphereVolume
    • Your function should have ​one ​input argument:

        ◦ a floating point parameter representing the radius - as a double

    • Your function should not return anything.

    • Your function should ​print ​the calculated volume.
        ◦ The output format should resemble that of the previous problem. For a radius of 5, the function should print: ​volume: 523.599

Follow the steps suggested for Problem 3. In Cloud9 the file should be called sphereVolume.cpp​and it will be one of 10 files you need to zip together for the Homework 3 (File Submission)​on Moodle.

Don’t forget to head over to Moodle to the link ​Homework 3 CodeRunner​. For Problem 5, in the Answer Box, paste ​only your function definition, not the entire program​, just like you did for Problem 3. Press the Check button. You can modify your code and re-submit (press Check again) as many times as you need to.
Problem 6 (5 points): ​sphereSurfaceArea function
Write a function called ​sphereSurfaceArea ​that determines the surface area of a sphere with given radius and prints the result to the screen. ​Hint: Recycle some of your work for Problem 5 to save some time here!

    • Your function ​MUST​be named ​sphereSurfaceArea
    • Your function should have ​one ​input argument:

        ◦ a floating point parameter representing the radius - as a double

    • Your function should not return anything.

    • Your function should ​print ​the calculated surface area.
        ◦ The output format should resemble that of the previous problem. For a radius of 5, the function should print: ​surface area: 314.159

In Cloud9 the file should be called ​sphereSurfaceArea.cpp​and it will be one of 10 files you need to zip together for the ​Homework 3 (File Submission)​on Moodle.

Don’t forget to head over to Moodle to the link ​Homework 3 CodeRunner​. For Problem 6, in the Answer Box, paste ​only your function definition, not the entire program​, just like you did for Problem 3. Press the Check button. You can modify your code and re-submit (press Check again) as many times as you need to.


Problem 7 (10 points): ​convertSeconds function
Write a function ​convertSeconds that takes one integer input as seconds and converts it to hours, minutes and seconds. Your function can round off the seconds to the nearest whole value after conversion. Then, your function will print the time in hours, minutes and seconds to the screen, as demonstrated below. You should convert the amount of time in such a way that maximizes the whole numbers of hours and minutes.

    • Your function ​MUST ​be named ​convertSeconds
    • Your function should accept only one input value, seconds, as an integer

    • Your function should not return any value

    • Your function should print the string in the following format:

h​hour(s) ​m​minute(s) ​s​second(s)

For example, given input seconds as 3671, it should print :

1 hour(s) 1 minute(s) 11 second(s)
In Cloud9 the file should be called ​convertSeconds.cpp​and it will be one of 10 files you need to zip together for the ​Homework 3 (File Submission)​on Moodle.

Don’t forget to head over to Moodle to the link ​Homework 3 CodeRunner​. For Problem 7, in the Answer Box, paste ​only your function definition, not the entire program​, just like you did for Problem 3. Press the Check button. You can modify your code and re-submit (press Check again) as many times as you need to.


Problem 8 (10 points): ​population function
The U.S. Census provides information about the current U.S. population as well as approximate rates of change. Using those rates and the current US population, write a function to calculate the U.S. population in exactly one year (365 days). Your function should return the result of your calculations. If you end up with a non-integer projected population, then round down to the nearest whole person.

Three rates of change are provided:

    • There is a birth every 8 seconds

    • There is a death every 12 seconds

    • There is a new immigrant arriving in the US every 27 seconds

Specifications:
    • Your function ​MUST​be named ​population
    • Your function should accept only one input argument: the ​initial population, as an integer

    • Your function should return the population value in a year - you should be able to figure out the type of the return value.

    • Your function should not print/display/output anything to the screen

For example, given an initial population of 1,000,000, your function would return ​3,482,000​.

In Cloud9 the file should be called ​population.cpp​and it will be one of 10 files you need to zip together for the ​Homework 3 (File Submission)​on Moodle.

Don’t forget to head over to Moodle to the link ​Homework 3 CodeRunner​. For Problem 8, in the Answer Box, paste ​only your function definition, not the entire program​, just like you did for Problem 3. Press the Check button. You can modify your code and re-submit (press Check again) as many times as you need to.
Problem 9 (10 points): ​celciusToFahrenheit function
Write a function to convert a temperature value from Celsius to Fahrenheit and print the temperature in Fahrenheit using the format of the example.
Hint​: the formula for converting Celsius(C) to Fahrenheit(F):  F = 95 C + 32

    • Your function name ​MUST​be named ​celsiusToFahrenheit
    • Your function should accept one input argument, temperature in Celsius, as an int

    • Your function should not return any value

    • Your function should print the resulting temperature Fahrenheit value, with a two digit precision, in the following format:

For example, given the temperature as 38, it should print :

The temperature of 38 in Fahrenheit is 100.40

For setting the two digit precision of your output, see the ​Chapter 2.3.3 Formatted Output​from the textbook. The examples in ​Table 8​will be very helpful. ​Don’t forget to include the appropriate library at the top of your program!

In Cloud9 the file should be called ​celsiusToFahrenheit.cpp​and it will be one of 10 files you need to zip together for the ​Homework 3 (File Submission)​on Moodle.

Don’t forget to head over to Moodle to the link ​Homework 3 CodeRunner​. For Problem 9, in the Answer Box, paste ​only your function definition, not the entire program​, just like you did for Problem 3. Press the Check button. You can modify your code and re-submit (press Check again) as many times as you need to.


Problem 10 (10 points): ​marioKart function
On a warm day, Baby Mario is driving around at a constant speed (​u​) in his brand new kart. All of a sudden, another kart, just ahead of Baby Mario, breaks down and comes to an immediate halt. Write a function that takes in parameters for the initial speed, ​u​, and distance between Baby Mario's kart and the broken one, ​s​, and determines the deceleration, ​d​, required for Baby Mario’s kart to stop right behind the faulty one.

You can use the formula d = u22s . Where ​d stands for deceleration, ​u stands for initial speed and ​s​stands for distance.
    • The function ​must​be named ​marioKart​.

    • The function takes in two arguments, in this order:

        ◦ initial speed, of type ​double​.
        ◦ distance between Baby Mario’s kart and the faulty one at the time of break down) of type ​double​.
    • The function should return the deceleration required as ​double​.

For example, with ​u​= 4.0 and ​s​= 8.0, then the function should return ​1​.

You can start creating ​marioKart​function with the following code:





























In Cloud9 the file should be called ​marioKart.cpp​and it will be one of 10 files you need to zip together for the ​Homework 3 (File Submission)​on Moodle.

Don’t forget to head over to Moodle to the link ​Homework 3 CodeRunner​. For Problem 10, in the Answer Box, paste ​only your function definition, not the entire program​, just like you did for Problem 3. Press the Check button. You can modify your code and re-submit (press Check again) as many times as you need to.







6. Homework 3 points summary

Criteria

Pts


CodeRunner (problem 1 - 10)

Comments

Algorithms

Test cases

Recitation attendance (Jan 29 or Jan 31)* Total

70

5

5

20

-30

100

    • if your attendance is not recorded, you will lose points. Make sure your attendance is recorded on Moodle.

More products