In: Computer Science
C language and that must contain the following:
- Error Checking
- Use of Functions
- Menu system
- Array Processing
#include
float addOp(int a,int b)
{
return (a+b);
}
float subOp(int a,int b)
{
return (a-b);
}
float mulOp(int a,int b)
{
return (a*b);
}
float divOp(int a,int b)
{
return (float)a/b;
}
int main()
{
int a,b,ch;
int array[3];
float div ;
while(1)
{
printf("\n1. addition ");
printf("\n2. subtraction ");
printf("\n3. multiplication ");
printf("\n4. division ");
printf("\n5. exit ");
printf("\nEnter your choice ");
scanf("%d",&ch);
if(ch!=5)
{
printf("\nenter value of a ");
scanf("%d",&a);
printf("\nenter value of b ");
scanf("%d",&b);
if(ch==1)
{
div=(a+b);
printf("%d + %d= %f ",a,b,div);
}
if(ch==2)
{
div=subOp(a,b);
printf("%d - %d= %f ",a,b,div);
}
if(ch==3)
{
div=mulOp(a,b);
printf("%d * %d =%f ",a,b,div);
}
if(ch==4)
{
if(b==0) //error processing
printf("error division by 0 yields infinity \n");
else
{
div=divOp(a,b);
printf("%d / %d =%f ",a,b,div);
}
}
}
else
{
//use of array to store the last input and output
array[0]=a;
array[1]=b;
array[2]=div;
printf("the last input was %d and %d ",array[0],array[1]);
printf("\nThe last output was %f ",div);
break;
}
}
return 0;
}