Question

C program help: Write a C program that uses three functions to perform the following tasks:...

C program help:

Write a C program that uses three functions to perform the following tasks: – The first function should read the price of an item sold at a grocery store together with the quantity from the user and return all the values to main().

example:

#include


void getInput(int *pNum1, int *pNum2); // note: *pNum1 & *pNum2 are pointers to ints

int main ()
{

   int numDucks,numCats;

   getInput(&numDucks, &numCats); // passing addresses of num1 & num2 to getInput()

   printf("\nYou have %d ducks and %d cats\n\n",numDucks, numCats);

   return 0;
}

// Gets 2 numbers from user via pointers
void getInput(int *pNum1, int *pNum2)
{
   printf("Enter num ducks: ");
   scanf("%d",pNum1); // note no '&' on pNum1 because pNum1 is already an address
   printf("Enter num cats: ");
   scanf("%d",pNum2); // note no '&' on pNum2 because pNum2 is already an address
}

  The second function should calculate and return the total purchase price for the item.
The last function should display the price, quantity and the total purchase price. The user should be given a choice to continue with another item. If 'n' is entered the program displays the grand total price of all the items purchased and terminates.

Question: This is my code, why is always sum the first inputs, but it should be back to intital, and then get all sum of vaule. Thanks for your patient.

#include <stdio.h>
#include <math.h>
void readprice(int*quantity, int*price){
   printf("Enter the number of the items:\n");
   scanf("%d",quantity);/*read the number of items*/
   printf("Enter the price of item:\n");
   scanf("%d",price);/*read the price of items*/
}
void Display(int quantity,int price,int value)
{
   printf("the number of items: %d \nthe price of items: %d \nthe total value of items: %d\n",quantity,price,value);/*diplay the quantity price and value*/
}
int totalPrice(int quantity,int price)
{
   return price*quantity;/*the fuction for caculate the total price*/
}
void main ()
{
char Y_N;
   while(1){
   int quantity;
   int price;
   int sum=0;
   int value=0;
   readprice(&quantity,&price);
       value=value+totalPrice(quantity,price);
       Display(quantity,price,value);
       printf("Do you want run the program agaian ?, if not please enter(n):\n");
       scanf(" %c",&Y_N);
   if (Y_N=='n'){
       sum= sum+value;/* the sum value*/
       printf("the sum of total prices:%d",sum);
       system("pause");
   }
   }
}

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

thanks for the question, your problem overall looks good, but you have made mistake in declaring the sum and and also some calculation mistakes which i have corrected. Now the program is working fine.

Here is the updated code

===================================================================================

#include <stdio.h>

// no need to include math.h library

void readprice(int*quantity, int*price)

{

    printf("Enter the number of the items: ");

    scanf("%d",quantity);/*read the number of items*/

    printf("Enter the price of item: ");

    scanf("%d",price);/*read the price of items*/

}

void Display(int quantity,int price,int value)

{

    printf("The number of items: %d \nThe price of items: $%d \nthe total value of items: $%d\n",quantity,price,value);/*display the quantity price and value*/

}

int totalPrice(int quantity,int price)

{

    return price*quantity;/*the function for calculate the total price*/

}

void main ()

{

    char Y_N;

    int sum=0; // you need to declare it outside while loop

  while(1)

    {

        int quantity;

        int price=0;

        int value=0;

        readprice(&quantity,&price);

        value=totalPrice(quantity,price);

        sum= sum+value;/* the sum value*/

        Display(quantity,price,value);

        printf("Do you want run the program again ?, if not please enter(n):\n");

        scanf(" %c",&Y_N);

        if (Y_N=='n')

        {

            printf("the sum of total prices $:%d",sum);

            break; // break out of while loop

        }

   }

}

===================================================================================

Add a comment
Know the answer?
Add Answer to:
C program help: Write a C program that uses three functions to perform the following tasks:...
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
  • 13. Write the program with at least two functions to solve the following problem (excluding the...

    13. Write the program with at least two functions to solve the following problem (excluding the main function). The members of a board are considering voting for a tax increase for 100 items. They are considering an increase of 5% for each item. Write a program that will prompt for and accept the current price for each item, then calculate and display their individual price after tax increases. At the end of the program, print the total price before and...

  • C program. Using do-while loop for this question. Question: write a program to calculate the average...

    C program. Using do-while loop for this question. Question: write a program to calculate the average of first n numbers. output: Enter the value of n : 18 The sum of first 18 numbers =171 The average of first 18 numbers = 9.00 my code couldn't give me the right avg and sum. Please help. #include<stdio.h> int main() {     int num;     int count =0;     int sum=0;     float avg;      printf("Enter the value of n: ");    ...

  • C++ HELP I need help with this program. I have done and compiled this program in...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. I am using printf and scanf. Instead of printf and scanf use cin and cout to make the program run. after this please split this program in three files 1. bill.h = contains the class program with methods and variables eg of class file class bill { } 2. bill.cpp = contains the functions from class...

  • C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the class program with methods and variables 2. bill.cpp = contains the functions from class file 3. main.cpp = contains the main program. Please split this program into three files and make the program run. I have posted the code here. #include<iostream>...

  • C Programming: Write a program that inputs two strings that represent floating-point values, convert the strings...

    C Programming: Write a program that inputs two strings that represent floating-point values, convert the strings to double values. Calculate the sum,difference,and product of these values and print them. int main(){    // character string value array for user    char stringValue[10];       double sum=0.0;// initialize sum to store the results from 2 double values       double difference=0.0; // initialize difference to store the results from 2 double values       double product = 0.0; // intitialzie product...

  • Write a complete C program for an automatic teller machine that dispenses money. The user should...

    Write a complete C program for an automatic teller machine that dispenses money. The user should enter the amount desired (a multiple of 10 dollars) and the machine dispenses this amount using the least number of bills. The bills dispenses are 50s, 20s, and 10s. Write a function that determines how many of each kind of bill to dispense. When writing your function begin to pass your variables using pointers (or rather "pointing" to your data". Use the TimeSpace program...

  • **how can I make my program break if 0 is entered by user as last number...

    **how can I make my program break if 0 is entered by user as last number not always 10 numbers output should be enter 10 numbers:1 2 0 numbers entered by user are: 1 2 Largest number: 2 **arrays can ONLY be used in the main function, other than the main function pointers should be used #include #define N 10 void max(int a[], int n, int *max); int main (void) { int a [N], i , big; printf("enter %d numbers:",N);...

  • Debug the following matrix program in C: // Program to read integers into a 3X3 matrix...

    Debug the following matrix program in C: // Program to read integers into a 3X3 matrix and display them #include <stdio.h> void display(int Matrix[3][3],int size); int main(void) {         char size;         double Matrix[size][size+1];         printf("Enter 9 elements of the matrix:\n");         int i;         for (i = 0: i <= size: i++)     {       int j = 0;       for (; j <= size++; j++){         scanf("%d", matrix[i--][4])       }     }         Display(Matrix,9);         return 0; void...

  • Need help with this C program? I cannot get it to compile. I have to use...

    Need help with this C program? I cannot get it to compile. I have to use Microsoft Studio compiler for my course. It's a word game style program and I can't figure out where the issue is. \* Program *\ // Michael Paul Laessig, 07 / 17 / 2019. /*COP2220 Second Large Program (LargeProg2.c).*/ #define _CRT_SECURE_NO_DEPRECATE //Include the following libraries in the preprocessor directives: stdio.h, string.h, ctype.h #include <stdio.h> /*printf, scanf definitions*/ #include <string.h> /*stings definitions*/ #include <ctype.h> /*toupper, tolower...

  • Use only C Program for this problem. Must start with given codes and end with given...

    Use only C Program for this problem. Must start with given codes and end with given code. CHALLENGE ACTIVITY 9.4.3: Input parsing: Reading multiple items. Complete scanf( to read two comma-separated integers from stdin. Assign userlnt1 and userInt2 with the user input. Ex: "Enter two integers separated by a comma: 3,5", program outputs: 3 + 5 = 8 1 #include <stdio.h> HNM 1 test passed int main(void) { int user Int1; int user Int2; All tests passed 000 printf("Enter two...

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
ADVERTISEMENT