0% found this document useful (0 votes)
18 views49 pages

Array (Updated)

The document provides an introduction to arrays in programming, explaining their definition, importance, and how to declare and initialize them in C. It covers both one-dimensional and two-dimensional arrays, including examples of input/output operations and common programming tasks such as finding sums and searching for elements. Additionally, it discusses matrix operations like addition, subtraction, and transposition.

Uploaded by

guruvignesh.k-29
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)
18 views49 pages

Array (Updated)

The document provides an introduction to arrays in programming, explaining their definition, importance, and how to declare and initialize them in C. It covers both one-dimensional and two-dimensional arrays, including examples of input/output operations and common programming tasks such as finding sums and searching for elements. Additionally, it discusses matrix operations like addition, subtraction, and transposition.

Uploaded by

guruvignesh.k-29
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

CS101: Introduction to

Programming

Vishal Singh
Lecturer, SCDS
Sai University
vishal.s@[Link]
What is an Array?
● An array is a collection of elements of the same data type stored in
contiguous memory locations.
● It allows storing multiple values using a single variable name.

“Arrays are like a row of similar containers, each


having its own number. You can store one item in
each, and access any of them directly using that
number”
Why We Need Arrays ?

Problem:
• Suppose we want to store marks of 5 students.
• Without arrays:
• int m1, m2, m3, m4, m5;
• Too many variables!
Solution → Use an array
• Store multiple values in one variable:
• int marks[5];
Why Array is important?
● To store large amounts of data efficiently
● Easy access using index numbers
● Simplifies code and improves readability
● Supports looping for repetitive operations
Array Declaration and Initialization
Size
Syntax:

int marks[ ]; // Invalid: you must specify the size or initialize it.

Note: Array index starts from 0 and goes up to (size - 1).


Conclusion of Declaration and Initialization

Declaration Memory Representation (1D Array)

G.V G.V G.V G.V G.V


Locally Uninitialized int arr[5];

Locally Fully Initialized int arr[5] = {10, 20, 30, 40, 50}; 10 20 30 40 50

Locally Partially Initialized int arr[5] = {10, 20}; 10 20 0 0 0

Globally Uninitialized int arr[5]; 0 0 0 0 0


Array for different datatype

int marks[3];
char name[10]; G.V G.V G.V
(Garbage Value)

float price[2];
Input &Output
scanf("%d", &marks[0]); Used when we have to take input from the user

printf("%d", marks[0]);

0 1 2
Accessing Array Elements

Each element can be accessed using its index.


Example:
Using Loops with Arrays
Global vs local Declaration
Global Declaration Local Declaration
Example — Sum of Array Elements
1. Write a C program to input 10 integers from the user and
store them in an [Link], display all the integers in the
same order as they were entered.
2. Write a C program to input n integers from the user and find their average.
3. Write a C program to input n integers from the user and store them in an array.
Then, count and print how many numbers are even and how many are odd.
4. Write a C program to input an array of integers and a number,
then check whether the given number exists in the array or not.(Linear Search)

Steps:
[Link], take input for the size of the array (n).
[Link], store all values in the array arr.
[Link] the user for the number to search.
[Link] a for loop to check each element:
1. If any element matches, set found = 1 and
break out of the loop.
[Link], print whether the number was found or not.
About Flag Variable
Example: Searching for your pen on the table
Imagine you lost your pen and you are searching for it on the table.
On the table, there are many items — book, mobile, key, pen, bottle, etc.
Before you start searching, you decide:
“If I find the pen, I’ll raise my hand to say yes, I found it!”

So you have a small signal in your mind — that signal is your flag.
Step-by-step:
[Link] searching →
You think: “I haven’t found it yet.”
flag = 0
[Link] start checking each item one by one:
1. Book → not pen
2. Mobile → not pen
3. Key → not pen
4. Pen → found it!
Now you change your signal: flag = 1
[Link] checking everything:
1. If flag == 1, you say “Yes, I found my pen!”
2. If flag == 0, you say “No, it’s not on the table.”
5. Write a C program to input elements in an array and calculate the sum of all
positive numbers and the sum of all negative numbers separately.(H.W)

Steps:
6. Write a C program to find the largest element in an array.

Example Input/Output:
Input:
Enter size of array: 5
Enter elements: 10 25 7 56 42
Output:
Largest element = 56
Thank You
2D Arrays
Why 2D Arrays?
● “If I want to store marks of 2 students in 3subjects — how will I do it?”
● The problem with a single 1D array. It can store only one student
marks of 3 subject
● Introduce the new concept:
● A 2D array is like a table (rows and columns) that stores data in two
dimensions.
Sub1 Sub2 Sub3
1d array
Stu1 45 50 60

Sub1 Sub2 Sub3


Stu1 45 50 60 2d array

Stu2 55 65 75
Declaration and Initialization
• Multidimensional arrays are declared by more than one set of square brackets after the array variable
name.
• Syntax for 2D Array
• Declaration:
dataType arrayName[#rows][#columns];
• Initialized version:
dataType arrayName[#rows][#columns] = { {v01, v02, ...},{v11, v12, ...}, ... };

Syntax:
int marks[2][3]
Here:
• 2 → number of rows (students) a[0][0] a[0][1] a[0][2]
• 3 → number of columns (subjects) a[1][0] a[1][1] a[1][2]
It means:
• marks has 2 rows
45 a[0][0] 50 a[0][1] 60 a[0][2]
• Each row has 3 columns
55 a[1][0] 65 a[1][1] 75 a[1][2]
Method 1: With Braces for Each Row

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


● Here:
○ {1, 2, 3} → fills Row 0
○ {4, 5, 6} → fills Row 1
Method 2: Without Inner Braces

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

This is also valid — C will fill the array row by row automatically.

Initialization Meaning Same Output?


Explicitly defines
int a[2][3] = {{1,2,3},{4,5,6}}; Yes
each row
Fills row by row
int a[2][3] = {1,2,3,4,5,6}; Yes
automatically
Common mistakes

1. Writing too many elements


int a[2][3] = {1,2,3,4,5,6,7}; // Error:
too many initializers

1. Partial initialization → missing values become 0


int a[2][3] = {1, 2, 3}; // rest (4,5,6) will be 0
So memory will be:
Row 0: 1 2 3
Row 1: 0 0 0
Understanding 2D Array Initialization
Example 1: int a[3][3] = {1};
•Initializes first element only → a[0][0] = 1
•All other elements automatically become 0
Resulting Matrix:
1 0 0
0 0 0
0 0 0

Example 2: int b[3][3] = {0};


•Initializes all elements to 0
•Works same as writing {0, 0, 0, 0, 0, 0, 0, 0, 0}
Resulting Matrix:
0 0 0
0 0 0
0 0 0
Example 3: Partial Initialization
int c[3][3] = {
{1, 2},
{3, 4, 5}
};

Resulting Matrix:
1 2 0
3 4 5
0 0 0

Key Points
•Missing elements are automatically filled with 0.
•{1} means only the first element is set to 1.
•{0} (or empty braces {}) means all become 0.
•Always think row-wise while initializing 2D arrays.
Input and Output
Write a C program to input
elements into a 2×3 matrix
and then display the matrix in
proper row and column form.
Example Input/Output:

Enter elements:
123
456

Matrix is:
123
456
j
0 1 2

0 a[0][0] 1 a[0][1] 2 a[0][2] 3


i
1 a[1][0] 4 a[1][1] 5 a[1][2] 6

Outer Loop (i) Inner Loop (j) Operation a[ i ][ j ] Value Read / Printed
0 0 Input a[0][0] 1
0 1 Input a[0][1] 2
0 2 Input a[0][2] 3
1 0 Input a[1][0] 4
1 1 Input a[1][1] 5
1 2 Input a[1][2] 6

0 0 Print a[0][0] 1
0 1 Print a[0][1] 2
0 2 Print a[0][2] 3
1 0 Print a[1][0] 4
1 1 Print a[1][1] 5
1 2 Print a[1][2] 6
Important examples
When you declare a 2D array in C, you must specify the number of columns (the second dimension),
because the compiler needs to know how many elements are in each row to calculate memory layout correctly.
Conclusion:

Declaration Valid? Explanation

int a[2][3]; Both dimensions given explicitly

Column size known → row count


int a[ ][3] = {{1,2,3}, {4,5,6}};
deduced from initializer

Column size missing → compiler


int a[2][ ] = {{1,2,3}, {4,5,6}};
cannot calculate memory layout

Both dimensions missing →


int a[ ][ ] = {{1,2,3}, {4,5,6}};
completely invalid declaration
1. Write a C program to input a 3×3 matrix and find the sum
of all its elements.
Explanation:
You have to add all numbers present in the matrix.
This means adding every element a[i][j] for all rows and
columns.

Example Input/Output:
Enter elements of 3x3 matrix:
1 2 3
4 5 6
7 8 9

Sum of all elements = 45


Q2. Write a C program to input a 3×3 matrix and find the sum of
each row and each column.
Explanation:
You need to calculate:
• The sum of all elements in each row, and
• The sum of all elements in each column.
We use two nested loops —
one to sum rows, and another to sum columns.
Example Input/Output:
Enter elements of 3x3 matrix:
1 2 3
4 5 6
7 8 9
Output:
Row 0 sum = 6
Row 1 sum = 15
Row 2 sum = 24
Column 0 sum = 12
Column 1 sum = 15
Column 2 sum = 18
Write a C program to input a 3×3 matrix and display its diagonal
elements.

Explanation:

A diagonal element is one where the row index equals


the column index (i == j).

For example, in a 3×3 matrix:


Elements a[0][0], a[1][1], and a[2][2] form the
main diagonal.

Example Input/Output:
Enter elements of 3x3 matrix:
1 2 3
4 5 6
7 8 9
Output:
Diagonal elements: 1 5 9
H.W
Q1. Write a C program to input a 3×3 matrix and find the sum of its main diagonal elements.
Example Input/Output:
Enter elements of 3x3 matrix:
1 2 3
4 5 6
7 8 9

Sum of main diagonal elements = 15


Hint:
Main diagonal elements are those where the row index and column index are the same, i.e., a[0][0], a[1][1], a[2][2].

Q2. Write a C program to input elements in a 2D array and count how many numbers are even and how many are odd.
Example Input/Output:
Enter elements of 3x3 matrix:
1 2 3
4 5 6
7 8 9
Even numbers = 4
Odd numbers = 5
Hint:
Use the modulus operator (%) to check whether a number is even or odd.
Topic: Matrix Addition,
Subtraction & Transpose
Matrix addition

a[0][0] a[0][1] b[0][0] b[0][1] a[0][0]+b[0][0] a[0][1]+b[0][1]


+ = a[1][0]+b[1][0] a[1][1]+b[1][1]
a[1][0] a[1][1] b[1][0] b[1][1]
Matrix Addition
Two matrices can be added only if they have the
same number of rows and columns.
Formula:
C[i][j] = A[i][j] + B[i][j]
Matrix Subtraction(H.W)
Concept:
C[i][j] = A[i][j] - B[i][j]
Matrix Transpose
Concept:
In transpose, rows become columns.
If
A = 1 2 3
4 5 6
then
Aᵀ = 1 4
2 5
3 6
Formula:
T[j][i] = A[i][j]
Thank You

You might also like