Day: February 3, 2018

  • Sorting Array Elements Using Bubble Sort in C Programming

    In previous post we discussed about how to pass an array into function. In following simple program we will create a function which will accept an array as an input. We will pass an array as input to a function called bubblesort. That function will then sort the elements of the array in ascending order.

    [js]

    #include <stdio.h>

    void bubbleSort(int arr[], int size); // function prototype

    int main(void){

    int arrayName[10] = {90,44,33,83,49,34,51,84,56,44}; // defined an array with 10 elements

    //output each element of array
    printf("Original values of the arraynn");
    printf("Array indextttValuen");
    for(int j=0; j<10; j++){
    printf("%11dttt%4dn", j, arrayName[j]);
    }

    bubbleSort(arrayName, 10); // function call

    //output each element of array
    printf("nnValues of the array after passing to the bubbleSort functionnn");
    printf("Array indextttValuen");
    for(int j=0; j<10; j++){
    printf("%11dttt%4dn", j, arrayName[j]);
    }

    return 0;
    }

    //function definition
    void bubbleSort(int arr[], int size){
    //BubbleSort algorithm will sort the array elements in ascending order. Outer loop is for passes and inner loop will do comparisons
    for(int l=1; l<=size; l++){
    for(int m=0; m<size-1; m++){
    if(arr[m]>arr[m+1]){
    int temp;
    temp = arr[m];
    arr[m] = arr[m+1];
    arr[m+1] = temp;
    }
    }
    }
    }

    [/js]

     

    Output:

  • Passing Arrays into Functions

    In previous two posts we discussed about arrays and functions. In following simple program we will create a function which will accept an array as an input. Furthermore that function will multiply each element of the array with 20. At the and it outputs the elements of the array.

    It’s to be noted that when we pass an array into function, then by default it’s a function call by reference. Which means that any processing done by the called function will change/modify the values of the original array elements.

    [js]

    #include <stdio.h>

    void modifyArray(int arr[], int size); // function prototype

    int main(void){

    int arrayName[10] = {2,5,3,6,9,34,23,84,56,44}; // defined an array with 10 elements

    //output each element of array
    printf("Original values of the arraynn");
    printf("Array indextttValuen");
    for(int j=0; j<10; j++){
    printf("%11dttt%4dn", j, arrayName[j]);
    }

    modifyArray(arrayName, 10); // function call

    //output each element of array
    printf("nnValues of the array after passing to the functionnn");
    printf("Array indextttValuen");
    for(int j=0; j<10; j++){
    printf("%11dttt%4dn", j, arrayName[j]);
    }

    return 0;
    }

    //function definition
    void modifyArray(int arr[], int size){
    //multiplied each element of array with 20
    for(int i=0; i<size; i++){
    arr[i] = arr[i] * 20;
    }
    }

    [/js]

     

    Output:

  • Arrays in C Programming

    Arrays in C Programming: Array is a type of variable which can store multiple items of the same data type. In following simple program we will define an array of 10 elements. After that we will multiply each element of array with 5 and then we will output the value of each element of the array.

    [js]

    #include <stdio.h>

    int main(void){

    int arrayName[10] = {2,5,3,6,9,34,23,84,56,44}; // defined an array with 10 elements

    //output each element of array
    printf("Original values of the arraynn");
    printf("Array indextttValuen");
    for(int j=0; j<10; j++){
    printf("%11dttt%4dn", j, arrayName[j]);
    }

    //multiplied each element of array with 5
    for(int i=0; i<10; i++){
    arrayName[i] = arrayName[i] * 5;
    }

    //output each element of array
    printf("nnValues of the array after multiplicationnn");
    printf("Array indextttValuen");
    for(int j=0; j<10; j++){
    printf("%11dttt%4dn", j, arrayName[j]);
    }

    return 0;
    }

    [/js]

     

    Output: