0% found this document useful (0 votes)
3 views10 pages

Array Notes

The document provides a comprehensive overview of arrays in C programming, detailing their definition, characteristics, advantages, and disadvantages. It covers both one-dimensional and two-dimensional arrays, including syntax, memory representation, and example programs for operations like addition, subtraction, and finding maximum/minimum values. Additionally, it highlights the differences between 1D and 2D arrays and includes dry run examples for clarity.

Uploaded by

Chat Gpt
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)
3 views10 pages

Array Notes

The document provides a comprehensive overview of arrays in C programming, detailing their definition, characteristics, advantages, and disadvantages. It covers both one-dimensional and two-dimensional arrays, including syntax, memory representation, and example programs for operations like addition, subtraction, and finding maximum/minimum values. Additionally, it highlights the differences between 1D and 2D arrays and includes dry run examples for clarity.

Uploaded by

Chat Gpt
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

ARRAY IN C (Detailed Theory Notes – NEB)

🔰 1. Introduction to Array (Long Explanation) In programming, data plays a very important role.
Sometimes we need to store only one value, such as age or roll number. In such cases, a single
variable is enough. However, in many real-life situations, we need to store a large amount of
similar data.

For example:

 Marks of 50 students
 Salary of 100 employees
 Temperature of 7 days
 Prices of 12 months

If we try to store them using normal variables, we would need:

int m1, m2, m3, m4, m5, ..., m50;

This approach creates many problems:

 Difficult to remember variable names


 Difficult to manage
 Repetition of code
 Not efficient

To solve this problem, C provides a special data structure called Array.

An array allows us to store multiple values of the same data type under a single variable name.
These values are stored in continuous memory locations, which makes access faster and
systematic.

Thus, an array is very useful when dealing with large amounts of similar data.

🔰 2. Definition of Array

An array is a collection of elements of the same data type stored in contiguous memory locations
and accessed using a common name with index numbers.

🔰 3. Characteristics of Array (7 Points)


1️⃣ Same Data Type: All elements in an array must be of the same data type.

int a[5]; // only integers

2️⃣ Contiguous Memory Allocation: Elements are stored in consecutive memory locations

3️⃣ Fixed Size: Size must be declared at the time of declaration.

4️⃣ Index Based Access: Elements are accessed using index numbers.

a[0], a[1], a[2]

5️⃣ Index Starts from 0: First element has index 0.

6️⃣ Random Access: Any element can be accessed directly

. 7️⃣ Single Name for Multiple Values: Multiple values are stored under one variable name.

🔰 4. Advantages of Array (7 Points)

1️⃣ Easy Handling of Large Data: Store many values using a single variable.

2️⃣ Reduces Code Length: Loops reduce repetitive code.

3️⃣ Fast Access: Continuous memory allocation ensures faster access.

4️⃣ Organized Storage: Data stored systematically with indexes.

5️⃣ Easy to Use with Loops: Loops work efficiently with arrays.

6️⃣ Basis for Other Data Structures: Arrays are foundation for strings, matrices, stack, queue. 7️⃣
Efficient Memory Usage: Structured allocation in memory.

🔰 5. Disadvantages of Array (7 Points)

1️⃣ Fixed Size Limitation: Size cannot change during execution.

2️⃣ Memory Wastage: Declared size larger than needed wastes memory.

3️⃣ Same Data Type Only: Cannot store mixed types.

4️⃣ Insertion is Difficult: Shifting required to insert in middle.


5️⃣ Deletion is Difficult: Shifting required to delete

. 6️⃣ No Bound Checking: C does not automatically check array overflow.

7️⃣ Not Suitable for Dynamic Memory Needs: Cannot easily adjust size.

ONE DIMENSIONAL ARRAY (1D)


🔹 Introduction A 1D array stores elements in a single row (linear). Used when data is in list
form.

Example Uses: Marks of students, monthly expenses, daily temperature.

🔹 Syntax

data_type array_name[size];

Example:

int marks[5];

🔹 Memory Representation

int a[5] = {10,20,30,40,50};

Memory layout: | Index | 0 | 1 | 2 | 3 | 4 | | Value |10 |20 |30 |40 |50 |

🔹 Accessing Elements

a[0]; // first element


a[4]; // last element

🔹 Example Program (Sum & Average)

#include <stdio.h>
int main(){
int marks[5], i, sum=0;
float avg;
printf("Enter 5 marks:\n");
for(i=0;i<5;i++){
scanf("%d", &marks[i]);
sum += marks[i];
}
avg = sum/5.0;
printf("Total=%d\n", sum);
printf("Average=%.2f", avg);
return 0;
}

🔹 Dry Run Example Input: 10 20 30 40 50

marks su
i
[i] m

0 10 10

1 20 30

2 30 60

3 40 100

4 50 150

Output: Total=150,
Average=30

TWO DIMENSIONAL ARRAY (2D)


🔹 Introduction A 2D array stores elements in rows & columns. Also called a matrix. Used when
data is in table form.

Example Uses: Marks sheet (students × subjects), matrix operations, game boards.

🔹 Syntax

data_type array_name[row][column];

Example:

int a[2][3]; // 2 rows, 3 columns

🔹 Memory Representation

int a[2][2] = {{1,2},{3,4}};

Matrix form: |1|2| |3|4|

🔹 Example Program (Matrix Addition)

#include <stdio.h>
int main(){
int a[2][2], b[2][2], c[2][2], i,j;
printf("Enter first matrix:\n");
for(i=0;i<2;i++)
for(j=0;j<2;j++) scanf("%d", &a[i][j]);
printf("Enter second matrix:\n");
for(i=0;i<2;i++)
for(j=0;j<2;j++) scanf("%d", &b[i][j]);
for(i=0;i<2;i++)
for(j=0;j<2;j++) c[i][j] = a[i][j]+b[i][j];
printf("Sum matrix:\n");
for(i=0;i<2;i++){
for(j=0;j<2;j++) printf("%d ", c[i][j]);
printf("\n");
}
return 0;
}

🔹 Dry Run Input A: 1 2 / 3 4 Input B: 5 6 / 7 8 Output C: 6 8 10 12

🔹 Matrix Subtraction Formula: c[i][j]=a[i][j]-b[i][j] Result example: Input A: 1 2 / 3 4, Input B: 5


6 / 7 8 Output C: -4 -4 / -4 -4

Difference Between 1D and 2D Array


1D Array 2D Array
Uses single Uses two indexes
index
Represents list Represents table
Stored in row Stored in rows &
columns
Example: Example: marks[5][3]
marks[5]

1D Array Programs (Short + Dry Run)

1. Sum and Average of Marks

#include <stdio.h>
int main() {
int marks[5], i, sum=0;
float avg;
for(i=0;i<5;i++) scanf("%d",&marks[i]);
for(i=0;i<5;i++) sum+=marks[i];
avg=sum/5.0;
printf("Sum=%d, Average=%.2f\n",sum,avg);
return 0;
}
Input: 50 60 70 80 90 Dry Run:

marks su
i
[i] m

0 50 50

1 60 110

2 70 180

3 80 260

4 90 350

Output: Sum=350,
Average=70.00

2. Maximum in Array

#include <stdio.h>
int main() {
int arr[5], i, max;
for(i=0;i<5;i++) scanf("%d",&arr[i]);
max=arr[0];
for(i=1;i<5;i++) if(arr[i]>max) max=arr[i];
printf("Max=%d\n",max);
return 0;
}

Input: 12 45 7 90 33 Dry Run:

arr[ ma
i
i] x

0 12 12

1 45 45

2 7 45

3 90 90

4 33 90
arr[ ma
i
i] x

Output:
Max=90

3. Minimum in Array

#include <stdio.h>
int main() {
int arr[5], i, min;
for(i=0;i<5;i++) scanf("%d",&arr[i]);
min=arr[0];
for(i=1;i<5;i++) if(arr[i]<min) min=arr[i];
printf("Min=%d\n",min);
return 0;
}

Input: 12 45 7 90 33 Dry Run:

arr[ mi
i
i] n

0 12 12

1 45 12

2 7 7

3 90 7

4 33 7

Output:
Min=7

4. Reverse Array

#include <stdio.h>
int main() {
int arr[5], i, t;
for(i=0;i<5;i++) scanf("%d",&arr[i]);
for(i=0;i<5/2;i++){ t=arr[i]; arr[i]=arr[4-i]; arr[4-i]=t; }
for(i=0;i<5;i++) printf("%d ",arr[i]);
return 0;
}
Input: 1 2 3 4 5 Dry Run:

swa
i Array
p

5234
0 1↔5
1

5432
1 2↔4
1

Output: 5 4 3
21

5. Count Even and Odd

#include <stdio.h>
int main() {
int arr[5], i, e=0, o=0;
for(i=0;i<5;i++) scanf("%d",&arr[i]);
for(i=0;i<5;i++) { if(arr[i]%2==0) e++; else o++; }
printf("Even=%d, Odd=%d\n",e,o);
return 0;
}

Input: 10 23 44 55 12 Dry Run:

arr[ Eve Od
i
i] n d

0 10 1 0

1 23 1 1

2 44 2 1

3 55 2 2

4 12 3 2

Output: Even=3,
Odd=2
2D Array Programs (2x2)
1. Matrix Addition

#include <stdio.h>
int main(){
int A[2][2],B[2][2],C[2][2],i,j;
for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&A[i][j]);
for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&B[i][j]);
for(i=0;i<2;i++) for(j=0;j<2;j++) C[i][j]=A[i][j]+B[i][j];
for(i=0;i<2;i++){for(j=0;j<2;j++) printf("%d ",C[i][j]); printf("\n");}
return 0;
}

Input: A=1 2;3 4 B=5 6;7 8 Dry Run:

A[i] B[i] C[i]


i j
[j] [j] [j]

0 01 5 6

0 12 6 8

1 03 7 10

1 14 8 12

Output:
68

10 12

2. Matrix Subtraction

#include <stdio.h>
int main(){
int A[2][2],B[2][2],C[2][2],i,j;
for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&A[i][j]);
for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&B[i][j]);
for(i=0;i<2;i++) for(j=0;j<2;j++) C[i][j]=A[i][j]-B[i][j];
for(i=0;i<2;i++){for(j=0;j<2;j++) printf("%d ",C[i][j]); printf("\n");}
return 0;
}

Output: -4 -4 -4 -4
3. Transpose of a Matrix

#include <stdio.h>
int main(){
int A[2][2],T[2][2],i,j;
for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&A[i][j]);
for(i=0;i<2;i++) for(j=0;j<2;j++) T[j][i]=A[i][j];
for(i=0;i<2;i++){for(j=0;j<2;j++) printf("%d ",T[i][j]); printf("\n");}
return 0;
}

Input: 1 2;3 4 Output: 1 3 2 4

4. Sum of Each Row and Column

#include <stdio.h>
int main(){
int A[2][2],i,j,rowSum,colSum;
for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&A[i][j]);
for(i=0;i<2;i++){rowSum=0; for(j=0;j<2;j++) rowSum+=A[i][j]; printf("Row %d=
%d\n",i,rowSum);}
for(j=0;j<2;j++){colSum=0; for(i=0;i<2;i++) colSum+=A[i][j]; printf("Col %d=
%d\n",j,colSum);}
return 0;
}

Output: Row 0=3 Row 1=7 Col 0=4 Col 1=6

5. Multiplication of 2 Matrices

#include <stdio.h>
int main(){
int A[2][2],B[2][2],C[2][2],i,j,k;
for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&A[i][j]);
for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&B[i][j]);
for(i=0;i<2;i++) for(j=0;j<2;j++){C[i][j]=0; for(k=0;k<2;k++) C[i][j]+=A[i]
[k]*B[k][j];}
for(i=0;i<2;i++){for(j=0;j<2;j++) printf("%d ",C[i][j]); printf("\n");}
return 0;
}

Output: 19 22 43 50

You might also like