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

C Arrays: Comprehensive Guide with Examples

The document provides detailed notes on arrays in C, including definitions, syntax, and various programs for reading, writing, and manipulating array elements. It covers finding maximum and minimum elements, calculating sums and averages, determining medians and modes, searching algorithms, sorting methods, and matrix addition. Each section includes a program example to illustrate the concepts discussed.

Uploaded by

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

C Arrays: Comprehensive Guide with Examples

The document provides detailed notes on arrays in C, including definitions, syntax, and various programs for reading, writing, and manipulating array elements. It covers finding maximum and minimum elements, calculating sums and averages, determining medians and modes, searching algorithms, sorting methods, and matrix addition. Each section includes a program example to illustrate the concepts discussed.

Uploaded by

yash.kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ARRAYS IN C – DETAILED NOTES WITH PROGRAMS

Introduction to Arrays

Definition

An array is a data structure in C that stores multiple values of the same data type in contiguous memory
locations. Each element is accessed using an index number, starting from 0.

Syntax

data_type array_name[size];

Example

int arr[5];

1. Reading and Writing Array Elements

Aim

To read elements into an array and display them.

Theory

This program introduces students to: - Declaring an array - Taking input using a loop - Displaying output
using a loop

Algorithm

1. Declare an array of size n


2. Use a loop to read elements
3. Use another loop to print elements

Program

#include <stdio.h>

int main() {

1
int n, i, arr[100];

printf("Enter number of elements: ");


scanf("%d", &n);

// Reading array elements


for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Displaying array elements


printf("Array elements are: ");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

return 0;
}

2. Finding Maximum Element in an Array

Aim

To find the largest element in an array.

Theory

The maximum element is found by comparing each element with a temporary variable.

Algorithm

1. Assign first element as max


2. Compare with remaining elements
3. Update max if a larger value is found

Program

#include <stdio.h>

int main() {
int arr[100], n, i, max;

printf("Enter number of elements: ");


scanf("%d", &n);

2
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

max = arr[0];
for(i = 1; i < n; i++) {
if(arr[i] > max)
max = arr[i];
}

printf("Maximum element = %d", max);


return 0;
}

3. Finding Minimum Element in an Array

Aim

To find the smallest element in an array.

Program

#include <stdio.h>

int main() {
int arr[100], n, i, min;

scanf("%d", &n);
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);

min = arr[0];
for(i = 1; i < n; i++) {
if(arr[i] < min)
min = arr[i];
}

printf("Minimum element = %d", min);


return 0;
}

3
4. Sum of Array Elements

Aim

To calculate the sum of all elements.

Program

#include <stdio.h>

int main() {
int arr[100], n, i, sum = 0;

scanf("%d", &n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}

printf("Sum = %d", sum);


return 0;
}

5. Average of Array Elements

Aim

To calculate average of elements.

Formula

Average = Sum / Number of elements

Program

#include <stdio.h>

int main() {
int arr[100], n, i, sum = 0;
float avg;

scanf("%d", &n);
for(i = 0; i < n; i++) {

4
scanf("%d", &arr[i]);
sum += arr[i];
}

avg = (float)sum / n;
printf("Average = %.2f", avg);
return 0;
}

6. Median of an Array

Theory

Median is the middle value of a sorted array. - Odd elements → middle value - Even elements → average of
two middle values

Program

#include <stdio.h>

int main() {
int arr[100], n, i, j, temp;
float median;

scanf("%d", &n);
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);

// Sorting
for(i = 0; i < n - 1; i++)
for(j = i + 1; j < n; j++)
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

if(n % 2 == 0)
median = (arr[n/2 - 1] + arr[n/2]) / 2.0;
else
median = arr[n/2];

printf("Median = %.2f", median);

5
return 0;
}

7. Mode of an Array

Theory

Mode is the value that occurs most frequently.

Program

#include <stdio.h>

int main() {
int arr[100], n, i, j, count, maxCount = 0, mode;

scanf("%d", &n);
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);

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


count = 1;
for(j = i + 1; j < n; j++) {
if(arr[i] == arr[j])
count++;
}
if(count > maxCount) {
maxCount = count;
mode = arr[i];
}
}

printf("Mode = %d", mode);


return 0;
}

6
8. Searching

(a) Linear Search

#include <stdio.h>

int main() {
int arr[100], n, key, i;

scanf("%d", &n);
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);

scanf("%d", &key);
for(i = 0; i < n; i++) {
if(arr[i] == key) {
printf("Element found at index %d", i);
return 0;
}
}

printf("Element not found");


return 0;
}

(b) Binary Search (Array must be sorted)

#include <stdio.h>

int main() {
int arr[100], n, key, low = 0, high, mid;

scanf("%d", &n);
for(int i = 0; i < n; i++)
scanf("%d", &arr[i]);

scanf("%d", &key);
high = n - 1;

while(low <= high) {


mid = (low + high) / 2;
if(arr[mid] == key) {
printf("Element found at index %d", mid);
return 0;
}

7
else if(arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}

printf("Element not found");


return 0;
}

9. Sorting – Bubble Sort

Theory

Bubble sort compares adjacent elements and swaps them.

Program

#include <stdio.h>

int main() {
int arr[100], n, i, j, temp;

scanf("%d", &n);
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);

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;
}
}
}

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


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

return 0;
}

8
10. Matrix Addition

Program

#include <stdio.h>

int main() {
int a[3][3], b[3][3], c[3][3];
int i, j;

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


for(j = 0; j < 3; j++)
scanf("%d", &a[i][j]);

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


for(j = 0; j < 3; j++)
scanf("%d", &b[i][j]);

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


for(j = 0; j < 3; j++)
c[i][j] = a[i][j] + b[i][j];

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


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

return 0;
}

End of Document

You might also like