Question

Project 1, Program Design 1. Write a C program replace.c that asks the user to enter...

Project 1, Program Design

1. Write a C program replace.c that asks the user to enter a three-digit integer and then replace each digit by the sum of that digit plus 6 modulus 10. If the integer entered is less than 100 or greater than 999, output an error message and abort the program. A sample input/output:

Enter a three-digit number: 928

Output: 584

2. Write a C program convert.c that displays menus for converting length and calculates the result. The program should support the following conversions:

Miles Kilometers 1.6093

Kilometers Miles 0.6214

Inches Centimeters 2.54

Centimeters Inches 0.3937

1) Display the menu options as numbers.

1 - Miles to Kilometers

2 – Kilometers to Miles

3 - Inches to Centimeters

4 – Centimeters to Inches

2) Asks the user to select an option. Use a switch statement for option selection.

3) Asks the user to enter the length that’s converting from, calculate the result, and display the output. For example, if user selected 1 for option selection, your program is supposed to ask for the number of miles, and display the corresponding kilometers.

4) If the entered option is not in the range of 1 to 4, display an error message and abort the program.

5) The output should display two digits after the decimal point. For example, 3.23.

Before you submit:

1. Compile with –Wall. –Wall shows the warnings by the compiler. Be sure it compiles on circe with no errors and no warnings.

gcc –Wall replace.c

gcc –Wall convert.c

2. Be sure your Unix source file is read & write protected. Change Unix file permission on Unix:

chmod 600 replace.c

chmod 600 convert.c

3. Test your program with the shell script try_replace and try_convert on Unix:

chmod +x try_replace

./try_replace

chmod +x try_convert

./try_convert

4. Download the programs from circe and submit replace.c and convert.c on Canvas>Assignments.

Grading

Total points: 100 (50 points each problem)

1. A program that does not compile will result in a zero.

2. Runtime error and compilation warning 5%

3. Commenting and style 15%

4. Functionality 80%

Programming Style Guidelines

The major purpose of programming style guidelines is to make programs easy to read and understand. Good programming style helps make it possible for a person knowledgeable in the application area to quickly read a program and understand how it works.

1. Your program should begin with a comment that briefly summarizes what it does. This comment should also include your name.

2. In most cases, a function should have a brief comment above its definition describing what it does. Other than that, comments should be written only needed in order for a reader to understand what is happening.

3. Variable names and function names should be sufficiently descriptive that a knowledgeable reader can easily understand what the variable means and what the function does. If this is not possible, comments should be added to make the meaning clear.

4. Use consistent indentation to emphasize block structure.

5. Full line comments inside function bodies should conform to the indentation of the code where they appear.

