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;
}
Re: index position in array Issue
Quote:
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? :confused:
The comma is used as the parameter delimiter, not the semicolon!
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)
Re: index position in array Issue
Quote:
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.
Re: index position in array Issue
sorry about that, thats just a mistake i did on here, not on the actual program
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
Re: index position in array Issue
its looping until all the values go through it
Re: index position in array Issue
Quote:
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.
Re: index position in array Issue
Quote:
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?
Re: index position in array Issue
Quote:
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"?
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)
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?
Re: index position in array Issue
Quote:
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.