Function Exercise 2

On this page, you will get some extra function exercises and solutions.


Exercise 1:

Write a function to print a custom message.

Sample Input Sample Output
This is a function

Solution:


   #include<stdio.h>
   void print_function(){
    printf("This is a function\n");
   }
   int main(){
    print_function();
    return 0;
   }


Output:


    This is a function  



Exercise 2:

Write a function to print an input character value

Sample Input Sample Output
3 Value received from main: 3
A Value received from main: A

Solution:


   #include<stdio.h>
   void print_char(char ch){
    printf("Value received from main: %c\n",ch);
   }
   int main(){
    char ch;
    scanf("%c",&ch);
    print_char(ch);
    return 0;
   }


Output:


  Value received from main: A



Exercise 3:

Write a program (WAP) that will take n integers into an array A and m positive integers into array B. Now find the intersection (set operation) of array A and B.

Sample Input Sample Output
80 33 27 Sum In Function: 140
Sum In Main: 140
100 -100 Sum In Function: 0
Sum In Main: 0

Solution:


    #include<stdio.h>
    int add(int arr[],int n){
      int sum = 0;
      for (int i = 0; i < n; i++) {
         sum+=arr[i];
      }
      printf("Sum In Function: %d\n",sum);
    }
  
    int main() {
      int arr[100];
      int n = 0, sum=0;
      while (scanf("%d", &arr[n]) != EOF) {
          n++;
      }
      for (int i = 0; i < n; i++) {
         sum+=arr[i];
      }
       add(arr, n);
       printf("Sum In Main: %d\n",sum);
  
      return 0;
    }


Output:


  80 33 27
  ^Z
  Sum In Function: 140
  Sum In Main: 140

[N:B Please enter ^Z whenever input is done



Exercise 4:

Write a function to calculate the sum of n numbers coming from the console and stored in an array.

Sample Input Sample Output
3
80 33 27
Sum In Function: 140
Sum In Main: 140
2
100 -100
Sum In Function: 0
Sum In Main: 0

Solution:


    #include<stdio.h>
    int add(int arr[],int n){
      int sum = 0;
      for (int i = 0; i < n; i++) {
         sum+=arr[i];
      }
      return sum;
  }
  
  int main() {
      int arr[100];
      int n, sum=0;
      scanf("%d",&n);
  
      for (int i = 0; i < n; i++) {
         scanf("%d",&arr[i]);
         sum+=arr[i];
      }
       printf("Sum In Main: %d\n",add(arr, n));
       printf("Sum In Main: %d\n",sum);
  
      return 0;
  }
  


Output:


  2
  100 -100  
  Sum In Function: 0
  Sum In Main: 0



Exercise 5:

Write a function to swap two numbers. (Restriction: Pass by value)

Sample Input Sample Output
10 20 Value in func: 20 10
Value in main: 10 20

Solution:


  #include<stdio.h>
  void swap(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
    printf("Value in func: %d %d\n", a, b);
  }
  int main() {
      int num1, num2;
      scanf("%d %d", &num1, &num2);
      printf("Value in main: %d %d\n", num1, num2);
      swap(num1, num2);
      return 0;
  }


Output:


  10 20
  Value in func: 20 10
  Value in main: 10 20



Exercise 6:

Write a function to swap two numbers. (Restriction: Pass by reference.)

Sample Input Sample Output
10 20 Value in func: 20 10
Value in main: 10 20

Solution:


  #include<stdio.h>
  void swap(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
  }

  int main() {
    int x = 10, y = 20;
    printf("Value in main: %d %d\n", x, y);
    swap(&x, &y);
    printf("Value in main: %d %d\n", x, y);
    return 0;
  }


Output:


  10 20
  Value in func: 20 10
  Value in main: 10 20



Exercise 7:

Write a function to determine only even numbers in an array of input integers.

Sample Input Sample Output
24 77 117 -512 1024 24 -512 1024
45 33 0 256 0 256

Solution:


  #include<stdio.h>
  int print_even(int arr[],int n){
    for (int i = 0; i < n; i++) {
      if(arr[i]%2==0){
          printf("%d ",arr[i]);
      }
    }
  }

  int main() {
    int arr[100];
    int n = 0;
    while (scanf("%d", &arr[n]) != EOF) {
      n++;
    }
    print_even(arr, n);
    return 0;
  }


Output:


  10 20
  Value in func: 20 10
  Value in main: 10 20



Exercise 8:

Write a function to swap two numbers. (Restriction: Pass by value)

Sample Input Sample Output
157 -28 -37 26 10 Minimum Value: -37

Solution:


  #include<stdio.h>
  #include
    int find_min(int arr[], int n){
      int min = 32000;
      for (int i = 0; i < n; i++) {
         if(arr[i] < min){
            min = arr[i];
         }
      }
      printf("Minimum Value: %d\n",min);
    }
  
    int main() {
      int arr[100];
      int n = 0;
      while (scanf("%d", &arr[n]) != EOF) {
          n++;
      }
     
      find_min(arr, n);
      return 0;
    }


Output:


  157 -28 -37 26 10
  ^Z
  Minimum Value: -37



Exercise 9:

Write a function that multiplies the array elements by 2 and returns the array.

Sample Input Sample Output
157 -28 -37 26 10 314 -56 -74 52 20

Solution:


  #include<stdio.h>
  #include<stdlib.h>
  int* m2x(int arr[], int n){
    int *result = malloc(n * sizeof(int));
    for (int i = 0; i < n; i++) {
      result[i] = arr[i]*2;
     }
   return result;
  }

  int main() {
    int arr[100];
    int n = 0;
    while (scanf("%d", &arr[n]) != EOF) {
        n++;
    }

    int *result= m2x(arr, n);
    for (int i = 0; i < n; i++) {
      printf("%d ",result[i]);
     }
    return 0;
  }


Output:


  157 -28 -37 26 10
  ^Z
  314 -56 -74 52 20



Exercise 10:

Write a function to sort and return an input array in ascending order.

Sample Input Sample Output
10 22 -5 117 0 -5 0 10 22 117

Solution:


  #include<stdio.h>
  #include<stdlib.h>
  int* sortArray(int arr[], int size) {
    int temp;
    for(int i=0; i < size-1; i++) {
        for(int j=i+1; j < size; j++) {
            if(arr[i] > arr[j]) {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
    return arr;
  }

  int main() {
    int n = 0, arr[1000];
    while (scanf("%d", &arr[n]) != EOF) {
        n++;
    }
    int* sortedArr = sortArray(arr, n);

    for(int i=0; i < n; i++) {
        printf("%d ", sortedArr[i]);
    }
    return 0;
  }


Output:


  10 22 -5 117 0
  ^Z
  -5 0 10 22 117