Last active
July 5, 2016 15:47
-
-
Save JamesCalleja/5cce19063b61dae3d4f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* helpers.c | |
* | |
* Computer Science 50 | |
* Problem Set 3 | |
* | |
* Helper functions for Problem Set 3. | |
*/ | |
#include <cs50.h> | |
#include <ctype.h> | |
#include <stdio.h> | |
#include "helpers.h" | |
/** | |
* Returns true if value is in array of n values, else false. | |
*/ | |
bool search(int value, int values[], int n) | |
{ | |
//return true if value is in the array values[]; value must be positive interger | |
if (isdigit(value) && value >= 0) | |
{ | |
for (int i = 0; i < n; i++) | |
{ | |
//printf("you're running if %d," i ) ; | |
if (value == values[i]) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
/** | |
* Sorts array of n values. | |
*/ | |
void sort(int values[], int n) | |
{ | |
// TODO: implement an O(n^2) sorting algorithm | |
int swapCount = 1; //set swapCount to enter while loop | |
while (swapCount != 0) | |
{ | |
swapCount = 0; //set to 0 and add 1 for each swap | |
for (int x = 0; x < n -1; x ++) | |
{ | |
int a = values[x]; //a hold the current value in the array | |
int b = values[x + 1]; //b holds the ajesent element to a | |
if (a > b) //if a is greater than b swap the values in the array | |
{ | |
values[x] = b; | |
values[x + 1] = a; | |
swapCount ++; //increment swapCount for another pass | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment