You are on page 1of 51

GATE Online Coaching

Classes
as per the Direction of
Ministry of Education
GOVERNMENT OF
ANDHRA PRADESH
PROGRAMMING CONCEPTS FOR GATE 2021
YouTube link is to be downloaded for every class on every
day as per the given schedule

https://jntua.ac.in/gate-online-classes/registration/
Topics Covered
• Doubts clarification of pre-increment
& post-increment operators
• GATE Questions & Solutions
• Introduction to Arrays concepts
• Programs related to the concepts of
arrays
Clarification of doubts raised in Google form

1. Comma operator
#include<stdio.h> Output:
main()
{
int x, y;

x=10, 20, 30;


y=(10, 20, 30);
printf("x = %d and y= %d\n", x, y);

}
Pre-increment & Post-increment
#include<stdio.h>
main() Output:

{
int i=5, j;
j = i++ + i++; // 1. Question
printf( “i=%d j=%d\n”, i, j);
}
Pre-increment & Post-increment
#include<stdio.h>
main() Output:

{
int i=5, j;
j = ++i + ++i; // 2. Question
printf( “i=%d j=%d\n”, i, j);
}
Pre-increment & Post-increment
#include<stdio.h>
main() Output:

{
int i=5, j;
j = ++i + i++; // 3. Question
printf( “i=%d j=%d\n”, i, j);
}
Pre-increment & Post-increment
#include<stdio.h>
main() Output:

{
int i=5, j;
j = i++ + ++i; // 4. Question
printf( “i=%d j=%d\n”, i, j);
}
Pre-increment & Post-increment
#include<stdio.h>
main() Output:
{
int i=5, j;
j = i++ + ++i + i++ + ++i + i++ + ++i ;
// 5. Question
printf( “i=%d j=%d\n”, i, j);
}
Pre-increment & Post-increment
#include<stdio.h>
main()
Output:
{
int i=5, j;
j = ++i + i++ + ++i + i++ + ++i + i++;
// 6. Question
printf( “i=%d j=%d\n”, i, j);
}
#include<stdio.h>
main()
{
int a=5;
printf("%d %d %d\n", ++a, a, a++);
} Output:
#include<stdio.h>
main()
{
int a=5;
printf("%d %d %d\n", a++, a, ++a);
} Output:
if(i=0) int
printf("true"); x=10,y=20,z=5,i;
else i=x<y<z;
printf("false"); printf("%d", i) ;

Output : Output : ((x<y)<z)


i=1
false
Ans: option (d)

going through each code, you will find


that foo function prints all the digits of a
number stored in variable k

Solution 6
f(1) -- n=1

i=1, if is FALSE, n=n+i= 2, i=2

f(2) -n =2
i=2, n=n+i=2+2=4, i=3

f(4)- n=4
i=3, n=n+i=4+3=7, i=4

f(7) - n=7

Hence n=7 is returned.


Answer: c
Ans: option (c)
Explanation: Give some values to x and y and work
out the code step by step. The program code is an
implementation of Euclid's algorithm which is an
efficient method for computing the greatest common
divisor (GCD) of two integers.
Let x=12, y=4 then, m=12, n=4; While is True---
1. m=12-4=8 2. m= 8-4=4, while False---- return n=4
Answer: d -> 16+2=18
1 int a, b, c = 0;
2 void prtFun (void);
3 int main ()
4 {
5 static int a = 1; /* line 1 */
6 prtFun();
7 a += 1;
8 prtFun();
9 printf ( "\n %d %d " , a, b) ;
10 }
11
12 void prtFun (void)
13 {
14 static int a = 2; /* line 2 */
15 int b = 1;
16 a += ++b;
17 printf (" \n %d %d " , a, b);
18 }
1 int a, b, c = 0;
2 void prtFun (void);
3 int main ()
3
4 {
5 static int a = 1; /* line 1 */
6 prtFun();
7 a += 1;
8 prtFun();
9 printf ( "\n %d %d " , a, b) ; 4
10 }
11
12 void prtFun (void)
13 {
14 static int a = 2; /* line 2 */
15 int b = 1;
16 a += ++b;
17 printf (" \n %d %d " , a, b);
18 }
3
1 3

2 4 4

5
Codea.c
1 int a, b, c = 0;
2 void prtFun (void);
3 int main ()
4 {
5 auto int a = 1; /* line 1 */
6 prtFun();
7 a += 1;
8 prtFun();
9 printf ( "\n %d %d " , a, b) ;
10 }
11
12 void prtFun (void)
13 {
14 register int a = 2; /* line 2 */
15 int b = 1;
16 a += ++b;
17 printf (" \n %d %d " , a, b);
18 }
1 int a, b, c = 0;
2 void prtFun (void);
3 int main ()
4 {
5 auto int a = 1; /* line 1 */
6 prtFun();
7 a += 1;
8 prtFun();
9 printf ( "\n %d %d " , a, b) ;
10 }
11
12 void prtFun (void)
13 {
14 register int a = 2; /* line 2 */
15 int b = 1;
16 a += ++b;
17 printf (" \n %d %d " , a, b);
18 }
1 3

2 4

Codeb.c
If get(6) function is being called in main( ) then how many times will the get() function be
invoked before returning to the main( )?
(a) 15
(b) 25
(c) 35
(d) 45
Arrays
• An "Array" is a group of similar data type to
store series of homogeneous pieces of data that
all are same in type.
• It is a derived data type which is created with the help
of basic data type. An array takes contiguous memory
blocks to store series of values.
• There are three types of an array: 1) One
Dimensional (One-D) Array, 2) Two Dimensional
(Two-D) Array and 3) Multi Dimensional Array.
Arrays
To process large amounts of data we need a
powerful data structure, the array. An array
is a collection of elements of the same data
type.
Since an array is a sequenced collection,
we can refer to the elements in the array as
the first element, the second element, and
so forth until we get to the last element.
29
Properties/characteristics of an array in C
language
1. An array is a derived data type, which is defined using basic data
types like int, char, float and even structures (which is called the
array of structures).
2. Array elements are stored in contiguous memory
blocks/subsequent memory blocks in primary memory.
3. Array name represents its base address. The base address is the
address of the first element of the array.
4. Array’s index starts with 0 and ends with N-1. Here, N stands for
the number of elements. For Example, there is an integer array
of 5 elements, then it’s indexing will be 0 to 4.
5. Only constants and literal values (an integer value like 5, 10,
12,...) can be assigned the number of elements in an array.
Properties/characteristics of an array in C
language
6) While declaring an array, we can also assign each element with 0
like this.
Like: int age[10]={0};
7) There is no need to provide the number of elements while
declaring an array, but we have to provide the values at the time
of declaration. In this case, array size will be the number of
values that you have provided.
Like: int age[]={21,22,25,45,56};
Here, size of the array will be 5, because there are 5 values.
8) An array can be allocated anywhere in these memory permanents:
A) Data segment for static or globally declare array
B) Heap segment for dynamically allocated array
C) Stack segment for locally declare array
/* Valid array declaration */

int main()
{
const int MAX = 100; //an integer constant
//valid arrray declaration
int students[MAX];
//valid arrray declaration
int students[100];
//more...
}
/* Invalid array declaration */
int main()
{
int MAX = 100; //an integer constant

//invalid arrray declaration

int students[MAX];
//more...
}
34
35
Note
Only fixed-length arrays can be initialized when they are defined. Variable length
arrays must be initialized by inputting or assigning the values.

36
37
Note
One array cannot be copied to another using assignment.

38
39
Accessing Array Elements
• x and y are similar arrays (i.e., of the same data
type, dimensionality, and size), then assignment
operations, comparison operations, etc.,
involving these two arrays must be carried out
on an element-by-element basis.
• Examples using the elements of an array named
‘numbers’ are shown here:
numbers [0] = 98;
numbers [1] = numbers [0] – 11
numbers [2] = 2 * (numbers [0] – 6);
numbers [3] = 79;
numbers [4] = (numbers [2] + numbers [3] – 3)/2;
total = numbers[0] + numbers[1] + numbers[2] + numbers[3] +
numbers[4];
Cont.
• This makes statements such as:
– total = numbers[0] + numbers[1] + numbers[2] +
numbers[3] + numbers [4];
• One extremely important advantage of using integer
expressions as subscripts is that it allows sequencing
through an array using a for loop.
• Example, the for loop statements,
total = 0; /*initialize total to zero */
for(i = 0; i <5; ++i)
total = total + numbers[i]; /* add in a number */

© Oxford University Press 2013. All


rights reserved.
ARRAY:Other Allowed Operations
These operations include the following, for an array named ‘ar’.
(a) To increment the ith element, the int ar[10],br[10];
given statements can be used. for(i = 0; i < 10; i = i + 1)
ar[i]++; br[i] = ar[i];
ar[i] += 1;
ar[i] = ar[i] + 1; (e) To exchange the values in ar[i] and
(b) To add n to the ith element, the ar[k], a ‘temporary’ variable must
following statements may be used, be declared to hold one value, and
ar[i] += n; it should be the same data type as
ar[i] = ar[i] + n; the array elements being swapped.
(c) To copy the contents of the ith int temp;
element to the kth element, the temp = ar[i];
following statement may be written. /* save a copy of value in ar[i] */
ar[k] = ar[i]; ar[i] = ar[j];
(d) To copy the contents of one array ‘ar’ /* copy value from ar[j] to ar[i] */
to another array ‘br’, it must again be
done one by one. ar[j] = temp;
/* copy saved value of ar[i] to ar[j] */
Storing values given by the user in an array
• Reading the input into an array is done as
shown. int a[10]; /* an array with 10 “int”
elements */
int i;
for(i=0 ; i< 10; i++)
scanf(“%d”, &a[i]);
• The idea is that first a value must be read and
copied into a[0], then another value read and
copied into a[1], and so on, until all the input
values have been read.
Internal Representation of Arrays in C
• References to elements outside of the array bounds It
is important to realize that there is no array bound checking
in C.

• A bit of memory allocation It has been seen how arrays


can be defined and manipulated. It is important to learn how
to do this because in more advanced C programs it is
necessary to deal with something known as dynamic memory
management.
– It is given a small area of the computer’s memory to use. This
memory, which is known as the stack, is used by variables in the
program
int a = 10;
float values[100];
Dope Vector

Dope Vectors is a data structure that is used by compilers to store some


metadata about the array like its total size, the size of one unit also called
stride of the array, etc. Dope vectors help compilers to access the arrays
with ease. Different checks that are implemented by compiler like Out of
Bound check, datatype check, etc. are all possible because of dope
vector associated with the array.

Q. The information about an array used in a program will be sorted in

A. Symbol table B. Activation record C. Both (a) and (b) D. Dope vector
The const feature can be applied to
A. an identifier
B. an array
C. an array argument
D. All of these

Choose the correct statements


A. All The elements of the array should be of the same data
type and storage class
B. The number of subscripts determines the dimension of the
array
C. The array elements are of the same storage class
D. In an array definition, the subscript can be any expression
yielding a non-zero integer value
If storage class is missing in the array definition, by default
it will be taken to be
A. automatic
B.external
C.static
D.either automatic or external depending on the place of occurrence

Consider the array definition


int num [10] = {3, 3, 3};
Pick the Correct answers
A.num [9] is the last element of the array num
B.The value of num [ 8] is 3
C.The value of num [ 3 ] is 3
D.None of the above
Consider the following type definition.
typedef char x[10];
x myArray[5];
What will sizeof(myArray) be ? (Assume one character occupies 1 byte)
A.15 bytes
B.10 bytes
C.50 bytes
D.30 bytes

The following program


main( )
{
static int a[ ] = { 7, 8, 9 } ;
printf( "%d", 2[ a ] + a[ 2 ] ) ;
}
A.results in bus error
B.results in segmentation violation error
C.will not compile successfully
D.none of the above
Consider the statement
int val[2] [4] = { 1, 2, 3, 4, 5, 6, 7, 8} ;
4 will be the value of
A.val[0 ][ 3]
B.val[0][4]
C.val[1][1]
D.none of the above

Under which of the following conditions, the size of an one-dimensional


array need not be specified?
A.when initialization is a part of definition
B.when it is a declaration
C.when it is a formal parameter and an actual argument
D.All of the above
C does no automatic array bound
checking. This is
A. true
B. false
C. C's asset
D. C's shortcoming
THANK YOU

Online Test 3 at
shorturl.at/eBKS1

You might also like