Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #07. Topic: Pointer in C Date:

Similar documents
Practice Sheet #07 with Solutions

M1-R4: Programing and Problem Solving using C (JAN 2019)

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #06

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #05

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

Procedural programming with C

Introduction to C Language (M3-R )

DECLARAING AND INITIALIZING POINTERS

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

Variation of Pointers

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

ESC101N: Fundamentals of Computing End-sem st semester

Lecture 2: C Programm

Arrays and Pointers (part 1)

Algorithms & Data Structures

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Principles of C and Memory Management

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Lectures 5-6: Introduction to C

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

PDS Class Test 2. Room Sections No of students

2. Which of the following will print the value 2 for the above code?

Online Judge and C. Roy Chan. January 12, Outline Information Online Judge Introduction to C. CSC2100B Data Structures Tutorial 1

Basic and Practice in Programming Lab7

More Arrays. Last updated 2/6/19

Subject: Fundamental of Computer Programming 2068

Lectures 5-6: Introduction to C

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Arrays and Pointers. CSE 2031 Fall November 11, 2013

First of all, it is a variable, just like other variables you studied

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

Arrays and Pointers (part 1)

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

Intermediate Programming, Spring 2017*

Arrays. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

Character Strings. String-copy Example

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

Section - Computer Science. int main() {! int a=10,b=20;! printf("a:%d B:%d\n",a,b);! a=(a+b)-(b=a);! printf("a:%d B:%d\n",a,b);!

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

COL 100 Introduction to Programming- MINOR 1 IIT Jammu

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Programming & Data Structure Laboratory. Day 2, July 24, 2014

Lab 3. Pointers Programming Lab (Using C) XU Silei

CS 0449 Sample Midterm

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Programming & Data Structure Laboratory. Arrays, pointers and recursion Day 5, August 5, 2014

Advanced C Programming and Introduction to Data Structures

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

MTH 307/417/515 Final Exam Solutions

Computer Science & Engineering 150A Problem Solving Using Computers

Programming in C++: Assignment Week 1

MIDTERM TEST EESC 2031 Software Tools June 13, Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

C PROGRAMMING QUESTIONS AND

Structures. Basics of Structures (6.1) EECS l Now struct point is a valid type. l Defining struct variables: struct point { int x; int y; };

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #10

COMPUTER APPLICATION

Language comparison. C has pointers. Java has references. C++ has pointers and references

At the end of this module, the student should be able to:

Arrays and Pointers in C. Alan L. Cox

Midterm Exam Answers Instructor: Randy Shepherd CSCI-UA.0201 Spring 2017

Advanced Pointer Topics

The output will be: marks all or nothing. 1 #include <stdio.h> 2 main() { 3 int i; int j; 4 int *p; int *q; 6 p = &i; 7 q = &j; 8 i = 1;

Outline. Pointers arithme.c and others Func.ons & pointers

Intro to C: Pointers and Arrays

A pointer is a variable just like other variable. The only difference from other variables is that it stores the memory address other variables.

Government Polytechnic Muzaffarpur.

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #09. Topic: Structures in C Date:

Computer Programming Unit 3

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

Functions. Arash Rafiey. September 26, 2017

Storage class and Scope:

Section - Computer Science

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

Unit IV & V Previous Papers 1 mark Answers

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

Questions Bank. 14) State any four advantages of using flow-chart

Transcription:

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur Practice Sheet #07 Topic: Pointer in C Date: 23-02-2017 1. 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 compiletime 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 2. The following program fragment is written in a programming language that allows variables and does not allow nested declarations of functions. global int i = 100, j = 5; void P(x) int i = 10; print(x + 10); i = 200; j = 20; print(x); main() P(i + j); If the programming language uses static scoping and call by need parameter passing mechanism, the values printed by the above program are A. 115, 220 B. 25, 220 C. 25, 15

D. 115, 105 3. The following program fragment is written in a programming language that allows variables and does not allow nested declarations of functions. global int i = 100, j = 5; void P(x) int i = 10; print(x + 10); i = 200; j = 20; print(x); main() P(i + j); If the programming language uses dynamic scoping and call by name parameter passing mechanism, the values printed by the above program are : A. 115, 220 B. 25, 220 C. 25, 15 D. 115, 105 4. Consider the C program shown below. #include <stdio.h> #define print(x) printf("%d ", x) int x; void Q(int z) z += x; print(z); void P(int *y) int x = *y + 2; Q(x); *y = x - 1; print(x);

main(void) x = 5; P(&x); print(x); The output of this program is A. 12 7 6 B. 22 12 11 C. 14 6 6 D. 7 6 6 5. What does the following C-statement declare? int ( * f) (int * ) ; A. A function that takes an integer pointer as argument and returns an integer. B. A function that takes an integer as argument and returns an integer pointer. C. A pointer to a function that takes an integer pointer as argument and returns an integer. D. A function that takes an integer pointer as argument and returns a function pointer 6. Consider this C code to swap two integers and these five statements after it: void swap(int *px, int *py) *px = *px - *py; *py = *px + *py; *px = *py - *px; S1: will generate a compilation error S2: may generate a segmentation fault at runtime depending on the arguments passed S3: correctly implements the swap procedure for all input pointers referring to integers stored in memory locations accessible to the process S4: implements the swap procedure correctly for some but not all valid input pointers S5: may add or subtract integers and pointers. A. S1 B. S2 and S3

