Question 01

Write a C program that finds the maximum value in a predefined array of integers. Let the array be equal to {10, 3, 45, 21, 7, 88, 32, 15}.

  • Use a for loop to traverse the array.

  • Use an if statement to check and update the current maximum value.

Answer 01

    #include <stdio.h>  
    int main() 
    { 
        int size = 8;
        
        // Array declaration
        int arr[size] = {10, 3, 45, 21, 7, 88, 32, 15};
        
        // Declare max and set it to first element
        int max = arr[0];
        
        // Loop through the array
        for (int i = 1; i < size; i++) 
        {
            // Compare array content against max
            if (arr[i] > max) 
            {
                // Update max
                max = arr[i];
            }
        }       

        // Print max value to check
        printf("Maximum value in the array is: %d\n", max);
        
        return 0;
    }

Question 02

Write a C program that copies an array of integers into another array. Your program should have an original array and a copy array. Make the array equal to {0, 7, 8, 10, -4, 5, 6, 9, 11, -32}.

Answer 02

    #include <stdio.h>
    
    int main() 
    {   
        // Declare the array
        int arr[10] = {0, 7, 8, 10, -4, 5, 6, 9, 11, -32};
        // Declare the copy
        int arrCopy[10];
        
        // Copy the array
        for (int i = 0; i < 10; i++) 
        {
            arrCopy[i] = arr[i];
        }
        
        // Print the copied array
        printf("Copy of array: ");
        for (int i = 0; i < 10; i++) 
        {
            printf("%d ", arrCopy[i]);
        }
        printf("\n");
        
        return 0;
    }

Question 03

Write a C program that copies an array of integers into another array in reverse order. Your program should have an original array and a reversed array. Make the array equal to {0, 7, 8, 10, -4, 5, 6, 9, 11, -32}.

Answer 03

        #include <stdio.h>
    
        int main()
        {
            // Declare the array
            int arr[10] = {0, 7, 8, 10, -4, 5, 6, 9, 11, -32};
            // Declare the array that holds the reversed copy
            int arrMirror[10];
            
            // Copy the array in reverse order
            for (int i = 0; i < 10; i++)
            {
                arrMirror[i] = arr[10-i-1];
            }
            
            // Print reversed array copy
            printf("Reversed copy of array: ");
            for (int i = 0; i < 10; i++)
            {
                printf("%d ", arrMirror[i]);
            }
            printf("\n");
            
            return 0;
        }

Question 04

Write a C program that reverses an array of integers in place i.e. you should not create another array. Make the array equal to {0, 7, 8, 10, -4, 5, 6, 9, 11, -32}.

Answer 04

        #include <stdio.h>
        
        int main()
        {
            // Declare the array
            int arr[10] = {0, 7, 8, 10, -4, 5, 6, 9, 11, -32};
            
            // We wuill use this when we swap numbers
            int temp;
            
            // Reverse the content of the array
            // Loop only until half of the array
            // Looping through the whole array will reverse,
            // then, reverse reverse i.e. bring back to
            // original position
            for (int i = 0; i < 5; i++)
            {
                int temp = arr[i];
                arr[i] = arr[10-i-1];
                arr[10-i-1] = temp;
            }
            
            // Print reversed array
            printf("Reversed array: ");
            for (int i = 0; i < 10; i++)
            {
                printf("%d ", arr[i]);
            }
            printf("\n");
            
            return 0;
        }

Question 05

Write a C program that finds the second largest number in an array of integers.

Answer 05

        #include <stdio.h>
        
        #include <stdio.h>
        int main() {
            // Declare the array
            int arr[10] = {0, 7, 8, 10, -4, 5, 6, 9, 11, -32};
            //int arr[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};         
            
            // Initialize largest and second largest to the first element
            int largest = arr[0];
            int secondLargest = arr[0];
            
            // Find largest and second largest
            for (int i = 1; i < 10; i++) 
            {
                if (arr[i] > largest) 
                {
                    // Update second largest
                    secondLargest = largest; 
                    // Update largest
                    largest = arr[i]; 
                } 
                else if (arr[i] < largest && arr[i] > secondLargest) 
                {
                    // Update second largest
                    secondLargest = arr[i]; 
                }
            }
            
            // Check if second largest was updated
            if (largest == secondLargest) 
            {
                printf("There is no second largest number in the array.\n");
            } 
            else 
            {
                printf("The second largest number is: %d\n", secondLargest);
            }
        
            return 0;
        }
Last modified: Thursday, 28 November 2024, 11:47 AM