Programming. Data Structure

Similar documents
Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print

INTRODUCTION TO C-PROGRAMING LANGUAGE

ECET 264 C Programming Language with Applications

ECET 264 C Programming Language with Applications

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Week 1 / Lecture 2 8 March 2017 NWEN 241 C Fundamentals. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington

EL2310 Scientific Programming

CS133 C Programming. Instructor: Jialiang Lu Office: Information Center 703

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Introduction to C. Systems Programming Concepts

Work relative to other classes

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

Presented By : Gaurav Juneja

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

CMPE-013/L. Introduction to C Programming

Preview from Notesale.co.uk Page 6 of 52

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Principles of C and Memory Management

EL2310 Scientific Programming

Pointers (2) Applications

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012

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

A Fast Review of C Essentials Part I

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

Basic Types and Formatted I/O

Data in Memory. variables have multiple attributes. variable

C Language, Token, Keywords, Constant, variable

CS 61C: Great Ideas in Computer Architecture Introduction to C

Computers Programming Course 5. Iulian Năstac

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

Motivation was to facilitate development of systems software, especially OS development.

CS Programming In C

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

Course Outline Introduction to C-Programming

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

Creating, Compiling and Executing

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

Motivation was to facilitate development of systems software, especially OS development.

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

The C language. Introductory course #1

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

Programming in C. What is C?... What is C?

Fundamental of Programming (C)

Chapter 1 & 2 Introduction to C Language

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

High Performance Computing Lecture 1. Matthew Jacob Indian Institute of Science

BLM2031 Structured Programming. Zeyneb KURT

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

6.096 Introduction to C++ January (IAP) 2009

ISA 563 : Fundamentals of Systems Programming

Programming in C and C++

PRINCIPLES OF OPERATING SYSTEMS

Tutorial-2a: First steps with C++ programming

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

THE FUNDAMENTAL DATA TYPES

Introduction to Computing Lecture 01: Introduction to C

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Structure of this course. C and C++ Past Exam Questions. Text books

Introduction to C Programming

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

Structured Programming. Jon Macey

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

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

Lecture 03 Bits, Bytes and Data Types

Draft. Chapter 1 Program Structure. 1.1 Introduction. 1.2 The 0s and the 1s. 1.3 Bits and Bytes. 1.4 Representation of Numbers in Memory

Incoming Exam. CS 201 Introduction to Pointers. What is a Pointer? Pointers and Addresses. High Speed Memory (RAM) Size of Variable Types.

Variables and Constants

MODULE 5: Pointers, Preprocessor Directives and Data Structures

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

1.1 Introduction to C Language. Department of CSE

edunepal_info

F28HS2 Hardware-Software Interface. Lecture 1: Programming in C 1

C Programming Language Training. This instruction relies on the C language described in C++: The Complete Reference Third Edition By Herbert Schildt

Programming in C and C++

Fundamental of C programming. - Ompal Singh

EL2310 Scientific Programming

INTRODUCTION 1 AND REVIEW

Kurt Schmidt. October 30, 2018

CS 110 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

C Programming Review CSC 4320/6320

CS 110 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Topic 6: A Quick Intro To C

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 1. Types Variables Expressions & Statements 2/23

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

EC 413 Computer Organization

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Object-Oriented Programming


ET156 Introduction to C Programming

introduction week 1 Ritsumeikan University College of Information Science and Engineering Ian Piumarta 1 / 20 imperative programming about the course

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Basic Elements of C. Staff Incharge: S.Sasirekha

CSCI 171 Chapter Outlines

C++ Tutorial AM 225. Dan Fortunato

Transcription:

Programming & Data Structure For Computer Science & Information Technology By www.thegateacademy.com

Syllabus Syllabus for Programming and Data Structures Programming in C, Arrays, Stacks, Queues, Linked Lists, Trees, Binary Search Trees, Binary Heaps, Graphs. Analysis of GATE Papers Year Percentage of Marks Over All Percentage 2015 18.00 2014 13.00 2013 18.00 2012 19.00 2011 13.0 2010 18.00 12.10% 2009 4.67 2008 4.67 2007 4.67 2006 8.00 : 080-617 66 222, info@thegateacademy.com Copyright reserved. Web:www.thegateacademy.com

Contents Contents Chapters Page No. #1. Introduction to C Programming and Array 1 54 C Programming 1 3 Basic Datatypes 3 5 Variable Types 6 10 Operators 11 14 Flow Control Statements 15 19 Pointing to Data 19 28 Functions and Variables 28 30 Strings 30 32 Working with Files 32 37 Macro Caveats 38 Dynamic Memory Allocation 38 39 Arrays 40 41 Memory as an Array 41 44 User Defined Data types 45 47 Abstract Data Types (ADT) 48 49 Assignment 50 53 Answer Keys & Explanations 54 #2. Stacks and Queues 55 81 Stacks 55 56 Stack ADT Implementations 56 58 The Stack Permutation 58 62 Binary Expression Tree 62 66 Queue 66 68 Linked Representation of Queue 68 72 Queue Operations 72 73 Queue Applications 73 75 Assignment 1 76 77 Assignment 2 77 79 Answer keys & Explanations 79 81 #3. Trees 82 111 Binary Tree 82 84 Height Analysis 84 85 Tree Traversals 85 In-Order Traversal 86 Post-Order Traversal 87 Pre-Order Traversal 87 89 Binary Search Trees 90 : 080-617 66 222, info@thegateacademy.com Copyright reserved. Web:www.thegateacademy.com i

Basic BST Operations 90 95 AVL Trees 95 96 Advantage of AVL Tree 96 100 Assignment 1 101 104 Assignment 2 105 107 Answer keys & Explanations 107 111 #4. Linked List 112 134 Linked List 112 116 Memory Allocation: Garbage Collection 116 117 Operation on Linked List 117 124 Header Linked Lists 124 126 Circular Linked List 127 Doubly Linked List 127 Operation on DLL 128 130 Application of Linked List 130 Assignment 131 132 Answer Keys & Explanations 133 134 #5. Priority Queues (Heaps) 135 152 Introduction 135 Binary Heap 135 138 Array Representation of Binary Heap 138 139 Min-Heap vs. Max-Heap 139 Basic Heap Operation 139 146 Assignment 147 149 Answer keys & Explanations 150 152 #6. Graph 153 177 Important Points 153 154 Representation of Graphs 154 Single Source Shortest Path Algorithm 154 159 Minimum Spanning Tree 159 165 Graph Traversal Algorithm 165 169 Connected Components 170 Assignment 1 171 172 Assignment 2 172 175 Answer keys & Explanations 175 177 Module Test 178 184 Test Questions 178 182 Answer Keys & Explanations 183 184 Reference Books 185 Contents : 080-617 66 222, info@thegateacademy.com Copyright reserved. Web:www.thegateacademy.com ii

CHAPTER 1 Obstacles are those frightful things you can see when you take your eyes off your goal." Henry Ford Introduction to C Programming and Array Learning Objectives After reading this chapter, you will know: 1. C Programming 2. Basic Datatypes 3. Variable Types 4. Operators 5. Flow Control Statements 6. Pointing to Data 7. Functions and Variables 8. Strings 9. Working with Files 10. Macro Caveats 11. Dynamic Memory Allocation 12. Arrays 13. Memory as an Array 14. User Defined Data types 15. Abstract Data Types (ADT) C Programming Basic Introduction C is a general-purpose high level language that was originally developed by Dennis Ritchie for the Unix operating system. The Unix operating system and all Unix applications are written in the C language. C has now become a widely used professional language for various reasons. Easy to learn Structured language It produces efficient programs It can handle low-level activities It can be compiled on a variety of computers Facts about C C was invented to write an operating system called UNIX C is a successor of B language which was introduced around 1970 The language was formalized in 1988 by the American National Standard Institute (ANSI) By 1973 UNIX OS almost totally written in C Today C is the most widely used System Programming Language Most of the state of the art software have been implemented using C : 080-617 66 222, info@thegateacademy.com Copyright reserved. Web:www.thegateacademy.com 1

Introduction to C Programming and Array Why to use C? C was initially used for system development work, in particular the programs that make-up the operating system. C was adopted as a system development language because it produces code that runs nearly as fast as code written in assembly language. Some examples of the use of C might be: Operating Systems Language Compilers Assemblers Text Editors Print Spoolers Network Drivers Modern Programs Data Bases Language Interpreters Utilities C Program File All the C programs are written into text files with extension ".c" for example hello.c. You can use "vi" editor to write your C program into a file. Program Structure: A C program basically has the following form: Preprocessor Commands Functions Variables Statements & Expressions Comments The following program is written in the C programming language. Open a text file hello.c using vi editor and put the following lines inside that file. #include <stdio.h> int main() printf("hello, Tech Preparation! \n"); return 0; Preprocessor Commands: These commands tells the compiler to do preprocessing before doing actual compilation. For example #include <stdio.h> is a preprocessor command which tells a C compiler to include stdio.h file before going to actual compilation. You will learn more about C Preprocessors in C Preprocessors session. Functions: Functions are main building blocks of any C Program. Every C Program will have one or more functions and there is one mandatory function which is called main() function. This function is prefixed with keyword int which means this function returns an integer value when it exits. This integer value is returned using return statement(main function also return void). The C Programming language provides a set of built-in functions. In the above example printf() is a C built-in function which is used to print anything on the screen. : 080-617 66 222, info@thegateacademy.com Copyright reserved. Web:www.thegateacademy.com 2

Introduction to C Programming and Array Variables: Variables are used to hold numbers, strings and complex data for manipulation. You will learn in detail about variables in C Variable Types. Statements & Expressions: Expressions combine variables and constants to create new values. Statements are expressions, assignments, function calls, or control flow statements which make up C programs. Comments: Comments are used to give additional useful information inside a C Program. A command can be a multiple line or single line. Multiple line comments will be put inside /*...*/ and single line commands written after //. Note: C is a case sensitive programming language. It means in C printf and Printf will have different meanings. C has a free-form line structure. End of each C statement must be marked with a semicolon. Multiple statements can be one the same line. White Spaces (ie tab space and space bar ) are ignored. Statements can continue over multiple lines. C program Compilation To compile a C program you would have to Compiler name and program files name. Assuming your compiler's name is cc and program file name is hello.c, give following command at Unix prompt. $cc hello.c This will produce a binary file called a.out and an object file hello.o in your current directory. Here a.out is your first program which you will run at Unix prompt like any other system program. If you don't like the name a.out then you can produce a binary file with your own name by using -o option while compiling C program. See an example below $cc -o hello hello.c Now you will get a binary with name hello. Execute this program at Unix prompt but before executing / running this program. Basic Datatypes C has a concept of 'data types' which are used to define a variable before its use. The definition of a variable will assign storage for the variable and define the type of data that will be held in the location. The value of a variable can be changed any time. C has the following basic built-in datatypes. int float double char Please note that there is not a Boolean data type. C does not have the traditional view about logical comparison, but that's another story. int - data type: int is used to define integer numbers. : 080-617 66 222, info@thegateacademy.com Copyright reserved. Web:www.thegateacademy.com 3

Introduction to C Programming and Array int count; Count = 5; float - data type: float is used to define floating point numbers. float Miles; Miles = 5.6; double - data type: double is used to define BIG floating point numbers. It reserves twice the storage for the number. On PCs this is likely to be 8 bytes. double Atoms; Atoms = 2500000; char - data type: char defines characters. char Letter; Letter = 'x'; Modifiers The data types explained above have the following modifiers. short long signed unsigned The modifiers define the amount of storage allocated to the variable. The amount of storage allocated is not cast in stone. ANSI has the following rules: short int <= int <= long int float <= double <= long double What this means is that a 'short int' should assign less than or the same amount of storage as an 'int' and the 'int' should be less or the same bytes than a 'long int'. What this means in the real world is: Type Bytes Range Short int 2 32,768 > +32,767 (32kb) Unsigned short int 2 0 > +65,535 (64Kb) Unsigned int 4 0 > +4,294,967,295 ( 4Gb) Int 4 2,147,483,648 > +2,147,483,647 ( 2Gb) Long int 4 2,147,483,648 > +2,147,483,647 ( 2Gb) Signed char 1 128 > +127 Unsigned char 1 0 > +255 Float 4 Double 8 Long double 12 : 080-617 66 222, info@thegateacademy.com Copyright reserved. Web:www.thegateacademy.com 4

Introduction to C Programming and Array These figures only apply to today's generation of PCs. Mainframes and midrange machines could use different figures, but would still comply with the rule above. You can find out how much storage is allocated to a data type by using the sizeof operator discussed in Operator Types Session. Here is an example to check size of memory taken by various datatypes. int main() printf("sizeof(char) == %d\n", sizeof(char)); printf("sizeof(short) == %d\n", sizeof(short)); printf("sizeof(int) == %d\n", sizeof(int)); printf("sizeof(long) == %d\n", sizeof(long)); printf("sizeof(float) == %d\n", sizeof(float)); printf("sizeof(double) == %d\n", sizeof(double)); printf("sizeof(long double) == %d\n", sizeof(long double)); printf("sizeof(long long) == %d\n", sizeof(long long)); return 0; Qualifiers A type qualifier is used to refine the declaration of a variable, a function, and parameters, by specifying whether: The value of a variable can be changed. The value of a variable must always be read from memory rather than from a register Standard C language recognizes the following two qualifiers: const volatile The const qualifier is used to tell C that the variable value cannot change after initialization. const float pi=3.14159; Now pi cannot be changed at a later time within the program. Another way to define constants is with the #define preprocessor which has the advantage that it does not use any storage The volatile qualifier declares a data type that can have its value changed in ways outside the control or detection of the compiler (such as a variable updated by the system clock or by another program). This prevents the compiler from optimizing code referring to the object by storing the object's value in a register and re-reading it from there, rather than from memory, where it may have changed. You will use this qualifier once you will become expert in "C". So for now just proceed. : 080-617 66 222, info@thegateacademy.com Copyright reserved. Web:www.thegateacademy.com 5

Introduction to C Programming and Array Variable Types A variable is just a named area of storage that can hold a single value (numeric or character). The C language demands that you declare the name of each variable that you are going to use and its type, before you actually try to do anything with it. The Programming language C has two main variable types Local Variables Global Variables Local Variables Local variables scope is confined within the block or function where it is defined. Local variables must always be defined at the top of a block. When a local variable is defined - it is not initialized by the system(garbage value), you must initialize it yourself. When execution of the block starts the variable is available, and when the block ends the variable 'dies'. Check following example's output main() int i=4; int j=10; i++; if (j > 0) /* i defined in 'main' can be seen */ printf("i is %d\n",i); if (j > 0) /* 'i' is defined and so local to this block */ int i=100; printf("i is %d\n",i); /* 'i' (value 100) dies here */ printf("i is %d\n",i); /* 'i' (value 5) is now visable.*/ This will generate following output i is 5 i is 100 i is 5 : 080-617 66 222, info@thegateacademy.com Copyright reserved. Web:www.thegateacademy.com 6