C. S2 and S4 D. S2 and S5 7. What is printed by the following C program? include <stdio.h> int f(int x, int *py, int **ppz) int y, z; **ppz += 1; z = **ppz; *py += 2; y = *py; x += 3; return x + y + z; void main() int c, *b, **a; c = 4; b = &c; a = &b; printf( "%d", f(c,b,a)); getchar(); A. 18 B. 19 C. 21 D. 22 8. Output of following program? #include <stdio.h> int fun(int n, int *f_p) int t, f; if (n <= 1) *f_p = 1; return 1; t = fun(n- 1,f_p);

f = t+ * f_p; *f_p = t; return f; int x = 15; printf (" %d \n", fun(5, &x)); A. 6 B. 8 C. 14 D. 15 9. What does the following program print? #include void f(int *p, int *q) p = q; *p = 2; int i = 0, j = 1; f(&i, &j); printf("%d %d \n", i, j); getchar(); A. 2 2 B. 2 1 C. 0 1 D. 0 2 10. #include<stdio.h> int f(int *a, int n) if(n <= 0)

else if(*a % 2 == 0) return *a + f(a+1, n-1); else return *a - f(a+1, n-1); int a[] = 12, 7, 13, 4, 11, 6; printf("%d", f(a, 6)); getchar(); A. -9 B. 5 C. 15 D. 19 11. What is the return value of f(p,p), if the value of p is initialized to 5 before the call? Note that the first parameter is passed by reference, whereas the second parameter is passed by value. int f(int &x, int c) c = c - 1; if (c==0) return 1; x = x + 1; return f(x,c) * x; A. 3024 B. 6561 C. 55440 D. 161051 12. The output of the following C program is. void f1 (int a, int b) int c; c=a; a=b; b=c; void f2 (int *a, int *b)

int c; c=*a; *a=*b;*b=c; int a=4, b=5, c=6; f1(a, b); f2(&b, &c); printf ( %d, c-a-b); A. -5 B. -4 C. 5 D. 3 13. What is the output of the following C code? Assume that the address of x is 2000 (in decimal) and an integer requires four bytes of memory. #include <stdio.h> unsigned int x[4][3] = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12; printf("%u, %u, %u", x+3, *(x+3), *(x+2)+3); A. 2036, 2036, 2036 B. 2012, 4, 2204 C. 2036, 10, 10 D. 2012, 4, 6 14. Consider the following function written in the C programming language. The output of the above function on input ABCD EFGH is void foo (char *a) if (*a && *a!= ` `)

foo(a+1); putchar(*a); A. ABCD EFGH B. ABCD C. HGFE DCBA D. DCBA 15. Consider the following C program segment. # include <stdio.h> int main( ) char s1[7] = "1234", *p; p = s1 + 2; *p = '\0' ; printf ("%s", s1); What will be printed by the program? A. 12 B. 120400 C. 1204 D. 1034 16. Consider the following C program. # include <stdio.h> int main( ) static int a[] = 10, 20, 30, 40, 50; static int *p[] = a, a+3, a+4, a+1, a+2; int **ptr = p; ptr++; printf("%d%d", prt-p, **ptr; The output of the program is A. 140 B. 120

C. 100 D. 40 17. Predict the output of the program char *ptr = "IITKharagpur"; printf("%c\n", *&*&*ptr); getchar(); A- I B- II C- IIT D- IITK 18 Predict the output of the below code #include<stdio.h> int *fun() int x = 5; return &x; int *p = fun(); fflush(stdin); printf("%d", *p); A- A garbage Address B- 5 C- Error at int *p= fun(); D- Error at printf( %d,*p); 19. Predict the output of the below code #include<stdio.h> int *fun()

static int x = 5; return &x; int *p = fun(); fflush(stdin); printf("%d",*p); A- A Garbage Address B- 5 C- Error at int *p=fun(); D- Error at printf( %d,*p); 20. Predict the output of the below code #include<stdlib.h> int x = 4; float y = 5.5; void *ptr; ptr = &x; printf("integer variable is = %d", *( (int*) ptr) ); ptr = &y; printf("\nfloat variable is= %f", *( (float*) ptr) ); A. Integer variable is = 4 Float variable is= 5.500000 B. Integer variable is = 5 Float variable is= 4.000000 C. Integer variable is = 4 Float variable is= 5.000000 D. Integer variable is = 5 Float variable is= 5.500000 21. Predict the output of the below code #include <stdio.h> int *ptr = NULL; printf("the value of ptr is %u", ptr); A. The value of ptr is 0

