Arrays:
Q1: Write a program to find the sum of elements in an array.
output.cpp
// C code
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
printf("Sum of elements: %d\n", sum);
return 0;
}
Q2: Write a program to find the average of elements in an array.
output.cpp
// C code
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int sum = 0;
double average;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
average = (double)sum / n;
printf("Average of elements: %.2lf\n", average);
return 0;
}
Q3: Write a program to find the largest element in an array.
output.cpp
// C code
#include <stdio.h>
int main() {
int arr[] = {12, 45, 78, 33, 95, 56, 23, 67};
int n = sizeof(arr) / sizeof(arr[0]);
int largest = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}
printf("Largest element in the array: %d\n", largest);
return 0;
}
Q4: Reverse the order of elements in an array.
output.cpp
// C code
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
// Reverse the array in-place
for (int i = 0; i < n / 2; i++) {
int temp = arr[i];
arr[i] = arr[n - i - 1];
arr[n - i - 1] = temp;
}
printf("Reversed array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Q5: Implement sorting algorithm to sort an array in ascending or descending order.
output.cpp
// C code
#include <stdio.h>
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array in ascending order: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Q6: Remove duplicate elements from an array, leaving only unique elements.
output.cpp
// C code
#include <stdio.h>
void removeDuplicates(int arr[], int *n) {
if (*n <= 1) {
return; // No duplicates to remove
}
int uniqueIndex = 1;
for (int i = 1; i < *n; i++) {
int isDuplicate = 0;
for (int j = 0; j < uniqueIndex; j++) {
if (arr[i] == arr[j]) {
isDuplicate = 1;
break;
}
}
if (!isDuplicate) {
arr[uniqueIndex] = arr[i];
uniqueIndex++;
}
}
*n = uniqueIndex;
}
int main() {
int arr[] = {1, 2, 2, 3, 4, 4, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
removeDuplicates(arr, &n);
printf("Array with duplicates removed: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Q7: Write a program to search for a specific element in an array and print its index or occurrence count on screen. (Hint: You can use for loop and conditional statement for searching).
output.cpp
// C code
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 2, 5, 2, 6, 2};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 2;
int occurrenceCount = 0;
printf("Indices of element %d: ", target);
for (int i = 0; i < n; i++) {
if (arr[i] == target) {
printf("%d ", i);
occurrenceCount++;
}
}
printf("\n");
printf("Total occurrences of element %d: %d\n", target, occurrenceCount);
return 0;
}
Q8: Given an array and a target sum, find two numbers that add up to the target sum.
output.cpp
// C code
#include <stdio.h>
int main() {
int arr[] = {2, 7, 11, 15};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 9;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] + arr[j] == target) {
printf("Numbers that add up to %d: %d and %d\n", target, arr[i], arr[j]);
return 0;
}
}
}
printf("No two numbers found that add up to %d.\n", target);
return 0;
}
Q9: Find and print common elements in two arrays.
output.cpp
// C code
#include <stdio.h>
int main() {
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {3, 4, 5, 6, 7};
int n1 = sizeof(arr1) / sizeof(arr1[0]);
int n2 = sizeof(arr2) / sizeof(arr2[0]);
int set[1000] = {0}; // Assuming a maximum element value of 1000
printf("Common elements in the two arrays: ");
for (int i = 0; i < n1; i++) {
set[arr1[i]] = 1;
}
for (int i = 0; i < n2; i++) {
if (set[arr2[i]] == 1) {
printf("%d ", arr2[i]);
set[arr2[i]] = 0; // Mark as printed
}
}
printf("\n");
return 0;
}
Q10: Find the union of two arrays
output.cpp
// C code
#include <stdio.h>
#include <stdbool.h>
void printUnion(int arr1[], int arr2[], int m, int n) {
bool hash[1000] = {false}; // Assuming a maximum element value of 1000
printf("Union of the two arrays: ");
for (int i = 0; i < m; i++) {
if (!hash[arr1[i]]) {
printf("%d ", arr1[i]);
hash[arr1[i]] = true;
}
}
for (int i = 0; i < n; i++) {
if (!hash[arr2[i]]) {
printf("%d ", arr2[i]);
hash[arr2[i]] = true;
}
}
printf("\n");
}
int main() {
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {3, 4, 5, 6, 7};
int m = sizeof(arr1) / sizeof(arr1[0]);
int n = sizeof(arr2) / sizeof(arr2[0]);
printUnion(arr1, arr2, m, n);
return 0;
}
Q11: Flatten a multi-dimensional array into a single-dimensional array.
output.cpp
// C code
#include <stdio.h>
int main() {
int multiArray[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int flattenedArray[9];
int index = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
flattenedArray[index] = multiArray[i][j];
index++;
}
}
printf("Flattened array: ");
for (int i = 0; i < 9; i++) {
printf("%d ", flattenedArray[i]);
}
printf("\n");
return 0;
}
Q12: Generate the Fibonacci series up to a given number using an array.
output.cpp
// C code
#include <stdio.h>
int main() {
int n;
printf("Enter the number of Fibonacci terms to generate: ");
scanf("%d", &n);
if (n <= 0) {
printf("Invalid input. Please enter a positive number.\n");
return 1;
}
unsigned long long fib[n];
fib[0] = 0;
fib[1] = 1;
printf("Fibonacci sequence up to %d terms: ", n);
printf("%llu %llu ", fib[0], fib[1]);
for (int i = 2; i < n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
printf("%llu ", fib[i]);
}
printf("\n");
return 0;
}