Guide for The C Programming Language Chapter 5

Similar documents
A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

MODULE 5: Pointers, Preprocessor Directives and Data Structures

MODULE V: POINTERS & PREPROCESSORS


NCS 301 DATA STRUCTURE USING C


CSCI 171 Chapter Outlines

DEEPIKA KAMBOJ UNIT 2. What is Stack?

For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit

STACKS 3.1 INTRODUCTION 3.2 DEFINITION

Linked List. April 2, 2007 Programming and Data Structure 1

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

Prepared By:- Dinesh Sharma Asstt. Professor, CSE & IT Deptt. ITM Gurgaon

DS Assignment II. Full Sized Image

Advanced C Programming and Introduction to Data Structures

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

Unit IV & V Previous Papers 1 mark Answers

Subject: Fundamental of Computer Programming 2068

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

Linear Data Structure

Content: Learning Objectives

DC54 DATA STRUCTURES DEC 2014

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

The Queues. Front. Rear. Front. Rear. Rear = 0 Front = 0. Fig Queue is empty. Fig push(10) Rear = 1 Front = 0. Fig. 4.3.

NCS 301 DATA STRUCTURE USING C

CP2 Revision. theme: dynamic datatypes & data structures

Dynamic Memory Allocation

Procedural programming with C

Arrays and Pointers (part 1)

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

Lecture 4 Stack and Queue

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

Arrays and Pointers (part 1)

Bangalore South Campus

CS PROGRAMMING & DATA STRUCTURES. UNIT I Part - A. 2. What is the difference between if and while statement?

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

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

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Dynamic memory allocation (malloc)

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

Programming and Data Structure Solved. MCQs- Part 2

Code No: R Set No. 1

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Outline. Briefly review the last class Pointers and Structs Memory allocation Linked lists

Postfix (and prefix) notation

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING B.E SECOND SEMESTER CS 6202 PROGRAMMING AND DATA STRUCTURES I TWO MARKS UNIT I- 2 MARKS

Introduction to C Language (M3-R )

Lectures 5-6: Introduction to C

Memory Allocation in C

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

Chapter 6. Data Structure. /* start contains the address of the first node */ start = &elephant1; print_elephants( start ); return EXIT_SUCCESS;

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

EM108 Software Development for Engineers

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

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


PESIT Bangalore South Campus Hosur road, 1km before ElectronicCity, Bengaluru -100 Department of Basic Science and Humanities

Lecture Data Structure Stack

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

Lectures 5-6: Introduction to C

CS240: Programming in C

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

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

SAURASHTRA UNIVERSITY

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

Darshan Institute of Engineering & Technology for Diploma studies Unit 4

Pointers. Introduction

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Technical Questions. Q 1) What are the key features in C programming language?

This document can be downloaded from with most recent updates. 1 Data Structures using C: Module 4 (16MCA11) LINKED LISTS

Arrays and Pointers. CSE 2031 Fall November 11, 2013

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

JTSK Programming in C II C-Lab II. Lecture 3 & 4

Computer Programming Unit 3

i location name 3 value at location 6485 location no.(address)

[0569] p 0318 garbage

Problem Solving and 'C' Programming

DC104 DATA STRUCTURE JUNE Q.2 a. If you are using C language to implement the heterogeneous linked list, what pointer type will you use?

Introduction to Data Structures and Algorithms

AMCAT Automata Coding Sample Questions And Answers

Quick review pointer basics (KR ch )

In Java we have the keyword null, which is the value of an uninitialized reference type

Cpt S 122 Data Structures. Data Structures

CSE2301. Dynamic memory Allocation. malloc() Dynamic Memory Allocation and Structs

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

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

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

Prepared by Mrs.D.Maladhy (AP/IT/RGCET) Page 1

Scheme of valuations-test 3 PART 1

High Performance Programming Programming in C part 1

Fundamentals of Programming

Pointers and File Handling

AE52/AC52/AT52 C & Data Structures JUNE 2014

Basic and Practice in Programming Lab7

Kurt Schmidt. October 30, 2018

Transcription:

1. Differentiate between primitive data type and non-primitive data type. Primitive data types are the basic data types. These data types are used to represent single values. For example: Character, Integer, Float, Double etc. Data types that are derived from basic data types are known as non-primitive data types. These data types are used to store group of data. For example: Array, Structure, Union, Queue, Stack etc. 2. What is a pointer? Explain how the pointer variable is declared and initialized. A pointer is a variable that contains the address of a variable. Syntax of declaration of pointer: datatype *pointername; For example: int *ip; /* data type is integer and pointer name is ip.*/ char *cp; /* data type is character and pointer name is cp.*/ Initialization of pointer: Initialization is the assigning of address of a variable to a pointer variable. For example: int a=5; int *ip; ip=&a; where & is the unary operator gives the address of an object. 3. Explain preprocessor directives in C with example. The #define is a macro preprocessor directive. Syntax of #define directive: #define identifier token-string OR #define identifier (identifier-list) token-string The #define directive causes compiler to substitute the token-string for each occurrence of the identifier in the source file. The token-string may consist of a series of token such as constant, keyword or complete sentence. Example: #define PI 3.14 #undef is a macro preprocessor directive. Syntax of #undef directive: #undef identifier The #undef directive removes the current definition of the identifier and subsequent occurrence of the identifier is ignored by the processor. Page: 1

Example: #define AGE 40....... #undef AGE The #include directive loads a specific file to the program. Syntax of #include directive: (a) #include <filename> It causes the replacement of the line by the entire content of file filename. The search for the file is made in the standard directories. (b) #include filename It causes the replacement of the line by the entire content of file filename. The search for the file is made in the current directory and standard directories. For example: #include <stdio.h> Conditional Compilation: Parts of the program may be compiled conditionally. The compiler compiles selected portion of the program based on the condition. #ifdef and # are conditional compilation directive. Syntax: #ifdef <identifier> statment1; statement2; # statment3; statement4; #endif If the identifier is defined, then #if block is compiled. If the identifier is not defined, then # block is compiled. For example: #include <stdio.h> #define PI 3.14 float radius,area; printf( \nenter the radius of the circle: ); scanf( %f,&radius); #ifdef PI Page: 2

area=pi*radius*radius; # area=3.14*radius*radius; #endif printf( \n Area of the circle:%0.2f,area); If the PI is defined, then #if block is compiled. If the PI is not defined, then # block is compiled. 4. Write a C program to swap two numbers using call by pointers (address) method. void swap(int *m, int *n); int a=2,b=3; printf( \n Before the function call: a=%d and b=%d\n,a,b); swap(&a,&b); printf( \n After the function call: a=%d and b=%d\n,a,b); void swap(int *m, int *n) int *r; *r=*m; *m=*n; *n=*r; 5. Explain how pointers and arrays are related with examples. There is a strong relationship between pointers and arrays. An operation that can be performed by arrays can also achieved by pointers. Consider the declaration of the array below: int a[5]; defines an array of size 5. A block of consecutive elements named a[0],a[1],a[2],a[3],a[4]. a[0] a[1] a[2] a[3] a[4] The notation a[i] refers to the i th element of the array. Now, let us declare a pointer as below: int *ptr; Then, the assignment ptr=&a[0]; Page: 3

sets ptr to point to the element a[0] of the array. ptr contains the address of a[0]. a[i] can be written as *(ptr+i). ptr ptr+1 ptr+2 ptr+3 ptr+4 a[0] a[1] a[2] a[3] a[4] Further, ptr=&a[0] can also be written as ptr=a ; and (ptr+i) can be written as (a+i). Hence, a[i] can be written as *(ptr+i) and a[i] can also be written as *(a+i). 6. Write C programs to define macros for (i) arithmetic operator(ii)logical operators. (i) #define multiply(a,b) a*b int x,y,res; printf( \n Enter two integers: ); scanf( %d %d, &x, &y); res=multiply(x,y); printf( \n Result=%d,res); (ii) #define LOGIC_AND (a,b) a&&b int x,y,res; printf( \n Enter two boolean(0 or 1) ); scanf( %d %d, &x, &y); res=logic_and(x,y); printf( \n Result=%d,res); 7. Write a C program using pointers to compute the Sum, Mean and Standard deviation of n elements stored in an array of n real numbers. #include<math.h> Page: 4

float sum=0, mean=0, std=0, s=0; float arr[100], *p; int i,n; printf( \n Enter the number of elements: ); scanf( %d,&n); for(i=0; i<n; i++) scanf( %f, arr+i); p=arr; for(i=0; i<n; i++) sum=sum+*p; p=p+1; mean= sum/n; p=arr; for(i=0; i<n; i++) s=s + (*p-mean)*(*p-mean); p=p+1; std=sqrt(s/n); printf( \n sum=%0.2f,sum); printf( \n mean=%0.2f,mean); printf( \n standard deviation=%0.2f,std); 8. Explain the array of pointers with example. Since pointers are variables, they can be stored in arrays similar to any other variables. For example: #include <stdio.h> int i; int arr[5]=5,6,7,8,9; int *ptr[5]; for(i=0; i<5; i++) ptr[i]=&arr[i]; for(i=0; i<5; i++) printf( \n arr[%d]=%d, i,*ptr[i]); Page: 5

9. What is dynamic memory allocation? What is the need for dynamic memory allocation? Dynamic memory allocation enables the programmer to allocate memory at runtime. This allows the programmer to obtain more memory when required and to release the memory when it is not required. Dynamic memory allocation prevents the wastage of memory. A lot of memory is wasted in static memory allocation because all the memory allocated may not be utilized. 10. Explain the functions for dynamic memory allocation in C. Following are the dynamic memory allocation functions: malloc():this function is used to allocate a block of memory in bytes. Syntax for this function is: ptr=malloc(number of elements*size of the elements); Type of pointer returns is void. Therefore, typecast is required. For example: #include<stdlib.h> int i; int *ptr; ptr=(int *)malloc( 5*sizeof(int)); printf( \n Enter 5 integers: ); for(i=0; i<5; i++) scanf( %d, ptr+i); printf( \n Output: ); for(i=0; i<5; i++) printf( %d,*(ptr+i)); free(ptr); calloc():this function allocate a block of memory for an array. It initializes all bits to 0. Syntax: ptr=calloc(number of elements, size of each element); For example: #include<stdlib.h> int i; int *ptr; ptr=(int *)calloc( 5,sizeof(int)); Page: 6

printf( \n Enter 5 integers: ); for(i=0; i<5; i++) scanf( %d, ptr+i); printf( \n Output: ); for(i=0; i<5; i++) printf( %d,*(ptr+i)); free(ptr); realloc(): This function is used to resize the memory which is already allocated. Syntax: ptr=realloc(ptr, new size); For example: #include<stdlib.h> int i; int *ptr; ptr=(int *)malloc( 5*sizeof(int)); ptr=(int *)realloc( ptr,6*sizeof(int)); printf( \n Enter 6 integers: ); for(i=0; i<6; i++) scanf( %d, ptr+i); printf( \n Output: ); for(i=0; i<6; i++) printf( %d,*(ptr+i)); free(ptr); free(): This function is used to de-allocate previously allocated memory. Syntax: Free(ptr); Example: Any of the above example for malloc(), calloc() or realloc(); 11. Explain Stack and Queue data structures along with their applications. Stack is a linear data structure. We can insert or delete an element from one end. We may perform LIFO (Last In First Out) or FILO (First In Last out). The insertion operation of an element onto the stack is called as push and deletion operation is called pop. The end where the insertion or deletion takes place is called top. Page: 7

4 Top of stack 3 2 1 Application of stack: (i) Expression evaluation. (ii)expression conversion, for example, Infix to postfix conversion (iii) Redo-Undo operation (iv) Forward and backward features of web browsers. (v) Used in algorithms like tree traversals, span problem etc (vi) Applications like Backtracking, Queen problem etc. (vii) Parsing (viii)simulation of recursion. Queue is a linear data structure. It is a homogeneous collection of elements in which elements are appended in one end called rear end and elements are deleted at other end called front end. Queue follows FIFO (First In First Out). Application of Queue: (i) Queue is used in operating systems maintain a queue of processes that are waiting for execution. (ii) Queue is used where data is transferred asynchronously between two processes. For example, buffer. (iii) Queue is used for sharing resource among different consumers. (ii) Breadth First Search uses queue. (iii) Queue is performs jobs in a network printer. 12. Write a C program using array to demonstrate the operations on stack. #define MAX 100 void push(); void pop(); void disp(); int stack[max]; int top=-1; int choice; clrscr(); while(1) Page: 8

printf( \n 1.Insert element to the stack ); printf( \n 1.Delete element from the stack ); Printf( \n 1.Display elements of the stack ); printf( \n 4.Quit ); printf( \n Enter your choice: ); scanf( %d,&choice); switch(choice) case 1: push(); case 2: pop(); case 3: disp(); case 4: exit(0); default: printf( \n Wrong choice ); void push() int item; if (top=max-1) printf( \n Stack is full ); printf( \n Enter the element to be inserted: ); scanf( %d,&item); top=top+1; stack[top]=item; void pop() if(top==-1) printf( \n Stack is empty ); Page: 9

printf( \n Element deleted from the stack is :%d, stack[top]); top=top-1; void disp() int i; if(top==-1) printf( \n Stack is empty. ); printf( \n Stack is: ); for(i=top; i>=0; i--) printf( %d, stack[i]); print( \n ); 13. Write a C program using link list to demonstrate the operations on stack. #include <stdio.h> #include<stdlib.h> struct node int info; struct node *next; ; typedef struct node NODEPTR; NODEPTR top,temp; void push(); void pop(); void disp(); int choice; clrscr(); while(1) printf( \n 1.Insert element to the stack ); printf( \n 2.Delete element from the stack ); printf( \n 3.Display elements of the stack ); Page: 10

printf( \n 4.Quit ); printf( \nenter your choice: ); scanf( %d,&choice); switch(choice) case 1; push(); case 2; pop(); case 3; disp(); case 4; exit(0); default: printf( \n Wrong choice ); void push() int item; printf( \nenter the element to be inserted: ); scanf( %d,&item); if(top==null) top=(nodeptr)malloc(sizeof(struct node)); top->next=null; top->info=item; temp=(nodeptr)malloc(sizeof(struct node)); temp->next=top; temp->info=item; top=temp; Page: 11

void pop() if(top==null) printf( \nerror:stack is empty ); if(top->next!=null) temp=top->next; printf( \nelement deleted from the stack is:%d, top->info); free(top); top=temp; printf( \nelement deleted from the stack is:%d, top->info); free(top); top=null; void disp() temp=top; if(top==null) printf( \nstack is empty. ); printf( Stack is: ); while(temp!=null) printf( %d,temp->info); temp=temp->next; printf( \n ); 14. Write a C program using array to demonstrate the operations on queue. #define MAX 100 void insert(); void del(); Page: 12

void disp(); int queue[max]; int rear=-1; int front=-1; int choice; clrscr(); while(1) printf( \n 1.Insert element to the queue ); printf( \n 1.Delete element from the queue ); Printf( \n 1.Display elements of the queue ); printf( \n 4.Quit ); printf( \n Enter your choice: ); scanf( %d,&choice); switch(choice) case 1: insert(); case 2: del(); case 3: disp(); case 4: exit(0); default: printf( \n Wrong choice ); void insert() int item; if (rear=max-1) printf( \n Queue overflows); Page: 13

if(front==-1) front=0; printf( \n Enter the element to be inserted: ); scanf( %d,&item); rear=rear+1; queue[rear]=item; void del() if(front==-1 front>rear) printf( \n Queue is empty ); printf( \n Element deleted from the queue is :%d, queue[front]); front=front+1; void disp() int i; if(front==-1) printf( \n Queue is empty. ); printf( \n Queue is: ); for(i=front; i<=rear; i++) printf( %d, queue[i]); print( \n ); 15. Write a C program using link list to demonstrate the operations on queue. #include <stdio.h> #include<stdlib.h> Page: 14

struct node int info; struct node *next; ; typedef struct node NODEPTR; NODEPTR front,rear,temp; void insert(); void del(); void disp(); int choice; clrscr(); while(1) printf( \n 1.Insert element to the queue ); printf( \n 2.Delete element from the queue ); printf( \n 3.Display elements of the queue ); printf( \n 4.Quit ); printf( \nenter your choice: ); scanf( %d,&choice); switch(choice) case 1; insert(); case 2; del(); case 3; disp(); case 4; exit(0); default: printf( \n Wrong choice ); Page: 15

void insert() int item; printf( \nenter the element to be inserted: ); scanf( %d,&item); if(rear==null) rear=(nodeptr)malloc(sizeof(struct node)); rear->next=null; rear->info=item; front=rear; temp=(nodeptr)malloc(sizeof(struct node)); rear->next=temp; temp->info=item; temp->next=null; rear=temp; void del() if(front==null) printf( \nerror:queue is empty ); if(front->next!=null) temp=front->next; printf( \n Element deleted from the queue is:%d, front->info); free(front); front=temp; printf( \n Element deleted from the queue is:%d, front->info); free(front); front=null; rear=null; Page: 16

void disp() temp=front; if(front==null) printf( \nqueue is empty. ); printf( Queue is: ); while(temp!=rear->next) printf( %d,temp->info); temp=temp->next; printf( \n ); 16. What is tree data structure? State the application of tree data structure. A tree is hierarchical data structure. It is a set of entities called nodes. Nodes are connected by edges. Each node contains data. First node of the tree is called the root. Applications of tree data structure are: (i) Manipulation of data which are arranged in hierarchical manner. (ii)search for data easily. (iii)prepare sorted list of data easily. (iv)router algorithm (v) Operating system maintains disk file system as a tree. (vi) Trees are used in language processing programs. Page: 17