010.133 Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 3.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

CSE Lecture 3 – Algorithms I
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Dr. Sajib Datta  We can also have arrays of arrays, also known as multidimensional arrays.  E.g., A two-dimensional array is an array of several.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT5: Array (1D and 2D) CS2311 Computer Programming.
1 ICS103 Programming in C Lecture 14: Searching and Sorting.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching Arrays Linear search Binary search small arrays
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
Introduction to C Programming CE Lecture 9 Data Structures Arrays.
1 Two-Dimensional Arrays. 2 Can be visualized as consisting m rows, each of n columns Syntax: datatype arrayname [row] [ column] ; Example: int val[3]
Programming Arrays. Question Write a program that reads 3 numbers from the user and print them in ascending order. How many variables do we need to store.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
1 Sorting Algorithms (Basic) Search Algorithms BinaryInterpolation Big-O Notation Complexity Sorting, Searching, Recursion Intro to Algorithms Selection.
Chapter 16: Searching, Sorting, and the vector Type.
EENG212 Algorithms and Data Structures
Array.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
Computer programming Lecture 5. Lecture 5: Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character arrays.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Searching. The process used to find the location of a target among a list of objects Searching an array finds the index of first element in an array containing.
Data Strcutures.
ARRAY Prepared by MMD, Edited by MSY1.  Introduction to arrays  Declaring arrays  Initializing arrays  Examples using arrays  Relationship with pointers.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
ECE 103 Engineering Programming Chapter 24 Sorting Herbert G. Mayer, PSU CS Status 6/2/2015 Initial content copied verbatim from ECE 103 material developed.
Data Structure Introduction.
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
Computer programming Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character.
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing arrays.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 5.
Chapter 8: Arrays Gator Engineering One-dimensional array Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Move the first element to the.
8. ARRAYS. Aggregate variables Scalar variables –Single value Aggregate variables –Collection of values –Arrays: elements have the same type.
Dr. Sajib Datta Feb 11,  Example of declaring and initializing an array. ◦ double someData[3]; /* declare the array someData that will.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
Arrays Department of Computer Science. C provides a derived data type known as ARRAYS that is used when large amounts of data has to be processed. “ an.
Array Sort. Sort Pass 1 Sort Pass 2 Sort Pass 3.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
Chapter 16: Searching, Sorting, and the vector Type.
Dr. Sajib Datta Sep 10,  #include  void main()  {  int a = 25;  int b = 0;  int c = -35;  if( a || b ) ◦ printf("Test1\n");  else.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
Consultation Hours. Mubashir: – Tuesday from 12:30 to 1:30 with ease of Students. Zohaib – Wednesday b/w 9:30 -10:30 Location: TA Room (next to HOD Office)
Windows Programming Lecture 03. Pointers and Arrays.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
1-d Arrays.
Growth of Functions & Algorithms
Chapter 7 - A closer look at Arrays
Array 9/8/2018.
Algorithms Chapter 3 With Question/Answer Animations
Programming and Data Structure
Algorithms.
ICS103: Programming in C Searching, Sorting, 2D Arrays
Presentation transcript:

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

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Arrays An array is a data structure containing a certain number of elements, all of which have the same type To declare an array, specify the type and number of the elements For example, int a[10]; declares a to be an array of 10 integers

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Arrays (contd.) To access an element of an array, write the array name followed by a subscript In C, subscripts always start with 0 For example, the elements of array a are a[0], a[1], …, a[9]

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Arrays (contd.) An array is initialized by listing the values If the number of values is less than that of the array elements, the remaining elements are given value 0 int a[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; int a[10] = {0}; float a[3] = { 0.1, 0.2, 0.0 }; int a[3] = { 3, 4 }; int a[] = { 2, -4, 1 }; int a[4] = { 2, -4, 1 }; char s[] = “abcd”; char s[] = { ‘a’,‘b’,‘c’,‘d’,‘\0’ }

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Finding Maximum The program below reads ten values into an array ab and then finds the maximum among the values #include int main(void) { int n, i, max; int ab[10]; printf("Enter n: "); scanf("%d", &n); printf("Enter n numbers: "); for (i=0; i<n; i++) scanf("%d", &ab[i]); max = ab[0]; for (i=1; i<n; i++) if (ab[i] > max) max = ab[i]; printf("max %d\n", max); return 0; }

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Bubble Sort Sorts array ab of n elements in non-decreasing order The first iteration of the inner for loop brings the maximum element to the last position, the second iteration brings the second maximum to the second-to-last position, etc. The three assignments inside the if statement exchange the values of ab[j] and ab[j+1]

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Bubble Sort (contd.) for (i=1; i<n; i++) for (j=0; j<n-i; j++) if (ab[j] > ab[j+1]) { temp = ab[j]; ab[j] = ab[j+1]; ab[j+1] = temp; }

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee void insertionSort( int a[], int n ) { int i, j, val; for( i = 1; i < n; i++){ val = a[i]; j = i - 1; while ( ( j >= 0 ) && ( a[j] > val ) ) { a[j+1] = a[j]; j--; } a[j+1] = val; printIntArray( a, n ); } Insertion Sort

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee #include #define N 10 void insertionSort( int *, int ); void printIntArray( int *, int ); int main( void ) { int a[N] = { 23, -3, 5, 9, 11, 33, 87, -7, -24, 50 }; printIntArray( a, N ); insertionSort( a, N ); return 0; } void printIntArray( int a[], int n ) { int i; for( i = 0; i < n; i++) printf("%4d ", a[i]); printf("\n"); } Insertion Sort (contd.)

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Insertion Sort (contd.)

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Search Suppose that n elements are stored in an array ab Given a new element x, we want to find if x is in array ab x is one of the elements stored in ab An easy solution for search is to scan the elements in array ab one by one and check if it is equal to x Linear search

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Linear Search #include int main(void) { int n, i, x; int ab[100]; printf("Enter n: "); scanf("%d", &n); printf("Enter n numbers: "); for (i=0; i<n; i++) scanf("%d", &ab[i]); printf("Enter x: "); scanf("%d", &x); for (i=0; i<n; i++) if (x == ab[i]) { printf("%d\n", i); return 0; } printf("%d\n", -1); return -1; }

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Binary Search If the elements in array ab are stored in non- decreasing order after sorting, we can solve the search problem more efficiently than linear search We first compare x with the element in the middle (i.e., median) If x is equal to the median, we have found it If x is smaller than the median, we are sure that x is not in the upper part of array ab, so we look for x in the lower part Otherwise, x is larger than the median, we look for x in the upper part Binary search is faster than linear search

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Binary Search (contd.) low = 0; high = n-1; while (low <= high) { mid = (low+high) / 2; if (x < ab[mid]) high = mid – 1; else if (x > ab[mid]) low = mid + 1; else { printf("%d\n", mid); return 0; } printf("%d\n", -1); return -1;

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Two-dimensional Arrays Two dimensional arrays can be visualized as a multicolumn table or grid int b[2][5]; Declares a two-dimensional array b that has 2 rows and 5 columns We can initialize a two-dimensional array as follows: int b[2][5] = {{1,0,0,1,1},{0,0,1,1,1}};

Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Math.h The library contains mathematical functions (x is of type double, and all functions return double) sqrt(x)square root of x exp(x)exponential function ex log(x)natural logarithm ln(x)log10(x) sin(x)sine of x cos(x)cosine of x tan(x)tangent of x