Skip to main content
added 1185 characters in body
Source Link

EDIT:

Code for Binary Search

bool search(int value, int values[], int n)
{
    // TODO: implement a searching algorithm i.e. Binary Search
    if (n == 1 && values[0] == value)
    {
        return true;
    }
    
    while (n > 1)
    {
        int middle = round(n / 2); 

        if (value == values[middle])
        {
            return true;
        }    
        else if (value < values[middle])
        {
            for(int p = middle - 1; p > 0; p--)
            {
                if (value == values[p])
                {
                    return true;
                }
            }
        }
        else if (value > values[middle])
        {
            for(int p = middle + 1; p <= n; p++)
            {
                if (value == values[p])
                {
                    return true;
                }
                                
            }
        }
        else
            return false;
        
    }
    return false;
}

EDIT:

Code for Binary Search

bool search(int value, int values[], int n)
{
    // TODO: implement a searching algorithm i.e. Binary Search
    if (n == 1 && values[0] == value)
    {
        return true;
    }
    
    while (n > 1)
    {
        int middle = round(n / 2); 

        if (value == values[middle])
        {
            return true;
        }    
        else if (value < values[middle])
        {
            for(int p = middle - 1; p > 0; p--)
            {
                if (value == values[p])
                {
                    return true;
                }
            }
        }
        else if (value > values[middle])
        {
            for(int p = middle + 1; p <= n; p++)
            {
                if (value == values[p])
                {
                    return true;
                }
                                
            }
        }
        else
            return false;
        
    }
    return false;
}
deleted 1209 characters in body
Source Link
ChrisG
  • 7.4k
  • 2
  • 17
  • 26
/**
 * helpers.c
 *
 * Computer Science 50
 * Problem Set 3
 *
 * Helper functions for Problem Set 3.
 */
       
#include <cs50.h>

#include "helpers.h"

/**
 * Returns true if value is in array of n values, else false.
 */
bool search(int value, int values[], int n)
{
    // TODO: implement a searching algorithm i.e. Binary Search
    if (n == 1 && values[0] == value)
    return true;
    
    while (n > 1)
    {
        int middle = n/2; 

        if (value == values[middle])
            return true;
        else if (value < values[middle])
        {
            for(int p = middle - 1; p > 0; p--)
            {
                if (value == values[p])
                {
                    return true;
                }
            }
        }
        else if (value > values[middle])
        {
            for(int p = middle + 1; p <= n; p++)
            {
                if (value == values[p])
                {
                    return true;
                }
            }
        }
    }
    return false;
}

/**
 * Sorts array of n values.
 */
void sort(int values[], int n)
{
    // TODO: implement an O(n^2) sorting algorithm i.e. Selection Sort
    int temp;
    for (int i = 0; i < n - 1; i++)
    {
        int min = values[i];
        
        for (int j = i + 1; j < n; i++)
        {
            if(values[j] < values[min])
            min = values[j];
        
            if (min != values[i])
            {
                temp = values[min];
                values[min] = values[i];
                values[i] = temp;
            }
        }
        
    }
    
}

[code removed to comply to academic honesty]

/**
 * Sorts array of n values.
 */
void sort(int values[], int n)
{
    // TODO: implement an O(n^2) sorting algorithm i.e. Selection Sort
    int temp;
    for (int i = 0; i < n - 1; i++)
    {
        int min = values[i];
        
        for (int j = i + 1; j < n; i++)
        {
            if(values[j] < values[min])
            min = values[j];
        
            if (min != values[i])
            {
                temp = values[min];
                values[min] = values[i];
                values[i] = temp;
            }
        }
        
    }
    
}

I'm unable to figure out that why is it giving me segmentation fault error when everwhenever I run it. What is causing this bug?

/**
 * helpers.c
 *
 * Computer Science 50
 * Problem Set 3
 *
 * Helper functions for Problem Set 3.
 */
       
#include <cs50.h>

#include "helpers.h"

/**
 * Returns true if value is in array of n values, else false.
 */
bool search(int value, int values[], int n)
{
    // TODO: implement a searching algorithm i.e. Binary Search
    if (n == 1 && values[0] == value)
    return true;
    
    while (n > 1)
    {
        int middle = n/2; 

        if (value == values[middle])
            return true;
        else if (value < values[middle])
        {
            for(int p = middle - 1; p > 0; p--)
            {
                if (value == values[p])
                {
                    return true;
                }
            }
        }
        else if (value > values[middle])
        {
            for(int p = middle + 1; p <= n; p++)
            {
                if (value == values[p])
                {
                    return true;
                }
            }
        }
    }
    return false;
}

/**
 * Sorts array of n values.
 */
void sort(int values[], int n)
{
    // TODO: implement an O(n^2) sorting algorithm i.e. Selection Sort
    int temp;
    for (int i = 0; i < n - 1; i++)
    {
        int min = values[i];
        
        for (int j = i + 1; j < n; i++)
        {
            if(values[j] < values[min])
            min = values[j];
        
            if (min != values[i])
            {
                temp = values[min];
                values[min] = values[i];
                values[i] = temp;
            }
        }
        
    }
    
}

I'm unable to figure out that why is it giving me segmentation fault error when ever I run it. What is causing this bug?

[code removed to comply to academic honesty]

/**
 * Sorts array of n values.
 */
void sort(int values[], int n)
{
    // TODO: implement an O(n^2) sorting algorithm i.e. Selection Sort
    int temp;
    for (int i = 0; i < n - 1; i++)
    {
        int min = values[i];
        
        for (int j = i + 1; j < n; i++)
        {
            if(values[j] < values[min])
            min = values[j];
        
            if (min != values[i])
            {
                temp = values[min];
                values[min] = values[i];
                values[i] = temp;
            }
        }
        
    }
    
}

I'm unable to figure out that why is it giving me segmentation fault error whenever I run it. What is causing this bug?

deleted 36 characters in body
Source Link
/**
 * helpers.c
 *
 * Computer Science 50
 * Problem Set 3
 *
 * Helper functions for Problem Set 3.
 */
       
#include <cs50.h>
#include <math.h>
#include "helpers.h"

/**
 * Returns true if value is in array of n values, else false.
 */
bool search(int value, int values[], int n)
{
    // TODO: implement a searching algorithm i.e. Binary Search
    if (n == 1 && values[0] == value)
    return true;
    
    while (n > 1)
    {
        int middle = round(n/2);2; 

        if (value == values[middle]) 
            return true;
        else if (value < values[middle])
        {
            for(int p = 0;middle - 1; p <> middle;0; p++p--)
            {
                if (value == values[p])
                {
                    return true;
                }
            }
        }
        else if (value > values[middle])
        {
            for(int p = middle + 1; p <= n; p++)
            {
                if (value == values[p])
                {
                    return true;
                }
            }
        }
    }
    return false;
}

/**
 * Sorts array of n values.
 */
void sort(int values[], int n)
{
    // TODO: implement an O(n^2) sorting algorithm i.e. Selection Sort
    int temp;
    for (int i = 0; i < n - 1; i++)
    {
        int min = values[i];
        //int temp = 0;
        for (int j = i + 1; j < n; i++)
        {
            if(values[j] < values[min])
            min = values[j];
        
            if (min != values[i])
            {
                temp = values[min];
                values[min] = values[i];
                values[i] = temp;
            }
        }
        
    }
    
}
/**
 * helpers.c
 *
 * Computer Science 50
 * Problem Set 3
 *
 * Helper functions for Problem Set 3.
 */
       
#include <cs50.h>
#include <math.h>
#include "helpers.h"

/**
 * Returns true if value is in array of n values, else false.
 */
bool search(int value, int values[], int n)
{
    // TODO: implement a searching algorithm i.e. Binary Search
    if (n == 1 && values[0] == value)
    return true;
    
    while (n > 1)
    {
        int middle = round(n/2); 

        if (value == values[middle]) 
            return true;
        else if (value < values[middle])
        {
            for(int p = 0; p < middle; p++)
            {
                if (value == values[p])
                {
                    return true;
                }
            }
        }
        else if (value > values[middle])
        {
            for(int p = middle + 1; p <= n; p++)
            {
                if (value == values[p])
                {
                    return true;
                }
            }
        }
    }
    return false;
}

/**
 * Sorts array of n values.
 */
void sort(int values[], int n)
{
    // TODO: implement an O(n^2) sorting algorithm i.e. Selection Sort
    int temp;
    for (int i = 0; i < n - 1; i++)
    {
        int min = values[i];
        //int temp = 0;
        for (int j = i + 1; j < n; i++)
        {
            if(values[j] < values[min])
            min = values[j];
        
            if (min != values[i])
            {
                temp = values[min];
                values[min] = values[i];
                values[i] = temp;
            }
        }
        
    }
    
}
/**
 * helpers.c
 *
 * Computer Science 50
 * Problem Set 3
 *
 * Helper functions for Problem Set 3.
 */
       
#include <cs50.h>

#include "helpers.h"

/**
 * Returns true if value is in array of n values, else false.
 */
bool search(int value, int values[], int n)
{
    // TODO: implement a searching algorithm i.e. Binary Search
    if (n == 1 && values[0] == value)
    return true;
    
    while (n > 1)
    {
        int middle = n/2; 

        if (value == values[middle])
            return true;
        else if (value < values[middle])
        {
            for(int p = middle - 1; p > 0; p--)
            {
                if (value == values[p])
                {
                    return true;
                }
            }
        }
        else if (value > values[middle])
        {
            for(int p = middle + 1; p <= n; p++)
            {
                if (value == values[p])
                {
                    return true;
                }
            }
        }
    }
    return false;
}

/**
 * Sorts array of n values.
 */
void sort(int values[], int n)
{
    // TODO: implement an O(n^2) sorting algorithm i.e. Selection Sort
    int temp;
    for (int i = 0; i < n - 1; i++)
    {
        int min = values[i];
        
        for (int j = i + 1; j < n; i++)
        {
            if(values[j] < values[min])
            min = values[j];
        
            if (min != values[i])
            {
                temp = values[min];
                values[min] = values[i];
                values[i] = temp;
            }
        }
        
    }
    
}
Source Link
Loading