0% found this document useful (0 votes)
11 views40 pages

unit-2-2

Notes

Uploaded by

sudharsr
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)
11 views40 pages

unit-2-2

Notes

Uploaded by

sudharsr
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
You are on page 1/ 40

 Two Dimensional Array requires Two Subscript Variables

 Two Dimensional Array stores the values in the form of matrix.


 One Subscript Variable denotes the “Row” of a matrix.
 Another Subscript Variable denotes the “Column” of a matrix.

3.1.2 INITIALIZING OF 2D ARRAY


An array of two dimensions can be declared as follows:
data_type array_name[size1][size2];
Here data_type is the name of some type of data, such as int. Also,
size1 and size2 are sizes of the array’s first and second dimensions
respectively.
Consider the below program –
#include<stdio.h>
int main() { Output :
int i, j; 14
int a[3][2] = { { 1, 4 }, 52
{ 5, 2 }, 65
{ 6, 5 }};
for (i = 0; i < 3; i++) {
for (j = 0; j < 2; j++) {
printf("%d ", a[i][j]);
}
printf("\n");
} return 0;
}
We have declared an array of size 3 X 2, It contain overall 6 elements.
Row 1 : { 1 , 4 },
Row 2 : { 5 , 2 },
Row 3 : { 6 , 5 }
We have initialized each row independently
a[0][0] = 1
A[0][1] = 4
Method 2 : COMBINE ASSIGNMENT
Initialize all Array elements but initialization is much
straight forward. All values are assigned sequentially and
row-wise
int a[3][2] = {1 , 4 , 5 , 2 , 6 , 5 };
So here it automatically assigns that number 3 has row and
number 2 has column.
Consider the below program –
#include <stdio.h> Output :
int main() { 14
int i, j; 52
int a[3][2] = { 1, 4, 5, 2, 6, 5 };
for (i = 0; i < 3; i++) { 65
for (j = 0; j < 2; j++) {
printf("%d ", a[i][j]);
}
printf("\n");
} return 0;
}
Method 3: SELECTIVE ASSIGNMENT
int a[3][2] = {
{ 1 },
{ 5 , 2 },
{6}
};
Now we have again going with the way 1 but we are removing some
of the elements from the array. In this case we have declared and
initialized 2-D array like this
Consider the below program –
#include <stdio.h> Output :
int main() { 10
int i, j; 52
int a[3][2] = { { 1 }, 60
{ 5, 2 },
{ 6 }};
for (i = 0; i < 3; i++) {
for (j = 0; j < 2; j++) {
printf("%d ", a[i][j]);
} printf("\n"); }
return 0;
}
You can initialize a three dimensional array in a similar way like a
two dimensional array. Here's an example,

int test[2][3][4] = {
{ {3, 4, 2, 3}, {0, -3, 9, 11}, {9, 1, 6, 2} },
{ {13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9} }
};

In this program we mention size1 – row, size2 – column, size3 –


number of elements in that array.
C Program to store values entered by the user in a three-dimensional array
and display it.
#include <stdio.h>
int main()
{
int i, j, k, test[2][3][2]; // this array can store 12 elements
printf("Enter 12 values: \n");
for(i = 0; i < 2; ++i) {
for (j = 0; j < 3; ++j) {
for(k = 0; k < 2; ++k ) {
scanf("%d", &test[i][j][k]);
}
}
}
printf("\nDisplaying values:\n");//Displaying values with
proper index.
for(i = 0; i < 2; ++i) {
for (j = 0; j < 3; ++j) {
for(k = 0; k < 2; ++k ) {
printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]);
}
}
}
return 0;
}
OUTPUT: Displaying Values:
Enter 12 values: test[0][0][0] = 1
1 test[0][0][1] = 2
test[0][1][0] = 3
2 test[0][1][1] = 4
3 test[0][2][0] = 5
4 test[0][2][1] = 6
test[1][0][0] = 7
5
test[1][0][1] = 8
6 test[1][1][0] = 9
7 test[1][1][1] = 10
8 test[1][2][0] = 11
test[1][2][1] = 12
9
10
11
12
3.3 ARRAY PROGRAMS – 2D

