Broad Network


Some Useful Functions Coding in the C Computer Language

Part 32 of the Complete C Course

Foreword: Some Useful Functions Coding in the C Computer Language

By: Chrysanthus Date Published: 22 Jun 2024

Introduction

Swapping Two Numbers
The numbers to be swapped must be of the same type: either two ints, two floats, two doubles, etc. Note: two chars can also be swapped, following the same scheme. Consider the following program:

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

    int main(int argc, char *argv[])
    {
        swap(a, b);
        printf("%i, %i\n", a, b);

        return 0;
    }

After including "stdio.h", the variables a and b are defined. The definition of the swap() function then follows. In the swap() function definition, the first statement assigns a to temp (temporary variable). With that, a can have the value of b, since the value of a is now safe in temp. The second statement assigns the value of b to a. Now that the value of b is held by a, b can then take the value of a (from temp). In the third statement, b takes the value of a from temp. temp remains with the value of a. After the swap() function is called, the temp variable is no longer useful. In the C main() function, the swap() function is called, with a and b as arguments. The predefined printf() function call, prints the swapped result, which is 5 for a and 3 for b.

Maximum of Two Values
A function can be written to find the maximum of two values. The entities to be compared must be of the same type: either two ints, or two floats, or two doubles, etc. Note: two chars can also be compared, following the same scheme. Consider the following program:

    #include <stdio.h>
    
    float a = 3.3;
    float b = 5.5;
    
    float max(float x, float y) {
         if (y > x)
             return y;
         else
             return x;
    }

    int main(int argc, char *argv[])
    {
        float maxflt = max(a, b);
        printf("%f\n", maxflt);

        return 0;
    }

The reader should read and test the program, if he/she has not already done so. At this point in the chapter, the program should be self-explanatory. The output is: 5.500000. It will be explained, how to remove the added extra decimal digits, later in this chapter.

Using the conditional operator, the code outside the main() function, can be erased and retyped differently, in the main() function as:

        float a = 3.3;
        float b = 5.5;
    
        float maxflt = b > a ? b : a ;
        printf("%f\n", maxflt);

Minimum of Two Values
A function can be written to find the minimum of two values. The entities to be compared must be of the same type: either two ints, or two floats, or two doubles, etc. Note: two chars can also be compared, following the same scheme. Consider the following program:

    #include <stdio.h>
    
    char a = 'p';
    char b = 'q';
    
    char min(char x, char y) {
         if (x < y)
             return x;
         else
             return y;
    }

    int main(int argc, char *argv[])
    {
        char minChar = min(a, b);
        printf("%c\n", minChar);

        return 0;
    }

The reader should read and test the program, if he/she has not already done so. At this point in the chapter, the program should be self-explanatory. The output is: 'p'. Characters (chars) are represented in the computers by integers. As characters ascend from 'A' to 'Z' and 'a' to 'z', so does the integers of the two groups.

Using the conditional operator, the code outside the main() function, can be erased and retyped differently, in the main() function as:

        char a = 'p';
        char b = 'q';
    
        char minChar = a < b ? a : b ;
        printf("%c\n", minChar);

Maximum Value in an Array
All the elements of the array must be of the same type. The maximum value in an array can be found using the temporary variable. Read and test the following program:

    #include <stdio.h>
    
    int maxArr(int arra[], int temp, int N) {
        for (int i=0; i<N; i++) {
            if (arra[i] > temp)
                temp = arra[i];
        }
        return temp;
    }

    int main(int argc, char *argv[])
    {
        int arr[] = {2, 2, 8, 0, 1};
        int maxVal = 0;
        int n = 5;    // number of elements in the array
        
        int maxInt = maxArr(arr, maxVal, n);
        printf("%i\n", maxInt);

        return 0;
    }

The idea is to first assume that the maximum value is the minimum possible value in the array, or any value less than the minimum. This value is the first value for the temporary variable. As the array is scanned from left to right, each element is compared with the temporary variable value. For each comparison, the value of the temporary variable, is replaced with the higher value. The maximum function returns the final value of the temporary variable, at the end of the scan.

Minimum Value in an Array
The minimum value of an array is found in a similar way to the maximum value. However, the idea here is to first assume that the minimum value is the maximum possible value in the array, or any value greater than the maximum. This value is the first value for the temporary variable. As the array is scanned from left to right, each element is compared with the temporary variable value. For each comparison, the value of the temporary variable, is replaced with the lower value. The minimum function returns the final value of the temporary variable, at the end of the scan. The if-coding this time is:

            if (arra[i] < temp)
                temp = arra[i];

Function to determine if a Character is a Vowel
The function returns Boolean true (1), if the char sent to it as argument is a vowel, or Boolean false (0), if the char sent to it as argument is not a vowel. Read and test the following program:

    #include <stdio.h>
    
    _Bool isVowel(char ch) {
        switch (ch) {
            case 'a': case 'A':
            case 'e': case 'E':
            case 'i': case 'I':
            case 'o': case 'O':
            case 'u': case 'U':
                return 1;
            default:
                return 0;
        }
    }

    int main(int argc, char *argv[])
    {
        _Bool bl1 = isVowel('e');
        _Bool bl2 = isVowel('R');
        
        printf("%i\n", bl1);
        printf("%i\n", bl2);

        return 0;
    }

The output is 1 and 0 in two separate lines, confirming that 'e' is a vowel and 'R' is a consonant. In a previous section of this chapter, it was said that a case label in a switch statement, should be followed by its body (in braces). Well, if all the case labels have the same body, then all the case labels should be typed, followed by one body, as in the above code. The one body is just one statement, "return 1;" . Lowercase and uppercase case labels have been used, because a lowercase and its corresponding uppercase letters, are represented in the computer by different integers.



Related Links

More Related Links

Cousins

BACK NEXT

Comments