0% found this document useful (0 votes)
5 views

Arrays in C-2D

This document provides an overview of arrays in C, focusing on two-dimensional arrays, their declaration, initialization, and memory storage methods. It includes examples of array operations such as matrix multiplication and transposition, as well as quizzes to test understanding of the concepts. The content aims to help students grasp the practical applications of arrays in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Arrays in C-2D

This document provides an overview of arrays in C, focusing on two-dimensional arrays, their declaration, initialization, and memory storage methods. It includes examples of array operations such as matrix multiplication and transposition, as well as quizzes to test understanding of the concepts. The content aims to help students grasp the practical applications of arrays in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Arrays

Arrays in C
1. Through this lecture, students will understand
Learning the concept of arrays, types of arrays,
Outcomes application in real life
2. Students will learn how to declare arrays in C
Contents
• Types of array – 2D
• How to declare 2D arrays
Two dimensional Array (2-D array)
Syntax: data_type varname[size1][size2];

Initialization Method 1: int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

Initialization Method 2: int matrix[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};

Unsized Initialization: int matrix[][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};

#include <stdio.h>
int main()
{ int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, i, j;
for(i = 0; i < 3; i++)
{ 123
for(j = 0; j < 3; j++) 456
{ 789
printf("%2d ", matrix[i] [j]);
} printf("\n");
}
return 0; }
Initialization of a 2-D Array
int a[2][3]={1,2,3,4,5,6};
int a[2][3]={{1,2,3}, {4,5,6}};
int a[][3]={{1,2,3}, {4,5,6}}
int a[2][3]={0}

int a[3][]={2,4,6,8,10,12};
int a[][]={2,4,6,8,10,12};

Note: If the first bracket pair is empty, then compiler take the size
from the number of inner brace pairs
Example
#include <stdio.h>
#define row 4
#define col 3
void main()
{
int M[row][col];
int i,j,k;
printf(“Enter data for Matrix M1\n”);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf(“%d”,&M1[i][j]);
}
printf(“\n”);
}
Memory Map for 2-D Arrays
Kept in memory as a linear sequence of variables.
Two methods for storing-
– Row major
– Column major
Example:
int a[3][3];
Row major storage:
a[0][0], a[0][1], a[0][2], a[1][0], a[1][1], a[1][2],
a[2][0], a[2][1], a[2][2]
Column major storage:
a[0][0], a[1][0], a[2][0], a[0][1], a[1][1], a[2][1],
a[0][2], a[1][2], a[2][2]
Program on Multidimensional Arrays
#include <stdio.h>
int main()
Output:
{ Temperature @ location 1
float temp[2][4] = {{12.31, 14.36, 13.4, 12.4}, {11.2, ---> 12.31 (1 time)
11.24, 14.2, 12.5}}; Temperature @ location 1
---> 14.36 (2 time)
int i, j; Temperature @ location 1
for(i = 0; i < 2; i++) ---> 13.4 (3 time)
{ Temperature @ location 1
---> 12.4 (4 time)
for(j = 0; j < 4; j++)
Temperature @ location 2
{ ---> 11.2 (1 time)
printf("\nTemperature @location%d ---> %f (%d time) Temperature @ location 2
---> 11.24 (2 time)
",i+1, temp[i][j] ,j+1);
Temperature @ location 2
} ---> 14.2 (3 time)
} Temperature @ location 2
return 0; ---> 12.2 (4 time

}
Transpose of a matrix
#include <stdio.h> printf("Transpose of matrix is \n");
void main() for (j = 0; j < n; ++j)
{ static int array[10][10]; {
int i, j, m, n;
for (i = 0; i < m; ++i)
printf("Enter the order of the matrix \n");
{
scanf("%d %d", &m, &n);
printf(" %d", array[i][j]);
printf("Enter the coefiicients of the matrix\n");
}
for (i = 0; i < m; ++i)
{ for (j = 0; j < n; ++j) printf("\n");
{ scanf("%d", &array[i][j]); }
}} }
printf("The given matrix is \n");
for (i = 0; i < m; ++i)
{ for (j = 0; j < n; ++j)
{ printf(" %d", array[i][j]); }
printf("\n"); }
Matrix Multiplication
#include <stdio.h> else
int main() { printf("Enter the elements of second matrix\n");
{ int m, n, p, q, c, d, k, sum = 0; for ( c = 0 ; c < p ; c++ )
int first[10][10], second[10][10], multiply[10][10]; for ( d = 0 ; d < q ; d++ )
printf("Enter the number of rows and columns of first scanf("%d", &second[c][d]);
matrix\n");
for ( c = 0 ; c < m ; c++ )
scanf("%d%d", &m, &n);
{ for ( d = 0 ; d < q ; d++ )
printf("Enter the elements of first matrix\n");
{ for ( k = 0 ; k < p ; k++ )
for ( c = 0 ; c < m ; c++ )
{ sum = sum + first[c][k]*second[k][d]; }
for ( d = 0 ; d < n ; d++ )
multiply[c][d] = sum;
scanf("%d", &first[c][d]);
sum = 0; } }
printf("Enter the number of rows and columns of
second matrix\n"); printf("Product of entered matrices:-\n");

scanf("%d%d", &p, &q); for ( c = 0 ; c < m ; c++ )

if ( n != p ) { for ( d = 0 ; d < q ; d++ )

printf("Matrices with entered orders can't be printf("%d\t", multiply[c][d]);


multiplied with each other.\n"); printf("\n"); } } }
Quiz
1. Consider the following declaration of a ‘two-dimensional array in C:
char a[100][100];
Assuming that the main memory is byte-addressable and that the array is stored starting
from memory address 0, the address of a[40][50] is:
(A) 4040 (B) 4050 (C) 5040 (C) 5050

2. For a C program accessing X[i][j][k], the following intermediate code is generated by a compiler.
Assume that the size of an integer is 32 bits and the size of a character is 8 bits.
t0 = i * 1024 t1= j * 32 t2 = k * 4 t3 =t1 + t0 t4 = t3 + t2 t5 = X[t4]Which one of the following
statement about the source code of C program is correct?
(A) X is declared as “int X[32][32][8]” (B) X is declared as “int X[4][1024][32]”
(C) X is declared as “char X[4][32][8]” (D) X is declared as “char X[32][16][2]”
3. Assume the following C variable declaration
int *A [10], B[10][10]; of the following expressions
I. A[2] II. A[2][3] III. B[1] IV. B[2][3], which will not give compile-time errors if
used as left hand sides of assignment statements in a C program.
(A) I, II, and IV only (B) II, III, and IV only
(C) II and IV only (D) IV only

4. Assume the following C variable declaration: int *A [10], B[10][10];


Of the following expressions I. A[2] II. A[2][3] III. B[1] IV. B[2][3] which will not give
compile-time errors if used as left hand sides of assignment statements in a C
program ?
A) I, II, and IV only B) II, III, and IV only C) II and IV only D) IV only
5. int main()
{ int i;
int arr[5] = {1};
for (i = 0; i < 5; i++)
printf("%d ", arr[i]);
return 0; }
A) 1 followed by four garbage values B) 1 0 0 0 0 C) 1 1 1 1 1 D) 0 0 0 0 0

6. Consider the following declaration of a ‘two-dimensional array in C:char a[100][100];


Assuming that the main memory is byte-addressable and that the array is stored starting from
memory address 0, the address of a[40][50] is :
B) 4040 B) 4050 C) 5040 D) 5050

7. 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
8. Pick the best statement for the below: int arr[50] = {0,1,2,[47]=47,48,49};
A) This isn’t allowed in C and it’ll give compile error
B)This is allowed in C as per standard. Basically, it’ll initialize arr[0], arr[1], arr[2], arr[47], arr[48]
and arr[49] to 0,1,2,47,48 and 49 respectively. The remaining elements of the array would be
initialized to 0.
Thank You......

You might also like