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

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

View Full Document Right Arrow Icon
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?
Background image of page 1

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

View Full DocumentRight Arrow Icon
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); } }
Background image of page 2
// Pre-conditions: max_index is a valid index to the array values. // Post-condition: The largest value in the array stored in between
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/25/2010 for the course COMPUTER S COP3223 taught by Professor Lobo during the Spring '10 term at University of Central Florida.

Page1 / 9

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

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

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