120k views
4 votes
3.19 LAB: Interstate highway numbers Primary U.S. interstate highways are numbered 1-99. Odd numbers (like the 5 or 95 ) go north/south, and evens (like the 10 or 90 ) go east/west. Auxiliary highways are numbered 100-999, and service the primary highway indicated by the rightmost two digits. Thus, I-405 services I-5, and I-290 services I-90. Note: 200 is not a valid auxiliary highway because 00 is not a valid primary highway number. Given a highway number, indicate whether it is a primary or auxiliary highway. If auxiliary, indicate what primary highway it serves. Also indicate if the (primary) highway runs north/south or east/west. Ex: If the input is: 90 the output is: Ex: If the input is: 290 the output is: Ex: If the input is: 0 the output is: Ex: If the input is: 200 the output is:

1 Answer

4 votes

Use conditional statements to check if the input is valid, primary, or auxiliary. Use modulo operator to determine if the highway runs north/south or east/west.

To code this in C++, you can use conditional statements and string concatenation to generate the output based on the given highway number. Here's an example code:

#include <iostream>

#include <string>

using namespace std;

int main() {

int highwayNum;

string output;

cout << "Enter the highway number: ";

cin >> highwayNum;

if (highwayNum >= 1 && highwayNum <= 99) {

output = "I-" + to_string(highwayNum) + " is primary, going ";

if (highwayNum % 2 == 0) {

output += "east/west.";

} else {

output += "north/south.";

}

} else if (highwayNum >= 100 && highwayNum <= 999 && highwayNum % 100 != 0) {

int primaryNum = highwayNum % 100;

output = "I-" + to_string(highwayNum) + " is auxiliary, serving I-" + to_string(primaryNum) + ", going ";

if (primaryNum % 2 == 0) {

output += "east/west.";

} else {

output += "north/south.";

}

} else {

output = to_string(highwayNum) + " is not a valid interstate highway number.";

}

cout << output << endl;

return 0;

}

The code first prompts the user to enter the highway number, then uses conditional statements to determine if the number is a valid primary or auxiliary highway number. The output is generated using string concatenation based on the type of highway and its direction. If the highway number is invalid, the output indicates so. Finally, the output is displayed to the user using the cout statement.

User Janely
by
7.4k points