-
December 9th, 2014, 10:12 PM
#1
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;
}
-
December 10th, 2014, 05:03 AM
#2
Re: index position in array Issue
Originally Posted by kopp00
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
-
December 10th, 2014, 06:37 AM
#3
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)
-
December 10th, 2014, 06:54 AM
#4
Re: index position in array Issue
Originally Posted by kopp00
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.
-
December 10th, 2014, 12:25 PM
#5
Re: index position in array Issue
sorry about that, thats just a mistake i did on here, not on the actual program
-
December 10th, 2014, 12:29 PM
#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
-
December 10th, 2014, 12:29 PM
#7
Re: index position in array Issue
its looping until all the values go through it
-
December 10th, 2014, 12:40 PM
#8
Re: index position in array Issue
Originally Posted by kopp00
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.
-
December 10th, 2014, 12:59 PM
#9
Re: index position in array Issue
Originally Posted by kopp00
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)
-
December 10th, 2014, 04:04 PM
#10
Re: index position in array Issue
Originally Posted by kopp00
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...
-
December 10th, 2014, 07:32 PM
#11
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)
-
December 10th, 2014, 09:50 PM
#12
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?
-
December 11th, 2014, 04:35 AM
#13
Re: index position in array Issue
Originally Posted by GCDEF
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|