CS 1430: Programming in C++.

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

Array. Convert Numbers in Different Base Systems Generate values to a series of numbers in different base systems: Base is between 2 and 9; Maximum number.
Sorting int s[20], size; size = 5; Original array Final array (in Ascending Order) Final array (in Descending Order)
1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
1 11/27/06CS150 Introduction to Computer Science 1 Searching Arrays.
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
CS 1400 March 30, 2007 Chapter 8 Searching and Sorting.
Searching Arrays Linear search Binary search small arrays
Administrative MUST GO TO CORRECT LAB SECTION! Homework due 11:59pm on Tuesday. 25 points off if late (up to 24 hours) Cannot submit after 11:59pm on Wednesday.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
1 CS 1430: Programming in C++. 2 IF Statement if (cond) statement //Next statement if (cond) { statement1 statement2 … } //Next statement.
Review Binary Numbers Bit : 0 or 1 Byte: 8 bites 256 different values 2 8 KB : 1024 bytes 2 10 bytes MB : 1024 * 1024 bytes 2 10 * 2 10 (2 20 ) bytes GB.
Selection Sorting S[] : array of int or float Size: number of elements of s[] Pseudocode for i = 0 to size - 2 find the index of a smallest element between.
Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
1 CS 1430: Programming in C++. 2 Find Max, Min, Average of m Sections Max, Min and Average of each section Max, Min and Average of all sections together.
1 CS 1430: Programming in C++. Quiz Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function.
1 CS 1430: Programming in C++. 2 C++ Loops Sentinel Controlled Count Controlled EOF Controlled.
CS 1430: Programming in C++ 1. Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s)
CS 1430: Programming in C++ Function Design 1. Good Functions Focusing on one thing Function name tells what it does sqrt(val) pow(base, exp) cin.eof()
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
CS 1430: Programming in C++ 1. Test 2 Friday Functions Arrays For Loops Understand Concepts and Rules Memorize Concepts and Rules Apply Concepts and Rules.
1 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function Definition float sqrt(float x) { // compute.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
CS 1430: Programming in C++.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
Arrays float Scores[9]; ? index: element // one dimensional array 1.
Reference and Value Parameters Reference Parameters (&) The formal parameter receives the address of the actual parameter, and the function can read and.
Nested Structures struct TDate { int year, month, day; }; struct StudentType { string id, firstName, lastName; float gpa; TDate DOB; }; struct SectionType.
Selection Sorting Pseudocode (Forward) for i = 0 to size - 2 find the index of the required element between s[i] and s[size - 1] If i not the same as index.
1 C++ Classes and Data Structures Course link…..
Searching Arrays Linear search Binary search small arrays
Data Structures and Algorithms
CS 1430: Programming in C++ No time to cover HiC.
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. cout >
CS 1430: Programming in C++.
CS 1430: Programming in C++.
CS 1430: Programming in C++.
Student Data Score First Name Last Name ID GPA DOB Phone ...
CS 1430: Programming in C++.
Proving algorithms (programs) correct with induction
CS 1430: Programming in C++.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT};
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 1430: Programming in C++ No time to cover HiC.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Working With Arrays.
CS 1430: Programming in C++ No time to cover HiC.
CS 1430: Programming in C++.
Search,Sort,Recursion.
Standard Input/Output Stream
CS 1430: Programming in C++.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 1430: Programming in C++.
Arrays Lecture 11.
CS150 Introduction to Computer Science 1
Search,Sort,Recursion.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Module 8 – Searching & Sorting Algorithms
CS150 Introduction to Computer Science 1
Class StudentList class StudentList { private: int numStudents;
CS 1430: Programming in C++ No time to cover HiC.
Selection Sorting S[] : array of int or float
Presentation transcript:

CS 1430: Programming in C++

Linear Search Is a value in the array? Compare index against array values Find the last lowest value: 1. for (I = 0; I < size; I ++) if (s[i] <= s[index]) index = I; 2. for (int I = size – 1; I >= 0; I --) if (s[i] < s[index])

. . . . void InputArray(float s[], int& size); int main() { int count; float myArray[50], target; InputArray(myArray, count); // Assume count is 8 // Is 51 in the array? // Is 60 in the array? cin >> target; // Is target in the array? // Linear search! return 0; } 40 45 51 44 59 50 ? . . . . 0 1 2 3 4 5 6 7 49

Linear Search Function Find Target in an Array The function returns true if target is found in the array, false otherwise. Function Prototype Function Name SearchArray FindTarget or Find Function Type bool (true/false) Function parameters floatArray[]: array of type float size : int, number of elements of floatArray target : a float number In, Out, InOut? bool SearchArray(const float floatArray[], int size, float target);

//------------------------------------------------------------------- // The function has three parameters: // floatArray[]: array of type float // size : int, number of elements of floatArray // target : a float number // The function returns true if target is found in floatArray, // false otherwise. // Parameters: (in, in, in) bool SearchArray(const float floatArray[], int size, float target) { for (int i = 0; i < size; i++) if (floatArray[i] == target) return true; else // ? } // When to return false? return false; 40 45 51 44 59 50 ? . . . . 0 1 2 3 4 5 6 7

//------------------------------------------------ // The function has three parameters: // floatArray[]: array of type float // size : int, number of elements of // floatArray // target : a float number // The function returns a bool value: // true if target is found in floatArray, // false otherwise. // Parameters: (in, in, in) bool SearchArray(const float floatArray[], int size, float target) { for (int i = 0; i < size; i++) if (floatArray[i] == target) return true; } return false;

//------------------------------------------------ // The function has three parameters: // floatArray[]: array of type float // size : int, number of elements of // floatArray // target : a float number // The function returns a bool value: // true if target is found in floatArray, // false otherwise. // Parameters: (in, in, in) bool SearchArray(const float floatArray[], int size, float target) { for (int i = 0; i < size; i++) if (i == target) return true; } return false; // Correct? // if (floatArray[i] == target)

void InputArray(float s[], int& size) bool SearchArray(const float floatArray[], int size, float target); int main() { float myArray[50], target; int count; InputArray(myArray, count); cin >> target; while (!cin.eof()) if (SearchArray(myArray, count, target)) cout << “Value “ << target << “ is in the array.”; else cout << “Value “ << target << “ is NOT in the array.”; cin >> target; } return 0; }

void InputArray(float s[], int& size); bool SearchArray(const float floatArray[], int size, float target); int main() { float myArray[50], target; int count; bool found; InputArray(myArray, count); cin >> target; while (!cin.eof()) found = SearchArray(myArray, count, target); if (found) cout << “Value ” << target << “ is in the array.”; else cout << “Value ” << target << “ is NOT in the array.”; cin >> target; } return 0; } // if (found == true)

Linear Search Is target in the array? Check array elements one at a time Until target is found or All array elements have been checked

Find the Largest Value of All Array Elements Linear Search Find the Largest Value of All Array Elements

Find the Largest Value of All Array Elements Function Prototype Function Name MaxArrayValue Function Type int (float) Same as the array type Function parameters s[] : array of int size : int, number of elements in s[] In, Out, InOut? // Parameters: (in, in) int MaxArrayValue(const int s[], int size);

//----------------------------------------------------------- // The function has two parameters: // s[]: array of int // size : int, number of elements in s[] // The function finds and returns the largest array element // of s[], assuming size is positive and in the range. // Parameters: (in, in) int MaxArrayValue(const int s[], int size) { int max; max = s[0]; for (int i = 1; i < size; i ++) if (s[i] > max) max = s[i]; return max; }

//----------------------------------------------------------- // The function has two parameters: // s[]: array of int // size : int, number of elements in s[] // The function finds and returns the largest array element // of s[], assuming size is positive and in the range. // Parameters: (in, in) int MaxArrayValue(const int s[], int size) { int max; max = s[0]; for (int i = 0; i < size; i ++) if (s[i] > max) max = s[i]; return max; } Correct? Good?

//----------------------------------------------------------- // The function has two parameters: // s[]: array of int // size : int, number of elements in s[] // The function finds and returns the largest array element // of s[], assuming size is positive and in the range. // Parameters: (in, in) int MaxArrayValue(const int s[], int size) { int max; max = 0; for (int i = 0; i < size; i ++) if (s[i] > max) max = s[i]; return max; } Correct? max = s[0];

//----------------------------------------------------------- // The function has two parameters: // s[]: array of int // size : int, number of elements in s[] // The function finds and returns the largest array element // of s[], assuming size is positive and in the range. // Parameters: (in, in) int MaxArrayValue(const int s[], int size) { int max; max = s[0]; for (int i = 1; i < size; i ++) if (i > max) max = s[i]; return max; } Correct? if (s[i] > max)

Schedule Quiz5-4 Due 10 PM, Wednesday Friday Quiz2 (10 points) Lab6

Any Questions about Grading? Prog3 Any Questions about Grading?

Optional Pairs Start Early! Prog4 Optional Pairs Start Early!