Thread: Searching Arrays

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    8

    Searching Arrays

    Hello again!
    I'm still working hard on some exam practice preparation and I'm having some trouble searching an array in my program. I was able to search an array before with a linear search, so that is what I'm trying to put into this overall program, but things are just not simply working out. I've been confronted by a large number of errors and I cannot determine where the main cultprit of such output is. I'd be happy to post the errors if requested, but the problem seems to be lying somewhere in linearSearch or something to do with SIZE. In the original linear search code that I am basing this program off of, SIZE is defined, but would I need to do that if I already defined the array?
    The goal I'm working towards is prompting the user to imput a value and to search the array to find the value. If it is found, then the function will return the location of the value, but if not, it should return -1.

    Code:
    #include <stdio.h>#include <stddef.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    void display(int testArray[]);
    void randPop(int testArray[]);
    size_t linearSearch(const int testArray[], int key, size_t size);
    
    
    int main()
    {
        int min, i, position;
        int testArray [5] = {22, 3, 5, 707, 1};
        
        printf("Enter integer search key: ");
        int searchKey;
        scanf("%d", &searchKey);
        
        size_t index = linearSearch(testArray, searchKey);
        
        if (index != -1)
        {
            printf("Found value at index %d\n", index);
        }
        else
        {
            puts("Value not found.");
        }
        
        srand((unsigned int)time(NULL));
        
        display(testArray);
        randPop(testArray);    
        
        min = testArray[0];
        for(i = 1; i < 5; i++)
        {
            if(min > testArray[i])
            {
                min = testArray[i];
            }
        }
        printf("The smallest integer of the array is: %d\n", min);
        
        return 0;
    }
    
    
    void display(int testArray[])
    {
        for (size_t i = 0; i < 0; ++i) {
            printf("%7zu%13d\n", i, testArray[i]);
        }
    }
    
    
    void randPop(int testArray[])
    {
        for (size_t i = 0; i < 5; ++i) {
            testArray[i] = rand() % 100 + 1;
            printf("%7zu%13d\n", i, testArray[i]);
        }
    }
    
    
    size_t linearSearch(const int testArray[]; int key, size_t size)
    {
        for (size_t n = 0; n < size; ++n)
        {
            if (testArray[n] == key)
            {
                return n;
            }
        }
        return -1;
    }
    Thank you for all help in advance. Finals are such a stressful time! I'd like to be as prepared as possible, but it seems like this stress is making me lose my mind!
    Best!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You made a simple typo error, and then searched too hard for a complex error

    This:
    Code:
    size_t linearSearch(const int testArray[]; int key, size_t size)
    should have been:
    Code:
    size_t linearSearch(const int testArray[], int key, size_t size)
    Also, you forgot to provide the size when calling linearSearch.

    By the way, note that size_t is unsigned, so when you return -1 you're returning the largest size_t value, not the value of -1 itself.
    Last edited by laserlight; 04-29-2019 at 07:45 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    You have a ; after "const int testArray[]" instead of a comma

    [edit] Too fast for me laserlight! [/edit]
    Last edited by Click_here; 04-29-2019 at 07:45 PM.

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
     for (size_t i = 0; i < 0; ++i) {
    Have a closer look at the i<0 - Is that what you want?

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    8
    I wondered if it was going to be some typo error...
    Those always seem to happen at the worst of times!
    Thank you for finding it for me!
    I have fixed it and the errors have certainly narrowed.

    However, it still seems like there is still some problem with linearSearch, despite the fix. One of the errors says that there are too few arguments to function. Would you happen to know anything about this?

    Thanks again! I took a huge sigh of relief after that little fix so far!

    Best!

    EDIT:
    Whoops! I didn't refresh in time to see laserlight's edited post! It's fixed up now! Thanks again!
    Last edited by G707; 04-29-2019 at 07:58 PM.

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    8
    Actually, that is what I want.
    I know it looks odd, but I'm following a set of prompts my professor gave as a study guide.
    Thank you for looking out for it though!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching for a job
    By C_ntua in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 04-25-2010, 09:04 PM
  2. Searching Multiple Arrays?
    By Cell in forum C Programming
    Replies: 3
    Last Post: 03-19-2009, 03:45 PM
  3. Basic C++ question <searching char arrays>
    By Saggio in forum C++ Programming
    Replies: 11
    Last Post: 09-15-2006, 07:24 PM
  4. Searching and Scanning Arrays
    By apoc632 in forum C Programming
    Replies: 7
    Last Post: 11-28-2003, 03:28 PM
  5. Searching arrays for strings
    By Zaarin in forum C++ Programming
    Replies: 14
    Last Post: 09-03-2001, 06:13 PM

Tags for this Thread