C Programming – Basic Array Problems
1. Array Initialization and Traversal
Write a C program to initialize a 1D array of 10 integers, assign values to the array, and
print all the elements.
C Program Code:
#include <stdio.h>
int main() {
int arr[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
printf("Array elements: ");
for(int i = 0; i < 10; i++)
printf("%d ", arr[i]);
return 0;
}
Sample Output:
Array elements: 10 20 30 40 50 60 70 80 90 100
2. Reverse an Array
Write a C program to reverse the elements of a 1D array in place.
C Program Code:
#include <stdio.h>
int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
for(int i = 0; i < n; i++)
scanf("%d", &arr[i]);
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]);
return 0;
}
Sample Input:
5
12345
Sample Output:
Reversed array: 5 4 3 2 1
3. Sum of Even and Odd Elements
Write a C program to find the sum of even and odd elements in a 1D array.
C Program Code:
#include <stdio.h>
int main() {
int n, even = 0, odd = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
if(arr[i] % 2 == 0)
even += arr[i];
else
odd += arr[i];
}
printf("Sum of even elements: %d\n", even);
printf("Sum of odd elements: %d\n", odd);
return 0;
}
Sample Input:
5
10 15 20 25 30
Sample Output:
Sum of even elements: 60
Sum of odd elements: 40
4. Find Minimum and Maximum in an Array
Write a C program to find the smallest and largest element in a 1D array.
C Program Code:
#include <stdio.h>
int main() {
int n, min, max;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
for(int i = 0; i < n; i++)
scanf("%d", &arr[i]);
min = max = arr[0];
for(int i = 1; i < n; i++) {
if(arr[i] < min) min = arr[i];
if(arr[i] > max) max = arr[i];
}
printf("Minimum = %d\nMaximum = %d\n", min, max);
return 0;
}
Sample Input:
6
391752
Sample Output:
Minimum = 1
Maximum = 9
5. Count Positive, Negative, and Zero Elements
Write a C program to count positive, negative, and zero values in a 1D array.
C Program Code:
#include <stdio.h>
int main() {
int n, pos = 0, neg = 0, zero = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
if(arr[i] > 0) pos++;
else if(arr[i] < 0) neg++;
else zero++;
}
printf("Positive: %d\nNegative: %d\nZero: %d\n", pos, neg, zero);
return 0;
}
Sample Input:
6
-3 0 2 -5 0 4
Sample Output:
Positive: 2
Negative: 2
Zero: 2
6. Copy One Array to Another
Write a C program to copy all elements of one array into another array.
C Program Code:
#include <stdio.h>
int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
int a[n], b[n];
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
for(int i = 0; i < n; i++)
b[i] = a[i];
printf("Copied array: ");
for(int i = 0; i < n; i++)
printf("%d ", b[i]);
return 0;
}
Sample Input:
4
10 20 30 40
Sample Output:
Copied array: 10 20 30 40
7. Find the Average of Array Elements
Write a C program to find the average of elements in a 1D array.
C Program Code:
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
printf("Average = %.2f\n", (float)sum/n);
return 0;
}
Sample Input:
5
2 4 6 8 10
Sample Output:
Average = 6.00
C Programming – Intermediate and Advanced Array Problems
1. Search for an Element
Write a C program to search for a specific integer in a 1D array and print its index if found,
or print a message if not found.
C Program Code:
#include <stdio.h>
int main() {
int n, key, found = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for(int i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Enter the element to search: ");
scanf("%d", &key);
for(int i = 0; i < n; i++) {
if(arr[i] == key) {
printf("Element found at index %d\n", i);
found = 1;
break;
}
}
if(!found)
printf("Element not found.\n");
return 0;
}
Sample Input:
5
10 20 30 40 50
30
Sample Output:
Element found at index 2
2. Frequency of Elements
Write a C program to find the frequency of each element in a 1D array of integers.
C Program Code:
#include <stdio.h>
int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n], freq[n];
for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
freq[i] = -1;
}
for(int i = 0; i < n; i++) {
if(freq[i] == 0) continue;
int count = 1;
for(int j = i + 1; j < n; j++) {
if(arr[i] == arr[j]) {
count++;
freq[j] = 0;
}
}
freq[i] = count;
}
printf("Element | Frequency\n");
for(int i = 0; i < n; i++) {
if(freq[i] != 0)
printf("%d | %d\n", arr[i], freq[i]);
}
return 0;
}
Sample Input:
6
122333
Sample Output:
Element | Frequency
1|1
2|2
3|3
3. Merge Two Arrays
Write a C program to merge two 1D arrays into a single array.
C Program Code:
#include <stdio.h>
int main() {
int n1, n2;
printf("Enter size of first array: ");
scanf("%d", &n1);
int arr1[n1];
printf("Enter %d elements: ", n1);
for(int i = 0; i < n1; i++) scanf("%d", &arr1[i]);
printf("Enter size of second array: ");
scanf("%d", &n2);
int arr2[n2];
printf("Enter %d elements: ", n2);
for(int i = 0; i < n2; i++) scanf("%d", &arr2[i]);
int merged[n1 + n2];
for(int i = 0; i < n1; i++) merged[i] = arr1[i];
for(int i = 0; i < n2; i++) merged[n1 + i] = arr2[i];
printf("Merged array: ");
for(int i = 0; i < n1 + n2; i++) printf("%d ", merged[i]);
return 0;
}
Sample Input:
3
123
2
45
Sample Output:
Merged array: 1 2 3 4 5
4. Find the Second Largest Element
Write a C program to find the second-largest element in a 1D array.
C Program Code:
#include <stdio.h>
int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
for(int i = 0; i < n; i++) scanf("%d", &arr[i]);
int first = arr[0], second = -9999;
for(int i = 1; i < n; i++) {
if(arr[i] > first) {
second = first;
first = arr[i];
} else if(arr[i] > second && arr[i] < first) {
second = arr[i];
}
}
printf("Second largest element: %d\n", second);
return 0;
}
Sample Input:
5
10 20 5 8 15
Sample Output:
Second largest element: 15
5. Array Rotation
Write a C program to rotate the elements of a 1D array to the right by a given number of
positions.
C Program Code:
#include <stdio.h>
int main() {
int n, k;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n], rotated[n];
for(int i = 0; i < n; i++) scanf("%d", &arr[i]);
printf("Enter number of positions to rotate: ");
scanf("%d", &k);
for(int i = 0; i < n; i++)
rotated[(i + k) % n] = arr[i];
printf("Rotated array: ");
for(int i = 0; i < n; i++) printf("%d ", rotated[i]);
return 0;
}
Sample Input:
5
12345
2
Sample Output:
Rotated array: 4 5 1 2 3