Exercise 2 (35Pts). (Pointer and function) Write a C function that receives two
ID: 3845231 • Letter: E
Question
Exercise 2 (35Pts). (Pointer and function) Write a C function that receives two integer pointers. The function should calculate and return the result from the following equation: f(a,b)- a2/2 b3/3 Write a C function that receives one integer pointer and one float integer. The function should calculate and print the result from the following equation: fa,b) 2 a cos(b) in that a is the integer value and b is the float value Write a C program that reads an option (option value can be i, 2 or 3). Option- 1: calls function 1 Option- 2: calls function 2 Option 3: exits and prints "Exit program... While option equals 1 or 2, repeat option choosing. Output example:Explanation / Answer
CODE:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define PI 3.14159265
// func1 defined here
float func1(int * a, int * b){
float a2 = pow(*a,2);
float b3 = pow(*b,3);
return ((a2/2)+(b3/3));
}
// func2 defined here
float func2(int *a, float b){
float val = PI/180.0;
return (2*(*a)-cos(b*val));
}
int main(){
int choice=0;
int a,b;
float c;
printf("Enter the choice ");
do{
printf("Option 1: calls function 1 ");
printf("Option 2: calls function 2 ");
printf("Option 3: exit ");
printf("Option: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Two integers: ");
scanf("%d%d",&a,&b);
printf(" f(%d,%d) = ",a,b);
printf("%f ",func1(&a,&b));
break;
case 2: printf("One integer and one float: ");
scanf("%d%f",&a,&c);
printf(" f(%d,%f) = ",a,c);
printf("%f ",func2(&a,c));
break;
case 3: printf("Exit program ");
exit(0);
break;
default: break;
}
}while(choice >0 && choice <4);
}
OUTPUT:
$gcc prog.c
$./a.out
Enter the choice
Option 1: calls function 1
Option 2: calls function 2
Option 3: exit
Option: 1
Two integers: 3 4
f(3,4) = 25.833334
Option 1: calls function 1
Option 2: calls function 2
Option 3: exit
Option: 2
One integer and one float: 3 60.0
f(3,60.000000) = 5.500000
Option 1: calls function 1
Option 2: calls function 2
Option 3: exit
Option: 3
Exit program
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.