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

Programming in C Write a small program that calculates the sum, the difference a

ID: 3863497 • Letter: P

Question

Programming in C

Write a small program that calculates the sum, the difference and the product of two doubless with four user-defined functions:

//takes two double arguments and returns the sum of the two arguments
double CalculateSum (double num1, double num2);

//takes two double arguments and returns the difference of the two arguments
double CalculateDifference(double num1, double num2);

//takes two double arguments and returns the product of the two arguments
double CalculateProd (double num1, double num2);

//takes two double arguments and two double pointer arguments
//1. calculate the sum and stores the result in sumPtr
//2. calculate the difference and store the result in diffPtr
//3. calculate the product and stores the result in prodPtr
void CalculateBoth(double num1, double num2, double* sumPtr, double *diffPt, double* prodPtr);

Prompt the user for 2 doubles. Call all four functions from main.
Print the results in both the function definitions (value at) and back in the main function.
DO NOT DECLARE ANY POINTERS IN THE MAIN FUNCTION!

Explanation / Answer

#include <stdio.h>

//takes two double arguments and returns the sum of the two arguments
double CalculateSum (double num1, double num2)
{
    return (num1 + num2);
}

//takes two double arguments and returns the difference of the two arguments
double CalculateDifference(double num1, double num2)
{
    return (num1 - num2);
}

//takes two double arguments and returns the product of the two arguments
double CalculateProd (double num1, double num2)
{
    return (num1 * num2);
}

//takes two double arguments and two double pointer arguments
//1. calculate the sum and stores the result in sumPtr
//2. calculate the difference and store the result in diffPtr
//3. calculate the product and stores the result in prodPtr
void CalculateBoth(double num1, double num2, double* sumPtr, double *diffPt, double* prodPtr)
{
    *sumPtr = num1+num2;
    *diffPt = num1-num2;
    *prodPtr = num1*num2;
  
  
}

int main(void)
{
    double num1,num2;
    double sum,diff,prod;
    printf("Enter two numbers");
    scanf("%lf %lf",&num1,&num2);
  
    printf(" Sum : %.3lf",CalculateSum(num1,num2));
    printf(" Difference : %.3lf",CalculateDifference(num1,num2));
    printf(" Product : %.3lf",CalculateProd(num1,num2));
  
  
    printf(" Using pointers :");
  
    CalculateBoth(num1,num2,&sum,&diff,&prod);//send addresses of variables
    printf(" Sum : %.3lf",sum);
    printf(" Difference : %.3lf",diff);
    printf(" Product : %.3lf",prod);
  
  
   return 0;
}


Output:

Enter two numbers 45.676 32.987

Sum : 78.663

Difference : 12.689

Product : 1506.714

Using Pointers :

Sum : 78.663

Difference : 12.689

Product : 1506.714