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

Institute - Uie Department-Academic Unit-2: Array & Strings

Here are the answers to the questions: 1. The answer is B. For every type T except void and function type, there can be an array of T. 2. The output will be 0 3 0 0 0 0. When an array is passed to a function, the function receives a pointer to the first element of the array. So the changes made inside the function will be reflected in the original array. 3. The output will be 1 2 3 4 5. In a 2D array, elements are stored row-wise in memory. So the given initialization will store the elements in the order shown without any junk values. The key points to note are: - Arrays of all types except

Uploaded by

Arjun B
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Institute - Uie Department-Academic Unit-2: Array & Strings

Here are the answers to the questions: 1. The answer is B. For every type T except void and function type, there can be an array of T. 2. The output will be 0 3 0 0 0 0. When an array is passed to a function, the function receives a pointer to the first element of the array. So the changes made inside the function will be reflected in the original array. 3. The output will be 1 2 3 4 5. In a 2D array, elements are stored row-wise in memory. So the given initialization will store the elements in the order shown without any junk values. The key points to note are: - Arrays of all types except

Uploaded by

Arjun B
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

INSTITUTE - UIE

DEPARTMENT- ACADEMIC UNIT-2


Bachelor of Engineering (Computer Science & Engineering)
Subject Name and Code: Problem Solving with Programming
20CST-111

DISCOVER . LEARN . EMPOWER


Array & Strings
OBJECTIVES
The course aims to provide exposure to problem solving with programming

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

LEARNING CO No. TITLE LEVEL


CO1 Identify situations where computational methods Understand
OUTCOMES CO2
would be useful
Approach the programming tasks using learned Remember
techniques and write pseudocode

CO3 Choose the right data representation format based Understand


on problem requirement
CO4 Compare and check the limitations of Understand
programming constructs for the suitability of the
task
S.No All Elements Marks
1 1st Hourly Test 12
2 2nd Hourly Test 12
COURSE 3 Element 1- Online Quiz 4*3 = 12
4 Element 2 - Surprise Test 3*3 = 9
SCHEME 5 Element 3 - Assignments 4*3 = 12
6 Element 4 - Tutorials 3*3 = 9
7 Attendance (if more than 90%) 2
•Two Dimensional Array
•Initialization of Two Dimensional Array
Content •Memory Representation of 2D Array
•Examples
•References
A two-dimensional array is an array where its elements are selected
(identified) using two indices. In 2-D array, to declare and access
elements of a 2-D array we use 2 subscripts instead of 1.
Syntax:  data_type array_name[ROW][COL];
 The total number of elements in a 2-D array is ROW*COL.
 Example: int a[m][n];
In rectangular 2-dimensional arrays, m number of rows and n number of
columns in the array has the same number of array elements.

2-Dimensional
Array

A conceptual representation of 2D array


Initialization of two-dimensional array: Initialization of 2-D array is
similar to a 1-D array.
Example:

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

1. Write a C Program to addition of two matrices.


2. Write a program in C to calculate determinant of a 3 x 3 matrix.
3. Write a program in C to accept two matrices and check whether
they are equal.
FREQUENTLY 4. Write a C program to transpose of two matrices.

ASKED 5. Program to multiply two matrices.

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++)

TO ANSWER for (j = 0; j < 3; j++)


printf("%d", a[i][j]);
Let us see how much you have }
learned from the lecture and
void main()
how effectively you can apply
{
your knowledge…!!
int a[2][3] = {0};
f(a);
}
a) 0 3 0 0 0 0
b) Junk 3 junk junk junk junk
c) Compile time error
d) All junk values
3. What will be the output of the following C code?
#include <stdio.h>
void main()
{
UTILISE int a[2][3] = {1, 2, 3, 4, 5};
YOUR int i = 0, j = 0;
KNOWLEDGE for (i = 0; i < 2; i++)
TO ANSWER for (j = 0; j < 3; j++)
Let us see how much you have printf("%d", a[i][j]);
learned from the lecture and
how effectively you can apply }
your knowledge…!!
a) 1 2 3 4 5 0
b) 1 2 3 4 5 junk
c) 1 2 3 4 5 5
d) Run time error
Book References:
[1] Thareja Reema (2014) Programming in C. 2nd ed.
[2] Zed A. Shaw, Learn C the Hard Way’
[3] https://en.wikibooks.org/wiki/C_Programming
 
REFERENCES Vedio Lecture: https://nptel.ac.in/courses/106/105/106105171/
https://nptel.ac.in/courses/106/106/106106127/
BOOKS https://spoken-
tutorial.org/watch/C+and+Cpp/Working+With+2D+Arrays/English/
WEBSITES
 
COURSES Websites: https://beginnersbook.com/2014/01/2d-arrays-in-c-example/
https://processing.org/tutorials/2darray/
https://www.programiz.com/c-programming/c-multi-dimensional-arrays
 
 
THANK YOU

You might also like