Chapter 2 Arrays and Structures

Size: px
Start display at page:

Download "Chapter 2 Arrays and Structures"

Transcription

1 Data Structure Chapter 2 Arrays and Structures Angela Chih-Wei Tang Department of Communication Engineering National Central University Jhongli, Taiwan 2 Spring

2 Outline Array Structures Polynomial Sparse Matrix Multidimensional Arrays C.E., NCU, Taiwan Angela Chih-Wei Tang, 2 2

3 2. The Array as An Abstract Data Type Array a group of consecutive memory locations (the same type and name) When considering abstract data type operations (create, retrieve, store ) int c[2], *pc[2]; Variable Memory Address c[] base address = a c[] a + sizeof(int) c[2] a + 2*sizeof(int) Name of array (Note that all elements of this array have the same name, c) c[] c[] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[] c[] c[3] a + 3*sizeof(int) Position number of c[4] a + 4*sizeof(int) 78 the element within array c 3

4 Example 2.: One-Dimensional Array Addressing int one[ ] = {,, 2, 3, 4}; void print(int *ptr ptr, int rows) { /* print out a one-dimensional array using a pointer */ int i; printf( Address Contents\n ); Address Contents for (i = ; i < rows; i++) printf( %8u%5d\n, %5d\ ptr+i, *(ptr+i)); 228 printf( \n ); 23 } Function call: print(&one[], 5); C.E., NCU, Taiwan Angela Chih-Wei Tang, 2 4

5 Outline Array Structures Polynomial Sparse Matrix Multidimensional Arrays C.E., NCU, Taiwan Angela Chih-Wei Tang, 2 5

6 2.2. Structures Array: collections of data of the same type Structure : collections of related variables (different data types are permitted) under one name An example of create our own structure data types: struct { char name[]; int age; float salary; } human_being; C.E., NCU, Taiwan Angela Chih-Wei Tang, 2 6

7 Case Case Self-Referential Structures An example: item struct list{ a link NULL char data; list *link; item2 }; b link NULL struct list item, item2, item3; Item.data = a ; item3 item2.data = b ; c link NULL item3.data = c ; item.link=item2.link=item3.link=null; Case : null pointer Item.link=&item2; item2.link=&item3; item item2 item3 b link b link c link NULL Case 2: the address in memory 7

8 Outline Array Structurest Polynomial Sparse Matrix Multidimensional Arrays C.E., NCU, Taiwan Angela Chih-Wei Tang, 2 8

9 2.3 The Polynomial Abstract Data Type A ( x) = 2x + and B( x) = x + x + 3x + D( x) = A( x) + B( x) ==> How? MAX_TERMS /*size of terms array*/ struct{ float coef; int expon; } polynomial; struct polynomial terms[max_terms]; int avail = ; starta finisha startb finishb avail coef exp A B D C.E., NCU, Taiwan 9

10 Program 2.5: Function to Add Two Polynomials (/2) void padd(int starta, int finisha, int startb, int finishb, int *started, int *finishd) i { /*add A(x) and B(x) to obtain D(x) */ float coefficient; *startd = avail; while (starta <= finisha && startb <= finishb) switch(compare(terms[starta].expon, terms[startb].expon)) { case -: /* a expon < b expon */ attach(terms[startb].coef, terms[startb. expon); startb++; beak; case : /* equal expontents */ coefficient i = terms[starta]. t t coef + terms[startb].coef; C.E., NCU, Taiwan

11 Program 2.5: Function to Add Two Polynomials (2/2) } if (coefficient) attach(coefficient, terms[starta].expon); expon); starta++; startb++; break; case : /* a expon > b expon */ attach(terms[starta].coef, terms[starta].expon); starta++; t } /* add in remaining terms of A(x) */ for (; starta <= finisha; starta++) attach(terms[starta].coef, terms[starta].expon); /* add in remaining terms of B(x) */ for (; startb <= finishb; startb++) attach(terms[startb].coef, terms[startb].expon); *finishd = avail-; C.E., NCU, Taiwan Angela Chih-Wei Tang, 2

12 Function to Add a New Term void attach(float coefficient, int exponent) { /* add a new term to the polynomial */ if (avail > MAX_TERMS) { fprintf(stderr, Too many terms in the polynomial\n ); exit(); } terms[avail].coef = coefficient; terms[avail++].expon = exponent; } C.E., NCU, Taiwan Angela Chih-Wei Tang, 2 2

13 Outline Array Structurest Polynomial Sparse Matrix Multidimensional Arrays C.E., NCU, Taiwan Angela Chih-Wei Tang, 2 3

14 row row row 2 row 3 row The Sparse Matrix Abstract Data Type col col col a[max_rows][max_cols] OK! row row row 2 row 3 row 4 row 5 col col col2 col3 col4 col Many s a[max_rows][max_cols] inefficient How to efficiently store this matrix? C.E., NCU, Taiwan Angela Chih-Wei Tang, 2 4

15 Abstract Data Type Sparse_Matrix #define MAX_TERMS /* maximum number of terms +*/ row col value struct term{ a[] int col; a[] 5 int row; a[2] 3 22 int value; a[3] 5-5 }term; a[4] struct term a[max_terms]; a[5] 2 3 The number of the rows a[6] The number of the cols The number of nonzero entries a[7] 4 9 a[8]

16 2.4.2 Transposing a Matrix row col value b[] b[] 5 b[2] 4 9 Algorithm: for all elements in column j place element <i, j, value> in element <j, i, value> b[3] The number of the rows b[4] 2 3 b[5] b[6] 3 22 b[7] b[8] 5-5 The number of the cols The number of nonzero entries C.E., NCU, Taiwan Angela Chih-Wei Tang, 2 6

17 void transpose(term a[], term b[]) /* b is set to the transpose of a */ { int n, i, j, currentb; n=a[].value; b[].row = a[].col; b[].value = n; if(n>) {/*non zero matrix */ currentb=l; for(i=; i<a[].col; i++) /*transpose by the columns in a*/ for (j=; j<=n; j++) /* find elements from the current column*/ if (a[j].col == i) { /* element is in current column, add it to b */ b[currentb].row = a[j].col; b[currentb].col col = a[j].row; b[currentb].value = a[j].value; currentb++; } } } Transpose of A Sparse Matrix C.E., NCU, Taiwan Angela Chih-Wei Tang, 2 7

18 Asymptotic Time Complexity of The Transpose Algorithm The original transpose algorithm for(j=; j< columns; j++) for(i=; i<rows;i++) b[j][i]=a[i][j]; O(columns*rows) ( ) The revised transpose algorithm for loop: a[].col times iteration of the inner for loop : a[].value time a[].value: the number of elements in the original matrix O(columns*elements) Less space, more time??? Solution: fast transpose algorithm C.E., NCU, Taiwan Angela Chih-Wei Tang, 2 8

19 Fast Transpose of A Sparse Matrix void fast_transpose(term t a[], term b[]) { /* the transpose of a is placed in b*/ O(columns+elements) O(columns*rows) int row_terms[max_col], starting_pos[max_col]; int i, j, num_cols = a[].col, num_terms = a[].value; b[].row=num_cols; b[].col=a[];. Determine the number of b[].value =num_terms; elements in each row of if(num_terms > ) {/* nonzero matrix */ the transposed matrix for(i=; i< num_ cols; i++) row_terms[i]=; 2. Determine the starting for(i=; i<=num_terms; i++) position of each row in row_terms[a[i].col]++; the transpose matrix starting_pos[]=; 2 for(i=; i< num_cols; i++) starting_pos[i] = starting_pos[i-]+row_terms[i-]; terms[i ]; 3 for(i=; i<=num_terms; i++) { j=starting_pos[a[i].col]++; 3. Moving the elements in b[j].row=a[i].col; [i] b[j].col=a[i].row; [i] the original matrix into b[j].value=a[i].value; their correct position in C.E., NCU, Taiwan } the transpose matrix 9

20 2.4.3 Matrix Multiplication Definition: Given A and B, where A is mxn and B is nxp, the product matrix D has dimension mxp. Its [i][j] element is d ij = n k= for i < m and j < p. Rows a a a2 a a a2 a 2 a 2 a 22 a ik b kj b b b Columns b b b b b b 2 C.E., NCU, Taiwan Angela Chih-Wei Tang, 2 2

21 Multiplication of p Two Sparse Matrices (/3) How to multiply 2 matrices represented as an ordered list? Notice: the product of two sparse matrices may no longer be sparse = C.E., NCU, Taiwan Angela Chih-Wei Tang, 2 2

22 Multiplication of Two Sparse Matrices (2/3) row col value a[] a[] 5 a[2] 3 22 a[3] 5-5 a[4] a[5] 2 3 a[6] a[7] 4 9 a[8] row col value b[] b[] 5 b[2] 4 9 b[3] b[4] 2 3 b[5] b[6] 3 22 b[7] b[8] 5-5 Rows X Columns C.E., NCU, Taiwan Angela Chih-Wei Tang, 2 22

23 Multiplication of Two Sparse Matrices (3/3) C is the transpose matrix of B row col value a[] a[] 5 a[2] 3 22 a[3] 5-5 a[4] a[5] 2 3 a[6] a[7] 4 9 a[8] row col value c[] c[] 5 c[2] 3 22 c[3] 5-5 c[4] c[5] 2 3 c[6] c[7] 4 9 c[8] Angela Chih-Wei Tang, 2 23 Rows X Rows Apply operations similar to polynomial additions!

24 Outline The Array as An Abstract Data Type Structures The Polynomial Abstract Data Type The Sparse Matrix Abstract Data Type Multidimensional Arrays C.E., NCU, Taiwan Angela Chih-Wei Tang, 2 24

25 Representation of Arrays Multidimensional arrays are usually implemented by one dimensional array via either row major order or column major order. Example: One dimensional array α α+ α+2 α+3 α+4 α+5 α+6 α+7 α+8 α+9 α+ α+2 A[] A[] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[] A[] C.E., NCU, Taiwan Angela Chih-Wei Tang, 2 25

26 Two Dimensional Array Row Major Order Row Row Col Col Col 2 Col u 2 - X X X X X X X X Row u - X X X X u 2 elements u 2 elements Row Row Row i Row u - i * u 2 element C.E., NCU, Taiwan Angela Chih-Wei Tang, 2 26

27 Generalizing Array Representation The address indexing of Array A[i ][i 2 ],,[i n ] is α+ i u 2 u 3 u n + i 2 u 3 u 4 u n + i 3 u 4 u 5 u n : : + i n- u n + i n =α+ a n n j i ja j where k= j + j= = u k a n = j n C.E., NCU, Taiwan Angela Chih-Wei Tang, 2 27

Advantage of an array ADT:

Advantage of an array ADT: Data Structures using C Chapter-2 ARRAYS AND SRUCTURES Array an array is a set of pairs, , such that each index that is defined has a value associated with it a consecutive set of memory

More information

ARRAYS AND STRUCTURES

ARRAYS AND STRUCTURES CHAPTER 2 ARRAYS AND STRUCTURES All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed Fundamentals of Data Structures in C, CHAPTER 2 1 Arrays Array: a

More information

CHAPTER 2: ARRAYS AND STRUCTURES

CHAPTER 2: ARRAYS AND STRUCTURES CHAPTER 2: ARRAYS AND STRUCTURES ARRAY It is collections of elements of the same basic data type. Structure Array is objects: A set of pairs where for each value of index there is a value

More information

Data Structure. Chapter 4 List (Part II) Department of Communication Engineering National Central University Jhongli, Taiwan.

Data Structure. Chapter 4 List (Part II) Department of Communication Engineering National Central University Jhongli, Taiwan. Data Structure Chapter 4 List (Part II) Angela Chih-Wei Tang Department of Communication Engineering National Central University Jhongli, Taiwan 2009 Spring Outline Polynomials Sparse matrices Doubly linked

More information

Arrays. Example: int list[5]: list[0],, list[4] each contains an integer. List

Arrays. Example: int list[5]: list[0],, list[4] each contains an integer. List Arrays Arrays Array: a set of index and value data structure For each index, there is a value associated with that index. representation (possible) implemented by using consecutive memory. Example: int

More information

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES BBM 201 DATA STRUCTURES Lecture 4: Lower/Upper Triangular Matrix Band Matrix Sparse Matrix 2015-2016 Fall Lower Triangular Matrix Lower Triangular Matrix Does the definition of a special data structure

More information

DATA STRUCTURES WITH C (Common to CSE & ISE)

DATA STRUCTURES WITH C (Common to CSE & ISE) (Common to CSE & ISE) Subject Code: 10CS35 I.A. Marks : 25 Hours/Week : 04 Exam Hours: 03 Total Hours : 52 Exam Marks: 100 PART A UNIT - 1 8 Hours BASIC CONCEPTS: Pointers and Dynamic Memory Allocation,

More information

Data Structure. Chapter 5 Trees (Part II) Angela Chih-Wei Tang. National Central University Jhongli, Taiwan

Data Structure. Chapter 5 Trees (Part II) Angela Chih-Wei Tang. National Central University Jhongli, Taiwan Data Structure Chapter 5 Trees (Part II) Angela Chih-Wei Tang Department of Communication Engineering National Central University Jhongli, Taiwan 2010 Spring Threaded Binary Tree Problem: There are more

More information

Jawaharlal Nehru Engineering College

Jawaharlal Nehru Engineering College Jawaharlal Nehru Engineering College Laboratory Manual DATA STRUCTURES USING C For Second year Students (CSE) 16, Nov 2005 Rev 00 Comp Sc ISO 9000 Tech Document Author JNEC, Aurangabad FOREWORD It is my

More information

Data Structure. Chapter 3 Stacks and Queues. Department of Communication Engineering National Central University Jhongli, Taiwan.

Data Structure. Chapter 3 Stacks and Queues. Department of Communication Engineering National Central University Jhongli, Taiwan. Data Structure Chapter 3 Stacks and Queues Instructor: Angela Chih-Wei Tang Department of Communication Engineering National Central University Jhongli, Taiwan 29 Spring Outline Stack Queue A Mazing Problem

More information

2.1.5 Miscellaneous Topics

2.1.5 Miscellaneous Topics 2.1.5 Miscellaneous Topics struct definition of a data type union not specify whether a given member has public, private, protected no way to know what the data type is until runtime static class data

More information

Chapter 4: Lists 2004 년가을학기. 강경란 정보및컴퓨터공학부, 아주대학교

Chapter 4: Lists 2004 년가을학기. 강경란 정보및컴퓨터공학부, 아주대학교 Chapter 4: Lists 2004 년가을학기 강경란 korykang@ajou.ac.kr 정보및컴퓨터공학부, 아주대학교 1. Pointers 2. Singly Linked Lists Chapter 4 Lists 3. Dynamically Linked Stacks and Queues 4. Polynomials 5. Additional List Operations

More information

Fall, 2015 Prof. Jungkeun Park

Fall, 2015 Prof. Jungkeun Park Data Structures t and Algorithms Circular lists / Doubly linked lists Fall, 2015 Prof. Jungkeun Park Copyright Notice: This material is modified version of the lecture slides by Prof. Rada Mihalcea in

More information

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos. UNIT 2 ARRAYS Arrays Structure Page Nos. 2.0 Introduction 23 2.1 Objectives 24 2.2 Arrays and Pointers 24 2.3 Sparse Matrices 25 2.4 Polynomials 28 2.5 Representation of Arrays 30 2.5.1 Row Major Representation

More information

Chapter 4. LISTS. 1. Pointers

Chapter 4. LISTS. 1. Pointers Chap 4: LIST (Page 1) Chapter 4. LISTS TABLE OF CONTENTS 1. POINTERS 2. SINGLY LINKED LISTS 3. DYNAMICALLY LINKED STACKS AND QUEUES 4. POLYNOMIALS 5. ADDITIONAL LIST OPERATIONS 6. EQUIVALENCE RELATIONS

More information

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Introduction to C Programming Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline Printing texts Adding 2 integers Comparing 2 integers C.E.,

More information

2.2 The array as an ADT

2.2 The array as an ADT 2.2 The array as an ADT Chapter 2: Arrays Hsien-Chou Liao Depart. of Comp. Sci. and Info. Eng. Chaoyang Univ. of Tech. Many programmers view an array only as a consecutive set of memory locations. Is array

More information

Tribhuvan University Institute of Science and Technology 2065

Tribhuvan University Institute of Science and Technology 2065 1CSc.102-2065 2065 Candidates are required to give their answers in their own words as for as practicable. 1. Draw the flow chart for finding largest of three numbers and write an algorithm and explain

More information

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES BBM 201 DATA STRUCTURES Lecture 10: Implementation of Linked Lists (Linked stacks and queues, Circular linked lists) 2015-2016 Fall Linked list implementation of stacks The cost of insert and delete at

More information

Arrays. 2.2 The Array as an Abstract Data Type. Array:

Arrays. 2.2 The Array as an Abstract Data Type. Array: Arrays 2.2 The Array as an Abstract Data Type Array: A set of pairs: (correspondence or mapping) Two operations: retrieve, store Now we will use the C++ class to define an ADT. 1 GeneralArray

More information

We will introduce another operator & using the example below

We will introduce another operator & using the example below Pointers Pointers are often thought to be the most difficult aspect of C. In C when we define a pointer variable by preceding its name with an *. In C we also give our pointer a type which refers to the

More information

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES BBM 201 DATA STRUCTURES Lecture 10: Implementation of Linked Lists (Stacks, Queue, Hashtable) 2017-2018 Fall Linked list implementation of stacks The cost of insert and delete at the beginning of a linked

More information

Unit 3 Decision making, Looping and Arrays

Unit 3 Decision making, Looping and Arrays Unit 3 Decision making, Looping and Arrays Decision Making During programming, we have a number of situations where we may have to change the order of execution of statements based on certain conditions.

More information

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

Programming & Data Structure Laboratory. Arrays, pointers and recursion Day 5, August 5, 2014 Programming & Data Structure Laboratory rrays, pointers and recursion Day 5, ugust 5, 2014 Pointers and Multidimensional rray Function and Recursion Counting function calls in Fibonacci #include

More information

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal MA 511: Computer Programming Lecture 3: http://www.iitg.ernet.in/psm/indexing_ma511/y10/index.html Partha Sarathi Mandal psm@iitg.ernet.ac.in Dept. of Mathematics, IIT Guwahati Semester 1, 2010-11 Last

More information

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

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008 C Programming Language: C ADTs, 2d Dynamic Allocation Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008 Overview Row major format 1 and 2-d dynamic allocation struct

More information

C Structures, Unions, Bit Manipulations, and Enumerations

C Structures, Unions, Bit Manipulations, and Enumerations C Structures, Unions, Bit Manipulations, and Enumerations Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline 10.2 Structure Definitions 10.4

More information

Matrix Multiplication

Matrix Multiplication Matrix Multiplication Nur Dean PhD Program in Computer Science The Graduate Center, CUNY 05/01/2017 Nur Dean (The Graduate Center) Matrix Multiplication 05/01/2017 1 / 36 Today, I will talk about matrix

More information

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Arrays CS10001: Programming & Data Structures Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur 1 Array Many applications require multiple data items that have common

More information

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan. Functions Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2009 Fall Outline 5.1 Introduction 5.3 Math Library Functions 5.4 Functions 5.5

More information

Multi-Dimensional arrays

Multi-Dimensional arrays Multi-Dimensional arrays An array having more then one dimension is known as multi dimensional arrays. Two dimensional array is also an example of multi dimensional array. One can specify as many dimensions

More information

MTH 307/417/515 Final Exam Solutions

MTH 307/417/515 Final Exam Solutions MTH 307/417/515 Final Exam Solutions 1. Write the output for the following programs. Explain the reasoning behind your answer. (a) #include int main() int n; for(n = 7; n!= 0; n--) printf("n =

More information

How to declare an array in C?

How to declare an array in C? Introduction An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type. Representation of a large number of homogeneous values.

More information

Matrix Multiplication

Matrix Multiplication Matrix Multiplication CPS343 Parallel and High Performance Computing Spring 2013 CPS343 (Parallel and HPC) Matrix Multiplication Spring 2013 1 / 32 Outline 1 Matrix operations Importance Dense and sparse

More information

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body; CLASSROOM SESSION Loops in C Loops are used to repeat the execution of statement or blocks There are two types of loops 1.Entry Controlled For and While 2. Exit Controlled Do while FOR Loop FOR Loop has

More information

Lab #10 Multi-dimensional Arrays

Lab #10 Multi-dimensional Arrays Multi-dimensional Arrays Sheet s Owner Student ID Name Signature Group partner 1. Two-Dimensional Arrays Arrays that we have seen and used so far are one dimensional arrays, where each element is indexed

More information

MODULE 3: LINKED LIST

MODULE 3: LINKED LIST MODULE 3: LINKED LIST DEFINITION A linked list, or one-way list, is a linear collection of data elements, called nodes, where the linear order is given by means of pointers. That is, each node is divided

More information

Matrix Multiplication

Matrix Multiplication Matrix Multiplication CPS343 Parallel and High Performance Computing Spring 2018 CPS343 (Parallel and HPC) Matrix Multiplication Spring 2018 1 / 32 Outline 1 Matrix operations Importance Dense and sparse

More information

Pointers. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Pointers. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Pointers Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline 7.1 Introduction 7.2 Pointer Variable Definitions and Initialization 7.3 Pointer

More information

C Program Control. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

C Program Control. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan C Program Control Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline The for repetition statement switch multiple selection statement break

More information

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

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100 Code: DC-05 Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100 NOTE: There are 11 Questions in all. Question 1 is compulsory and carries 16 marks. Answer to Q. 1. must be written in the space

More information

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type. Data Structures Introduction An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type. Representation of a large number of homogeneous

More information

Lists (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues

Lists (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues By: Pramod Parajuli, Department of Computer Science, St. Xavier

More information

Dynamic memory allocation

Dynamic memory allocation Dynamic memory allocation outline Memory allocation functions Array allocation Matrix allocation Examples Memory allocation functions (#include ) malloc() Allocates a specified number of bytes

More information

Pointers. Array. Solution to the data movement in sequential representation

Pointers. Array. Solution to the data movement in sequential representation 1 LISTS Pointers Array sequential representation some operation can be very time-consuming (data movement) size of data must be predefined static storage allocation and deallocation Solution to the data

More information

Data Structure. Chapter 10 Search Structures (Part II)

Data Structure. Chapter 10 Search Structures (Part II) Data Structure Chapter 1 Search Structures (Part II) Instructor: ngela Chih-Wei Tang Department of Communication Engineering National Central University Jhongli, Taiwan 29 Spring Outline VL trees Introduction

More information

PDS Class Test 2. Room Sections No of students

PDS Class Test 2. Room Sections No of students PDS Class Test 2 Date: October 27, 2016 Time: 7pm to 8pm Marks: 20 (Weightage 50%) Room Sections No of students V1 Section 8 (All) Section 9 (AE,AG,BT,CE, CH,CS,CY,EC,EE,EX) V2 Section 9 (Rest, if not

More information

Lecture 3 Linked List

Lecture 3 Linked List Lecture 3 Linked List Bo Tang @ SUSTech, Spring 2018 Our Roadmap Linked List Definition Linked List Operators Illustration Example 2 Representing a Sequence of Data An ordered collection of items (position

More information

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Arrays CS10001: Programming & Data Structures Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Array Many applications require multiple data items that have common

More information

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

Lab 3. Pointers Programming Lab (Using C) XU Silei Lab 3. Pointers Programming Lab (Using C) XU Silei slxu@cse.cuhk.edu.hk Outline What is Pointer Memory Address & Pointers How to use Pointers Pointers Assignments Call-by-Value & Call-by-Address Functions

More information

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

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size]; Arrays An array is a collection of two or more adjacent memory cells, called array elements. Array is derived data type that is used to represent collection of data items. C Array is a collection of data

More information

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

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE is to be

More information

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator EXAMINATION ( End Semester ) SEMESTER ( Spring ) Roll Number Section Name Subject Number C S 1 0 0 0 1 Subject Name Programming

More information

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 #04 Department of Computer Science & Engineering Indian Institute of Technology Kharagpur Topic: Arrays and Strings Practice Sheet #04 Date: 24-01-2017 Instructions: For the questions consisting code segments,

More information

Arrays. Dr. Madhumita Sengupta. Assistant Professor IIIT Kalyani

Arrays. Dr. Madhumita Sengupta. Assistant Professor IIIT Kalyani Arrays Dr. Madhumita Sengupta Assistant Professor IIIT Kalyani INTRODUCTION An array is a collection of similar data s / data types. The s of the array are stored in consecutive memory locations and are

More information

Character Strings. String-copy Example

Character Strings. String-copy Example Character Strings No operations for string as a unit A string is just an array of char terminated by the null character \0 The null character makes it easy for programs to detect the end char s[] = "0123456789";

More information

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc. C for Java Programmers 1 Last Week Very short history of C Overview of the differences between C and Java The C language (keywords, types, functies, etc.) Compiling (preprocessor, compiler, linker) C for

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

AMCAT Automata Coding Sample Questions And Answers

AMCAT Automata Coding Sample Questions And Answers 1) Find the syntax error in the below code without modifying the logic. #include int main() float x = 1.1; switch (x) case 1: printf( Choice is 1 ); default: printf( Invalid choice ); return

More information

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows: Pointers and Arrays Part II We will continue with our discussion on the relationship between pointers and arrays, and in particular, discuss how arrays with dynamical length can be created at run-time

More information

Self-referential Structures and Linked List. Programming and Data Structure 1

Self-referential Structures and Linked List. Programming and Data Structure 1 Self-referential Structures and Linked List Programming and Data Structure 1 Linked List :: Basic Concepts A list refers to a set of items organized sequentially. An array is an example of a list. The

More information

Types of Data Structures

Types of Data Structures DATA STRUCTURES material prepared by: MUKESH BOHRA Follow me on FB : http://www.facebook.com/mukesh.sirji4u The logical or mathematical model of data is called a data structure. In other words, a data

More information

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

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables. When a program is run, memory space is immediately reserved for the variables defined in the program. This memory space is kept by the variables until the program terminates. These variables are called

More information

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size] (November 10, 2009 2.1 ) Arrays An array is a collection of several elements of the same type. An array variable is declared as type array name[size] I The elements are numbered as 0, 1, 2... size-1 I

More information

APS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example:

APS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example: Collecting Elements How to collect elements of the same type? Eg:., marks on assignments: APS105 Arrays Textbook Chapters 6.1-6.3 Assn# 1 2 3 4 5 6 Mark 87 89 77 96 87 79 Eg: a solution in math: x 1, x

More information

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator EXAMINATION ( Mid Semester ) SEMESTER ( Autumn ) Roll Number Section Name Subject Number C S 1 0 0 0 1 Subject Name Programming

More information

List of Programs: Programs: 1. Polynomial addition

List of Programs: Programs: 1. Polynomial addition List of Programs: 1. Polynomial addition 2. Common operations on vectors in c 3. Matrix operation: multiplication, transpose 4. Basic Unit conversion 5. Number conversion: Decimal to binary 6. Number conversion:

More information

Sparse Matrices. sparse many elements are zero dense few elements are zero

Sparse Matrices. sparse many elements are zero dense few elements are zero Sparse Matrices sparse many elements are zero dense few elements are zero Special Matrices A square matrix has the same number of rows and columns. Some special forms of square matrices are Diagonal: M(i,j)

More information

In the previous lecture, we learned about how to find the complexity of algorithms and search algorithms on array.

In the previous lecture, we learned about how to find the complexity of algorithms and search algorithms on array. Lecture 2 What we will learn 1. Definition of Arrays 2. Representation of Arrays in memory 3. Row-major Order and Column-major Order In the previous lecture, we learned about how to find the complexity

More information

Introduction to Scientific Computing and Problem Solving

Introduction to Scientific Computing and Problem Solving Introduction to Scientific Computing and Problem Solving Lecture #22 Pointers CS4 - Introduction to Scientific Computing and Problem Solving 2010-22.0 Announcements HW8 due tomorrow at 2:30pm What s left:

More information

Solutions of systems of linear equations

Solutions of systems of linear equations Class Progress Basics of Linux, gnuplot, C Visualization of numerical data Roots of nonlinear equations (Midterm ) Solutions of systems of linear equations Solutions of systems of nonlinear equations Monte

More information

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017 Pointer Arithmetic Lecture 4 Chapter 10 Robb T. Koether Hampden-Sydney College Wed, Jan 25, 2017 Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 1 / 36 1 Pointer Arithmetic

More information

Arrays and Pointers. Lecture Plan.

Arrays and Pointers. Lecture Plan. . Lecture Plan. Intro into arrays. definition and syntax declaration & initialization major advantages multidimensional arrays examples Intro into pointers. address and indirection operators definition

More information

File Processing. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

File Processing. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan File Processing Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline 11.2 The Data Hierarchy 11.3 Files and Streams 11.4 Creating a Sequential

More information

Array Initialization

Array Initialization Array Initialization Array declarations can specify initializations for the elements of the array: int primes[10] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ; initializes primes[0] to 2, primes[1] to 3, primes[2]

More information

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator EXAMINATION ( End Semester ) SEMESTER ( Autumn ) Roll Number Section Name Subject Number C S 1 1 0 0 1 Subject Name Programming

More information

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

Programming & Data Structure Laboratory. Day 2, July 24, 2014 Programming & Data Structure Laboratory Day 2, July 24, 2014 Loops Pre and post test loops for while do-while switch-case Pre-test loop and post-test loop Condition checking True Loop Body False Loop Body

More information

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE is to be

More information

NCS 301 DATA STRUCTURE USING C

NCS 301 DATA STRUCTURE USING C NCS 301 DATA STRUCTURE USING C Unit-1 Part-3 Arrays Hammad Mashkoor Lari Assistant Professor Allenhouse Institute of Technology www.ncs301ds.wordpress.com Introduction Array is a contiguous memory of homogeneous

More information

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12 Week 9 Part 1 Kyle Dewey Overview Dynamic allocation continued Heap versus stack Memory-related bugs Exam #2 Dynamic Allocation Recall... Dynamic memory allocation allows us to request memory on the fly

More information

SOFTWARE Ph.D. Qualifying Exam Spring Consider the following C program, which includes three function definitions, including the main function.

SOFTWARE Ph.D. Qualifying Exam Spring Consider the following C program, which includes three function definitions, including the main function. (i) (6 pts.) SOFTWARE Ph.D. Qualifying Exam Spring 2017 Consider the following C program, which includes three function definitions, including the main function. #include #include

More information

C Program Development and Debugging under Unix SEEM 3460

C Program Development and Debugging under Unix SEEM 3460 C Program Development and Debugging under Unix SEEM 3460 1 C Basic Elements SEEM 3460 2 C - Basic Types Type (32 bit) Smallest Value Largest Value short int -32,768(-2 15 ) 32,767(2 15-1) unsigned short

More information

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator EXAMINATION ( Mid Semester ) SEMESTER ( Autumn ) Roll Number Section Name Subject Number C S 1 0 0 0 1 Subject Name Programming

More information

Linked Lists. .. and other linked structures. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Linked Lists. .. and other linked structures. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR 1 Linked Lists.. and other linked structures Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Dynamic memory allocation: review typedef struct { int hitemp;

More information

/* Polynomial Expressions Using Linked List*/ #include<stdio.h> #include<conio.h> #include <malloc.h> struct node. int num; int coeff;

/* Polynomial Expressions Using Linked List*/ #include<stdio.h> #include<conio.h> #include <malloc.h> struct node. int num; int coeff; /* Polynomial Expressions Using Linked List*/ #include #include #include struct node int num; int coeff; struct node *next; ; struct node *start1 = NULL; struct node *start2

More information

CSE101-Lec#18. Multidimensional Arrays Application of arrays. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

CSE101-Lec#18. Multidimensional Arrays Application of arrays. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming CSE101-Lec#18 Multidimensional Arrays Application of arrays Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU Outline Defining and processing 1D array 2D array Applications of arrays 1-D array A

More information

Structured Program Development

Structured Program Development Structured Program Development Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline Introduction The selection statement if if.else switch The

More information

Twister: Language Reference Manual

Twister: Language Reference Manual Twister: Language Reference Manual Manager: Anand Sundaram (as5209) Language Guru: Arushi Gupta (ag3309) System Architect: Annalise Mariottini (aim2120) Tester: Chuan Tian (ct2698) February 23, 2017 Contents

More information

POINTER & REFERENCE VARIABLES

POINTER & REFERENCE VARIABLES Lecture 9 POINTER & REFERENCE VARIABLES Declaring data pointer variables Assignment operations with pointers Referring objects using pointer variables Generic pointers Operations with pointer variables

More information

CS , Fall 2001 Exam 2

CS , Fall 2001 Exam 2 Andrew login ID: Full Name: CS 15-213, Fall 2001 Exam 2 November 13, 2001 Instructions: Make sure that your exam is not missing any sheets, then write your full name and Andrew login ID on the front. Write

More information

PART 1: SHORT ANSWER QUESTIONS: TOTAL MARKS 40/100

PART 1: SHORT ANSWER QUESTIONS: TOTAL MARKS 40/100 CMPT 102 SAMPLE FINAL SOLUTIONS PART 1: SHORT ANSWER QUESTIONS: TOTAL MARKS 40/100 1. Give a brief algorithm, outlining the following methods a. (5 points) Horner s algorithm for evaluating a polynomial

More information

Iosif Ignat, Marius Joldoș Laboratory Guide 5. Functions. FUNCTIONS in C

Iosif Ignat, Marius Joldoș Laboratory Guide 5. Functions. FUNCTIONS in C FUNCTIONS in C 1. Overview The learning objective of this lab session is to: Understand the structure of a C function Understand function calls both using arguments passed by value and by reference Understand

More information

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler) 1 Arrays General Questions 1. What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array? A. The element will be set to 0. B. The compiler would

More information

2nd Year Computational Physics Week 1 (experienced): Series, sequences & matrices

2nd Year Computational Physics Week 1 (experienced): Series, sequences & matrices 2nd Year Computational Physics Week 1 (experienced): Series, sequences & matrices 1 Last compiled September 28, 2017 2 Contents 1 Introduction 5 2 Prelab Questions 6 3 Quick check of your skills 9 3.1

More information

國立清華大學電機工程學系. Outline

國立清華大學電機工程學系. Outline 國立清華大學電機工程學系 EE2410 Data Structure Chapter 4 Linked List (Part II) Outline Equivalence Class Sparse Matrices Doubly Linked Lists Generalized Lists Virtual Functions and Dynamic Binding ch4.2-2 1 Equivalence

More information

Introduction to the new AES Standard: Rijndael

Introduction to the new AES Standard: Rijndael Introduction to the new AES Standard: Rijndael Paul Donis This paper will explain how the Rijndael Cipher Reference Code in C works. Rijndael is a block cipher that encrypts and decrypts 128, 192, and

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 9 Pointer Department of Computer Engineering 1/46 Outline Defining and using Pointers

More information

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming 1 2 3 CMPE 013/L and Strings Gabriel Hugh Elkaim Spring 2013 4 Definition are variables that can store many items of the same type. The individual items known as elements, are stored sequentially and are

More information

Lesson 7. Reading and Writing a.k.a. Input and Output

Lesson 7. Reading and Writing a.k.a. Input and Output Lesson 7 Reading and Writing a.k.a. Input and Output Escape sequences for printf strings Source: http://en.wikipedia.org/wiki/escape_sequences_in_c Escape sequences for printf strings Why do we need escape

More information

Maths for Signals and Systems Linear Algebra in Engineering. Some problems by Gilbert Strang

Maths for Signals and Systems Linear Algebra in Engineering. Some problems by Gilbert Strang Maths for Signals and Systems Linear Algebra in Engineering Some problems by Gilbert Strang Problems. Consider u, v, w to be non-zero vectors in R 7. These vectors span a vector space. What are the possible

More information