0% found this document useful (0 votes)
35 views17 pages

Array Problem Solve

Uploaded by

fuadsadik39
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
35 views17 pages

Array Problem Solve

Uploaded by

fuadsadik39
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 17

1.

Calculate Sum and Average of an Array


#include <stdio.h>
int main() {
int n, i;
float sum = 0.0, average;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: ");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
average = sum / n;
printf("Sum = %.2f\n", sum);
printf("Average = %.2f\n", average);
return 0;
}

2. Find Maximum and Minimum Value


#include <stdio.h>
int main() {
int n, i, max, min;
printf("Enter the number of elements: ");
scanf("%d", &n);0
int arr[n];
printf("Enter the elements: ");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

max = min = arr[0];


for(i = 1; i < n; i++) {
if(arr[i] > max) {
max = arr[i];
}
if(arr[i] < min) {
min = arr[i];
}
}
printf("Maximum = %d\n", max);
printf("Minimum = %d\n", min);
return 0;
}

3. Search for a Specific Number


#include <stdio.h>
int main() {
int n, i, search, found = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: ");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the number to search: ");
scanf("%d", &search);
for(i = 0; i < n; i++) {
if(arr[i] == search) {
printf("Number found at position %d\n", i + 1);
found = 1;
break;
}
}
if(!found) {
printf("Number not found\n");
}
return 0;
}

4. Delete the First Element


#include <stdio.h>
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);

int arr[n];
printf("Enter the elements: ");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for(i = 0; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
n--;
printf("Array after deletion: ");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}

5. Delete a Specific Number


#include <stdio.h>
int main() {
int n, i, num, found = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: ");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the number to delete: ");
scanf("%d", &num);
for(i = 0; i < n; i++) {
if(arr[i] == num) {
found = 1;
break;
}
}
if(found) {
for(; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
n--;
printf("Array after deletion: ");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
} else {
printf("Number not found\n");
}
return 0;
}
6. Sum and Average of a Two-Dimensional Array
#include <stdio.h>
int main() {
int rows, cols, i, j;
float sum = 0.0, average;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &rows, &cols);
int arr[rows][cols];
printf("Enter the elements: ");
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
scanf("%d", &arr[i][j]);
sum += arr[i][j];
}
average = sum / (rows * cols);
printf("Sum = %.2f\n", sum);
printf("Average = %.2f\n", average);
return 0;
}

7. Sum of Border Elements of a Two-Dimensional Array


#include <stdio.h>
int main() {
int rows, cols, i, j, sum = 0;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &rows, &cols);
int arr[rows][cols];
printf("Enter the elements: ");
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
scanf("%d", &arr[i][j]);
}
}
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
if(i == 0 || i == rows - 1 || j == 0 || j == cols - 1) {
sum += arr[i][j];
}
}
}
printf("Sum of border elements = %d\n", sum);
return 0;
}

8. Sum of Diagonal Elements of a Square Matrix


#include <stdio.h>
int main() {
int n, i, j, sum = 0;
printf("Enter the size of the square matrix: ");
scanf("%d", &n);
int arr[n][n];
printf("Enter the elements: ");
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
scanf("%d", &arr[i][j]);
}
}
for(i = 0; i < n; i++) {
sum += arr[i][i]; // Primary diagonal
if(i != n - i - 1) {
sum += arr[i][n - i - 1]; // Secondary diagonal
}
}
printf("Sum of diagonal elements = %d\n", sum);
return 0;
}
9. Add Two Matrices
#include <stdio.h>
int main() {
int rows, cols, i, j;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &rows, &cols);
int mat1[rows][cols], mat2[rows][cols], sum[rows][cols];
printf("Enter the elements of first matrix: ");
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
scanf("%d", &mat1[i][j]);
}
}
printf("Enter the elements of second matrix: ");
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
scanf("%d", &mat2[i][j]);
}
}
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
sum[i][j] = mat1[i][j] + mat2[i][j];
}
}
printf("Sum of the matrices: \n");
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}

10. Multiply Two Matrices


#include <stdio.h>
int main() {
int rows1, cols1, rows2, cols2, i, j, k;
printf("Enter the number of rows and columns of first matrix: ");
scanf("%d %d", &rows1, &cols1);
printf("Enter the number of rows and columns of second matrix: ");
scanf("%d %d", &rows2, &cols2);
if(cols1 != rows2) {
printf("Matrix multiplication not possible\n");
return 0;
}
int mat1[rows1][cols1], mat2[rows2][cols2], product[rows1][cols2];
printf("Enter the elements of first matrix: ");
for(i = 0; i < rows1; i++) {
for(j = 0; j < cols1; j++) {
scanf("%d", &mat1[i][j]);
}
}
printf("Enter the elements of second matrix: ");
for(i = 0; i < rows2; i++) {
for(j = 0; j < cols2; j++) {
scanf("%d", &mat2[i][j]);
}
}
for(i = 0; i < rows1; i++) {
for(j = 0; j < cols2; j++) {
product[i][j] = 0;
for(k = 0; k < cols1; k++) {
product[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
printf("Product of the matrices: \n");
for(i = 0; i < rows1; i++) {
for(j = 0; j < cols2; j++) {
printf("%d ", product[i][j]);
}
printf("\n");
}
return 0;
}

11. Replace a Specific Number


#include <stdio.h>
int main() {
int n, i, oldNum, newNum;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: ");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the number to replace: ");
scanf("%d", &oldNum);
printf("Enter the new number: ");
scanf("%d", &newNum);
for(i = 0; i < n; i++) {
if(arr[i] == oldNum) {
arr[i] = newNum;
}
}
printf("Array after replacement: ");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}

12. Insert a New Element in the First Position


#include <stdio.h>
int main() {
int n, i, newElement;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n + 1];
printf("Enter the elements: ");
for(i = 1; i <= n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the new element: ");
scanf("%d", &newElement);
for(i = n; i >= 1; i--) {
arr[i] = arr[i - 1];
}
arr[0] = newElement;
printf("Array after insertion: ");
for(i = 0; i <= n; i++) {
printf("%d ", arr[i]);
}
return 0;
}

13. Insert a New Element at a Specific Position


#include <stdio.h>
int main() {
int n, i, pos, newElement;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n + 1]; // Array size increased by 1 to accommodate the new element
printf("Enter the elements: ");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the position to insert the new element (1 to %d): ", n + 1);
scanf("%d", &pos);
if(pos < 1 || pos > n + 1) {
printf("Invalid position!\n");
return 1;
}
printf("Enter the new element: ");
scanf("%d", &newElement);
for(i = n; i >= pos; i--) {
arr[i] = arr[i - 1];
}
arr[pos - 1] = newElement;
printf("Array after insertion: ");
for(i = 0; i <= n; i++) {
printf("%d ", arr[i]);
return 0;
}

14. Insert a New Element Before a Specific Element


#include <stdio.h>
int main() {
int n, i, specificElement, newElement, pos = -1;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n + 1];
printf("Enter the elements: ");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the specific element: ");
scanf("%d", &specificElement);
printf("Enter the new element: ");
scanf("%d", &newElement);
for(i = 0; i < n; i++) {
if(arr[i] == specificElement) {
pos = i;
break;
}
}
if(pos != -1) {
for(i = n; i > pos; i--) {
arr[i] = arr[i - 1];
}
arr[pos] = newElement;
n++;
} else {
printf("Specific element not found\n");
}
printf("Array after insertion: ");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}

15. Sort an Array Using Bubble Sort


#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
for(i = 0; i < n - 1; i++) {
for(j = 0; j < n - i - 1; j++) {
if(arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: ");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
bubbleSort(arr, n);
printf("Sorted array in ascending order: ");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}

16. Binary Search Method


#include <stdio.h>
int binarySearch(int arr[], int n, int key) {
int low = 0, high = n - 1, mid;
while(low <= high) {
mid = (low + high) / 2;
if(arr[mid] == key) {
return mid;
} else if(arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
int main() {
int n, i, key, result;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements in sorted order: ");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the element to search: ");
scanf("%d", &key);
result = binarySearch(arr, n, key);
if(result != -1) {
printf("Element found at position %d\n", result + 1);
} else {
printf("Element not found\n");
}
return 0;
}

17. Merge Two Sorted Arrays


#include <stdio.h>
void mergeArrays(int arr1[], int n1, int arr2[], int n2, int merged[]) {
int i = 0, j = 0, k = 0;
while(i < n1 && j < n2) {
if(arr1[i] < arr2[j]) {
merged[k++] = arr1[i++];
} else {
merged[k++] = arr2[j++];
}
}
while(i < n1) {
merged[k++] = arr1[i++];
}
while(j < n2) {
merged[k++] = arr2[j++];
}
}
int main() {
int n1, n2, i;
printf("Enter the number of elements in first array: ");
scanf("%d", &n1);
int arr1[n1];
printf("Enter the elements of first array: ");
for(i = 0; i < n1; i++) {
scanf("%d", &arr1[i]);
}
printf("Enter the number of elements in second array: ");
scanf("%d", &n2);
int arr2[n2];
printf("Enter the elements of second array: ");
for(i = 0; i < n2; i++) {
scanf("%d", &arr2[i]);
}
int merged[n1 + n2];
mergeArrays(arr1, n1, arr2, n2, merged);
printf("Merged array: ");
for(i = 0; i < n1 + n2; i++) {
printf("%d ", merged[i]);
}
return 0;
}

18. Store and Print Array of Structures


#include <stdio.h>
struct Student {
int roll;
char name[50];
float marks;
};
int main() {
int n, i;
printf("Enter the number of students: ");
scanf("%d", &n);
struct Student students[n];
for(i = 0; i < n; i++) {
printf("Enter roll number, name, and marks of student %d: ", i + 1);
scanf("%d %s %f", &students[i].roll, students[i].name, &students[i].marks);
}
printf("Student details:\n");
for(i = 0; i < n; i++) {
printf("Roll: %d, Name: %s, Marks: %.2f\n", students[i].roll, students[i].name,
students[i].marks);
}
return 0;
}

20. Solve Problems Using User-Defined Functions


This involves creating functions for each problem and calling them from
the main function. For example:
#include <stdio.h>
void calculateSumAndAverage(int arr[], int n, float *sum, float *average) {
int i;
*sum = 0.0;
for(i = 0; i < n; i++) {
*sum += arr[i];
}
*average = *sum / n;
}
int main() {
int n, i;
float sum, average;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: ");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
calculateSumAndAverage(arr, n, &sum, &average);
printf("Sum = %.2f\n", sum);
printf("Average = %.2f\n", average);
return 0;
}

You might also like