CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Dec 2014
    Posts
    6

    index position in array Issue

    my program takes the values from one array and searches for their index position in another array(linear search algorithm).
    this is an example of the issue im having(its not part of the actual code below)
    a[]={1,2,3,4,5,6}
    Arr[]={1,2,2,3,4,5}
    if it finds 1 in arr, it returns 0, which is fine but if it finds 2 in arr it return 1 and 1 instead of 1 and 2. any thoughts on how to fix this?
    Code:
    for (int q=0; q=size2;q++)
    {
      int rs=secfunc(array1;size1;array2[q])
      if(rs>=0)
      {
        cout<<rs << "\n";
      }
    }
    
    return 0;
    
    int secfunc(int arr[],int SIZE, int values)
    for (int a=0;a<SIZE;a++)
     {
      if(arr[a]==values)
      {
       return a;
      }
     }
    return -1;
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: index position in array Issue

    Quote Originally Posted by kopp00 View Post
    if it finds 1 in arr, it returns 0, which is fine but if it finds 2 in arr it return 1 and 1 instead of 1 and 2. any thoughts on how to fix this?
    Code:
    for (int q=0; q=size2;q++)
    {
      int rs=secfunc(array1;size1;array2[q])
      if(rs>=0)
      {
        cout<<rs << "\n";
      }
    }
    ...
    Does this code compile?
    The comma is used as the parameter delimiter, not the semicolon!
    Victor Nijegorodov

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: index position in array Issue

    You have:
    Code:
    for (int q=0; q=size2;q++)
    This sets q = size2 every time thru the loop (and the loop never terminates)

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: index position in array Issue

    Quote Originally Posted by kopp00 View Post
    my program takes the values from one array and searches for their index position in another array(linear search algorithm).
    this is an example of the issue im having(its not part of the actual code below)
    a[]={1,2,3,4,5,6}
    Arr[]={1,2,2,3,4,5}
    if it finds 1 in arr, it returns 0, which is fine but if it finds 2 in arr it return 1 and 1 instead of 1 and 2. any thoughts on how to fix this?
    What do you mean by that? A function can only return one value.

  5. #5
    Join Date
    Dec 2014
    Posts
    6

    Re: index position in array Issue

    sorry about that, thats just a mistake i did on here, not on the actual program

  6. #6
    Join Date
    Dec 2014
    Posts
    6

    Re: index position in array Issue

    yea it returns one value at a time since its looping, but the problem is everytime it finds 2, the index position is 1 both times. if it finds 3 in arr[] it returns a 3, 4 in arr[] returns 4, and 5 in arr[] returns a 5 which is correct except for repeated values

  7. #7
    Join Date
    Dec 2014
    Posts
    6

    Re: index position in array Issue

    its looping until all the values go through it

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: index position in array Issue

    Quote Originally Posted by kopp00 View Post
    yea it returns one value at a time since its looping, but the problem is everytime it finds 2, the index position is 1 both times. if it finds 3 in arr[] it returns a 3, 4 in arr[] returns 4, and 5 in arr[] returns a 5 which is correct except for repeated values
    That's what you're telling it to do. You're starting at 0 each time. Since it's not really clear to me exactly what you're trying to do, I'd recommend figuring out the algorithm before writing code. If it were me, I'd pass in an array to hold the indexes as an output parameter.

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: index position in array Issue

    Quote Originally Posted by kopp00 View Post
    yea it returns one value at a time since its looping, but the problem is everytime it finds 2, the index position is 1 both times. if it finds 3 in arr[] it returns a 3, 4 in arr[] returns 4, and 5 in arr[] returns a 5 which is correct except for repeated values
    You're trying to code from an unclear design. First design the program and then code from the design.

    What are the input(s), what are the required output(s) and how are the output(s) to be obtained from the input(s)?

    Will a[] and arr[] always be sorted ascending?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: index position in array Issue

    Quote Originally Posted by kopp00 View Post
    yea it returns one value at a time since its looping, but the problem is everytime it finds 2, the index position is 1 both times. if it finds 3 in arr[] it returns a 3, 4 in arr[] returns 4, and 5 in arr[] returns a 5 which is correct except for repeated values
    Who is looping???
    Your code from post #1 searches ONCE for each value in array2, always from the beginning of array1.
    BTW, which one of them is "a" and which "Arr"?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  11. #11
    Join Date
    Dec 2014
    Posts
    6

    Re: index position in array Issue

    This is the assignment. if anyone was confused. everything works except for the part in which it reads the first match and sends it back even if there are multiples of it
    For this problem, please implement a linear search algorithm that performs this function. You
    will be given two input files, “LSStandard.txt” and “LSTest.txt”. The LSStandard.txt
    file contains integer values against which we are searching. (There will be no more than 100 of
    these.) The LSTest.txt file contains a set of numbers that we are trying to locate within the
    standard data set. (There will be no more than 50 of these.) Read both of these into separate
    arrays and then determine which of the numbers in the LSTest file are included in the
    LSStandard data set by using a Linear Search algorithm. Have your program print out a report
    (to the console only is sufficient) that indicates whether the number was found or not.
    Your output should look something like:
    Number 1 ( 34) was located in position 15.
    Number 2 ( 74) was not in the file.
    Number 3 ( 56) was not in the file.
    Number 4 (103) was located in position 75.
    etc.
    Your function header for the Linear Search function should look like:
    int searchList(int stdList [], int numElems, int value)

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: index position in array Issue

    I don't think it's us that's confused. Your original post said you want to find all positions. The assignment says you only need to find the first occurrence, and it appears you're doing that.

    Okay. I'm confused. What's your question?

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: index position in array Issue

    Quote Originally Posted by GCDEF View Post
    I don't think it's us that's confused. Your original post said you want to find all positions. The assignment says you only need to find the first occurrence, and it appears you're doing that.

    Okay. I'm confused. What's your question?
    Me too! Perhaps it would be helpful if you posted your current complete code if you require further advice and guidance.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured