# Lecture15 - Array Example Searching Imagine being given an...

This preview shows pages 1–4. Sign up to view the full content.

Array Example: Searching Imagine being given an array of values, searching for a particular one. In an unsorted array, we can do this as follows: Go through each element one by one, comparing it to what we are searching for. If it's found, we're done. If we go all the way through the array and we don't find it, it's not there. Let's write a function to take care of this task. Here's the prototype: // Pre-conditions: length is the length of the array values. // Post-condition: The function returns 1 if val is stored in the // array values and 0 otherwise. int Search(int values[], int length, int val); Here is the code: int Search(int values[], int length, int val) { int index; for (index = 0; index<length; index++) if (values[index] == val) return 1; return 0; } Could you adjust this to return the number of occurences of val in the array values?

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

View Full Document
Array Example: Sorting Included below is a small program that illustrates an algorithm to sort an array of 20 integers. We will now develop this program, step by step. // Arup Guha // 10/15/01 // A simple sorting program #include <stdio.h> #include <time.h> #include <stdlib.h> #define SIZE 20 void Print_Array(int values[], int length); void Fill_Array(int values[], int length, int max); void swap(int values[], int i, int j); void Move_Max(int values[], int max_index); void Simple_Sort(int values[], int length); int main() { int my_vals[SIZE]; srand(time(NULL)); // Initalize the random number generator. // Initialize the array with random values and print these. Fill_Array(my_vals, SIZE, 100); printf("original array : "); Print_Array(my_vals, SIZE); // Sort this array and print the result. Simple_Sort(my_vals, SIZE); printf("sorted array : "); Print_Array(my_vals, SIZE); return 0; } // Pre-condtions: the second parameter is the length of the array // specified by the first parameter. // Post-conditions: The first parameter will be sorted from lowest to // highest values. void Simple_Sort(int values[], int length) { int i; // Find the largest value and put that in its correct location, // successively. for (i=length-1; i> 0; i--) { Move_Max(values, i); } }
// Pre-conditions: max_index is a valid index to the array values.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern