Answer to Question #245000 in C for joe

Question #245000

Write a C program to Create an array of (void * -type) pointers of length 5. Each of these pointers should be pointing to individual functions that perform operations like addition, subtraction, division, multiplication, exponentiation. 

add(){   sub(){           mul(){      div(){                       exp(){

 }     }     }                 }                             }


These functions could take arguments just like regular functions.




1
Expert's answer
2021-09-30T14:23:19-0400


#include <stdio.h>
#include <math.h>


void add(int a, int b) {
	printf("%d + %d = %d\n",a,b,(a+b));
}


void sub(int a, int b) {
	printf("%d - %d = %d\n",a,b,(a-b));
}
void div(int a, int b) {
	printf("%d / %d = %.2f\n",a,b,((float)a/(float)b));
}
void mul(int a, int b) {
	printf("%d * %d = %d\n",a,b,(a*b));
}
void exp(int a, int b) {
	printf("%d ^ %d = %.2f\n",a,b,pow((float)a,(float)b));
}


int main() {
	int i;
	int number1;
	int number2;
	typedef void (*type)(int, int); 
	type functions[] = {&add, &sub, &div, &mul, &exp};


	printf("Enter number 1: ");
	scanf("%d",&number1);
	printf("Enter number 2: ");
	scanf("%d",&number2);


	for (i = 0; i < 5; ++i){
		functions[i](number1, number2);
	}


	getchar();
	getchar();
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS