Dr. Sajib Datta Sep 10, 2014.  #include  void main()  {  int a = 25;  int b = 0;  int c = -35;  if( a || b ) ◦ printf("Test1\n");  else.

Slides:



Advertisements
Similar presentations
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Advertisements

ICS103: Programming in C Searching, Sorting, 2D Arrays
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.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CS1061 C Programmuing Lecture 12 Arrays A. O’Riordan, 2004.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
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.
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Programming Arrays. Example 1 Write a program that reads 3 numbers from the user and print them in reverse order. How many variables do we need to store.
Array.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
Computer programming Lecture 5. Lecture 5: Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character arrays.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
C Lecture Notes 1 Arrays Lecture 6. C Lecture Notes 2 6.1Introduction Arrays –Structures of related data items –Static entity – same size throughout program.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
CSCI 171 Presentation 4. Execution of a C Program Execution starts in main( ) Top down style –sequential flow Unrealistic to expect sequence in more complicated.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
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 3.
IN THE NAME OF ALLAH WHO IS THE MOST BENEFICENT AND MOST MERCIFUL.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
ICS103: Programming in C Searching, Sorting, 2D Arrays Muhamed F. Mudawar.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
Computer programming Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
Arrays, Part 2 We have already learned how to work with arrays using subscript notation. Example: float myData[] = {3.5, 4.0, 9.34}; myData[0] += 2; printf("myData[0]
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
Arrays.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
CSE 251 Dr. Charles B. Owen Programming in C1 Intro to Arrays Storing List of Data.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
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 and Matrices. One-Dimensional Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
Arrays. Arrays are objects that help us organize large amounts of information.
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.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
Dr. Sajib Datta  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters as integers.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
Functions and Pointers
CSE 1320 Search, Sort and Strings
CSE1320 Loop Dr. Sajib Datta
Functions and Pointers
Arrays.
ICS103: Programming in C Searching, Sorting, 2D Arrays
Presentation transcript:

Dr. Sajib Datta Sep 10, 2014

 #include  void main()  {  int a = 25;  int b = 0;  int c = -35;  if( a || b ) ◦ printf("Test1\n");  else ◦ printf("Test2\n");  if( a && c ) ◦ printf("Test3\n");  else ◦ printf("Test4\n"); if( !a ) printf("Test5\n"); else printf("Test6\n"); if( (a && b) || c ) printf("Test7\n"); else printf("Test8\n"); if( !c || !b ) printf("Test9\n"); else printf("Test10\n"); }

 #include  void main()  {  int a = 25;  int b = 0;  int c = -35;  if( a || b )  printf("Test1\n");  else  printf("Test2\n");  if( a && c )  printf("Test3\n");  else  printf("Test4\n");  if( !a )  printf("Test5\n");  else  printf("Test6\n");  if( (a && b) || c )  printf("Test7\n");  else  printf("Test8\n");  if( !c || !b )  printf("Test9\n");  else  printf("Test10\n");  } Answer: Test1 Test3 Test6 Test7 Test9

 Example of declaring and initializing an array. ◦ double someData[3]; /* declare the array someData that will hold 3 doubles variables*/  /* later we can provide the specific array values. notice how the array index begins at 0 */  someData[0] = 3.5;  someData[1] = 4.0;  someData[2] = 9.34;

 We may also declare and initialize the array at the same time, so we don’t need to include the number of array elements in the square brackets.  double myData[] = {3.5, 4.0, 9.34};  Never access a variable in an array beyond the index bound!!!!

 If we initialize only some of the array values when the array is declared, the initialized values will be at the beginning of the array.  The remaining values will be initialized to zero for an array of int, “space” for an array of char.

 #include  int main(void)  {  int j, intarr[3] = {2,3};  char charr[3] = {'a', 'b'};  for (j = 0; j < 3; j++)  {  printf(" %d ", intarr[j]);  }  printf("\n");  for (j = 0; j < 3; j++)  {  printf(" %c ", charr[j]);  }  return 0;  }

 int n=19;  int iarr[n];  or  #define ARRAY_SIZE 19 // have it after #include  int iarr[ARRAY_SIZE];

 Given  int array1D[5] = { 32,44,33,12,65};  Output:  The min value is 12.  The max value is 65.

 #include  int main(void)  {  int i, min, max, array1D[5] = {32,44,33,12,65};  min = max = array1D[0];  for (i = 1; i < 5; i++)  {  if (array1D[i] > max)  max = array1D[i];  if (array1D[i] < min)  min = array1D[i];  }  printf("The min value is %d.\n", min);  printf("The max value is %d.\n", max);  return 0;  }

 Original sentence: I am at UTA  The reverse sentence: ATU ta ma I

 #include  int main(void)  {  int i;  char arraych[11] = {'I', ' ', 'a', 'm', ' ', 'a', 't', ' ', 'U', 'T', 'A'};  printf("Original sentence: ");  for (i = 0; i <= 10; i++)  printf("%c", arraych[i]);  printf("\n");  printf("The reverse sentence: ");  for (i = 10; i >=0; i--)  printf("%c", arraych[i]);  printf("\n");  return 0;  }

 We can also have arrays of arrays, also known as multidimensional arrays.  E.g., A two-dimensional array is an array of several one-dimensional arrays.  The basic form for declaring a 2D array is ◦ type array_name[rows][columns];  The mathematical equivalent of a two- dimensional array is a matrix.

 A 2D array can be viewed like a table.  To create a 2D array for this table of values, we could use ◦ int some_data[2][3] = { {10, 5, 13}, {2, 7, 19} };  Or ◦ int some_data[][3] = { {10, 5, 13}, {2, 7, 19} };

 As with one-dimensional arrays, 2D arrays indices begin at zero. The difference is now we have to keep track of indices in two dimensions. So, if we create the following array: ◦ int Sales[2][3] = { {1, 2, 3}, {4, 5, 6} };  The individual elements are referenced with the following combinations of array name and indices:

 Print out a specific row or column in an array  #include  int main(void)  {  int row, column, Sales[2][3] = { {1, 2, 3}, {4, 5, 6} };  printf("The values on row 0 are : \n");  for (column = 0; column < 3; column++)  printf("%d ", Sales[0][column] );  printf("\n");  printf("The values on column 0 are: \n");  for (row = 0; row < 2; row++) ◦ printf("%d\n", Sales[row][0]);  return 0;  }

 In case of normal variables: to assign value of ‘a’ to ‘b’; ◦ int a,b; ◦ a=10; ◦ b = a;  Assume we want to copy the values of array ‘a’ to array ‘b’ ◦ int a[10], b[10]; ◦ Initialize a; ◦ b = a;//wrong

 Have to iterate over ‘a’ and assign individual array elements into ‘b’  for(i=0;i<10;i++) ◦ b[i] = a[i];

 Given an array on integers and another integer ‘x’, find if ‘x’ appears in the array. Print ‘Not found’, if ‘x’ does not appear, otherwise print ‘Found’.  Strategy:  Simply iterate over the array if look for a match

 int array[100];  int i,x, found=0;  ….  for(i=0; i<100;i++)  { ◦ if( array[i] == x) ◦ {  found =1;  printf(“Found %d”,x);  break; ◦ }  }  if(!found)  { ◦ printf(“%d not found”, x);  }

 The worst case – the loop is executed n times  On the average – the loop is executed n/2 times  What if the array has a million integers (about 2 20 )

 Can make it faster.  However, unlike linear search, binary search has a precondition ◦ The sequence must be sorted

 Strategy: ◦ Compare ‘x’ with the middle element.  If equal, we found it  Else If ‘x’ < middle element, we can discard the half of the elements; all elements that are greater than the middle element and middle element itself  Now search in the remaining half of the array  Similarly, if ‘x’ > middle element, we can discard the half of the elements; all elements that are smaller than the middle element and middle element itself

 Array ◦ __Array.htm  Sorting and searching ◦ Sort/Catalog0280__Search-Sort.htm  Time complexity ◦ tutorials&d2=complexity1 ◦ outs/brief-complexity.pdf ◦ _AnalysisOfAlgs.pdf (advanced!)