B. The value of ptr is 1 C. Error at printf statement D. The value of ptr is a garbage value 22. Predict the output of the below code #include<stdio.h> int a = 10; void *ptr = &a; printf("%d", *ptr); A- 10 B- Compile time error C- A garbage value D- Print 10 in binary format 23. Predict the output of the below code #include<stdio.h> int a = 10; void *ptr = &a; printf("%d", *(int *)ptr); A. 10 B. Compile time error C. A garbage value D. Print 10 in Binary format 24. Predict the output of the below code #include<stdio.h> int a[2] = 1, 2; void *ptr = &a; ptr = ptr + sizeof(int); printf("%d", *(int *)ptr); A. 2 B. 3 C. 1 D. A garbage value

25- Predict the output of the below code #include <stdio.h> int *i, *j; int *ii = NULL, *jj = NULL; if(i == j) printf("this might get printed if both i and j are same by chance."); if(ii == jj) printf("this is always printed coz ii and jj are same."); A- This is always printed coz ii and jj are same. B- This might get printed if both i and j are same by chance. C- Error at assignments of ii and jj D- Error at if(i==j) 26- Predict the output of the below code #include <stdio.h> void fun1() printf("fun1\n"); void fun2() printf("fun2\n"); void wrapper(void (*fun)()) fun(); wrapper(fun1); wrapper(fun2); A- Fun1 Fun2 B- Fun2 Fun1 C- Fun1 Fun1 D- Fun2 Fun2

Problems for Programming Practice (After the successful studies of Lecture 07 (Pointer in C), the students are supposed to solve the following problems in C programming language.) 1. A swap function is an operation to interchange the values in two storage locations. For example, if X = 55 and Y = 100, then after call of Swap(X, Y), the result will be X = 100 and Y = 55 etc. a) Define a function void Swap(int x, int y) to interchange the values in x and y. You should call the function from main for any two integer values and print the values both from main (before and after the call of Swap( ) ) and from inside the body of Swap( ). b) What modification in Swap( ) you should do to realize another swap function say void StringSwap(char *s1, char* s2) to interchange two strings s1 and s2. Repeat the same execution of your program to print the output as asked in A6/Q 1(a). 2. A dynamic array is an array whose size is only known during the runtime of a program, which uses the array. a) Define an array say vararray[ ] to store any number of integer values. Initialize the array so allocated and print the array then. b) Write a function void countprime(int*, int), which receives an integer array and its size, and returns the number of prime numbers in the array. 3. A word is defined as a string of alphabets that does not contain a blank or any special characters (such as, ;. \n etc.). A sentence is considered as a sequence of words separated by blanks and is terminated by.,? or \n character. Write a function int WordCount(char* s, char *w) that counts the number of occurrences of a word w in a sentence s. Use pointers to store sentence and word of any size. 4. You are to read any two integer values m and n from the keyboard. Then allocate the memory to store a two dimensional matrix of say DynamicMatrix[m][n]. Initialize the matrix so defined and print the matrix in the matrix form. 5. Read a list of names (First name + Last name and with maximum 15 characters in each) from the keyboard and sort them in alphabetical order. You should use the solution in A6/Q 4. to store the list of any number of names, and the size of the list is known while the program runs and A6/Q 1(b) for swapping two strings during sorting procedure. 6. A singly linked list whose nodes contain two fields: an integer value and a link to the next node. An example of such a list is shown in the figure below. You have to store a list of numbers using a single linked list structure.

a) Define a structure definition say struct Node for a node in single linked list. b) struct *Node CreateList(int n): To read n numbers from the keyboard and store them in a single linked list. c) void PrintList(struct *Node mylist) : Print the elements in the list mylist. 7. The user is asked to input two n dimensional vectors, where each vector is stored by allocating space with the help of pointers. Define a structure to store such a vector. Write the C functions to find the following. a) struct Vector VectorSum(struct *v1, *v2): The sum of the two vectors of the same dimension. b) float Magnitude(struct *v): The magnitude of a vector v. c) int OrthogonalTest(struct *v1, *v2): Whether two vectors v1 and v2 are orthogonal. The result computed by each function should return the result to the main program from where the result should be printed. 8. A set can be represented by a dynamic array of elements, where no repetition is permitted. Write C functions to perform the following operations on sets of integer valued elements. a) int* BuildSet(int n): Read n number of elements and store them in a set. b) int SearchSet(int* A, int x): Search the set A to find if an element x is in it. c) Given two sets, compute the following. i. int* Union (int *A, int *B): To return the union of two sets A and B. ii. int* Intersection (int *A, int *B): To return the intersection of two sets A and B. iii. int* Difference (int *A, int *B): To return the difference of two sets A and B. iv. struct OrderPair* CProduct (int *A, int *B): To return the Cartesian product of two sets A and B. [Hint: Define a structure to store an order pair.] The main program should call these functions. All results should be stored in their resultant sets and then display the results. [Hint: Define a function void Print(int *A) to print a set A.]