google-site-verification=EmVnnySXehAfTr_j8ZJN48hwvxJtfNf80pkPX1ObQlA Fast Track News: Introduction to programming in C Week4 Programming

Introduction to programming in C Week4 Programming

 Question1:

#include <stdio.h> int main() { int n; scanf("%d", &n); int arr[n]; for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } // Calculate the mean of the array int sum = 0; for (int i = 0; i < n; i++) { sum += arr[i]; } float mean = (float)sum / n; // Calculate the sum of elements above the mean int sum_above_mean = 0; for (int i = 0; i < n; i++) { if (arr[i] >= mean) { sum_above_mean += arr[i]; } } printf("%d", sum_above_mean); return 0; } Question2: #include <stdio.h> int main() { int n1, n2; scanf("%d", &n1); int arr1[n1]; for (int i = 0; i < n1; i++) { scanf("%d", &arr1[i]); } scanf("%d", &n2); int arr2[n2]; for (int i = 0; i < n2; i++) { scanf("%d", &arr2[i]); } int smallest = -1; for (int i = 0; i < n1; i++) { for (int j = 0; j < n2; j++) { if (arr1[i] == arr2[j]) { if (smallest == -1 || arr1[i] < smallest) { smallest = arr1[i]; } } } } printf("%d", smallest); return 0; } Question3: #include <stdio.h> #include <string.h> // Function to check if two strings are anagrams int areAnagrams(char str1[], char str2[]) { int len1 = strlen(str1); int len2 = strlen(str2); // If lengths are different, they can't be anagrams if (len1 != len2) { return 0; } // Count characters in both strings int count1[26] = {0}; int count2[26] = {0}; for (int i = 0; i < len1; i++) { count1[str1[i] - 'A']++; count2[str2[i] - 'A']++; } // Check if counts of characters are same for (int i = 0; i < 26; i++) { if (count1[i] != count2[i]) { return 0; } } return 1; // Strings are anagrams } int main() { int size; scanf("%d", &size); char str1[21], str2[21]; scanf("%s", str1); scanf("%s", str2); if (areAnagrams(str1, str2)) { printf("1"); } else { printf("0"); } return 0; }

No comments:

Post a Comment

April Week 2 || Lab 2 || Troubleshooting Data Models in Looker

  CREATE NEW FILE NAME: user_order_lifetime view: user_order_lifetime { derived_table: { sql: SELECT order_items.user_id as us...