Need help returning an index of a vector

Ok so what this does is puts in crimes of non violent, violent, and domestic crimes. Part of this is finding the max and min each and then print out the name of the city that has the highest and lowest of that crime.
example:
Highest Nonviolent Crime: Downtown
Lowest Nonviolent Crime : Canton
Highest Violent Crime: Downtown
Lowest Violent Crime: Canton
Highest Domestic Crime: Downtown
Lowest Domestic Crime: Canton
My problem is I can pass the information and find the max and min but I do not know how to get the index back for the city name.

The function I need help with is at the bottom of the page called findmaxmin and I will call it at the end of my main
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  #include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <sstream>
using namespace std;


string capitalize(string s);
string capitalizenormal(string s);
int searchDistrict(const vector<string> &district, const string &d);
void findmaxmin (const vector<string> &districtname,const vector<int> &crime);

int main(int argc, char **argv)
{
    string crimeFile; 
//dist_name is the actual name of each distract
    string District1, dist_name;
    double Nonviolent, Violences, domestic; 
    
    int index;
    vector<string> District;
    vector<int> Non_Violent_crime;
    vector<int> Violent_crime;
    vector<int> domestic_violence;
    vector<string> history;
    crimeFile = argv[1];
    
    if (argc!=3){
        cout << "Requires a file and number of burrows to print";
        return 0;
    }
    
    ifstream infile;
    infile.open(crimeFile.c_str());
    
    if(infile.fail()){
        cout << "Unable to open input file " << "'" << crimeFile << "'";
        return 0;
    }
    
    istringstream numDistrict;
    numDistrict.str(argv[2]);
    int num;
    numDistrict >> num;
    
    while(infile >> District1 >> Nonviolent >> Violences >> domestic ){
        Nonviolent *=10;
        Violences*=10;
        domestic*=10;
        
        
        District.push_back(District1);
        Non_Violent_crime.push_back(Nonviolent);
        Violent_crime.push_back(Violences);
        domestic_violence.push_back(domestic);

        
    }
        
    
    
    
    
    
    
    int top_n=num;
    if(num > District.size()){
        top_n= District.size();
    }
    
    
    
    
        cout << "Top " << top_n << " Crime Ridden Areas In Baltimore" << endl;
        cout << left << setw(25) << "District" << left << setw(4) << " VC" << " " << left << setw(4) << " DV" << "  " << setw(4) << "NVC" << endl << endl;
        
    for(int i = 0; i<top_n;i++){
        cout << left << setw(25) << District[i];
        cout << " " << left << setw(4) << Violent_crime[i];
        cout << " " << left << setw(4) << domestic_violence[i];
        cout << " " << left << setw(4) << Non_Violent_crime[i] << endl;
    }
    
    
    
    
    
    infile.close(); 
    
    
    while(true){
        cout << endl << "Enter a district: ";
        cin >> dist_name;\
        
          
        dist_name = capitalize(dist_name);
        if(dist_name== "Done"){
            break;
        }
        index = searchDistrict(District, dist_name);
        
        if(index==-1){
            
            //cout << "Top " << top_n << " Crime Ridden Areas In Baltimore" << endl;
            cout << left << setw(25) << " " << setw(4) << " VC" << " " << left << setw(4) << " DV" << "  " << setw(4) << left << "NVC" << endl;
            cout << left << setw(25) << capitalizenormal(dist_name);
            cout << " " << left << setw(4) << "----";
            cout << " " << left << setw(4) << "----";
            cout << " " << left << setw(4) << "----" << endl;
        }else{

            history.push_back(dist_name);
            cout << left << setw(25) << " " << setw(4) << " VC" << " " << left << setw(4) << " DV" << "  " << setw(4) << left << "NVC" << endl;
            cout << left << setw(25) << capitalizenormal(dist_name);
            cout << " " << left << setw(4) << Violent_crime[index];
            cout << " " << left << setw(4) << domestic_violence[index];
            cout << " " << left << setw(4) << Non_Violent_crime[index] << endl;
        }
        
    }
    
    //This is where I am calling the function I just have deleted it for now 
   
    return 0;
}




string capitalizenormal(string s){
    string ret = s;
    ret[0] = toupper(ret[0]);
    for (unsigned int i = 1; i<ret.length(); i++){

           ret[i]=tolower(ret[i]);
        }
        
        
    
    return ret;
}

string capitalize(string s){
    string ret = s;
    ret[0] = toupper(ret[0]);
    for (unsigned int i = 1; i<ret.length(); i++){
        if(ret[i]=='-'){
           i+=1;
           ret[i]=toupper(ret[i]);
        
        
        }
        else{
           ret[i]=tolower(ret[i]);
        }
        
        
    }
    return ret;
}



int searchDistrict(const vector<string> &district, const string &d){

    
    for(int unsigned i = 0; i < district.size(); i++){
        if(district[i]==d){
            return i;
            
        }
    }
    return -1;
}


//This is where I am trying to get the max and min. I am also required to print //it out in this function.
void findmaxmin (const vector<string> &districtname,const vector<int> &crime){
    bool first = true;
    int max, min, i;
   
    for(i = 0; i < districtname.size(); i++){
        if(first==true){
            max = crime[i];
            min = crime[i];
            first = false;
        }else if(crime[i]>max){
            max=crime[i];
            
        }else if (crime[i]<min){
            min=crime[i];
        }
            
        
    }
//this is where I want to print out the max and min cities from above.
      cout <<;
    
}
Last edited on
closed account (D80DSL3A)
The trick is to save the index to the max and min values rather than the max and min values directly.
Adapting from your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void findmaxmin (const vector<string> &districtname,const vector<int> &crime){
    bool first = true;
    int maxIdx, minIdx, i;
   
    for(i = 0; i < districtname.size(); i++){
        if(first==true){
            maxIdx = 0;
            minIdx = 0;
            first = false;
        }else if(crime[i]>crime[maxIdx]){
            maxIdx=i;
            
        }else if (crime[i]<crime[minIdx]){
            minIdx=i;
        }
            
        
    }
//this is where I want to print out the max and min cities from above.
      cout << "lowest crime is " << crime[minIdx] << " in district " << districtname[minIdx] << endl;
      cout << "highest crime is " << crime[maxIdx] << " in district " << districtname[maxIdx] << endl;
    
}
THANK YOU SO MUCH!!!!!!!!!!!!
Topic archived. No new replies allowed.