﻿ C exercises and solutions: function in C

# 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.
2. Subtract
3. Multiply
4. Divide
5. Modulus
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>

### 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;
}

``````