Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PASS Week 3/31 Solutions (MY SOLUTIONS)
- Problem #1
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- void loadArray(int[]);
- void printArray(int[]);
- int main(void){
- srand(time(NULL));
- int ary[100];
- loadArray(ary);
- printArray(ary);
- return 0;
- }
- void loadArray(int ary[]){
- for (int i = 0; i < 100; i++){
- ary[i] = rand() % 999 + 1;
- }
- return;
- }
- void printArray(int ary[]){
- for (int i = 0; i < 100; i++){
- if (i % 10 == 0){
- printf("\n");
- }
- printf("%3d ", ary[i]);
- }
- printf("\n");
- return;
- }
- Problem #2
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- void loadArray(int[]);
- void searchArray(int[]);
- void printArray(int[]);
- int main(void){
- srand(time(NULL));
- int ary[100];
- loadArray(ary);
- searchArray(ary);
- //printArray(ary);
- return 0;
- }
- void loadArray(int ary[]){
- for (int i = 0; i < 100; i++){
- ary[i] = rand() % 999 + 1;
- }
- return;
- }
- void searchArray(int ary[]){
- int search = 0;
- int counter = 0;
- printf("Enter a value to be searched for: ");
- scanf("%d", &search);
- for (int i = 0; i < 100; i++){
- if (ary[i] == search){
- printf("Number %d found at index %d.\n", search, i);
- counter++;
- }
- }
- if (counter == 0){
- printf("Number not found");
- }
- return;
- }
- void printArray(int ary[]){
- for (int i = 0; i < 100; i++){
- if (i % 10 == 0){
- printf("\n");
- }
- printf("%3d ", ary[i]);
- }
- printf("\n");
- return;
- }
- Problem #3 (Actually Problem #4, changed it cause original #3 was too hard)
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- void loadArray(int[]);
- void searchArray(int[]);
- void BubbleSort(int[],int);
- void printArray(int[]);
- int main(void){
- srand(time(NULL));
- int ary[100];
- loadArray(ary);
- printArray(ary);
- BubbleSort(ary, 99);
- printArray(ary);
- //searchArray(ary);
- return 0;
- }
- void loadArray(int ary[]){
- for (int i = 0; i < 100; i++){
- ary[i] = rand() % 999 + 1;
- }
- return;
- }
- void searchArray(int ary[]){
- int search = 0;
- int counter = 0;
- printf("Enter a value to be searched for: ");
- scanf("%d", &search);
- for (int i = 0; i < 100; i++){
- if (ary[i] == search){
- printf("Number %d found at index %d.\n", search, i);
- counter++;
- }
- }
- if (counter == 0){
- printf("Number not found\n");
- }
- return;
- }
- 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
- void printArray(int ary[]){
- for (int i = 0; i < 100; i++){
- if (i % 10 == 0){
- printf("\n");
- }
- printf("%3d ", ary[i]);
- }
- printf("\n");
- return;
- }
- Problem #4 (Actually Problem #5)
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- void loadArray(int[]);
- void searchArray(int[]);
- void BubbleSort(int[],int);
- int BinarySearch(int[], int, int);
- void printArray(int[]);
- int main(void){
- srand(time(NULL));
- int ary[100];
- int userInput = 0;
- loadArray(ary);
- printArray(ary);
- BubbleSort(ary, 99);
- printArray(ary);
- printf("Enter a value to be searched for: ");
- scanf("%d", &userInput);
- if (BinarySearch(ary, 99, userInput) != -1){
- printf("Number found at index %d\n", BinarySearch(ary,99,userInput));
- }
- else{
- printf("Number not found\n");
- }
- return 0;
- }
- void loadArray(int ary[]){
- for (int i = 0; i < 100; i++){
- ary[i] = rand() % 999 + 1;
- }
- return;
- }
- void searchArray(int ary[]){
- int search = 0;
- int counter = 0;
- printf("Enter a value to be searched for: ");
- scanf("%d", &search);
- for (int i = 0; i < 100; i++){
- if (ary[i] == search){
- printf("Number %d found at index %d.\n", search, i);
- counter++;
- }
- }
- if (counter == 0){
- printf("Number not found\n");
- }
- return;
- }
- 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
- 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
- void printArray(int ary[]){
- for (int i = 0; i < 100; i++){
- if (i % 10 == 0){
- printf("\n");
- }
- printf("%3d ", ary[i]);
- }
- printf("\n");
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement