So I am stumbled on this question, I am learning how to use Arrays and I do not understand the concept clearly enough to write this program. I wanted some help getting basic structure done. The question is:
In a program you need to store the populations of 12 countries.
A) define two arrays that may be used in parallel to store the names of the countries and their population
B)Write a loop that uses these arrays to print each country's name and its population.
So I know how to create an array I created Country[12] and Population[12] but I don't know the next steps and where to begin. Any guidance would be greatly appreciated.
@JLBorges @canucksfan1
JLBorges's example is more efficient. I was trying to keep it simple since you said you were having trouble wrapping your head around arrays. I didn't want to assume what you knew and end up adding more to the confusion (ie getline and ignore). Last thing I wanted was to add things for you to learn on top of trying to wrap your head around other features.
@BHXSpecter I did not think of using a string, would it still be possible to make the program without using string just a normal variable? If trying to calculate the highest population how would we do that? Using a for loop would do the trick? Example:
1 2 3 4 5 6 7 8
int highest
highest = population[i];
for (index = 1; index < 12; index++
{
if(population[i]>highest)
highest=population[i];
}
cout <<"Highest Population is:" << highest;
Well for holding the names of the countries a string would make it easy. Otherwise you could use a c-string char countries[12][50]; or char countries[12*50];, but I think for your purposes a string array is better.
Yeah, you have it pretty close. Highest would be like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
int highest = 0;
string highCountry;
for (int i = 0; i < 12; i++)
{
if (population[i] > highest)
{
highest = population[i];
highCountry = country[i]; // so you can print out the country too
}
}
cout << "Country with highest population is " << highCountry << " : " << highest;
You could have done highest = population[0]; and then go through comparing them, but i prefer to just initialize it to 0 and then start comparing the numbers, replacing it with the highest population between the two.
I wanted to know should I change the population data type to something more than integer? or can the data type Int be able to hold such large numbers? I was thinking of using something more like long double or etc?
> I wanted to know should I change the population data type to something more than integer?
> I was thinking of using something more like long double or etc?
As far as possible, do not - repeat do not - use floating point variables to hold integer values.
The population of the entire world is estimated to be about 7.2 billion. So:
1 2
// the most efficient integer type that can represent values up to at least 10 billion
decltype(10000000000) population[ARRAY_SIZE] ;
Or alternatively:
1 2
// at least 64 bits (can hold integers with up to 18 decimal digits)
longlong population[ARRAY_SIZE] ;
@JLBorges
Hmm...starting to think I learned from the wrong places, people, and books. The more I help others on here the more I learn and realize I learned bad methods. I would recommend the second method just in case his compiler doesn't have decltype implemented yet or he is using an older compiler that doesn't have it.
Nevermind I found out the solution I had to set the width of the country as well in order for the entire output to display aligned. So i just did setw infront of COUNTRY and replaced the \t.
1 2 3 4 5 6 7 8 9
cout <<"---------COUNTRY" << "------------------------------" << "POPULATION" << "-----------------------" << endl;
//Loop for outputing countries and populations
for (int index = 0; index < 12; index++)
{
cout << setw(20) << Country[index] << setw(43) << fixed << Population[index] << endl;
}
//End Footer
cout << "-------------------------------------------------------------------------------" << endl;
For some reason my output does not seem to align I try to set the width so the populations are aligned but it doesn't work any ideas?
1 2 3 4 5 6 7 8 9
cout <<"---------COUNTRY" << "------------------------------" << "POPULATION" << "-----------------------" << endl;
//Loop for outputing countries and populations
for (int index = 0; index < 12; index++)
{
cout << "\t" << Country[index] << setw(43) << fixed << Population[index] << endl;
}
//End Footer
cout << "-------------------------------------------------------------------------------" << endl;
The numbers underneath population come out differently and aren't aligned together