Skip to content

Instantly share code, notes, and snippets.

@OxiBo
Created July 28, 2017 05:02
Show Gist options
  • Save OxiBo/275d9d67d7cacd956737646d0b8134a2 to your computer and use it in GitHub Desktop.
Save OxiBo/275d9d67d7cacd956737646d0b8134a2 to your computer and use it in GitHub Desktop.
CS50 pset3 find - other version
/**
* cs50 pset3 - find
* Prompts user for as many as MAX values until EOF is reached,
* then proceeds to search that "haystack" of values for given needle.
*
* Usage: ./find needle
*
* where needle is the value to find in a haystack of values
*/
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include "helpers.h"
// maximum amount of hay
const int MAX = 65536;
int main(int argc, string argv[])
{
// ensure proper usage
if (argc != 2)
{
printf("Usage: ./find needle\n");
return -1;
}
// remember needle
int needle = atoi(argv[1]);
// fill haystack
int size;
int haystack[MAX];
for (size = 0; size < MAX; size++)
{
// wait for hay until EOF
printf("\nhaystack[%i] = ", size);
int straw = get_int();
if (straw == INT_MAX)
{
break;
}
// add hay to stack
haystack[size] = straw;
}
printf("\n");
// sort the haystack
sort(haystack, size);
// try to find needle in haystack
if (search(needle, haystack, size))
{
printf("\nFound needle in haystack!\n\n");
return 0;
}
else
{
printf("\nDidn't find needle in haystack.\n\n");
return 1;
}
}
/**
* generate.c
*
* Generates pseudorandom numbers in [0,MAX), one per line.
*
* Usage: generate n [s]
*
* where n is number of pseudorandom numbers to print
* and s is an optional seed
*/
#define _XOPEN_SOURCE
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// upper limit on range of integers that can be generated
#define LIMIT 65536
int main(int argc, string argv[])
{
// make sure user enters at least one commend-line argument but not more than two
if (argc != 2 && argc != 3)
{
printf("Usage: ./generate n [s]\n");
return 1;
}
// convert commend-line argument string (argv[1]) into an integer
int n = atoi(argv[1]);
// "seed" drand48 calling srand48 with an argument if it was given by user, otherwise call drand48 without seeding it
if (argc == 3)
{
srand48((long) atoi(argv[2]));
}
else
{
srand48((long) time(NULL));
}
// print pseudorandom integers within the given LIMIT
for (int i = 0; i < n; i++)
{
printf("%i\n", (int) (drand48() * LIMIT));
}
// success
return 0;
}
/**
* helpers.c
*
* Helper functions for Problem Set 3.
*/
#include <cs50.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)
{
//check if n is positive integer
if (n<0)
{
return false;
}
else
{
int left, mid, right;
//assign indexes to the left and rignt points of the search
left=0;
right=n-1;
//search for the value using "while loop" while left less than or quals to right
while (left<=right)
{
mid=(left+right)/2;
//check if the value of the mid point of the respective "half" of array equals the searched value
if (value<values[mid])
{
right=mid-1;
}
else if (value>values[mid])
{
left=mid+1;
}
else if (value==values[mid])
{
return true;
break;
}
}
return false;
}
}
/**
* Sorts array of n values.
*/
void sort(int values[], int n)
{
// implement an O(n^2) sorting algorithm (selection sort)
//iterate over pseudorandom integers
for(int i=0; i<n; i++)
{
//iterate over pseudorandom integers to find the smallest integer
for(int j=i+1; j<n; j++)
{
if(values[i]>values[j])
{
int swap=values[i];
//assign i-th element of array next smallest integer foucnd
values[i]=values[j];
//swap values [i] and [j]
values[j]=swap;
}
}
}
return;
}
/**
* helpers.h
*
* Helper functions for Problem Set 3.
*/
#include <cs50.h>
/**
* Returns true if value is in array of n values, else false.
*/
bool search(int value, int values[], int n);
/**
* Sorts array of n values.
*/
void sort(int values[], int n);
#
# Makefile
# CS50
#
all: find generate
find: find.c helpers.c helpers.h
clang -ggdb3 -O0 -std=c11 -Wall -Werror -o find find.c helpers.c -lcs50 -lm
generate: generate.c
clang -ggdb3 -O0 -std=c11 -Wall -Werror -o generate generate.c
clean:
rm -f *.o a.out core find generate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment