C++ syntax help

Thread Starter

arduinolego611

Joined Jan 23, 2022
75
I'm not following this logic at all. In general, there will be many paths from MCO to LAX. You only want to count the number of flights on the route with the fewest number.

Code:
for (i = 0; i < ??; i++)
if airports [j] == flights [i] [0]
airports [j]++
then I have to repeat, but switch something around so that it makes different results each time

again, not correct syntax, obviously, omitted variables and such...but along these lines generally I hope

am I at least getting closer?
 
Last edited:

MrChips

Joined Oct 2, 2009
34,809
The way to tackle a complex programming problem is not by writing code.
You start by creating a working algorithm on paper and pencil by drawing a flowchart or writing pseudo code.
Writing computer code comes after you have a working algorithm.
 

WBahn

Joined Mar 31, 2012
32,828
Code:
for (i = 0; i < ??; i++)
if airports [j] == flights [i] [0]
airports [j]++
then I have to repeat, but switch something around so that it makes different results each time

again, not correct syntax, obviously, omitted variables and such...but along these lines generally I hope

am I at least getting closer?
Go back to that example I gave you and try to figure out how you could determine, on a single pass through the table, which cities you can reach F in a single flight. Update the distance list with just that information. For instance, you can see that you can get from C to F. So the distance from C to F is 1 instead of infinity.

Now, the next time you go through the table, if you can find a city from which you can get to C in one flight, you know that you can get to F in two flights because the distance from C to F is known to be 1.

Unless you can do this (or whatever process you choose) by hand, you will have little hope of somehow writing a program to do it correctly.
 

Thread Starter

arduinolego611

Joined Jan 23, 2022
75
Go back to that example I gave you and try to figure out how you could determine, on a single pass through the table, which cities you can reach F in a single flight. Update the distance list with just that information. For instance, you can see that you can get from C to F. So the distance from C to F is 1 instead of infinity.

Now, the next time you go through the table, if you can find a city from which you can get to C in one flight, you know that you can get to F in two flights because the distance from C to F is known to be 1.

Unless you can do this (or whatever process you choose) by hand, you will have little hope of somehow writing a program to do it correctly.
I was going to say count++ + 1
every time we go through the list, add an extra 1

not count++ but count++ + 1 when there is a match

each count belongs to each specific airport, instead of one entity

then find B - F or MCO - LAX



