I need explanation for the C code and little modification

Programming

c progrmming

Medical University of South Carolina

Question Description

Here is my C code for question 1:

#include <stdio.h>

int main(void) {
    //printing our menu
    printf("Please select from the list:\n");
    printf("1. Lemons - $0.21 each,\n");
    printf("2. Lettuce - $2.39 each,\n");
    printf("3. Potatoes - $0.79/lb,\n");
    printf("4. Beans - $0.99/lb,\n");
    printf("5. Avocado - $1.50 each,\n");
    printf("6. Tomatoes - $1.99/lb\n");
    int choice;
    float total_amount; //variable to store total amount
    printf("Enter selection: ");
    scanf("%d",&choice); //taking choice of user as input
    while(choice!=0) //we need to stop when user enters 0
    {
        switch(choice) //switch choice
        {
            case 1: { //when user enters 1.
                    int items;
                    printf("Enter number of items: ");
                    scanf("%d",&items); //taking input items
                    total_amount+=(0.21*items); //cost of 1 lemon is 0.21 so we multiply it with number of items
                    break;
            }
            case 2: { //when user enters 2
                    int items;
                    printf("Enter number of items: ");
                    scanf("%d",&items);
                    total_amount+=(2.39*items); //cost of 1 lettuce is 2.39 so we multiply it with number of lettuce
                    break;
            }
            case 3: { //when user enters 3
                    float weight;
                    printf("Enter weight (lb): ");
                    scanf("%f",&weight); //taking input weight of potatoes
                    total_amount+=(0.79*weight); //cost of 1 lb potatoes is 0.79 so we multiply it with weight
                    break;
            }
            case 4: { //when user enters 4
                    float weight;
                    printf("Enter weight (lb): ");
                    scanf("%f",&weight); //taking input weight of beans
                    total_amount+=(0.99*weight); //cost of 1 lb beans is 0.99 so we multiply it with weight
                    break;
            }
            case 5: { //when user enters 5
                    int items;
                    printf("Enter number of items: ");
                    scanf("%d",&items);
                    total_amount+=(1.50*items); //cost of 1 avocado is 1.50 so we multiply it with items
                    break;
            }
            case 6: { //when user enters 6
                    float weight;
                    printf("Enter weight (lb): ");
                    scanf("%f",&weight);
                    total_amount+=(1.99*weight); //cost of 1 lb tomatoes is 1.99 so we multiply it weight
                    break;
            }
            default: printf("Invalid selection, select from 1 to 6,enter 0 to stop selection\n"); //when user enters a wrong choice
        }
        printf("Enter selection: "); //taking input the choice of user
        scanf("%d",&choice);
    }
    printf("Amount due($): %.2f\n",total_amount); //total amount due
    float cash; //cash received
    printf("Enter cash received: ");
    scanf("%f",&cash); //taking input cash
    printf("Your change is $%.2f",cash-total_amount); //displaying the change that needs to be given
        return 0;
}

Here is my C code for question 2

C Program:

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

int main()
{
char ch=' ';

//Reading phone number
printf("Enter Phone Number: ");

//Reading input from user
while(ch != '\n')
{
//Reading a character
ch = getchar();

//Checking character
switch(ch)
{
//Checking corresponding character
case 'A':
case 'B':
case 'C': printf("2"); break;
case 'D':
case 'E':
case 'F': printf("3"); break;
case 'G':
case 'H':
case 'I': printf("4"); break;
case 'J':
case 'K':
case 'L': printf("5"); break;
case 'M':
case 'N':
case 'O': printf("6"); break;
case 'P':
case 'Q':
case 'R':
case 'S': printf("7"); break;
case 'T':
case 'U':
case 'V': printf("8"); break;
case 'W':
case 'X':
case 'Y':
case 'Z': printf("9"); break;
default: printf("%c", ch); break;
}
}

return 0;
}

I need explanation for the C code and little modification
attachment_1

Unformatted Attachment Preview

Project 2, Program Design 1. (50 points) Tessa has recently started a produce stand. The prices are: 1. Lemons - $0.21 each, 2. Lettuce - $2.39 each, 3. Potatoes $0.79/lb, 4. Beans - $0.99/lb, 5. Avocado - $1.50 each 6. Tomatoes - $1.99/lb Write a program to help Tessa calculate the charge. The program allows the user to select an item, input the weight or the number of items, and calculate the total amount due. Tessa only accepts cash. The program will take input of the cash received and calculate the change. An example input/output: Please select from the list: 1. Lemons - $0.21 each, 2. Lettuce - $2.39 each, 3. Potatoes $0.79/lb, 4. Beans - $0.99/lb, 5. Avocado - $1.50 each 6. Tomatoes - $1.99/lb Enter selection: 1 Enter number of items: 3 Enter selection: 3 Enter weight (lb): 2 Enter selection: 7 Invalid selection, select from 1 to 6, enter 0 to stop selection Enter selection: 4 Enter weight (lb): 1.6 Enter selection: 5 Enter number of items: 1 Enter product selection: 0 Amount due ($): 5.29 Enter cash received ($): 20 Your change is $14.71 1) Name your program produce_stand.c 2) If the user selects a number out of the range (1 through 6), display a message and allows the user to select again. When user enters 0, the program should exit from the loop for selection. 3) Use a switch statement to compute the amount due according to the selection(s). 2. (50 points) Some phone numbers are represented with letters. For example, (813)AIRPORT, represents (813)247-7678. The correspondence between numbers 2 through 9 on the telephone keypad and the letters are as follows: Digit Letter Digit Letter 2 ABC 6 MNO 3 DEF 7 PQRS 4 GHI 8 TUV 5 JKL 9 WXYZ Write a program that prompts the user to enter a phone number. The program translates a letter in the phone number to the corresponding digit. Other characters should remain unchanged. 1) Name your program translate.c. 2) The user input ends with the user pressing the enter key (a new line character). 3) Use getchar() to read in the input. Example input/output: Input: 1-727-PET-CARE Output: 1-727-738-2273 Before you submit: 1. Compile with –Wall. –Wall shows the warnings by the compiler. Be sure it compiles on student cluster with no errors and no warnings. gcc –Wall produce_stand.c gcc –Wall translate.c 2. Be sure your Unix source file is read & write protected. Change Unix file permission on Unix: chmod 600 produce_stand.c chmod 600 translate.c 3. Test your programs with the shell script on Unix: chmod +x try_produce_stand ./try_produce_stand chmod +x try_translate ./try_translate 4. Download produce_stand.c and transalte.c from the student cluster to your computer and submit on Canvas. Grading: Total points: 100 (50 points problem 1 and 50 points problem 2) 1. 2. 3. 4. A program that does not compile will result in a zero. Runtime error and compilation warning 5% Commenting and style 15% Functionality requirement 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 either underscores or capitalization for compound names for variable: tot_vol, total_volumn, or totalVolumn. ...
Student has agreed that all tutoring, explanations, and answers provided by the tutor will be used to help in the learning process and in accordance with Studypool's honor code & terms of service.

This question has not been answered.

Create a free account to get help with this and any other question!

Similar Questions
Related Tags