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

Info icon This 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?
Image of page 1

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

View Full Document Right 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); } }
Image of page 2
// Pre-conditions: max_index is a valid index to the array values.
Image of page 3

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern