Two set of codes in one program

I don't know how to put this style into words. I put two set of { } and the output is the exact as what my prof expect me to do. Can you guys tell me what is the name of this style? And is it appropriate to do it like this?

The output should give, for example:


Enter the number of trains on Track 1. 5
Enter the number of trains on Track 2. 4
Enter the number of trains on Track 3. 3
Enter the number of trains on Track 4. 2
Track 1 has 1 train cleared and 4 train(s) stopped. Track 1 alert!
Track 2 has 4 train(s) stopped.
Track 3 has 3 train(s) stopped.
Track 4 has 2 train(s) stopped.



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
55
56
57
58
59
60
61
  #include <iostream>

using namespace std;

int findHighestPriorityTack(int tracks[], int size)
{
    
   int max = 0;
    
   for(int i = 0; i < size; ++i)
   {
       
       if(tracks[i] >= tracks[max])
       {
           
           max = i;
           
       }
       
   }
    
   return max;
    
}

int main()
{
    
   int tracks[4];   // initialize 4 tracks
    
   for(int i = 0; i < 4; ++i)
   {
       
       cout << "Enter the number of trains on Track " << (i + 1) << ". ";
       cin >> tracks[i];
       
   }
    
   int max = findHighestPriorityTack(tracks, 4);
    
   for(int i = 0; i < 4; ++i)
   {
       
       if(i == max)
       {
           
           cout << "Track " << i + 1 << " has 1 train cleared and " << tracks[i] - 1 << " train(s) stopped. Track " << i + 1 << " alert!" << endl;
           
       }
       
       else
       {
           
           cout << "Track " << i + 1 << " has " << tracks[i] << " train(s) stopped." << endl;
           
       }
       
   }
    
   return 0;
}
Topic archived. No new replies allowed.