0% found this document useful (0 votes)
20 views9 pages

Array Example 20

The document provides 20 unique examples of arrays in C programming, showcasing various functionalities such as declaration, initialization, searching, sorting, and operations on both single-dimensional and multi-dimensional arrays. Each example includes code snippets along with their respective outputs, covering topics like finding maximum elements, reversing arrays, and matrix operations. These examples serve as a comprehensive guide to understanding array manipulation in C.

Uploaded by

Akash Satdeve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views9 pages

Array Example 20

The document provides 20 unique examples of arrays in C programming, showcasing various functionalities such as declaration, initialization, searching, sorting, and operations on both single-dimensional and multi-dimensional arrays. Each example includes code snippets along with their respective outputs, covering topics like finding maximum elements, reversing arrays, and matrix operations. These examples serve as a comprehensive guide to understanding array manipulation in C.

Uploaded by

Akash Satdeve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Here are 20 unique examples of arrays in C programming, each with code and output.

These
examples cover a wide range of array functionalities, including single-dimensional, multi-
dimensional arrays, and operations like searching, sorting, and traversing arrays.

1. Basic Array Declaration and Initialization


#include <stdio.h>

int main() {
int arr[5] = {1, 2, 3, 4, 5};

for(int i = 0; i < 5; i++) {


printf("%d ", arr[i]);
}
return 0;
}

Output:

1 2 3 4 5

2. Sum of Elements in an Array


#include <stdio.h>

int main() {
int arr[5] = {10, 20, 30, 40, 50};
int sum = 0;

for(int i = 0; i < 5; i++) {


sum += arr[i];
}
printf("Sum: %d", sum);
return 0;
}

Output:

Sum: 150

3. Find Maximum Element in an Array


#include <stdio.h>

int main() {
int arr[5] = {12, 34, 56, 23, 45};
int max = arr[0];

for(int i = 1; i < 5; i++) {


if(arr[i] > max) {
max = arr[i];
}
}
printf("Maximum: %d", max);
return 0;
}
Output:

Maximum: 56

4. Reverse an Array
#include <stdio.h>

int main() {
int arr[5] = {1, 2, 3, 4, 5};

printf("Reversed Array: ");


for(int i = 4; i >= 0; i--) {
printf("%d ", arr[i]);
}
return 0;
}

Output:

Reversed Array: 5 4 3 2 1

5. Array of Strings
#include <stdio.h>

int main() {
char *arr[] = {"Apple", "Banana", "Cherry"};

for(int i = 0; i < 3; i++) {


printf("%s\n", arr[i]);
}
return 0;
}

Output:

Apple
Banana
Cherry

6. 2D Array Initialization
#include <stdio.h>

int main() {
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};

for(int i = 0; i < 2; i++) {


for(int j = 0; j < 3; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
Output:

1 2 3
4 5 6

7. Sum of Diagonal Elements in a 2D Array


#include <stdio.h>

int main() {
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int sum = 0;

for(int i = 0; i < 3; i++) {


sum += arr[i][i];
}
printf("Sum of diagonal: %d", sum);
return 0;
}

Output:

Sum of diagonal: 15

8. Linear Search in an Array


#include <stdio.h>

int main() {
int arr[5] = {10, 20, 30, 40, 50};
int search = 30;
int found = 0;

for(int i = 0; i < 5; i++) {


if(arr[i] == search) {
printf("Element found at index %d\n", i);
found = 1;
break;
}
}
if (!found)
printf("Element not found\n");

return 0;
}

Output:

Element found at index 2

9. Sorting an Array (Bubble Sort)


#include <stdio.h>

int main() {
int arr[5] = {5, 1, 4, 2, 3};
int n = 5;
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;
}
}
}

for(int i = 0; i < n; i++) {


printf("%d ", arr[i]);
}
return 0;
}

Output:

1 2 3 4 5

10. Check if Array is Sorted


#include <stdio.h>

int main() {
int arr[5] = {1, 2, 3, 4, 5};
int sorted = 1;

for(int i = 0; i < 4; i++) {


if(arr[i] > arr[i+1]) {
sorted = 0;
break;
}
}

if(sorted)
printf("Array is sorted\n");
else
printf("Array is not sorted\n");

return 0;
}

Output:

Array is sorted

11. Find the Average of Array Elements


#include <stdio.h>

int main() {
int arr[5] = {10, 20, 30, 40, 50};
int sum = 0;

for(int i = 0; i < 5; i++) {


sum += arr[i];
}
float avg = sum / 5.0;
printf("Average: %.2f", avg);

return 0;
}

Output:

Average: 30.00

12. Copy Array Elements


#include <stdio.h>

int main() {
int arr1[5] = {1, 2, 3, 4, 5};
int arr2[5];

for(int i = 0; i < 5; i++) {


arr2[i] = arr1[i];
}

printf("Copied Array: ");


for(int i = 0; i < 5; i++) {
printf("%d ", arr2[i]);
}
return 0;
}

Output:

Copied Array: 1 2 3 4 5

13. Insert an Element into an Array


#include <stdio.h>

int main() {
int arr[6] = {1, 2, 3, 5, 6};
int n = 5, pos = 3, value = 4;

for(int i = n; i >= pos; i--) {


arr[i] = arr[i-1];
}
arr[pos-1] = value;

for(int i = 0; i < 6; i++) {


printf("%d ", arr[i]);
}
return 0;
}

Output:

1 2 3 4 5 6
14. Delete an Element from an Array
#include <stdio.h>

int main() {
int arr[6] = {1, 2, 3, 4, 5, 6};
int n = 6, pos = 3;

for(int i = pos-1; i < n-1; i++) {


arr[i] = arr[i+1];
}

for(int i = 0; i < n-1; i++) {


printf("%d ", arr[i]);
}
return 0;
}

Output:

1 2 4 5 6

15. Multiplication of Two Matrices


#include <stdio.h>

int main() {
int a[2][2] = {{1, 2}, {3, 4}};
int b[2][2] = {{5, 6}, {7, 8}};
int c[2][2];

for(int i = 0; i < 2; i++) {


for(int j = 0; j < 2; j++) {
c[i][j] = 0;
for(int k = 0; k < 2; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}

for(int i = 0; i < 2; i++) {


for(int j = 0; j < 2; j++) {
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}

Output:

19 22
43 50

16. Transpose of a Matrix


#include <stdio.h>
int main() {
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
int transpose[3][2];

for(int i = 0; i < 2; i++) {


for(int j = 0; j < 3; j++) {
transpose[j][i] = arr[i][j];
}
}

for(int i = 0; i < 3; i++) {


for(int j = 0; j < 2; j++) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}

return 0;
}

Output:

CopyEdit
1 4
2 5
3 6

17. Counting Even and Odd Numbers in an Array


#include <stdio.h>

int main() {
int arr[6] = {1, 2, 3, 4, 5, 6};
int even = 0, odd = 0;

for(int i = 0; i < 6; i++) {


if(arr[i] % 2 == 0) {
even++;
} else {
odd++;
}
}

printf("Even: %d, Odd: %d", even, odd);


return 0;
}

Output:

Even: 3, Odd: 3

18. Find the Second Largest Element


#include <stdio.h>

int main() {
int arr[5] = {12, 34, 23, 56, 45};
int first = arr[0], second = -1;

for(int i = 1; i < 5; i++) {


if(arr[i] > first) {
second = first;
first = arr[i];
} else if(arr[i] > second && arr[i] != first) {
second = arr[i];
}
}

printf("Second Largest: %d", second);


return 0;
}

Output:

Second Largest: 45

19. Count the Frequency of an Element


#include <stdio.h>

int main() {
int arr[7] = {1, 2, 3, 2, 4, 5, 2};
int count = 0, search = 2;

for(int i = 0; i < 7; i++) {


if(arr[i] == search) {
count++;
}
}

printf("Frequency of %d: %d", search, count);


return 0;
}

Output:

Frequency of 2: 3

20. Find Missing Number in a Sequence


#include <stdio.h>

int main() {
int arr[5] = {1, 2, 3, 5, 6};
int sum = 0, expected_sum = 0;

for(int i = 0; i < 5; i++) {


sum += arr[i];
}

for(int i = 1; i <= 6; i++) {


expected_sum += i;
}

printf("Missing number: %d", expected_sum - sum);


return 0;
}

Output:

Missing number: 4

These examples will give you a good understanding of how arrays work in C.

You might also like