Using a string array and int array together.
Apr 18, 2013 at 3:36am UTC
I have a list 51 locations and there populations and I have the names of the states going into one array and the populations going into another array. I am using for loops to find the lowest and highest populations. My problem is I need to use cout to display the name of the location next to which population is the highest/lowest. I know that the elements would be stored in the same locations as they are on the same lines in .txt file. How do I access which element is storing the high/low populations?
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
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
ifstream fin;
const int NUM_STATE = 51;
string state [NUM_STATE];
int population [NUM_STATE];
int i;
int popHigh = 0;
int popLow = 9999999999999;
int totalPop = 0;
int averagePop;
fin.open("statePopulation.txt" );
if (!fin)
cout << "Error opening file. \n" ;
else
{
for (i = 0; i < NUM_STATE ; i++)
{
fin >> population[i];
fin.ignore();
getline(fin, state[i]);
}
for ( i = 0; i < NUM_STATE ;i++)
{
cout << state[i] << " " ;
cout << population[i] << endl;
totalPop += population[i];
averagePop = totalPop/NUM_STATE;
if (population[i] > popHigh)
popHigh = population[i];
if (population[i] < popLow )
popLow = population[i] ;
}
for ( i = 0; i < NUM_STATE ;i++)
{
if (population[i] > popHigh)
popHigh = population[i];
}
cout << population[i] << endl ;
cout << popHigh << endl;
cout << popLow << endl;
cout << totalPop << endl;
cout << averagePop << endl;
}
fin.close();
return 0;
}
Last edited on Apr 18, 2013 at 3:38am UTC
Apr 18, 2013 at 6:07am UTC
You need to save the index of the highest and lowest populations.
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
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
ifstream fin;
const int NUM_STATE = 5;
string state [NUM_STATE];
int population [NUM_STATE];
int i;
int popHigh = 0;
int popLow = 9999999999999;
int totalPop = 0;
double averagePop;
int indexOfPopHigh = 0;
int indexOfPopLow = 0;
fin.open("statePopulation.txt" );
if (!fin)
cout << "Error opening file. \n" ;
else
{
for (i = 0; i < NUM_STATE ; i++)
{
fin >> population[i];
fin.ignore();
getline(fin, state[i]);
}
for ( i = 0; i < NUM_STATE ;i++)
{
cout << state[i] << " " ;
cout << population[i] << endl;
totalPop += population[i];
if (population[i] > popHigh)
{
popHigh = population[i];
indexOfPopHigh = i; //<----saves index of highest population
}
if (population[i] < popLow )
{
popLow = population[i] ;
indexOfPopLow = i; //<-------saves index of the lowest population
}
}
averagePop = totalPop/NUM_STATE;
cout << "The highest population is " << popHigh << " " << state[indexOfPopHigh] << endl;
cout << "The lowest population is " << popLow << " " << state[indexOfPopLow] << endl;
cout << "The total population is " << totalPop << endl;
cout << "The average population is " << averagePop << endl;
}
fin.close();
cin.ignore();
return 0;
}
Topic archived. No new replies allowed.