Problem: Given an array of positive integers, find a pair of integers in the array that adds up to 10 (or indicate none exist). Your code should be as.

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
PHYS707: Special Topics C++ Lectures Lecture 2. Summary of Today’s lecture: 1.More data types 2.Flow control (if-else, while, do-while, for) 3.Brief introduction.
Execute Blocks of Code Multiple Times Telerik Software Academy C# Fundamentals – Part 1.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
An introduction to arrays
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Stacks.
CS 280 Data Structures Professor John Peterson. Example: log(N) This is where things get hairy! How would you compute Log 10 (N) in a very approximate.
CS 1400 March 21, 2007 Odds and Ends Review. Reading to the end of a file Example test.txt : … Suppose we need.
Multiple-Subscripted Array
Chapter 2. Complexity Space Complexity Space Complexity Instruction Space Instruction Space Data Space Data Space Enviroment Space Enviroment Space Time.
Searching Arrays Linear search Binary search small arrays
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
24/3/00SEM107 - © Kamin & ReddyClass 16 - Searching - 1 Class 16 - Searching r Linear search r Binary search r Binary search trees.
Starting Out with C++, 3 rd Edition 1 Searching an Arrays.
Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
Copyright © 2002, Department of Systems and Computer Engineering, Carleton University CONTROL STRUCTURES Simple If: if (boolean exp) { statements.
Searching Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index.
CS 162 Intro to Programming II Bubble Sort 1. Compare adjacent elements. If the first is greater than the second, swap them. Do this for each pair of.
21/3/00SEM107 - © Kamin & ReddyClass 14 - Sorting - 1 Class 14 - Review: Sorting & searching r What are sorting and searching? r Simple sorting algorithms.
More Array Access Examples Here is an example showing array access logic: const int MAXSTUDENTS = 100; int Test[MAXSTUDENTS]; int numStudents = 0;... //
An introduction to arrays, continued. Recall from last time… public static void main ( String args[] ) { //define number of rooms final int N = 100; //define.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
CS 1430: Programming in C++ 1. Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s)
A: A: double “4” A: “34” 4.
Vectors Jordi Cortadella Department of Computer Science.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
1 A Pointer: an address, a reference, a location of the computer memory A pointer of what? int, char, bool, double, or any kind of data type need to know.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Array Sort. Sort Pass 1 Sort Pass 2 Sort Pass 3.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Midterm 2 Review Notes on the CS 5 midterm Take-home exam due by 5:00 pm Sunday evening (11/14) Hand in your solutions under the door of my office, Olin.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
Chapter five exercises. a. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false.
T/F  The following code will compile without error. int x = 3, y = 4, z = 5; double k = 3.4; cout
Searching Arrays Linear search Binary search small arrays
Example 21 #include<iostream.h> int main() { char Letter = 0;
Andy Wang Object Oriented Programming in C++ COP 3330
C++ Lesson 1.
Chapter 2 Algorithm Analysis
while Repetition Structure
Section 3 Review Mr. Crone.
The Sequential Search (Linear Search)
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. cout >
Programming fundamentals 2 Chapter 1:Array
Andy Wang Object Oriented Programming in C++ COP 3330
Specifications What? Not how!.
לולאות קרן כליף.
null, true, and false are also reserved.
البرمجة بلغة الفيجول بيسك ستوديو
CS 1430: Programming in C++ No time to cover HiC.
Selection CSCE 121 J. Michael Moore.
Working With Arrays.
If Statements.
CS150 Introduction to Computer Science 1
Jordi Cortadella Department of Computer Science
CS150 Introduction to Computer Science 1
Boolean Variables & Values
CS150 Introduction to Computer Science 1
The Sequential Search (Linear Search)
The Sequential Search (Linear Search)
Presentation transcript:

Problem: Given an array of positive integers, find a pair of integers in the array that adds up to 10 (or indicate none exist). Your code should be as efficient as possible.

Solution 1 for (i=0, i<size, i++) { for (j=0, j<size, j++) { if (a[i]+a[j]==10) cout<< a[i]<<"plus"<< a[j[<<“=10"; else cout << “NO such pair exists” } }

Solution 2 int count = 0; for (i=0, i<size, i++) { for (j=0, j<size, j++) { if (i!=j) { if (a[i]+a[j]==10) { cout<< a[i]<<"plus"<< a[j[<<“=10"; count++; } } } } if (count == 0) cout<<"no such pair exists";

Solution 3 Boolean foundPair = false; for (i=0, i<size, i++) { for (j=i+1, j<size, j++) { if (a[i]+a[j]==10) { cout<< a[i]<<"plus"<< a[j[<<“=10"; foundPair = true; break; } } } if (!foundPair) cout<<"no such pair exists";

Solution 4 void main () { const int SIZE = 15; const int SUM = 10; bool foundPair = false; int a[SIZE]; bool see[SUM+1] = {false}; for (int i=0; i < SIZE && !foundPair; i++) { if (see[SUM-a[i]]) { cout << a[i] << " " << SUM-a[i] << "Exist in array and add to " << SUM << "\n" ; foundPair = true; } see[a[i]] = true; } if (! foundPair) cout << " Sum not found"; }