Institute - Uie Department-Academic Unit-2: Array & Strings
Institute - Uie Department-Academic Unit-2: Array & Strings
The course aims to raise the programming skills of students via logic building
OBJECTIVES capability
& With the knowledge of C language students would be able to model real world
problems
2-Dimensional
Array
Initialization 2-
D Array
After this initialization, each element is as follows:
temp[0][0] : 1
temp[0][1] : 2
temp[0][2] : 3
temp[1][0] : 11
temp[1][1] : 22
temp[1][2] : 33
The compiler will allocate the memory for two dimensional array row-
wise meaning the first element of the second row will be placed after the last
element of the first row.
Memory
Representation 2-
D Array
And if we assume that the first element of the array is at address
1000 and the size of type int is 2 bytes then the elements of the array
will get the following allocated memory locations.
Memory
Representation
2-D Array
1. Program to store the elements from the user and store
them in an 2D array.
#include<stdio.h> for(i=0; i<2; i++)
int main() {
{ for(j=0;j<3;j++)
int disp[2][3], i, j; {
Examples of 2-D for(i=0; i<2; i++)
{
printf("%d ", disp[i][j]);
if(j==2){ printf("\n");
Array for(j=0;j<3;j++) }
}
{
printf("Enter value for disp[%d][%d]:", i, j); }
scanf("%d", &disp[i][j]); return 0;
}
}
}
printf("Two Dimensional array elements:\n");
2.. Program to sum of even and odd of 2D
array.
even=even+arr[i][j];
#include<stdio.h> }
int main() else
{
{ odd=odd+arr[i][j];
}
int i=0,j=0;
}
Examples of int arr[3][4]={{1,2,3,4},{2,3,4,5}, }
{3,4,5,6}}; printf("Sum of even =%d \n",even);
2-D Array int even=0;int odd=0;
printf("Sum of odd =%d",odd);
return 0; }
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
if(arr[i][j]%2==0)
{
1.
In 2-D array, to declare and
access elements of a 2-D array
we use 2 subscripts instead of
1.
SUMMARY 2. 3.
In rectangular 2-dimensional
The compiler will allocate the
arrays, m number of rows and n
memory for two dimensional
number of columns in array row-wise meaning the
the array has the same number
first element of the second
of array elements.
row will be placed after the
last element of the first row.
PROGRAMS
QUESTIONS
1.Which of the following is true about arrays in C.
(A) For every type T, there can be an array of T.
(B) For every type T except void and function type, there can be an array of T.
(C) When an array is passed to a function, C compiler creates a copy of array.
(D) 2D arrays are stored in column major form
2. What will be the output of the following C code?
#include <stdio.h>
void f(int a[][3])
UTILISE {
YOUR a[0][1] = 3;
KNOWLEDGE
int i = 0, j = 0;
for (i = 0; i < 2; i++)