simple loop coding

TITLE:

Repetition structure: for loop, while loop, do..while loop

Handle Menu to re-display – Distance Converter with user-defined functions

TIME TO COMPLETE

2 Weeks for 16 weeks class 1 week for 8 weeks class

THE LAB

Remember to do the lab with the following:

*If you need help about the C++ syntax to write the code, read the instruction in the folder Skills Required

*From now and on yourLastName will be changed to your last name

*Your program should change Martinez to your last name

*Change Luis Martinez to your full name

*write the file name as the first comment line at the top of program

*After running your program, take the picture of the output window as below from your program with your name on and paste the picture at the bottom of the document having pseudo-code to turn in

Part 1: Use word document named SP2024_LAB5PART1_yourLastName to answer questions

Part 2:

Read the requirement; write in English the pseudo-code in a word document by listing the step by step what you suppose to do in main() and then save it with the name as Lab5_pseudoCode_yourLastName

:

Start Virtual Studio C++, create the project write the project name

For Part 2: SP2024_LAB5PART2_yourLastName

Add .cpp file

For Part 2: SP2024_DistanceConverter_yourLastName.cpp

After adding the cpp file, you have an empty window coming up, type the following template of a C++ program in:

//File name should be written here as comment line

#include <iostream>

using namespace std;

int main()

{

//add the code here below this line

..

system(pause); //This will pause the output to read

return 0;

}

Then follow the step by step in the pseudo-code, type the C++ code in after the line //add the code here below this line

Compile and run the program

Debug if there are any errors until compile successfully

LAB5 – PART1 REQUIREMENT

Use the word document to write the answers to the following questions:

QUESTION 1 for loop

DO NOT USE ARRAYS FOR THIS QUESTION

After running the loop 10 times, we have the output as below:

3, 4, 6, 9, 13, 18, 24, 31, 39, 48

Sum of these numbers is: 195

QUESTION 2 while loop

2A

Suppose we declare the following variables:

int count = 0;

int iNumber = 1, previous = 1, product = 1;

Write the while loop that do the following if product is still less than or equal to 2024

-increase count by 1

-set previous equal to product

-lets the user enter a number for intNumber, intNumber can be 1 or any number, each time enter different the previous values.

-multiply iNumber with product and the result is stored back to the variable product.

After the loop stop, display the output in the following format (for example)

At count =4

Previous product = 945

Input number= 32

Product =30240

2B

If product = 2024 instead of product = 1 at the beginning. How many times does the while loop iterate such that the product <= 2024?

do while loop

3A

Suppose we define the following variables:

int count = 0;

int iNumber = 1, previous = 1, product = 1;

Write the do..while loop that do the following when the product is less than or equal to 2024

-increase count by 1

-set previous equal to product

-lets the user enter a number for intNumber, intNumber can be 1 or any number, each time enter different the previous values.

-multiply iNumber with product and the result is stored back to the variable product.

After the loop stop, display the output in the following format (for example)

At count =4

Previous product = 945

Input number=32

Product =30240

3B

If product = 3000 instead of product = 1 at the beginning. How many times does the do..while loop iterate such that the product < 2022

Write/Read the file

4A.Write to file

Write the code to do the following:

-Display message to ask then read input from the keyboard about the following information:

* accountNumber (string)

* customerName (string)

* balance (float)

-open output file customer.txt to write:

Write to the output file customer.txt the following information that have read input from the keyboard in the following format:

accountNumber customerName balance

For example:

1561163623- Luis Martinez 2845.25

close file

4B Read from the file

Open notepad, type the following lines with the file name as data.txt

1112243433 – Mary Lane 1250.366

2123312344 John Smith 2134.25

1561175753 – James Smith 1255.25

-provide the C++ code to open file data.txt to read

-Use the loop to read file

Read each line of the file then display on screen

-continue reading and displaying on the screen all the lines until end of the file

Write: End of the file data.txt on the screen

close data.txt file

Use do..while loop to redisplay the menu

Provide the do..while loop to display the following menu on the screen. After users select a process; the loop must loop back to display the menu until users select 0 to exit

MENU

1. Process 1

2. Process 2

3. Process 3

0. Exit

Type a number 1, 2, 3 to select the Process:

Read the number.

Write the switch statement based on the selected number:

If selected number = 1: Display You select Process 1

If selected number = 2: Display You select Process 2

If selected number = 3: Display You select Process 3

If selected number = 0: Display You select Exit

For other number: Display Invalid Process.

LAB 5 – PART2 REQUIREMENT

SP2024_DistanceConverter_yourLastname.cpp

Use C++ to create an application that provides the following menu with 4 user-defined functions to convert the distance between miles and kilometers; then allow users to enter a number from 0, 1, 2, 3, 4 to select a function:

SP2024_DistanceConverterApplication_Martinez.cpp

MENU DISTANCE CONVERTER LUIS MARTINEZ

—————————————————

  • Formula: Miles to Kilometers
  • Formula: Kilometers to Miles
  • Estimate: Miles to Kilometers
  • Estimate: Kilometers to Miles
  • Exit

Type a number from 1 to 4 to select the method to convert or 0 to exit:

For each selected number, you should access one user-defined functions

4 USER-DEFINED FUNCTIONS are:

-function converts miles to kilometers: this function reads a miles value from the keyboard, then apply the following formula to convert it to kilometers and display the output

Distance in km= Distance in miles * 1.60935

-function converts kilometers to miles: this function reads a kilometer value from keyboard, then apply the following formula to convert kilometer value to mile and display the output

Distance in miles = Distance in km* 0.62137

-function converts miles to kilometers: this function reads a miles value from the keyboard, then apply the following estimate to convert it to kilometers and display the output

Distance in km= Distance in miles * 8/5

-function converts kilometers to miles: this function reads a kilometer value from keyboard, then apply the following estimate to convert it to mile and display the output

Distance in miles = Distance in km* 5/8

-If you do not have the source file and execute file, your lab gets 0 points

-If you do not provide file .exe of the part, you only have half of the max score of the part

You should turn in the following files:

1.LAB4PART1_Answers_yourLastName.docx

2.Lab5_pseudoCode_yourLastName.docx (including Pseudo-code and the output pictures part2)

3.SP2024_LAB5PART2_yourLastName.zip (including file SP2024_DistanceConverterApplication-YourLastName.cpp and file SP2024_LAB5PART2_yourLastName.exe)