C exercises and solutions: function in C


Function in C exercises

1. Write C code that will display the calculator menu.

The program will prompt the user to choose the operation choice (from 1 to 5). Then it asks the user to input two integer vales for the calculation. See the sample below.
  MENU
          1. Add
          2. Subtract
          3. Multiply
          4. Divide
          5. Modulus
Enter your choice: 1
Enter your two numbers: 12 15
Result: 27
 
Continue? y
 
The program also asks the user to decide whether he/she wants to continue the operation. If he/she input ‘y’, the program will prompt the user to choose the operation gain. Instead, the program will terminate.

Solution:
#include <stdio.h>  	
#include <stdlib.h>
 
void displaymenu(){
printf("================================\n");
printf("      MENU             \n");
printf("================================\n");
printf("     1.Add\n");
printf("     2.Subtract \n");
printf("     3.Multiply \n");
printf("     4.Divide \n");
printf("     5.Modulus \n");
}
int Add(int a,int b){
return(a+b);
}
 
int Substract(int a, int b){
return(a-b);
}
 
int Multiply(int a, int b){
return(a*b);
}
float Divide(int a,int b){
return(a/b);
}
int Modulus(int a, int b){
return(a%b);
}
 
int main(int argc, char *argv[])
{
//show menu
displaymenu();
int yourchoice;
int a;
int b;
char confirm;
do
{
printf("Enter your choice(1-5):");
scanf("%d:",&yourchoice);
printf("Enter your two integer numbers:");
scanf("%d %d",&a,&b);
printf("\n");
switch(yourchoice){
 case 1:printf("Result:%d",Add(a,b));break;
 case 2:printf("Result:%d",Substract(a,b));break;
 case 3:printf("Result:%d",Multiply(a,b));break;
 case 4:printf("Result:%.2f",Divide(a,b));break;
 case 5:printf("Result:%d",Modulus(a,b));break;
 default:printf("invalid");
     }
                  
printf("\nPress y or Y to continue:");
scanf("%s",&confirm);
}while(confirm=='y'||confirm=='Y');
  system("PAUSE");
  return EXIT_SUCCESS;
}

2. Write a C program that use the selection sort algorithm to sort an integer array in ascending order.


Solution:

#include <stdio.h>
#include <stdlib.h>
void mysort(int vallist[]){ 
int i,j; 
int min; 
for(i=0;i<10;i++){ 
min=i;
for(j=i+1;j<10;j++) 
if(vallist[min]>vallist[j]){ 
// search for the min item 
min=j;
} 
// swapping the min item with the beginning item of the array 
if(min!=i){
int temp=vallist[min];
vallist[min]=vallist[i];
vallist[i]=temp;

}
}
// Display array after being sorted
for(i=0;i<10;i++)printf("%d\n",vallist[i]); 
} 

int main(int argc, char *argv[]) 
{ 
int vallist[]={23,2,34,23,43,22,32,32,43,34}; 
mysort(vallist); 
system("PAUSE"); 
return EXIT_SUCCESS; 
} 

	



Comments

john comment

 john

#include <stdio.h>
#include <stdlib.h>


2015-08-06
charlie comment

 charlie

Does C supports 'function overloaded'?Why I have a compile error in this :
http://www.worldbestlearningcenter.com/index_files/c_functions_exercises.htm


2015-04-12



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.