Write A Program That Asks The User To Input The Number Of Mi

Write a program that asks the user to input the number of miles and convert the miles to kilometers, and then print the output to the screen. (Recall that 1 mi = 1. km.) Follow the Engineering problem solving methodology.

Problem Statement: Develop a program that prompts the user for a distance in miles, converts it to kilometers, and displays the result. The conversion factor is 1 mile = 1.60934 kilometers.

Input/Output:

  • Input: Distance in miles (numeric value entered by the user)
  • Output: Distance in kilometers (numeric value displayed to the user)

Solve by hand:

Suppose the user inputs 10 miles:

  • Conversion: 10 miles * 1.60934 km/mile = 16.0934 km

This manual calculation confirms the correctness of the program's conversion logic.

Pseudo-code Algorithm:

BEGIN

PROMPT user to enter miles

READ miles

SET kilometers = miles * 1.60934

DISPLAY kilometers

END

C++ Code in Visual Studio:

include <iostream>

int main() {

double miles, kilometers;

const double conversionFactor = 1.60934; // miles to km

std::cout << "Enter the number of miles: ";

std::cin >> miles;

kilometers = miles * conversionFactor;

std::cout << miles << " miles is equal to " << kilometers << " kilometers." & std::endl;

return 0;

}

Test with sample data:

  • Input: 5 miles → Output: 8.0467 km
  • Input: 0 miles → Output: 0 km
  • Input: 20 miles → Output: 32.1868 km

Problem #2: Flowchart of the algorithm in Problem #1

Flowchart of miles to kilometers conversion algorithm

Problem #3: Find the value of h in each problem and show work

a) h = d < c

Given: a=3.5, b=1.5, c=9, d=6

  • Evaluate d < c: 6 < 9 → TRUE (which is 1 in programming)
  • Therefore, h = 1

b) h = a >b// b >c

Given: a=3.5, b=1.5, c=9, d=6

  • Evaluate a > b: 3.5 > 1.5 → TRUE (1)
  • Evaluate b > c: 1.5 > 9 → FALSE (0)
  • Expression: a > b // b > c → 1 // 0
  • In many programming languages, integer division (//) results in 1 // 0 = 0
  • Thus, h = 0

c) h = d - a > b && a * b < d

Evaluate step by step:

  • Evaluate d - a: 6 - 3.5 = 2.5
  • Evaluate a > b: 3.5 > 1.5 → TRUE (1)
  • Evaluate a b: 3.5 1.5 = 5.25
  • Evaluate a * b < d: 5.25 < 6 → TRUE (1)
  • Logical AND (&&): 1 &&; 1 = 1
  • Therefore, h = d - a > b && a * b < d = 2.5 > 1 = 1

Problem #4: Draw flowcharts for the following statements

a) if (b <= 4) y = 2; else { y = 4 + x; x++; }

This flowchart begins with a decision node to check if the value of b is less than or equal to 4. If true, assign y=2. If false, assign y=4 + x and then increment x by 1.

b) if (y < 5) z = 0; else if (y < 10) z = 1; else z = 2;

This flowchart starts with a condition to check if y is less than 5. If yes, set z=0. If not, check if y is less than 10; if yes, set z=1, else set z=2. These decision points form a branching structure that assigns the correct z value based on y.