6. Macro definitions (#define) should be used for defining symbolic names for numeric constants. For example: #define PI 3.141592

7. Use names of moderate length for variables. Most names should be between 2 and 12 letters long.

8. Use underscores to make compound names easier to read: tot_vol or total_volumn is clearer than totalvolumn.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

replace.c

---------------------------

#include <stdio.h>
#include <math.h>
/**
* Name : <Your Name>
* Progarme Name : Replace
* Description : Get three-digit integer and then replace each digit by the sum of that digit plus 6 modulus 10
*/
#define NUM_DIGITS 3
//3- Digit input
int input ;
//Output
int result;

int first_digit, second_digit,third_digit;
int main(int argc, char *argv[])
{
    //Ask user to enter a Integer
    printf("Enter 3 Digit Input.\n");

    //Read From Console
    int read_result = scanf("%d", &input);

    //Validate the input
    if ((read_result == EOF) ||
            read_result == 0 ||
            input < 100 ||
            input > 999) {
        printf("Enter Proper Integer between 100 ... 999\n");
        exit(0);
    }
    //Factor to devide the input to get each digit
    int factor = pow(10,NUM_DIGITS);
    // Array to store the digits
    int digits[NUM_DIGITS];
    //Digits index
    int digit_index = 0;
    //Get the digits
    do
    {
        factor /= 10;
        digits[digit_index] = (input / factor);
        input %= factor;
        digit_index++;
    } while (input);

    //Convert the digits
    digit_index = 0;
    for(int i = (NUM_DIGITS -1) ; i > -1 ; i--){
        result += (( digits[digit_index] + 6 )%10) * pow(10,i);
        digit_index++;
    }
    printf("Result is %d", result);
    return 0;
}


convert.c

---------------------

#include <stdio.h>
/**
* Name : <Your Name>
* Progarme Name : Conver
* Description : Convert distances in one form to another form
*/
#define MILES_TO_KM 1
#define KM_TO_MILES 2
#define INCHES_TO_CM 3
#define CM_TO_INCHES 4
#define MILEKM 1.6093
#define KMTOMILE 0.6214
#define INCHTOCM 2.54
#define CMTOINCH 0.3937
int choice ;
float input,result;
int main(int argc, char *argv[])
{
    //Ask user to enter a choice
    printf("Enter a Chouce.\n");
    printf("1 - Miles to Kilometers \n");

    printf("2 – Kilometers to Miles \n");

    printf("3 - Inches to Centimeters \n");

    printf("4 – Centimeters to Inches \n");

    //Read From Console
    int read_result = scanf("%d", &choice);

    //Validate the input
    if ((read_result == EOF) ||
            read_result == 0 ||
            choice < 0 ||
            choice > 5) {
        printf("Enter Proper choice between 1 ... 4\n");
        exit(0);
    }
    //Ask user to enter a Distance
    printf("Enter Distance Input.\n");

    //Read From Console
    read_result = scanf("%f", &input);

    //Validate the input
    if ((read_result == EOF) ||
            read_result == 0
            ) {
        printf("Enter Proper Distance\n");
        exit(0);
    }
    switch(choice){
    case MILES_TO_KM :
            result = MILEKM * input;
            printf("%.2f Miles is equal to %.2f KM \n", input, result);
        break;
    case KM_TO_MILES :
        result = KMTOMILE * input;
        printf("%.2f K.M is equal to %.2f Miles \n", input, result);
        break;
    case INCHES_TO_CM :
        result = INCHTOCM * input;
        printf("%.2f Inches is equal to %.2f Centimeters \n", input, input);
        break;
    case CM_TO_INCHES :
        result = CMTOINCH * input;
        printf("%.2f Centimeters is equal to %.2f Inches \n", input, input);
        break;
    default :
    break;
    }

    return 0;
}

Add a comment
Know the answer?
Add Answer to:
Project 1, Program Design 1. Write a C program replace.c that asks the user to enter...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Design a modular program that asks the user to enter the monthly costs for the following...

    Design a modular program that asks the user to enter the monthly costs for the following expenses incurred from operating his or her automobile: loan payment, insurance, gas, oil, tires, and maintenance. The program should then display the total monthly cost of these expenses, the total trimester amount, the total semester amount and the total annual cost of these expenses. Submission should have: Source Code in PY format (No accepted screenshots of source code anymore) It should contain proper indentation...

  • Write a program that asks the user to enter a string and then asks the user...

    Write a program that asks the user to enter a string and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the string. python programming

  • Programming language C. A small U-Pick farm sells five different U-Pick citrus fruit products whose retail...

    Programming language C. A small U-Pick farm sells five different U-Pick citrus fruit products whose retail prices are: 1. Sugarbells - $1.99/lb, 2. Honeybells - $2.39/lb, 3. Red Grapefruit $1.69/lb, 4. Navel Oranges - $1.49/lb, 5. Pomelo - $1.89/lb. Write a program that allows the user to select one or more products, input the weights of each product, and calculate the total amount due. The farm only accepts cash. Your program will take input of the cash received and calculate...

  • Write a Program that has a menu with a  layout below, that asks the user if they...

    Write a Program that has a menu with a  layout below, that asks the user if they want to: 1. Converts from feet and inches to meter and centimeters 2. Converts from meter and centimeters to feet and inches 3. Quit the program The program should continue as long as the user asks it to. The program will use a switch statement for the menu option selection. Either a do loo or a do-while loop can be used for the overall...

  • Python Programming 4th Edition: Write a program that asks the user for the number of feet....

    Python Programming 4th Edition: Write a program that asks the user for the number of feet. The program converts those feet to inches by using the formula ’12 * feet’. Hint: Define main () program Get the number of feet from the user Define variables Call a function feet_to_inches(feet) The function receives the number of feet and returns the number of inches in that many feet. The output should look like as follows: Enter the number of feet: If the...

  • c++ Write a program that prompts the user to enter a length in feet and then...

    c++ Write a program that prompts the user to enter a length in feet and then enter a length in inches and outputs the equivalent length in centimeters. If the user enters a negative number or a non-digit number, throw and handle an appropriate exception and prompt the user to enter another set of numbers. Your error message should read A non positive number is entered and allow the user to try again.

  • Design a program that asks the user to enter a series of 20 numbers. The program...

    Design a program that asks the user to enter a series of 20 numbers. The program should store the numbers in the array and then display the following data: The lowest number in the array The highest number in the array The total of the number in the array The average of the numbers in the array PLEASE GIVE THE PSEUDOCODE AND CODE IN C PROGRAMMING

  • in C++, Design a program that asks the user to enter a password, and then analyze...

    in C++, Design a program that asks the user to enter a password, and then analyze the password, and then analyze the password for the following weaknesses: 1. Fewer than 8 characters 2. Does not contain at least one uppercase letter and one lowercase latter 3. Does not contain at least one numeric digit 4. Does not contain at least one special character( a character that is not a letter or numeric digit) 5. Is a sequence of consecutive uppercase...

  • Design a program in pseudocode that asks the user to enter a string containing a series...

    Design a program in pseudocode that asks the user to enter a string containing a series of ten single-digit numbers with nothing separating them. The program should display the sum of all the single digit numbers in the string except for the highest value digit. You may only use one main module and one sorting function; otherwise, do not modularize your program. For example, if the user enters 5612286470, the sum would be 33. (0+1+2+2+4+5+6+6+7) If there are multiple highest...

  • Write a C program that asks the user to enter two real numbers. Then your program...

    Write a C program that asks the user to enter two real numbers. Then your program displays a menu that asks the user to choose what arithmetic operation to be done on those numbers. Depending on the user's entry, the program should display the result to the screen. The sample runs below show what should be done to the numbers entered by the user. Your program should run exactly like shown in the sample runs. make your code run as...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT