Help with logical question

Hello there. I need to build a source code that calculates the total distance from the given distances of a city to another, using Multidimensional arrays.
The question is: I need to put in something that allows the user to tip in as many routes he wants from a city to another, and chooses when he wants to end it, making a sum of all the entered distances.

That's all I have yet:

Note that it's in Portuguese, but yet I think you can pretty much understand it.
(Distancia==Distance and
Digite a rota que voce deseja calcular = Tip in the route that you want to calculate.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
using namespace std;

int main ()
{
    int distancias[5][5] = {102,414,705,96,0,75,332,555,0,96,578,446,0,555,705,437,0,446,332,414,0,437,578,75,102}; //Array including the distances
    int Ita_Ouro=distancias[0][3];
    int Ita_SP=distancias[0][2];
    int Ita_RJ=distancias[0][1];
    int Ita_BH=distancias[0][0];
    int Ouro_Ita=distancias[1][4];
    int Ouro_sp=distancias[1][2];
    int Ouro_RJ=distancias[1][1];
    int Ouro_BH=distancias[1][0];
    int SP_ITA=distancias[2][4];
    int SP_Ouro=distancias[2][3];
    int SP_RJ=distancias[2][1];
    int SP_BH=distancias[2][0];
    int RJ_ITA=distancias[3][4];
    int RJ_Ouro=distancias[3][3];
    int RJ_SP=distancias[3][2];
    int RJ_BH=distancias[3][0];
    int BH_ITA=distancias[4][4];
    int BH_Ouro=distancias[4][3];
    int BH_SP=distancias[4][2];
    int BH_RJ=distancias[4][1];
    cout<<"As Distancias entre um destino e outro sao:"<<endl; //Shows the distances from a city to another
    cout<<"Ita_Ouro = "<<Ita_Ouro<<endl;
    cout<<"Ita_SP = "<<Ita_SP<<endl;
    cout<<"Ita_RJ = "<<Ita_RJ<<endl;
    cout<<"Ita_BH = "<<Ita_BH<<endl;
    cout<<"Ouro_Ita = "<<Ouro_Ita<<endl;
    cout<<"Ouro_sp = "<<Ouro_sp<<endl;
    cout<<"Ouro_RJ = "<<Ouro_RJ<<endl;
    cout<<"Ouro_BH = "<<Ouro_BH<<endl;
    cout<<"SP_ITA = "<<SP_ITA<<endl;
    cout<<"SP_Ouro = "<<SP_Ouro<<endl;
    cout<<"SP_RJ = "<<SP_RJ<<endl;
    cout<<"SP_BH = "<<SP_BH<<endl;
    cout<<"RJ_ITA = "<<RJ_ITA<<endl;
    cout<<"RJ_Ouro = "<<RJ_Ouro<<endl;
    cout<<"RJ_SP = "<<RJ_SP<<endl;
    cout<<"RJ_BH = "<<RJ_BH<<endl;
    cout<<"BH_ITA = "<<BH_ITA<<endl;
    cout<<"BH_Ouro = "<<BH_Ouro<<endl;
    cout<<"BH_SP = "<<BH_SP<<endl;
    cout<<"BH_RJ = "<<BH_RJ<<endl;

    for (int i,i>0) /*This is where I'm stuck. I need to get the values from above (which the user will enter BH_ITA or whichever he wants to calculate, then he'll continue entering until he decides to terminate the program, and that's where the sum needs to occurr*/
    {
    cout<<"Digite a rota que deseja calcular";
    
    }
Last edited on
What's the user supposed to enter? The names of the cities? Then you need a map where you can retrieve the index from the name like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <map>
#include <string>

....

map<string, int> city_index_map; // for the first index of 'distancias'
city_index_map["Ita"] = 0;
city_index_map["Ouro"] = 1;
city_index_map["SP"] = 2;
map<int, std::map<int, int> > city_to_city_index_map; // for the second index of 'distancias'
city_to_city_index_map[city_index_map["Ita"]][city_index_map["Ouro"]] = 0;
city_to_city_index_map[city_index_map["Ita"]][city_index_map["SP"]] = 1;

string answer;
int indexes[100];
int count = 0;
for(int i = 0; i < (sizeof(indexes) / sizeof(*indexes)); ++i)
{
  cout<<"enter city or 'q' to exit"<<endl;
  cin >> answer;
  if(answer != "q")
  {
    map<string, int>::iterator it = city_index_map.find(answer);
    if(it != city_index_map.end())
    {
      indexes[count] = it->second;
      ++count;
    }
    else
      cout << "invalid city" << endl;
  }
}
while(answer != "q");

int sum = 0;
for(int i = 1; i < count; ++i)
{
  sum += distancias[indexes[i - 1]][city_to_city_index_map[indexes[i - 1]][indexes[i]]];
}
That might give you an idea how it might work



I'd say this cout<<"Ita_Ouro = "<<Ita_Ouro<<endl; is not very intuitive. I'd find it better like so: cout<<"Ita -> Ouro = "<<Ita_Ouro<<endl;
Topic archived. No new replies allowed.