Fundamental of Programming (C)

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

Fundamental of Programming (C)

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

CS 223: Data Structures and Programming Techniques. Exam 2

Arrays. Arrays are of 3 types One dimensional array Two dimensional array Multidimensional array

Chapter 6. Arrays. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Matrices. Jordi Cortadella Department of Computer Science

Government Polytechnic Muzaffarpur.

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

CSCI 2132 Software Development. Lecture 19: Generating Permutations

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

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Fundamental of Programming (C)

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

CS 261 Data Structures. Big-Oh Analysis: A Review

Programming for Electrical and Computer Engineers. Pointers and Arrays

Arrays a kind of data structure that can store a fixedsize sequential collection of elements of the same type. An array is used to store a collection

C Programming Language

CSCI 2132 Software Development. Lecture 18: Functions

'C' Programming Language

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

Structured programming

Fundamentals of Programming. Lecture 14 Hamed Rasifard

Computer Science & Engineering 150A Problem Solving Using Computers

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

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

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

Programming Language B

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

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

Chapter 12: Pointers and Arrays. Chapter 12. Pointers and Arrays. Copyright 2008 W. W. Norton & Company. All rights reserved.

Module 6: Array in C

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

Arrays and Applications

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.

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

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

MTH 307/417/515 Final Exam Solutions

Data Structures CHAPTER 1 1

Matrix Multiplication

CS 108 Computing Fundamentals. October/November Array Bootcamp

CSC 270 Survey of Programming Languages. What is a Pointer?

CSCE 110 PROGRAMMING FUNDAMENTALS. Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays

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

ARRAYS(II Unit Part II)

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

PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE

Basic Structure and Low Level Routines

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Principles of C and Memory Management

Principles of Programming. Chapter 6: Arrays

How to declare an array in C?

16.216: ECE Application Programming Fall 2011

Memory Management. CSC215 Lecture

Pointers. Pointer References

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

Classification s of Data Structures

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

Detail from C Propaganda Poster // Reddit user TheBall BBM 101. Introduction to Programming I. Lecture #11 C Iterations, Functions, Multi-D Arrays

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

PDS Class Test 2. Room Sections No of students

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Chapter 9: Functions. Chapter 9. Functions. Copyright 2008 W. W. Norton & Company. All rights reserved.

Matrix Multiplication

BITG 1113: Array (Part 1) LECTURE 8

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

This is CS50. Harvard University Fall Quiz 0 Answer Key

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

CSE 230 Intermediate Programming in C and C++ Arrays and Pointers

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

Chapter 8. Arrays, Addresses, and Pointers : Structured Programming Structured Programming 1

Fundamentals of Programming Session 15

CSCI 2132 Software Development. Lecture 20: Program Organization

Week 8: Arrays and File I/O. BJ Furman 21OCT2009

Introduction to Computer Science Midterm 3 Fall, Points

Dynamic Allocation of Memory Space

Two Dimensional Arrays

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

Fundamental of Programming (C)

Week 3: Pointers (Part 2)

Array Initialization

Computers in Engineering. Linear Algebra Michael A. Hawker

Computers in Engineering COMP 208. Representing Vectors. Vector Operations 11/29/2007. Scaling. Adding and Subtracting

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

Introduction to Computing Lecture 10 Arrays (Part 1)

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

Arrays, Pointers, and Strings

Decision Making and Loops

Procedural Programming

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

Recursion. Data and File Structures Laboratory. DFS Lab (ISI) Recursion 1 / 20

Arrays in C. By Mrs. Manisha Kuveskar.

Midterm Exam 2 Solutions, C programming

Fundamentals of Programming

Arrays and Pointers. Lecture Plan.

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

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Transcription:

Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 8 Array typical problems, Search, Sorting Department of Computer Engineering

Outline Array typical problems Search Sorting Department of Computer Engineering 2/73

Find Maximum Find maximum value in data array Department of Computer Engineering 3/73

Find Average Department of Computer Engineering 4/73

Number of elements greater than average After finding the average as shown in previous slide, use the following code Department of Computer Engineering 5/73

Find pair sum Find sum of every pair in data and write into pair array data[0]=5 data[1]=7 data[2]=15 data[3]=5 data[98]=3 data[99]=12 } } } Department of Computer Engineering 6/73... pair[0]=12 pair[1]=20 pair[49]=15

solution Department of Computer Engineering 7/73

Randomly re-shuffle numbers 30 times data[0]=5 data[1]=7 data[2]=15 data[3]=5 data[98]=3 data[99]=12... data[0]=12 data[1]=7 data[2]=5 data[3]=15 data[98]=3 data[99]=5 Department of Computer Engineering 8/73

solution Department of Computer Engineering 9/73

Reverse an array 0 1 2 3 4 5 6 3 1 9 7 2 2 7 9 1 3 6 Department of Computer Engineering 10/73

Reverse an Array Department of Computer Engineering 11/73

Print sum of top-bottom pairs A[0]=3 A[1]=6 A[49]=5 A[50]=3 + + +. A[98]=4 A[99]=5 Department of Computer Engineering 12/73

Group avg Suppose we have a sorted array of hundred grades. Grade[0] } Grade[1] We want to find the average of. Grade[9] top ten, second top ten students etc. Grade[10] Grade[19] Grade[20] } Grade[90]. Grade[99] } Department of Computer Engineering 13/73

Initializing Variable Length arrays Department of Computer Engineering 14/73

#include <stdio.h> #define SIZE 20 void main(void){ int number[size]; double average; برنامهایبنویسیدکهبیستعددازورودی دریافتکندوتعداداعدادکوچکتروبزرگتراز میانگینراچاپکند. int sum, large_size, small_size, i; sum = large_size = small_size = 0; for(i = 0; i < SIZE; i++){ int tmp; scanf("%d", &tmp); number[i] = tmp; sum += number[i]; } average = (1.0 * sum) / SIZE; for(i = 0; i < SIZE; i++) if(number[i] >= average) large_size++; } else small_size++; printf("average = %f\n", average); printf("small Size = %d, Large Size = %d\n", small_size, large_size); Department of Computer Engineering 15/73

#include <stdio.h> void main(void){ } int n; printf("enter n: ", n); scanf("%d", &n); int number[n]; double average; int sum, large_size, small_size, i; sum = large_size = small_size = 0; for(i = 0; i < n; i++) for(i = sum average for(i = scanf("%d", &(number[i])); 0; i < n; i++) += number[i]; = (1.0 * sum) / n; 0; i < n; i++) if(number[i] >= average) else large_size++; small_size++; printf("average = %f\n", average); printf("small Size = %d, Larg Size = %d\n", small_size, large_size); برنامهایبنویسیدکهتعداداعدادویکرشتهاز ورودیدریافتکندوتعداداعدادکوچکترو بزرگترازمیانگینراچاپکند. Department of Computer Engineering 16/73

Array Elements in Functions int number[20]; number[i] is an integer variable Array element can be used for call by value input Array element can be use for output Department of Computer Engineering 17/73

Arrays in Functions Array cannot be used as output type of function int [] f(int x, int y); //compile error Arrays can be used in input list of functions Arrays are not passed by Call By Value Arrays are passed by Call By Reference If we change array elements in a function The element is changed in the caller function Department of Computer Engineering 18/73

Arrays in Functions Department of Computer Engineering 19/73

Exercise a[0]=3 a[1]=5 c=? 8 b= n=2 i=0 1 2 sum=0 3 8 Department of Computer Engineering 20/73

Exercise a[0]=3 20 a[1]=5 c=? 8 b= n=2 i=0 1 2 sum=0 3 8 Department of Computer Engineering 21/73

تابعیکهیکآرایهبهطول 10 رامیگیردو اعضایآنرابااعداد 0 تا 9 مقداردهی میکند. Department of Computer Engineering 22/73

Array Size in Functions If array is an input parameter of a function It cannot find out the size of the array Array size should be passed from caller function to called function Using definitions Department of Computer Engineering 23/73

Array Size in Functions (cont d) If array is declared in a function It knows the size of the array It can find out the size of the array using sizeof Department of Computer Engineering 24/73

تابعیبنویسیدکهیکآرایهرادریافتو محلبزرگترینعنصرآنرابازگرداند. Department of Computer Engineering 25/73

Liner search Binary search Search function Department of Computer Engineering 26/73

Unordered list linear search Department of Computer Engineering 27/73

Ordered list linear search Department of Computer Engineering 28/73

Binary Search Key = 76 Department of Computer Engineering 29/73

Binary Search Department of Computer Engineering 30/73

Selection Sort Bubble Sort Sort function Department of Computer Engineering 31/73

Selection Sort Department of Computer Engineering 32/73

Implementation Department of Computer Engineering 33/73

34 Bubble sort Bubble sort Several passes through the array Successive pairs of elements are compared If increasing order (or identical ), no change If decreasing order, elements exchanged Repeat Department of Computer Engineering 34/73

Bubble Sort Department of Computer Engineering 35/73

Bubble Sort Department of Computer Engineering 36/73

Bubble Sort Department of Computer Engineering 37/73

Bubble Sort Department of Computer Engineering 38/73

Bubble Sort Department of Computer Engineering 39/73

Bubble Sort Department of Computer Engineering 40/73

Bubble Sort Department of Computer Engineering 41/73

Bubble Sort Department of Computer Engineering 42/73

Bubble Sort Department of Computer Engineering 43/73

Bubble Sort Department of Computer Engineering 44/73

Bubble Sort Department of Computer Engineering 45/73

Bubble Sort Department of Computer Engineering 46/73

Bubble Sort Department of Computer Engineering 47/73

Bubble Sort Department of Computer Engineering 48/73

Bubble Sort Department of Computer Engineering 49/73

Bubble Sort Department of Computer Engineering 50/73

Bubble Sort Department of Computer Engineering 51/73

Bubble Sort Department of Computer Engineering 52/73

Bubble Sort Department of Computer Engineering 53/73

Bubble Sort Department of Computer Engineering 54/73

Bubble Sort Department of Computer Engineering 55/73

Bubble Sort Department of Computer Engineering 56/73

Bubble Sort Department of Computer Engineering 57/73

Implementation Department of Computer Engineering 58/73

Merge two sorted array Assume we have A and B arrays containing sorted numbers For example A = { 3, 5, 7, 9, 12} B = {4, 5, 10} Merge these two arrays as a single sorted array C, for example C = {3, 4, 5, 5, 7, 9, 10, 12} Department of Computer Engineering 59/73

Solution Department of Computer Engineering 60/73

Intersection Set Suppose we have two sets (groups) represented by A and B E.g., A is the set of students taking Math, B is the set of students taking Science. Find set C, the intersection of A and B, i.e., students taking both Math and Science For each element ID in A Search that ID in B if found, put ID into C 3 6 9 1 7 2 4 5 8 Department of Computer Engineering 61/73

Solution 6 3 1 9 7 2 4 2 5 6 1 8 6 1 2 Department of Computer Engineering 62/73

Multidimensional Arrays in Functions Can be used as input of functions All dimensions except the first one must be given void func(int a[10][20][5]); Input is a 10x20x5 integer matrix void func(int a[][20][30], int size); void func(int size1, int size2, int a[size1][size2]); Input is a matrix of integers that both rows and columns are variable Department of Computer Engineering 63/73

Multidimensional Arrays in Functions The first subscript therefore only indicates the amount of storage that is needed when the array is declared Others are required because the computer needs to know how far along to increment the pointer for each "row" Department of Computer Engineering 64/73

#define SIZE 5 void swap(int a[size][size], int i, int j){ int tmp; tmp = a[i][j]; a[i][j] = a[j][i]; a[j][i] = tmp; } محاسبه ترانهاده ماتريس void transpose(int a[][size]){ int i, j; for(i = 0; i < SIZE; i++) for(j = i; j < SIZE; j++) } swap(a, i, j); Department of Computer Engineering 65/73

2-Dim Arrays as Arguments to Functions void print_m(int m[3][4], int r, int c) int i, j, matrix[3][4]; for (i=0; i<3; i++) for (j=0; j<4; j++) matrix[i][j] = i; print_m(matrix, 3, 4); Department of Computer Engineering 66/73 void print_m(int m[][4], int r, int c) { int i,j; for (i=0; i < r; i++) { for (j=0; j < c; j++) printf("%.5d ",m[i][j]); printf("\n"); } printf("\n"); return; }

Matrix sum Compute the addition of two matrices 0 1 2 3 0 1 2 3 0 1 2 3 0 0 1 0 2 0 3-1 3 1 0 3 0 3 3 1-1 2 4 3 + 1 1 4 2 0 = 1 0 6 6 3 2 0-1 3 1 2 2 1 1 3 2 2 0 4 4 Department of Computer Engineering 67/73

solution int matrix1[3][4], matrix2[3][4], sum[3][4]; // initialize matrix1 and matrix2 for (i=0; i<3; i++) for (j=0; j<4; j++) sum[i][j]= matrix1[i][j]+matrix2[i][j]; Department of Computer Engineering 68/73

Exchange Two Rows 4 6 2 0 5 3 0 8 1 2 1 4 4 6 2 2 1 4 0 8 1 0 5 3 Department of Computer Engineering 69/73

Transpose void transpose(int a[nrows][ncols], int b[ncols][nrows]) { /* Declare Variables. */ } int i, j; /* Transfer values to the transpose matrix. */ for(i=0; i<nrows; i++) { for(j=0; j<ncols; j++) { b[j][i] = a[i][j]; } } return; a 1 5 3 4 2 6 b 1 4 5 2 3 6 Department of Computer Engineering 70/73

Matrix multiplication double a[3][2], b[2][4], c[3][4]; Find c = a * b; 3 4 5 2 1 6 2 3 7 1 x = 4 5 6 8 22 29 45 35 18 40 47 21 26 33 43 49 3*2 + 4*4=22 3*3 + 4*5=29 3*7 + 4*6=45 3*1 + 4*8=35 5*2 + 2*4=18 5*3 + 2*5=40 5*7 + 2*6=47 5*1 + 2*8=21 1*2 + 6*4=26 1*3 + 6*5=33 1*7 + 6*6=43 1*1 + 6*8=49 Department of Computer Engineering 71/73

Matrix Multiplication i 0 1 2 i=0 3 4 5 2 1 6 3 4 k 2 3 7 1 x = 4 5 6 8 x 2 4 Department of Computer Engineering 72/73 j 0 1 2 3 j=0 k = i 0 1 2 j 0 1 2 3 22 29 45 35 18 40 47 21 26 33 43 49 c[i][j] = a[i][k=0]*b[k=0][j] + a[i][k=1]*b[k=1][j]

Matrix Multiplication cont d #define N 3 #define M 2 #define L 4 void matrix_mul(a[n][m], int b[m][l], int c[n][l]) { int i, j, k; for(i=0; i < N; i++) { for(j=0; j < L; j++) { c[i][j] = 0; for(k=0; k < M; k++) { c[i][j] = c[i][j] + a[i][k] * b[k][j]; } } } return; } Department of Computer Engineering 73/73