c++3-26 - Sorting Given a partially filled array a of some...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Void func1(int a, double b, int c); Void func2(int & a, double& b, int& c); Void func3 (int a[a], double b[], intc[], int size); Void caller () { int x=10, y=20; double q=1.1; func1(x,y,static_cast<int(q); func2(x,y, static_cast<int>(q); error because pass by reference func2(1,1,7,3); int b1[0],b2[10]; double q1[10]; func1(b1[2], b2[4], static_cast q1(6); Two important applications: searching and sorting Searching: Given a (partially filled) array a[] of some base type and a key of that same base type, return the smallest index, k, such that a[k] == key. If no such index exists, return an invalid index (-1). Int search (string names[], int numNames, string who) For (int i=0; i<numNames; i++) If (names [i] ==who) Return I ; Return-1; }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
String list c7={ “jones”, “smith” } ; int nNames=30; int loc=search(list, nNames, “miller”);
Background image of page 2
Background image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Sorting Given a partially filled array a of some base type, rearrange the elements in a such that: A[0] &lt;= a[1] &lt;=&lt;=a[size-a\1] Remark -- many sorting algorithms Selection sort Insertion sort Bubble sort Mergesort Quicksort Quicksort Heapsort Tree sort Etc. Multiply dimensional arrays Two dimensions: table or spreadsheet Three dimensions Syntax: Type name[sz1][sz2]; Groups: Double scores [num_students][num_exams];- scores [3][2]- exam 3 score for 4 th student double averageExamScore(double scores(num_students][num_exams], int exam) { double sume = 0.0; for (int i=0; i&lt;num_students; i++) sum +=scores[i][exam]; return sum/num_students; } memory: arrays are stored in row major order doubly dimensioned arrays...
View Full Document

Page1 / 3

c++3-26 - Sorting Given a partially filled array a of some...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online