GSP115 Lab 1: Part A,B,C,D with Visual Studio 2012 source code, word document and screenshots – Instant Delivery – Perfect Solution
A. Lab Activity # GSP115-A1
B. Lab 1 of 7: Visual Studio and Simple Variables
C. Lab Overview – Scenario / Summary:
TCO(s) that the lab supports:
1 Given a set of user requirements for a game, design and check a solution algorithm that is expressed in terms of pseudo code, program notes, or input-process-output (IPO) analysis leading to a flow chart.
4 Given a program with logic errors that are intended as a solution to a simple game problem, employ debugging diagnostics (breakpoints, watch window, call stack) functions to remove and correct errors.
Summary:
Visual Studio is the most used Integrated Development Environment (IDE) in the industry. As a result, you want to learn this very important IDE. You will code, build and execute several programs to help you learn and become comfortable with Visual Studio and also learn how to create and initialize simple variables. You will be introduced to several C++ commands and operators that we will cover in more detail in the following weeks.
Learning outcomes:
1. To be able to create a C++ Win32 console project in Visual Studio.
2. To be able to translate user requirements into Inputs, Processes, and Outputs.
3. To be able to diagram the identified inputs, processes, and outputs as a flowchart.
4. To be able to create and use simple variables.
D. Deliverables:
Section Deliverable
Part A Step 6. Program Listing and Output
Part B Step 5. Program Listing and Output
Part C Step 5. Program Listing and Output
Part D Step 5. Program Listing and Output
E. Lab Steps:
Preparation:
This lab requires Visual Studio 2012. Please follow the steps given at the Student Software Store to download and install Visual Studio 2012.
Lab Activity:
Part A: Programming Exercise 1 with Lab Tutorial
Step 1: Start Visual Studio 2012
□ Start Visual Studio 2012
□ Click File | New | Project on the menu bar or click the New Project icon on the toolbar.
□ Click the black triangles to open choices on the left pane, if needed. Click on Visual C++. On the right pane, select Empty Project.
□ Give the project a name in the Name field near the bottom. For example, Week 1 iLab Part A would be an appropriate name.
□ Change the location of the project to your Desktop or USB drive or other appropriate location. You can change the location by clicking the Browse button on the right side of the Location field.
□ Click the OK button to create your project.
□ Open your Solution Explorer by clicking View | Solution Explorer on the menu bar.
□ Right-click on the Source Files folder and select Add | New Item.
□ Click on C++ File (.cpp) and then give the file a name using the Name box near the bottom. For example, main.cpp would be an appropriate name.
Step 2: Create the starting code
□ Add the following code to the Program.cpp file window:
#include
using namespace std;
int main()
{
cout << "Hello World!" << endl;
// pause
cout << "\nPress any key to continue...";
cin.get();
return 0;
}
□ Look in the main method. Do you see the line “cout << "Hello World!" << endl;”? This line of code outputs the text that is inside of the quotes. The text is called a string literal. If you want to output information to the player, you can use cout and then the arrows (insertion operator) and then whatever you want to output. Remember to end the line with endl so the cursor drops down to the next line. Finally, put a semi-colon at the end of the line to tell C++ that the command is complete.
□ Do you see the line “cin.get( );”? This line tells C++ to get a character from the user. The application will pause until the user hits the Enter key. You will need to add this to your iLabs in order to see the program results when running with debugging. When running without debugging (CNTL+F5), the program will stop and wait for a key press. As a result, when running without debugging, you will have to hit enter twice to exit the program.
Step 3: Get input, create random number, and show output
□ We need to create variables to hold the information that we get from the user. Variables are like clipboards. They hold information until the variable is written with new information or until the application ends. Create our variables by typing this code inside the curly braces right under the int main( ) method heading:
// create our variables (// means comment and gets ignored by C++)
string name = ""; // string (text) is held in a string variable
int number = 0; // whole numbers are held in integer variablesfloat cash = 0.0f; // decimal numbers are held in float variables (“floating point”)
□ We are going to use strings so we need to import the string library at the top.
#include
If you completed this step correctly, it should look like this:
□ Now, let’s get some text from the user. We have to tell the user what we want first. Then, we read the user’s input and put the text into a variable to hold the input until we need it. Here is the code:
// get a string from the user
cout << "Enter your name: "; // prompt
getline( cin, name ); // read the string input into the string variable
□ How do you get decimals from the user? Tell the user what we want first (prompt). Then, we read the user’s input. Add this code to get a float:
// get a decimal number (float) from the user
cout << "How much cash do you have? "; // prompt
cin >> cash; // read the number
cin.sync( ); // clear the Enter key off the keyboard buffer
□ As a game developer, you will generate random numbers a lot in order to give your players a new experience every time they play your game. The first step to create the random number is to “seed the randomizer”. That means that you need to give the randomizer the current system time down to the second. At the top, include the ctime library.
#include
□ In your code, seed the randomizer by giving the seed command and the current time from the computer’s clock. Put this code at the top of your main method before your variables. Here is the code:
// seed the randomizer
srand( time(0) );
□ Now, let’s generate a random number. The rand( ) function will give you a random whole number (int). We need to take the whole number and make it fit into a small range of numbers. We will use the modulus operator to accomplish this goal. The modulus operator (%) gives back the remainder of the division. In other words, 13 / 2 = 2 with a remainder of 3. Modulus gives back only the remainder. So, 13 % 2 = 3.
// generate a random number from 1 to 10
number = rand( ) % 10 + 1;
□ Look at the command: “rand( ) % 10” This command will result in a number from 0 to 9. Why?
o Let’s say the rand( ) function gives us 97. What is 97 % 10? The answer is 7 because 97 / 10 = 9 with a remainder of 7.
o What if rand( ) gives us 80? Then 80 % 10 = 0 because 80 / 10 = 8 with a remainder of 0.
o What is the biggest number that rand( ) % 10 can give us? The answer is 9 because 10 % 10, 20 % 10, 30 % 10, 40 % 10, and all factors of 10 will give us back a zero. The biggest remainder that we can have is 9 since 9 % 10 = 9. Think about it. What is 999 % 10? The answer is 9 since 999 / 10 = 99 with a remainder of 9.
□ If rand( ) % 10 gives a number from 0 to 9, how do we change this range to 1 to 10? Add a “step-up” value. Look carefully at the random number command. Do you see the “+ 1”? If the rand( ) % 10 gives us back zero, we will add one to it and get 1. If rand( ) % 10 gives us 9, then we will add one to it to get 10. So our range is now 1 to 10.
□ Let’s display all of the information back to the user. Remember, to show information to the user, you will use the cout command. Here is the code:
□ // show output
cout << endl; // blank line
cout << name << ", welcome to the game!" << endl;
cout << "You have this much cash: $" << cash << endl;
cout << "Your lucky number is " << number << endl;
□ If you did everything right, then your code should look like this:
Step 4: Save program
Save your program by clicking File on the menu bar and then clicking Save All, or by clicking the Save All button on the toolbar, or Ctrl + Shift + S.
Step 5: Build and execute the solution
□ To build and run the program, click Debug on the menu bar and then click the Start Debugging option. You should receive no error messages. If you see some error messages, check the code above to make sure you didn’t key in something wrong. Once you make your corrections to the code, go ahead and click Debug >> Start Debugging again to see your output.
Let’s test it. When you enter these values:
name: Your Name
cash: 17.11
you should see similar results:
Step 6: Capture the output
□ Type your name, GSP115, Lab1, Tutorial at the top of the new Word document.
□ Capture a screen print of the output from your program [Hold down the Alt key while you press the PrtScn (printscreen) button]. If you are using a notebook computer, you may need to hold down the Fn key and the Alt key while you press and release the PrtScr button.
□ Paste the screen copy into the Word document.
□ Select all of your code from your code window in Visual Studio 2012. Click Edit on the menu bar and then Copy. Go to your Word document and paste the code below the screen shot.
□ Save the Word document as GSP115_Lab1_YourLastName.docx. Be sure that the course number, your last name, and the lab number are part of the file name.
Step 7: Optional: Can you do this?
□ I like line numbers on my code window. If you like lines numbers as well, follow these steps.
□ Click Tools | Options on the menu bar.
□ Hit the triangle to the left of Text Editor and then select All Languages.
□ Put a checkmark in Line Numbers to turn on this feature.
END OF PART A
Part B: Programming Exercise 2
Step 1: Start Visual Studio 2012
□ Start Visual Studio 2012
□ Click File | New | Project on the menu bar or click the New Project icon on the toolbar.
□ Click the black triangles to open choices on the left pane, if needed. Click on Visual C++. On the right pane, select Empty Project.
□ Give the project a name in the Name field near the bottom. For example, Week 1 iLab Part B would be an appropriate name.
□ Change the location of the project to your Desktop or USB drive or other appropriate location. You can change the location by clicking the Browse button on the right side of the Location field.
□ Click the OK button to create your project.
□ Open your Solution Explorer by clicking View | Solution Explorer on the menu bar.
□ Right-click on the Source Files folder and select Add | New Item.
□ Click on C++ File (.cpp) and then give the file a name using the Name box near the bottom. For example, main.cpp would be an appropriate name.
Step 2: Use cout to display information to the user
□ Display your name on one line. Then, display your favorite game on the next line. Finally, display an interesting fact about you that most people do not know. Have fun with it!
Step 3: Save program
Save your program by clicking File on the menu bar and then clicking Save All, or by clicking the Save All button on the toolbar, or Ctrl + Shift + S.
Step 4: Build and execute the solution
To build and run the program, click Debug on the menu bar and then click the Start Debugging option. You should receive no error messages. If you see some error messages, check the code above to make sure you didn’t key in something wrong. Once you make your corrections to the code, go ahead and click Debug >> Start Debugging again to see your output.
Step 5: Capture the output
□ Open the file that you created for Part A called GSP115_Lab1_YourLastName.docx. Go to the bottom of the file (after the Part A section). Leave a few blank lines and then type your name, GSP115, Lab1 Part B.
□ Capture a screen print of your program’s output [Hold down the Alt key while you press the PrtScn (printscreen) button]. If you are using a notebook computer, you may need to hold down the Fn key and the Alt key while you press and release the PrtScr button.
□ Paste the screen copy into the Word document under the Part B section title.
□ Select all of your code from your code window in Visual Studio 2012. Click Edit on the menu bar and then Copy. Go to your Word document and paste the code below the screen shot.
□ Save the Word document that now contains Part A and Part B.
END OF PART B
Part C: Programming Exercise 3
Step 1: Start Visual Studio 2012
□ Start Visual Studio 2012
□ Click File | New | Project on the menu bar or click the New Project icon on the toolbar.
□ Click the black triangles to open choices on the left pane, if needed. Click on Visual C++. On the right pane, select Empty Project.
□ Give the project a name in the Name field near the bottom. For example, Week 1 iLab Part C would be an appropriate name.
□ Change the location of the project to your Desktop or USB drive or other appropriate location. You can change the location by clicking the Browse button on the right side of the Location field.
□ Click the OK button to create your project.
□ Open your Solution Explorer by clicking View | Solution Explorer on the menu bar.
□ Right-click on the Source Files folder and select Add | New Item.
□ Click on C++ File (.cpp) and then give the file a name using the Name box near the bottom. For example, main.cpp would be an appropriate name.
Step 2: Get input and output using cin and cout
□ Create five variables that can hold floating point numbers (num1, num2, num3, sum, result)
□ Prompt the player for the first variable and then read the player’s input
□ Prompt the player for the second variable and then read the player’s input
□ Prompt the player for the third variable and then read the player’s input
□ Add the three numbers together and put the result into the sum variable
□ Divide the sum by 3.0f and put the quotient into the result variable
□ Display the result variable to the player.
Program output should look something like this:
Please enter 3 numbers: 5 37 51
The average of those 3 numbers is: 31
press any key to continue
□ “Better-than-Average” Achievement: modify the program to ask for how many numbers to average, and find the average of that many numbers. (hint: This will require you to do some independent research, but you could use a while loop, which won’t be covered until week 3.)
Step 3: Save program
Save your program by clicking File on the menu bar and then clicking Save All, or by clicking the Save All button on the toolbar, or Ctrl + Shift + S.
Step 4: Build and execute the solution
To build and run the program, click Debug on the menu bar and then click the Start Debugging option. You should receive no error messages. If you see some error messages, check the code above to make sure you didn’t key in something wrong. Once you make your corrections to the code, go ahead and click Debug >> Start Debugging again to see your output.
Step 5: Capture the output
□ Open the file that you created for Part A and Part B called GSP115_Lab1_YourLastName.docx. Go to the bottom of the file (after the Part B section). Leave a few blank lines and then type your name, GSP115, Lab1 Part C.
□ Capture a screen print of your program’s output [Hold down the Alt key while you press the PrtScn (printscreen) button]. If you are using a notebook computer, you may need to hold down the Fn key and the Alt key while you press and release the PrtScr button.
□ Paste the screen copy into the Word document under the Part C section title.
□ Select all of your code from your code window in Visual Studio 2012. Click Edit on the menu bar and then Copy. Go to your Word document and paste the code below the screen shot.
□ Save the Word document that now contains Part A and Part B and Part C.
END OF PART C
Part D: Programming Exercise 4
Step 1: Start Visual Studio 2012
□ Start Visual Studio 2012
□ Click File | New | Project on the menu bar or click the New Project icon on the toolbar.
□ Click the black triangles to open choices on the left pane, if needed. Click on Visual C++. On the right pane, select Empty Project.
□ Give the project a name in the Name field near the bottom. For example, Week 1 iLab Part D would be an appropriate name.
□ Change the location of the project to your Desktop or USB drive or other appropriate location. You can change the location by clicking the Browse button on the right side of the Location field.
□ Click the OK button to create your project.
□ Open your Solution Explorer by clicking View | Solution Explorer on the menu bar.
□ Right-click on the Source Files folder and select Add | New Item.
□ Click on C++ File (.cpp) and then give the file a name using the Name box near the bottom. For example, main.cpp would be an appropriate name.
Step 2: Get input and output using cin and cout
Create a program that:
□ Converts temperatures from Fahrenheit to Celsius
o Asks the user for a Fahrenheit temperature
o Uses the formula C = (F – 32) * 5 / 9 to calculate the Celsius conversion of the Fahrenheit temperature
o Prints the Celsius equivalent of the given Fahrenheit.
Program output should look something like this:
Enter a temperature in degrees Fahrenheit:
98
The temperature is 36 degrees Celsius
press any key to continue
□ “Questionable Temperature” Achievement: modify the program to ask which conversion to do: Fahrenheit to Celsius, or Celsius to Fahrenheit. (hint: This will require you to do some independent research. You could use an if statement which we won’t cover until week 3.)
Step 3: Save program
Save your program by clicking File on the menu bar and then clicking Save All, or by clicking the Save All button on the toolbar, or Ctrl + Shift + S.
Step 4: Build and execute the solution
To build and run the program, click Debug on the menu bar and then click the Start Debugging option. You should receive no error messages. If you see some error messages, check the code above to make sure you didn’t key in something wrong. Once you make your corrections to the code, go ahead and click Debug >> Start Debugging again to see your output.
Step 5: Capture the output
□ Open the file that you created for Part A and Part B and Part C called GSP115_Lab1_YourLastName.docx. Go to the bottom of the file (after the Part C section). Leave a few blank lines and then type your name, GSP115, Lab1 Part D.
□ Capture a screen print of your program’s output [Hold down the Alt key while you press the PrtScn (printscreen) button]. If you are using a notebook computer, you may need to hold down the Fn key and the Alt key while you press and release the PrtScr button.
□ Paste the screen copy into the Word document under the Part D section title.
□ Select all of your code from your code window in Visual Studio 2012. Click Edit on the menu bar and then Copy. Go to your Word document and paste the code below the screen shot.
□ Save the Word document that now contains Part A and Part B and Part C.
END OF PART D
Submit Your Work!
Step 1: Zip up all of your lab requirements and upload to DropBox
□ You now have a Word document file that has a screen shot and code for Part A, Part B, and Part C.
□ Upload the Word document to Week 1 iLab DropBox in eCollege.
END OF SUBMITTING YOUR WORK