a) Basic 2d array program


b) Store and display values
c) Sum of two matrices using Two dimensional arrays
d) Transpose of Matrix
e) Multiplication of 2d matrices.
#include<stdio.h> //Displaying array elements
int main(){ printf("Two Dimensional array
int disp[2][3]; /* 2D array elements:\n");
declaration*/ for(i=0; i<2; i++) {
int i, j; /*Counter variables for(j=0;j<3;j++) {
for loop*/ printf("%d ", disp[i][j]);
for(i=0; i<2; i++) { if(j==2){
for(j=0;j<3;j++) { printf("\n");
printf("Enter value for }
disp[%d][%d]:", i, j); } }
scanf("%d", &disp[i][j]); return 0; }
}}
OUTPUT
Enter value for disp[0][0]:1
Enter value for disp[0][1]:2
Enter value for disp[0][2]:3
Enter value for disp[1][0]:4
Enter value for disp[1][1]:5
Enter value for disp[1][2]:6
Two Dimensional array elements:
123
456
#include <stdio.h> printf("\nDisplaying values:
const int CITY = 1; \n\n");
const int WEEK = 7; for (int i = 0; i < CITY; ++i) {
int main() for(int j = 0; j < WEEK; ++j)
{ {
int temperature[CITY][WEEK]; printf("City %d, Day %d =
for (int i = 0; i < CITY; ++i) { %d\n", i+1, j+1,
for(int j = 0; j < WEEK; ++j) { temperature[i][j]);
printf("City %d, Day %d: ", }
i+1, j+1); }
scanf("%d", return 0;
&temperature[i][j]); }
}
}
OUTPUT
City 1, Day 1: 33 Displaying values:
City 1, Day 2: 34 City 1, Day 1 = 33
City 1, Day 3: 35 City 1, Day 2 = 34
City 1, Day 4: 33 City 1, Day 3 = 35
City 1, Day 5: 32 City 1, Day 4 = 33
City 1, Day 6: 31 City 1, Day 5 = 32
City 1, Day 7: 30 City 1, Day 6 = 31
City 1, Day 7 = 30
#include <stdio.h> printf("Enter elements of 2nd
int main() matrix\n");
{ for(i=0; i<2; ++i)
float a[2][2], b[2][2], c[2][2]; for(j=0; j<2; ++j)
int i, j; // Taking input using nested {
for loop printf("Enter b%d%d: ", i+1, j+1);
printf("Enter elements of 1st scanf("%f", &b[i][j]);
matrix\n"); }// adding corresponding elements of
for(i=0; i<2; ++i) two arrays
for(j=0; j<2; ++j) for(i=0; i<2; ++i)
{ for(j=0; j<2; ++j)
printf("Enter a%d%d: ", i+1, j+1); {
scanf("%f", &a[i][j]); c[i][j] = a[i][j] + b[i][j];
} // Taking input using nested for loop }
OUTPUT:
Enter elements of 1st matrix
// Displaying the sum Enter a11: 2;
printf("\nSum Of Matrix:"); Enter a12: 0.5;
for(i=0; i<2; ++i) Enter a21: -1.1;
for(j=0; j<2; ++j) Enter a22: 2;
Enter elements of 2nd matrix
{
Enter b11: 0.2;
printf("%.1f\t", c[i][j]); Enter b12: 0;
if(j==1) Enter b21: 0.23;
printf("\n"); Enter b22: 23;
} Sum Of Matrix:
2.2 0.5
return 0;
-0.9 25.0
}
#include <stdio.h> printf("\nEntered Matrix: \n");
int main() for(i=0; i<r; ++i)
{ for(j=0; j<c; ++j)
int a[10][10], transpose[10][10], r, c, {
i, j; printf("%d ", a[i][j]);
printf("Enter rows and columns of if (j == c-1)
matrix: "); printf("\n\n");
scanf("%d %d", &r, &c); // Storing }// Finding the transpose of
elements matrix a
printf("\nEnter elements of for(i=0; i<r; ++i)
matrix:\n"); for(j=0; j<c; ++j)
for(i=0; i<r; ++i) {
for(j=0; j<c; ++j) transpose[j][i] = a[i][j];
{ }
printf("Enter element a%d%d:
",i+1, j+1);
scanf("%d", &a[i][j]); }
output
// Displaying the transpose of matrix a Enter rows and columns of matrix: 2 3
printf("\nTranspose of Matrix:\n"); Enter element of matrix:
for(i=0; i<c; ++i) Enter element a11: 2
for(j=0; j<r; ++j) Enter element a12: 3
{ Enter element a13: 4
printf("%d ",transpose[i][j]); Enter element a21: 5
if(j==r-1) Enter element a22: 6
printf("\n\n"); Enter element a23: 4
} Entered Matrix:
return 0; 2 3 4
} 5 6 4
Transpose of Matrix:
2 5
3 6
4 4
#include <stdio.h> printf("Enter number of rows and
int main() columns of second matrix\n");
{ scanf("%d%d", &p, &q);
int m, n, p, q, c, d, k, sum = 0; for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
int first[10][10], second[10][10],
scanf("%d", &second[c][d]);
multiply[10][10];
for (c = 0; c < m; c++) {
printf("Enter number of rows and for (d = 0; d < q; d++) {
columns of first matrix\n"); for (k = 0; k < p; k++) {
scanf("%d%d", &m, &n); sum = sum +
printf("Enter elements of first first[c][k]*second[k][d];
matrix\n"); }
for (c = 0; c < m; c++) multiply[c][d] = sum;
for (d = 0; d < n; d++) sum = 0;
scanf("%d", &first[c][d]); } }
printf("Product of the Enter the number of rows and
columns of second matrix:
matrices:\n"); 3
for (c = 0; c < m; c++) { 3
for (d = 0; d < q; d++) Enter the elements of second
printf("%d\t", multiply[c][d]); matrix:
printf("\n"); 112
211
} } 121
return 0; Product of entered matrices:-
} 5 3 4
OUTPUT 3 3 2
Enter the number of rows and columns 3 4 5
of first matrix: 3 3
Enter the elements of first matrix: 1 2 0
011
201
3.4 ARRAY CONTIGUOUS MEMORY
 When Big Block of memory is reserved or allocated then that
memory block is called as Contiguous Memory Block.
 Alternate meaning of Contiguous Memory is continuous
memory.
 Suppose inside memory we have reserved 1000-1200 memory
addresses for special purposes then we can say that these 200
blocks are going to reserve contiguous memory.
How to allocate contiguous memory:
 Using static array declaration.
 Using alloc() / malloc() function to allocate big chunk of
memory dynamically.
Contiguous Memory Allocation
 Two registers are used while implementing the
contiguous memory scheme. These registers are base
register and limit register.
When OS is executing a process inside the main memory then
content of each register are as –
Register - Content of register.
Base register- Starting address of the memory
location where process execution is happening.
Limit register- Total amount of memory in bytes
consumed by process.
When process try to refer a part of the memory then it will firstly
refer the base address from base register and then it will refer
relative address of memory location with respect to base address
3.5 ADVANTAGE AND LIMITATIONS OF ARRAY
3.5.1 Advantages:
 It is better and convenient way of storing the data of same
datatype with same size.
 It allows us to store known number of elements in it.
 It allocates memory in contiguous memory locations for its
elements. It does not allocate any extra space/ memory for its
elements. Hence there is no memory overflow or shortage of
memory in arrays.
 Iterating the arrays using their index is faster compared to any
other methods like linked list etc.
 It allows to store the elements in any dimensional array -
supports multidimensional array.
3.5.2 Limitations of Array:
A) Static Data
 Array is Static data Structure
 Memory Allocated during Compile time.
 Once Memory is allocated at Compile Time it Cannot be
Changed during Run-time.
B) Can hold data belonging to same Data types
 Elements belonging to different data types cannot be stored
in array because array data structure can hold data belonging
to same data type.
 Example : Character and Integer values can be stored inside
separate array but cannot be stored in single array
C) Inserting data in Array is Difficult
 Inserting element is very difficult because before inserting
element in an array have to create empty space by shifting
other elements one position ahead.
 This operation is faster if the array size is smaller, but same
operation will be more and more time consuming and non-
efficient in case of array with large size.
D) Deletion Operation is difficult
 Deletion is not easy because the elements are stored
in contiguous memory location.
 Like insertion operation , we have to delete element from the
array and after deletion empty space will be created and thus
we need to fill the space by moving elements up in the array.
E) Bound Checking
 If we specify the size of array as ‘N’ then we can access elements upto
‘N-1’ but in C if we try to access elements after ‘N-1’ i.e Nth element or
N+1th element then we does not get any error message.
 Process of Checking the extreme limit of array is called Bound checking
and C does not perform Bound Checking.
 If the array range exceeds then we will get garbage value as result.
F) Shortage of Memory
 Array is Static data structure. Memory can be allocated at compile time
only Thus if after executing program we need more space for storing
additional information then we cannot allocate additional space at run
time.
 Shortage of Memory , if we don’t know the size of memory in advance
G) Wastage of Memory
 Wastage of Memory , if array of large size is defined.
3.6 ARRAY CONSTRUCTION FOR REAL-TIME APPLICATION
COMMON PROGRAMMING ERRORS

(i) Constant Expression Require


#include<stdio.h>
void main()
{
int i=10;
int a[i];
}
In this example we see what’s that error?
 We are going to declare an array whose size is equal to
the value of variable.
 If we changed the value of variable then array size is
going to change.
 According to array concept, we are allocating memory
for array at compile time so if the size of array is going
to vary then how it is possible to allocate memory to
an array.
 i is initialized to 10 and using a[i] does not mean a[10]
because ‘i’ is Integer Variable whose value can be
changed inside program.
 Value of Const Variable Cannot be changed
 we know that value of Const Variable cannot be
changed once initialized so we can write above
example as below –
#include<stdio.h>
void main()
{
const int i=10;
int a[i];
}
or
int a[10];
Consider this example, we

(ii) Empty Valued 1D Array can see the empty pair of
#include<stdio.h> square brackets means we
void main() haven’t specified size of an
{ 1D Array. In this example
int arr[]; array ‘arr’ is undefined or
empty.
}
• Size of 1D Array should be
Instead of it Write it as – Specified as a Constant Value.
#include<stdio.h> #include<stdio.h>
void main() void main()
{ {
int a[] = {1,1}; int a[] = {};// This also
Cause an Error
}
}
(iii) 1D Array with no Bound  So we have Access to
Checking
Following Array Elements –
#include<stdio.h>
void main() a[0],a[1],a[2],a[3] and a[4].
{
 But accessing a[5] causes
int a[5];
printf("%d",a[7]); Garbage Value to be used
}
Here Array size specified is 5. because C Does not performs
Array Bound Check.
If the maximum size of array is “MAX” then we can access following elements of an
array –
Elements accessible for Array Size "MAX" = arr[0]
=.
= arr[MAX-1]
4. Case Sensitive
#include<stdio.h>
void main()
{
int a[5];
printf("%d",A[2]);
}
Array Variable is Case Sensitive so A[2] does not print
anything it Displays Error Message : “Undefined Symbol A“

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy