Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Converting lengths and weights Using only #include <stdio.h>, write a C program

ID: 674362 • Letter: C

Question

Converting lengths and weights

Using only #include <stdio.h>, write a C program that asks the user if he or she wants to convert values that are lengths or weights. If the user chooses lengths, the program calls a stub function convert_lengths that simply indicates the user’s choice. If the user chooses weights, the program calls a stub function convert_weights that simply indicates the user’s choice. Use the value 1 to indicate lengths and the value 2 to indicate weights. If the user enters 0, terminate the program. The program should continue to prompt the user for input until he or she correctly enters a value of 0, 1, or 2. In addition, the program should allow the user to call convert_lengths or convert_weights as many times as desired (i.e., until a value of 0 is input, indicating that the user is finished). Notice that this style of loop does not require you to ask the user if they want to continue but instead assumes they want to continue until they select 0.

The function convert_lengths will ask the user if he or she wants to convert lengths from feet/inches to meters/centimeters (input value of 1) or from meters/centimeters to feet/inches (input value of 2). An input value of 0 indicates that the user no longer wants to convert length measurements. Based on the user’s input, the convert_lengths function will call a stub function length_to_metric for an input value of 1, a stub function length_to_us for an input value of 2, and return control to the main program for an input value of 0. Returning control to the main program simply requires your function to return. You should not ever call the function main from your program. The convert_lengths function continues to prompt the user for input until he or she correctly enters a value of 0, 1, or 2. The new stub functions should simply indicate the user’s choice by printing an appropriate message. The function convert_weights will ask the user if he or she wants to convert weights from pounds/ounces to kilograms/grams (input value of 1) or from kilograms/grams to pounds/ounces. An input value of 0 indicates that the user no longer wants to convert weight measurements. Based on the user’s input, the convert_weights function will call a stub function weight_to_metric for an input value of 1, a stub function weight_to_us for an input value of 2, and return control to the main program for an input value of 0. The convert_weights function continues to prompt the user for input until he or she correctly enters a value of 0, 1, or 2. The new stub functions should simply indicate the user’s choice by printing an appropriate message.

Explanation / Answer


#include <stdio.h>
#include <stdlib.h>

void introduction();
bool get_input (int* choice);
void get_output (int choice, double lengths, double weights, double* value);
double convert_lengths (int choice, double value, double lenghths);
double convert_weights (int choice, double value, double weights);

int main(int argc, const char * argv[])
{
int choice(1);
double value(0), lengths(0), weights(0);

introduction();

while (choice !=0)
{
if(get_input(choice))
get_output(choice, lengths, weights, value);
}
return 0;
}

void introduction()
{
printf("Would you like to convert lengths or weights? ");
printf("Please emter 1 for lengths, 2 for weights, and 0 if you wish to end the program: ");

}

get_input(int* choice)
{
printf("Please enter your choice: ");
scanf("%d",choice);

return choice !=0;
}

void get_output(int choice, double lengths, double weights, double* value)
{
switch (choice)
{
case 1:
value = convert_lengths(choice, value, lengths);
break;

case 2:
value = convert_weights(choice, value, weights);
break;

}
}

double convert_lengths(int choice, double value, double lengths)
{
return 1;
}

double convert_weights(int choice, double value, double weights)
{
return 2;
}