(I also wanted to add that I realized I forgot to comment out the "airports" array when I posted my original code. There were 15 elements, all airports / strings...they didn't belong there. the only array I had an intention of dealing with in the program was the flights themselves. im not sure if this was confusing for you guys or not, I think it looks like I was iterating through "airporrts" and not "flights".)
 
Last edited:

WBahn

Joined Mar 31, 2012
32,828
I was going to say count++ + 1
every time we go through the list, add an extra 1

not count++ but count++ + 1 when there is a match

each count belongs to each specific airport, instead of one entity

then find B - F or MCO - LAX



(I also wanted to add that I realized I forgot to comment out the "airports" array when I posted my original code. There were 15 elements, all airports / strings...they didn't belong there. the only array I had an intention of dealing with in the program was the flights themselves. im not sure if this was confusing for you guys or not, I think it looks like I was iterating through "airporrts" and not "flights".)
What information does the variable 'count' contain?

If count is 5, what does that value of 5 tell me?
 

Thread Starter

arduinolego611

Joined Jan 23, 2022
75
What information does the variable 'count' contain?

If count is 5, what does that value of 5 tell me?
so for now, since im rewriting the whole program, count is just an imaginary variable. in the real program, each airport will have their own "count", and each of those variables will have their own name.

if count is 5, then you know there are four matches that came before you could link one specific air port to F (or LAX) from B (or MCO), and then the one more "count" for counting that last match that finally completes the path from B - F (or MCO to LAX)

basically, if count is 5, you know it takes 5 flights to link B to F or MCO atond LAX or whatever source / destination you want to calculate for.

its incrementing every time a match is found and adding another + 1 to each match each time it starts iterating through "flights" from the beginning
 
Last edited:

WBahn

Joined Mar 31, 2012
32,828
so for now, since im rewriting the whole program, count is just an imaginary variable. in the real program, each airport will have their own "count", and each of those variables will have their own name.

if count is 5, then you know there are four matches that came before you could link one specific air port to F (or LAX) from B (or MCO), and then the one more "count" for counting that last match that finally completes the path from B - F (or MCO to LAX)

basically, if count is 5, you know it takes 5 flights to link B to F or MCO atond LAX or whatever source / destination you want to calculate for.

its incrementing every time a match is found and adding another + 1 to each match each time it starts iterating through "flights" from the beginning
As I explained before, that doesn't do what you expect it to.

Let's say that there are 25 airports that have flights going to LAX. If you increment count for each one of them, then your value for count will be at least 25, even if there is a direct flight from MCO to LAX.
 

Thread Starter

arduinolego611

Joined Jan 23, 2022
75
As I explained before, that doesn't do what you expect it to.

Let's say that there are 25 airports that have flights going to LAX. If you increment count for each one of them, then your value for count will be at least 25, even if there is a direct flight from MCO to LAX.
I think my problem is that I don't understand why that is.

if I am iterating through the flights array (not the airports array) with a for loop, and im only telling the program to increment any sort of "count" when there is a matching relationship between i [0] of one element and i [1] of the previous, then wouldn't the count only increment when it finds this relationship? not every time it executes any of the other lines?


I don't mean to sound like a brick wall... I am still new to c++ and complex programming prompts like this one. I just don't understand why its incrementing once for every flight
 

MrChips

Joined Oct 2, 2009
34,809
I am going to say it one more time.

Stop writing computer code. It doesn't matter if your target programming language is BASIC, COBOL, SNOBOL, Fortran, Pascal, C, C++, Python, Perl, etc....

Create a working solution in your native spoken language before attempting to write computer code.
 

WBahn

Joined Mar 31, 2012
32,828
I think my problem is that I don't understand why that is.

if I am iterating through the flights array (not the airports array) with a for loop, and im only telling the program to increment any sort of "count" when there is a matching relationship between i [0] of one element and i [1] of the previous, then wouldn't the count only increment when it finds this relationship? not every time it executes any of the other lines?


I don't mean to sound like a brick wall... I am still new to c++ and complex programming prompts like this one. I just don't understand why its incrementing once for every flight
Look at your flight array and walk through it, by hand, using your approach.

Code:
        {"BOS", "LAX"}, // 4A
        {"ATL", "EWR"},
        {"DEN", "DFW"},
        {"ORD", "LAX"}, // 1B
        {"DEN", "BOS"}, // 3A
        {"EWR", "DEN"}, // 2A
        {"LGA", "JFK"},
        {"MCO", "EWR"}, // 1A
        {"LAS", "PHX"},
        {"SLC", "SEA"},

...

   for (i = 0; i < 11; i++)
    {
        if (flights [i] [1] == "LAX")
        {
            count++;
        }
   ...
What happens when i = 0? Since flights[0][1] is "LAX", you increment count.

What happens when i = 3? Since flights[3][1] is "LAX", you increment count.

If you had a flight schedule that had 200 flights that went from somewhere to "LAX", you would increment your count for each and every one of them.
 

Thread Starter

arduinolego611

Joined Jan 23, 2022
75
Look at your flight array and walk through it, by hand, using your approach.

Code:
        {"BOS", "LAX"}, // 4A
        {"ATL", "EWR"},
        {"DEN", "DFW"},
        {"ORD", "LAX"}, // 1B
        {"DEN", "BOS"}, // 3A
        {"EWR", "DEN"}, // 2A
        {"LGA", "JFK"},
        {"MCO", "EWR"}, // 1A
        {"LAS", "PHX"},
        {"SLC", "SEA"},

...

   for (i = 0; i < 11; i++)
    {
        if (flights [i] [1] == "LAX")
        {
            count++;
        }
   ...
What happens when i = 0? Since flights[0][1] is "LAX", you increment count.

What happens when i = 3? Since flights[3][1] is "LAX", you increment count.

If you had a flight schedule that had 200 flights that went from somewhere to "LAX", you would increment your count for each and every one of them.
what if for the sake of me learning, we get rid of any repititions. then would my code work? that's a jumping off place and I can fix this problem later. I just want to know.
 

Thread Starter

arduinolego611

Joined Jan 23, 2022
75
I am going to say it one more time.

Stop writing computer code. It doesn't matter if your target programming language is BASIC, COBOL, SNOBOL, Fortran, Pascal, C, C++, Python, Perl, etc....

Create a working solution in your native spoken language before attempting to write computer code.
There is a list of one way flights, with source and destination separated by commas

There is a list of airports separated by commas

So, ignore the airports array...there are paths between airports and you find the path by first

1. Finding the source of the flight
2. Look for another flight that has that source as a destination
3. Mark a one next to this flight
4. Find the source of that flight
5. Look for another flight that has that source as a destination
6. Mark two next to this flight

Repeat until you have a number next to the airport you wish to travel to LAX from
 

WBahn

Joined Mar 31, 2012
32,828
what if for the sake of me learning, we get rid of any repititions. then would my code work? that's a jumping off place and I can fix this problem later. I just want to know.
The repetitions are a fundamental part of what your code does. This approach is fundamentally flawed, so abandon all hope ye who enter.

Forget code. Work on an algorithm that works.
 

WBahn

Joined Mar 31, 2012
32,828
There is a list of one way flights, with source and destination separated by commas

There is a list of airports separated by commas

So, ignore the airports array...there are paths between airports and you find the path by first

1. Finding the source of the flight
2. Look for another flight that has that source as a destination
3. Mark a one next to this flight
4. Find the source of that flight
5. Look for another flight that has that source as a destination
6. Mark two next to this flight

Repeat until you have a number next to the airport you wish to travel to LAX from
No one is paying attention to your airports array since you aren't using it. Stop getting hung up on that.

This algorithm is what I described way back in Post #10, except that one is more efficient.

The algorithm you are describing can spend a long time finding very long routes to your destination, whereas the one I described will find the shortest route first.
 

WBahn

Joined Mar 31, 2012
32,828
There is a list of one way flights, with source and destination separated by commas

There is a list of airports separated by commas

So, ignore the airports array...there are paths between airports and you find the path by first

1. Finding the source of the flight
2. Look for another flight that has that source as a destination
3. Mark a one next to this flight
4. Find the source of that flight
5. Look for another flight that has that source as a destination
6. Mark two next to this flight

Repeat until you have a number next to the airport you wish to travel to LAX from
PLEASE... there is NO need to post the same thing in response to multiple posts. Everyone sees it the first time.
 

Thread Starter

arduinolego611

Joined Jan 23, 2022
75
PLEASE... there is NO need to post the same thing in response to multiple posts. Everyone sees it the first time.
No one is paying attention to your airports array since you aren't using it. Stop getting hung up on that.

This algorithm is what I described way back in Post #10, except that one is more efficient.

The algorithm you are describing can spend a long time finding very long routes to your destination, whereas the one I described will find the shortest route first.
I will try to come up with an entirely new algorithm
 

WBahn

Joined Mar 31, 2012
32,828
Minimum number of passes to get a list of flights from any city to F...

Also, am I looking for a graph search algorithm?
There are many kinds of graph searches and this falls into that category.

But you should be able to figure it out on your own -- and, in doing so, will probably have a light click on and will understand how and why it works and how to solve the problem.

Here's a hint. Let's say that I want to get from Boston to LAX. Let's also say that I know that I can get from Chicago to LAX in 2 flights and I know that I can get from Miami to LAX in 3 flights, and I know that I any other city is more than three flights. I now look at my list and see that I can get from Boston to Miami direct. How many flights does it take to get from Boston to LAX?
 

Thread Starter

arduinolego611

Joined Jan 23, 2022
75
There are many kinds of graph searches and this falls into that category.

But you should be able to figure it out on your own -- and, in doing so, will probably have a light click on and will understand how and why it works and how to solve the problem.

Here's a hint. Let's say that I want to get from Boston to LAX. Let's also say that I know that I can get from Chicago to LAX in 2 flights and I know that I can get from Miami to LAX in 3 flights, and I know that I any other city is more than three flights. I now look at my list and see that I can get from Boston to Miami direct. How many flights does it take to get from Boston to LAX?
4
 
Top