Unit 2

Orange

  1. Write a program that checks and prints if the given array of integers is a Palindrome or not. Receive all inputs from the user. [1,3,2,2,3,1] -> Palindrome [1,3,2,3,1] > Palindrome [10,32,25,32,10] -> Palindrome [1,4,6,3,1] >Not A Palindrome

    #include <stdio.h>
    int main(){
    	int arr[100];
    	int isPalindrome = 1;
    	int size;
    	int num;
    
    	printf("Enter number of elements"); scanf("%d",&size);
    	puts("Enter elements: ");
    	for (int i = 0; i < size; i++){
    		scanf("%d",&num);
    		arr[i] = num;
    	}
    	
    	for (int j = 0; j < size/2; j++){
    		if (arr[j]!=arr[size-j-1]){
    			isPalindrome = 0;
    			break;
    		}
    	}
    	
    	printf(isPalindrome ? "Palindrome" : "Not Palindrome");
    	return 0;
    }
    
  2. Write a Program to create an Array of n integers and check if the array is "Really Sorted", "Sorted","Not Sorted" Example #1 : [1,2,5,7,10] -> Really Sorted Example #2 : [1,2,5,5,10] -> Sorted Example #3 : [1,12,5,45,10] -> Not Sorted

    #include <stdio.h>
    int main(){
    	int arr[100];
    	int prev, next, n, num;
    	int sorted = 1, reallySorted = 1;
    	printf("Enter size of array: "); scanf("%d",&n);
    	puts("Enter the elements: ");
    	for (int i = 0; i < n; i++){
    		scanf("%d",&num);
    		arr[i] = num;
    	}
    	
    	for (int j = 0; j < n-1; j++){
    		if (arr[j] < arr[j+1]){
    			reallySorted = 1;
    		}
    		else{
    			reallySorted = 0;
    			break;
    		}
    	}
    	
    	for (int j = 0; j < n - 1; j++){
    		
    		if (arr[j] <= arr[j+1]){
    			sorted = 1;
    		}
    		else{
    			sorted = 0;
    			break;
    		}
    	}
    	
    	if (sorted){
    		if (reallySorted){
    			puts("Really Sorted");
    		}
    		else{
    			puts("Sorted");
    		}
    	}
    	else{
    		puts("Not Sorted");
    	}
    	return 0;
    	
    }
    
  3. Given the array of n integers, count the number of Unique Elements in the Array(not repeated elements) and store these elements in a new array. int arr[]= (5, 7, 3, 4, 5, 6, 8, 9, 10, 3}; Expected Input and Output: Given Array -> [5,7,3,4,5,6,8,9,10,3] New array ->[7,4,6,8,9,10] Number of Unique Elements -> 6

  4. Write a C Program that should rotate the given array by N positions at the left Expected Input and Output: Input: [5,4,7,3] IfN=2 After left Rotation -> [7,3,5,4]

    #include <stdio.h>
    
    void leftRotate(int arr[], int size, int n) {
        int temp[n];    
        for (int i = 0; i < n; i++) {
            temp[i] = arr[i];
        }
        
        for (int i = n; i < size; i++) {
            arr[i - n] = arr[i];
        }
    
        for (int i = 0; i < n; i++) {
            arr[size - n + i] = temp[i];
        }
    }
    
    int main() {
        int arr[] = {5, 4, 7, 3, 2, 4};
        int size = sizeof(arr) / sizeof(arr[0]);
        int n = 3;
    
        leftRotate(arr, size, n);
    
        printf("After left rotation: ");
        for (int i = 0; i < size; i++) {
            printf("%d ", arr[i]);
        }
    
        return 0;
    }
    
  5. Given a list of N integers, representing height of mountains. Find the height of the tallest mountain and the second tallest mountain. Handle edge cases by displaying appropriate messages. Example #1 : [4,7,6,3,1] 7 -> height of the tallest mountain 6 -> height of the second tallest mountain

  6. A shopkeeper, Mr. Smith, keeps track of his daily sales for five products over seven days in a 2Darray. He wants to find out the total sales for each product over the week. Write a function that takes the 2D array as input and modifies a 1D array containing the total sales of each product.

    #include <stdio.h>
    int main(){
        int sales[5][7];
        int total_sales[5] = {0}; // Initialize total sales for each product to 0
        puts("Enter the sales for 5 products over 7 days:");
        for (int i = 0; i < 5; i++){
            for (int j = 0; j < 7; j++){
                scanf("%d", &sales[i][j]);
                total_sales[i] += sales[i][j]; // Add daily sales to total sales for the product
            }
        }
        printf("[");
        for (int i = 0; i < 5; i++){
            printf("%d", total_sales[i]);
            if (i < 4) {
                printf(", ");
            }
        }
        printf("]\n");
        return 0;
    }
    
  7. Write a function in C to convert a decimal number to a binary number. Expected output when the test code is run is as below: Input any decimal number: 8 The Binary value is : 1000 Input any decimal number: -8 The Binary value is : -1000

    /* Write a function in C to convert a decimal number to a binary number. Expected
    output when the test code is run is as below:
    Input any decimal number: 8
    The Binary value is : 1000
    Input any decimal number: -8
    The Binary value is : -1000 */
    
    #include <stdio.h>
    void decimalToBinary(int n){
    	int bin[32];
    	int rem, i = 0;
    
        if (n < 0){
            n = -n;
            printf("-");
        }
    
    	while (n != 0){
    		rem = n % 2;
    		bin[i] = rem;
    		n = n/2;
    	}
    	
    	//displaying
    	
    	for (int j = i - 1; j > -1; j--){
    		printf("%d ",bin[j]);
    	}
        printf("\n");
    	
    }
    int main(){
    	int decimalNumber;
    	printf("Enter a decimal number: "); scanf("%d", &decimalNumber);
    	decimalToBinary(decimalNumber);
    	return 0;
    }
    
  8. Write a program to remove the duplicate elements from a sorted array. Fill the implementation of remove_duplicates function. Expected output: [1, 2, 3, 4, 5, 6] Client code is as below: int arr[]= {1, 1, 2, 2, 3, 4, 4, 5, 6, 6); int n = sizeof(arr)/sizeof(arr[0]); remove_duplicates(arr, n);

    /*Write a program to delete duplicate elements from an array*/
    #include <stdio.h>
    
    void remove_duplicates(int arr[], int n){
        int new_arr[100];
        int k = 0;
        for (int i = 0; i < n; i++){
            int is_duplicate = 0;
    
            for (int j = 0; j < k; j++){
                if (new_arr[j] == arr[i]){
                    is_duplicate = 1;
                    break;
                }
            }
    
            if (!is_duplicate){
                new_arr[k] = arr[i];
                k++;
            }
        }
    
        for (int i = 0; i < k; i++){
            printf("%d ", new_arr[i]);
        }
        printf("\n");
    }
    
    int main(){
        int arr[] = {1,2,3,3,2,3,4,2,4,5,6,4,4,5,3,7,8,9,7,5,7,7,8};
        int n = sizeof(arr)/sizeof(arr[0]);
        remove_duplicates(arr, n);
        return 0;
    }
    
  9. Implement a function that takes the array and the array size. This function must caleulate and display the largest sum of two Adjacent Elements (Neighbours) in the array. If the array size is less than 2, display appropriate message. Example #1 : [1,4,3,7,1] ->10 Example #2 : [5,7,1,5,2] ->12

  10. Given an array A of length N, implement a function to find the element which repeats maximum number of times and update the corresponding frequency_count. In case of a tie, choose the smaller element. Expected output: Given array => (2,2,2,1,1,1,1,2,2,31,32,32,32]; The element 2 repeats maximum number of times Frequency of this element is 5

    #include <stdio.h>
    int largestSumOfAdjacentElements(int arr[], int size) {
        if (size < 2) {
            printf("Array size must be at least 2 to calculate the largest sum of adjacent elements.\n");
            return -1; 
        }
    
        int maxSum = arr[0] + arr[1]; 
    
        for (int i = 1; i < size - 1; i++) {
            int currentSum = arr[i] + arr[i + 1]; 
            if (currentSum > maxSum) {
                maxSum = currentSum; 
            }
        }
    
        return maxSum; 
    }
    int main() {
        int arr1[] = {1, 4, 3, 7, 1};
        int size1 = sizeof(arr1) / sizeof(arr1[0]);
        int result1 = largestSumOfAdjacentElements(arr1, size1);
        if (result1 != -1) {
            printf("Largest sum of adjacent elements in arr1: %d\n", result1);
        }
    
        int arr2[] = {5, 7, 1, 5, 2};
        int size2 = sizeof(arr2) / sizeof(arr2[0]);
        int result2 = largestSumOfAdjacentElements(arr2, size2);
        if (result2 != -1) {
            printf("Largest sum of adjacent elements in arr2: %d\n", result2);
        }
    
        return 0;
    }
    
  11. Emma visited a grocery store for fresh supplies. There are N items in the store where the ith item has a freshness value A; and cost Bi ;. Emma has decided to purchase all the items having a freshness value greater than equal to X. Find the total cost of the groceries Emma buys.

    Client code is as below. int a[]= {15,67, 45, 60); int b[] = {10,90, 100, 150]; int n = sizeof(a) / sizcof(a[0]); int x = 20; // freshness level expected by Emma int t_cost= find_total_cost_freshness(a, b, n, x); printf("Total cost of items above freshness level %d is %dn", x, t_cost); Fill the implementation of find_total_cost_freshness to get the expected output. Expected output => Total cost of items above freshness level 20 is 340

  12. A teacher, Mrs. Patel, maintains a 2D array containing the scores of five students in four subjects. She wants to compute the average score of each student to determine their performance. Write a function that takes this 2D array as input and updates the 1D array with the average score of each student. Given, int marks[5][4]= { (90, 92, 89, 94], Expected output: Average marks of each student: Student 1: 83.25 Student 2: 79.50 Student 3: 91.25 Student 4: 65.75 Student 5: 85.25

  13. Alice, a data scientist, is analyzing a 4x4 dataset and wants to find the sum of all diagonal elements in a square matrix. Implement a function that takes an nxn matrix and returns the sum of its main diagonal elements. Display the sum of diagonal elements in the client code.

    {60, 65, 70, 68},

    (80, 85, 78, 90). (85, 88, 82, 86) };

    (70, 75, 88, 85},

  14. Sarah is a data analyst who has an integer array of size N. She wants to find the maximum and minimum values in the array. To make her program more efficient, she decides to use an array of pointers to access the integers. Help Sarah find the maximum and minimum integer in the array using an array of pointers to integers by sending this array to findMinMax function. Prototype of the function is as below with min and max as two variables in the infterface code. void findMinMax(int *arr[], int size, int *min, int *max); Function call: findMinMax(ptrArr, n, &min, &max);

  15. Mike is a mathematician who wants to calculate the sum of the squares of N numbers. He decides to use an array of pointers to access the numbers and perform the calculation. Implement the C code to help Mike calculate the sum of the squares of N numbers using an array of pointers. Prototype: int calculateSumOfSquares(int *arr|], int size);

  16. Bob is a programmer who has an array of pointers that point to N numbers stored in the array. He wants to reverse the order of these numbers and then print the reversed list of integers using an array of pointers to to make the program more efficient. Write a C function implementation to do this and test this function in the client code. Prototype of function => void reverseArray(int *arr|], int size);

  17. Emily is a mathematician who wants to calculate the product of all prime numbers stored in an array of pointers to integers. Implement the C function to help Emily calculate the product of all prime numbers in the array using array of pointers. Prototype of funetion =>> int calculateProductOfPrimes(int *arr|], int size)

  18. Alice is a programmer who has two fixed integer values, A and B. She wants to calculate their GCD (Greatest Common Divisor) and LCM (Least Common Multiple). Client code is as below. int a, b; printf("Enter two integers (A B): "); scanf("%d %d", &a, &b); int *ptrArr[2]= {&a, &b}: int gedResult, lemResult; calculateGCDAndLCM(ptrArr, &gedResult, & IcmResult); printf("GCD: %dnLCM: %dn", gedResult, lemResult);

    Fill the implementation of ealculateGCDAndLCM so that we get the expected output. Expected output:

    C:\Users\Dell>a Enter two integers (A B): 23

    GCD: 1 LCM: 92

    C:\Users\Dell>a Enter two integers (A B): 5 15 GCD: 5 LCM: 15