Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PROBLEM #1
- Create a program that will declare an array of 100 integers of values between 1 and
- 999. Create a user defined function that will load that array in the caller (main) with
- 100 random variables. Create a second function that will then be called to print the
- array to the screen 10 numbers per line for 10 lines. Use a minimum width specifier
- to ensure the numbers are ordered. The main function should ONLY CONTAIN the array
- declaration, two function calls, and a return statement. You must use srand() in
- main.
- /*** SKELETON CODE ***/
- // All of the problems build off one another, so only this one skeleton is needed
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define MAXSZ 100
- void LoadArray(int[], int);
- void PrintArray(int[], int);
- int main(void)
- {
- /*** LOCAL STATEMENTS AND DECLARATIONS GO HERE ***/
- return 0;
- }//end main
- /*** LOAD ARRAY FUNCTION GOES HERE ***/
- /*** PRINT ARRAY FUNCTION GOES HERE ***/
- Problem #2
- Using the program you created in problem #1, create a third function that will be
- called after the array is loaded and comment out the function call to print the array.
- The new function will prompt the user for an integer value between 1 and 999. Then
- the function will use a sequential search to search the loaded array.
- If the value is found, print out to the user the index location of the item and continue to search to
- the end of the array. (There may be more than one occurrence of the same value in
- the array). What about duplicate values?
- Problem #3
- Add yet another function to the program. This function will sort the array. I am
- asking you to use the bubblesort algorithm found in the Canvas material.
- Now change main to load the array, print the array, sort the array, and then print out the
- array again using your functions. Did it sort all the items? You should see the
- contents out of order and then you should see them in order. How could you change
- it from ascending to descending order? Walk through this function logically using an
- unsorted array of your own creation to understand it. Use a paper and pencil if need
- be.
- Here is the code for Bubblesort to add to your program:
- void BubbleSort(int aryIn[],int last)
- {
- int temp = 0;
- for(int current = 0; current < last; current++)
- {
- for(int walker = last; walker > current; walker--)
- {
- if(aryIn[walker] < aryIn[walker -1])
- {
- temp = aryIn[walker];
- aryIn[walker] = aryIn[walker - 1];
- aryIn[walker - 1] = temp;
- }//end if
- }//end inner for
- }//end outer for
- return;
- }//end bubbleSort
- Problem #4
- Add a new search function to your program that is a binary search. In main load the
- array, sort the array, then search the array (You can comment out your selection sort).
- Your function should return the index location of the item found.
- Recall that the item could be at index 0, so your function needs to check that.
- You will need to alter how you test the results in main. Walk through
- this function logically using a sorted array of your own creation to
- understand it. Use a paper and pencil if need be.
- Here is the function for the Binary Search to add to your program:
- //This function only works on sorted arrays!
- int BinarySearch(int list[], int end, int target)
- {
- int location = 0, first = 0, mid, last = end;
- while(first <= last)
- {
- mid = (first + last) /2;//Find the middle
- if(target > list[mid])
- first = mid + 1; //Look in the upper half of list
- else if (target < list[mid])
- last = mid - 1; //Look in the lower half of list
- else
- first = last + 1;//Found the item force quit
- }//end while
- if(list[mid] == target)//What if item is at index 0?
- return mid;
- else
- return -1;
- }//end BinarySearch
- { SECTION 1 CHALLENGE PROBLEM }
- Using the program from problem #2, modify the load array function to not insert
- repeat values. Modify the SequentialSearch() function to take an added parameter
- as an integer and take the user prompts out and move them to main. Now you can call
- your search function in your load array function to examine each random value for a
- match in the array before adding the new element.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement