0

I'm trying to make helpers work and for the life of me can't seem to convince it to do so.

/**
* helpers.c
*
* Computer Science 50
* Problem Set 3
*
* Helper functions for Problem Set 3.
*/

#include <cs50.h>

#include "helpers.h"
int i;

/**
* Returns true if value is in array of n values, else false.
*/
//Binary Search 
bool search(int value, int values[], int n)
{
    // TODO: implement a searching algorithm
    int MAX = n - 1;
    int MIN = 0;


    while (MIN <= MAX)
    {
        int MIDDLE = (MIN + MAX)/2;        

        if (value == array[MIDDLE])
            return true;
        else if (value > array[MIDDLE])
        {
            MIN = MIDDLE + 1;
        }
        else if (value < array[MIDDLE])
        {
            MAX = MIDDLE - 1;
        }
    }
        return false;
}

/**
* Sorts array of n values.
*/
void sort(int values[], int n)
{
    // TODO: implement an O(n^2) sorting algorithm - linear sort
    for (int i = 0; i < n, i++);
    {
        int index = values[i];
        int j = i;
        while (j > 0 && n[i-1] > index)
        {
            values[j] = values[j-1];
            j = j - 1;
            values[j] = index;        
        }
    }
    return;
}

The error code when I run it is as follows:

helpers.c:30:22: error: use of undeclared identifier 'array'
        if (value == array[MIDDLE])
                    ^
helpers.c:32:26: error: use of undeclared identifier 'array'
        else if (value > array[MIDDLE])
                        ^
helpers.c:36:26: error: use of undeclared identifier 'array'
        else if (value < array[MIDDLE])
                        ^
helpers.c:50:23: error: expression result unused [-Werror,-Wunused-value]
    for (int i = 0; i < n, i++);
                    ~ ^ ~
helpers.c:50:31: error: expected ';' in 'for' statement specifier
    for (int i = 0; i < n, i++);
                            ^
helpers.c:54:26: error: subscripted value is not an array, pointer, or vector
        while (j > 0 && n[i-1] > index)
                        ~^~~~
helpers.c:50:32: error: for loop has empty body [-Werror,-Wempty-body]
    for (int i = 0; i < n, i++);
                            ^
helpers.c:50:32: note: put the semicolon on a separate line to silence this warning
7 errors generated.

I've been scratching my head about this for a while and think I just need to be pointed in the right direction.

Thanks!

1 Answer 1

0

You need to read the errors and take them literally.

You have several errors in your search() function because you're using an array named array[]. You never declared an array called array in the function. You do have an array called values[] though, that is declared in the signature for the function itself, which you never use but should be using instead of array[].

The error with n[] is exactly what it says. N is a single integer, not an array, but you're trying to treat it as an array.

This may sound harsh, but you need to use more care in selecting variables and in how you use them. A lot of these problems resulted from trying to find the right element in an array, but not using the correct name, or using an index variable without using the name of the array and putting the index variable inside square brackets to select the correct element.

Other errors are purely syntax issues - commas where semicolons should be, semicolons where they shouldn't be, etc. You really need to review the structure and syntax of a for loop and of an array.

If this answers your questions, please click on the check mark to answer. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .