Complementary tutorial - arrays, loops, riddles Prepared by: Valentin Kravtsov.

Slides:



Advertisements
Similar presentations
EXCEL UNIT 3 Computer Technology Timpview High School.
Advertisements

For loops For loops are controlled by a counter variable. for( c =init_value;c
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.
§3 Compare the Algorithms 〖 Example 〗 Given (possibly negative) integers A 1, A 2, …, A N, find the maximum value of Max sum is 0 if all the integers.
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
Chapter 10 Algorithm Efficiency
Tutorial #5 Summer while x = -2; while (x < 0) { printf("x is still negative :(\n"); x++; } printf("x is no longer negative.\n");
C workshop Yuli Kaplunovsky - Today - Introduction to C Recommended book: The C programming Language / Kernighan & Ritchie.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
לולאות 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי 15 1 Department of Computer Science-BGU.
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
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.
1.7 Arrays academy.zariba.com 1. Lecture Content 1.Basic Operations with Arrays 2.Console Input & Output of Arrays 3.Iterating Over Arrays 4.List 5.Cloning.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
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.
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
Greedy Backtracking ? ? ? ?.  Fast, low complexity, gives acceptable solution (not necessarily the best)  At each step choose the best option considering.
CS1010E Programming Methodology Tutorial 3 Control Structures and Data Files C14,A15,D11,C08,C11,A02.
Standard Algorithms –search for an item in an array –count items in an array –find the largest (or smallest) item in an array.
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
2D-Arrays Quratulain. Learning Objectives Two-dimensional arrays Declaration Initialization Applications.
Introduction to C Programming CE Lecture 4 Further Control Structures in C.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
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.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 3.
1 Decision Making if (score
Lec 21 More Fun with Arrays: For Loops. Agenda Some backfill for Lab 20: – Using an array in an applet or class – array instance variables – using Math.random()
UNIT 10 Multidimensional Arrays.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
While loop Write a program that asks the user to enter a number, then displays whether this number is even or odd. The program repeats until the user quits.
DATA TYPE, MEMORY, AND FUNCTION Dong-Chul Kim BioMeCIS UTA 2/18/
DG8. FILE * fopen ( const char * filename, const char * mode ); Mode: r: Open a file for reading. The file must exist. w: Create an empty file for writing.
Array Examples Statistical Analysis. Read age statistics of people in a small building (which is in a file called Ages.txt) and calculate: – x m, Mean.
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.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 3. Time Complexity Calculations.
Array Sort. Sort Pass 1 Sort Pass 2 Sort Pass 3.
Dr. Sajib Datta Sep 10,  #include  void main()  {  int a = 25;  int b = 0;  int c = -35;  if( a || b ) ◦ printf("Test1\n");  else.
CS1101X: Programming Methodology Recitation 6 Arrays I.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
MULTI-DIMENSION ARRAY STRING Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Chapter 2 Array and String. Array Definition of Array : An array is a sequence or collection of the related data items that share a common name. Purpose.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
Programming Loops (continued).
Functions and Pointers
Intro to C Tutorial 4: Arithmetic and Logical expressions
Array 9/8/2018.
Functions Dr. Sajib Datta
Functions and Pointers
Control Structures Lecture 7.
Looping.
Arrays, For loop While loop Do while loop
Loops in C.
Analysis of Algorithms
Incremental operators
Example. Sort {5, 1, 12, -5, 16, 2, 12, 14} using selection sort. Complexity analysis.
Week 6 CPS125.
Arrays and Matrices.
Programming Fundamental
Presentation transcript:

Complementary tutorial - arrays, loops, riddles Prepared by: Valentin Kravtsov

Arrays – warm up questions Given array “arr”, print its content #define N 7 int i; int arr[N] = {1,2,3,4,5,6,7}; for( i=0 ; i<N ; i++ ){ printf("%d ",arr[i]); } Output:

Given array “arr”, print its content backwards #define N 7 int i; int arr[N] = {1,2,3,4,5,6,7}; for( i=N-1 ; i>=0 ; i-- ){ printf("%d ",arr[i]); } Output:

Given array “arr”, print its content backwards discarding the leading zeroes int i, leading_zeros = 1; //true int arr[N] = {1,2,3,4,5,0,0}; for( i=N-1; i>=0 ; i-- ){ if(leading_zeros && arr[i]==0) continue; printf("%d ",arr[i]); leading_zeros = 0; //false } Output: Solution #1 – straightforward

Given array “arr”, print its content backwards discarding the leading zeroes int i; int arr[N] = {1,2,3,4,5,0,0}; for( i=N-1 ; i>=0 && arr[i]==0 ; i-- ); for( ; i>=0 ; i--){ printf("%d ",arr[i]); } Output: Solution #2 – a bit more efficient

2D Arrays To print a multiplication table:

2D Arrays To print a multiplication table: int i, j; for( i=1 ; i<=N ; i++){ for( j=1 ; j<=N ; j++){ printf("%2d ",i*j); } printf("\n"); }

2D Arrays To print the “flag of Britain”: o o o ooooooooo ooo

2D Arrays To print the “flag of Britain”: int i,j; char c; for(i=0 ; i<N; i++){ for(j=0 ; j<N ; j++){ c = (i==j || i+j==N-1 || i==N/2 || j==N/2)?'o':' '; printf("%c",c); } printf("\n"); }

Given a 2D array arr[N][N], we have to print TRUE if there is at least one sorted column, FALSE otherwise. int i,j; for( j=0 ;j<N ; ++j ) { for( i=1; i<N ;++i){ if( arr[i][j] < arr [i-1][j] ) break; } if( i==N ) {printf(“TRUE”); return 1; } } printf(“FALSE”);

Your optional challenging home- assignment To print a “number spiral”

Given two arrays, arr1 & arr2, with sorted non- repeating numbers, print the intersection of these arrays. int arr1[N] = {1,5,7,8,15,20,22,23,40,43}; int arr2[N] = {2,3,4,5, 6,11,15,19,20,43}; int i,j; for( i=j=0 ; i<N && j<N ; ){ if(arr1[i] == arr2[j]){ printf("%d ", arr1[i]); i++; j++; } else if(arr1[i] > arr2[j]) { j++; } else { i++; } } Output:

Given a sorted array of size N, efficiently find if there are 2 numbers summing up to NUM. int i,j,arr[N] = {1,4,7,8,12,19,22}; //N=7, NUM = 16 for(i=0,j=N-1; i<j; ){ if(arr[i] + arr[j] == NUM){ printf("%d + %d = %d\n",arr[i],arr[j], NUM); return 0; }else if(arr[i] + arr[j] > NUM){ j--; }else{ i++; } } printf("There are no such numbers..."); Output: = 16

Medium to Hard questions Given an array of size N (0..N-1), filled with values between 0..N, we have one number that is missing in the array. Find the missing number. int i, arr[N] = {1,6,2,5,0,3,7}; //N=7, values=0..7, missing "4" int count[N+1] = {0}; for(i=0;i<N;i++){ count[ arr[i] ] = 1; } for( i=0 ; i<N+1 ; i++){ if(count[i]==0) printf("The missing number is %d\n",i); } Output: The missing number is 4 Version 1 Complicated…

int i,arr[N-1] = {1,6,2,5,0,3}; //N=7, missing "4" int count = 0; for( i=0 ; i<N-1 ; i++ ){ count += arr[i]; } printf("The missing number is: %d\n",(N-1)*N/2-count); Output: The missing number is 4 Version 2 The tricky one Given an array of size N (0..N-1), filled with values between 0..N, we have one number that is missing in the array. Find the missing number.

Given an array of size N with random values which might be negative, find the sum of a maximal sequence. int i,arr[N] = {7, -3, -8, 5, -1, 10, -19, 6, 6, -4}; //N=10 int currSum=0, maxSum = arr[0]; for(i=0; i<N; i++ ){ currSum +=arr[i]; if(currSum > maxSum){ maxSum = currSum; } if(currSum < 0){ currSum = 0; } } printf(“Max sum = %d\n",maxSum); Output: Max sum = 14

Given an array of size N with values between 0..99, find the number that appears most frequently. int arr[N] = {0, 6, 81, 6, 92, 1, 34, 0, 37, 6}; //N=10 int count[100] = {0}; int i, maxIndex, maxCount=0; for (i=0; i<N; ++i) { count[ arr[i] ]++; } for (i=0; i<100; ++i) { if (count[i] > maxCount) { maxCount = count[i]; maxIndex = i; } printf("Max=%d appeared %d times\n", maxIndex, maxCount); Output: Max=6 appeared 3 times

Given 2 arrays of small letters of size N, we must decide if one is a permutation of another. char arr1[N] = {'a','b','c','d'}, arr2[N] = {'b','d','c','a'}; int count[26] = {0}, i; for(i=0 ; i < N ; i++) { count[ arr1[i] - 'a']++; count[ arr2[i] - 'a']--; } for (i=0 ; i<26 ; ++i){ if (count[i] != 0){ printf("Illegal permutation"); return 0; } printf("Legal permutation"); return 1; Output: Legal permutation

Given an array of size N (N is even), we need to find the minimum and the maximum values by using at most 3N/2 cells comparisons. int i,min,max,tmpMin,tmpMax, arr[N] = {7,3,2,5,8,3,4,9}; //N=8 min=max=arr[0]; for(i = 0; i < N-1 ; i+=2){ if(arr[i] > arr[i+1]){ tmpMin = arr[i+1]; tmpMax = arr[i]; }else{ tmpMin = arr[i]; tmpMax = arr[i+1]; } if(tmpMin < min) min = tmpMin; if(tmpMax > max) max = tmpMax; } printf("min = %d, max = %d",min,max); Output: min = 2, max = 9