Searching in array then copying contents to another array

edit
Last edited on
 
if (flights[i].get_orig() = orig && flights[i].get_dest() = dest)


should be:
 
if (flights[i].get_orig() == orig && flights[i].get_dest() == dest)


And the same goes for the other comparison.
Thanks kbw.
I'm currently getting these errors for the first function. I'm also getting errors for the second function as well but I won't post those because that function is very incomplete.

flightUtil.cpp: In function 'int searchFlights(int*, Flight*, int, const std::st ring&, const std::string&)':
flightUtil.cpp:93: error: request for member 'set_orig' in '*(((int*)(((long uns igned int)j) * 4ul)) + flightIdx)', which is of non-class type 'int'
flightUtil.cpp:94: error: request for member 'set_dest' in '*(((int*)(((long uns igned int)j) * 4ul)) + flightIdx)', which is of non-class type 'int'
flightIdx is an array of int, not Flights.
Okay I see that. Maybe I misinterpreted what the function is for. Do you think it just wants where in the flights the flight is located?

stores indices of matching flights in 'flightIdx'

Not really sure what this means.
The first function is searching the list of all flights (flights) for those that match orig and dest and place the results in something that you've called flightIdx, which you've declared to be an array of ints.

We'll it may be more helpful if you called it matchedFlights, but more importantly, it's an array of flights.

You have the assignment the wrong way arround.

And finally, a design error. You don't tell the called how many flights actually matched and were placed in the first parameter. As you're not using the return value, you could return that value from the function.

So the code looks something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
///////////////////////////////////////
// 'searchFlights' searches for flights matching 'orig' and 'dest',
// stores indices of matching flights in 'flightIdx'
// return number of matching flights

int searchFlights(
    Flights matchedFlights[],
    Flight flights[], int nFlights,
    const string & orig, const string & dest)
{
	int j = 0;
	for (int i=0; i<nFlights; i++) 
	{
		if  (flights[i].get_orig() = orig && flights[i].get_dest() = dest)
		{
			matchedFlights[j] = flights[i];
			j++;
		}
	}

	return j;
}
Thanks kbw, that was helpful. I didn't write the function definition so it's supposed to be an array of ints, I guess. I think the comment could of definitely been clearer.
In which case, maybe flightsJdx has the indices where a match is found. It's impossible to tell without more information. Seeing how the function is meant to be used might settle the question of the specification.


edit
Last edited on
Ok, it as I thought. Where a match is found, it's added to flightIdx. The function returns the number of items added to the flighIdx (your counter j).
Kbw, are you saying your modifications were right?
Thanks

Edit: I think I'm beginning to understand it more. The way I see it, if the 'orig' and 'dest' match, then the index of the location in the array will be stored in flightIdx.
Last edited on
Topic archived. No new replies allowed.