Using Parallel Arrays to store countries

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.

Get input from user store in arrays. Once you have it stored in the arrays output it.

1
2
for(int i = 0; i < 12; i++)
     cout << Country[i] << " " << Population[i] << endl;
Last edited on
Okay, How would I get the input would I just do the following ??

1
2
cout << "Enter in Country Canada: "; 
           cin >> Country[1]
This is just a quick example to play with. Play with it and ask any questions you have about it here. We will be happy to help.
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
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string Country[12] = {""};
	int Population[12] = {0};
	
	string name;
	int populus;
	
		
	for (int i = 0; i < 12; i++)
	{
		cout << "Enter in country: ";
		cin >> name;
		cout << "Enter population: ";
		cin >> populus;
		if(Country[i] == "")
			Country[i] = name;
		if(Population[i] == 0)
			Population[i] = populus;
	}
	
	for (int i = 0; i < 12; i++)
	{
		cout << Country[i] << " " << Population[i] << endl;
	}
	
	return 0;
}
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
#include <iostream>
#include <string>

int main()
{
    constexpr int ARRAY_SIZE = 12 ; // avoid magic numbers

	std::string country[ARRAY_SIZE] ;
	int population[ARRAY_SIZE] = {0};

	for( int i = 0 ; i < ARRAY_SIZE ; ++i )
	{
		std::cout << "country: " ;
		// http://www.cplusplus.com/reference/string/string/getline/
		std::getline( std::cin, country[i] ) ; // name may contain spaces eg. 'South Africa'

		std::cout << "populaion: ";
		std::cin >> population[i] ;

		// throw away the new line remaining in the input buffer
		// http://www.cplusplus.com/reference/istream/istream/ignore/
		std::cin.ignore( 1000, '\n' ) ;

		// note: checks for errors in input are elided
	}
}
@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.
Thanks for the help! I appreciate it guys!!

@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; 


Would that be how I would set it up?

Thank you!
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.
Last edited on by closed account z6A9GNh0
Okay, thanks for the reply!

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?

Thanks!
Yes, if you want to hold a large number go with double. That should work just fine for your code.
> 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) 
long long 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.
@BHXSpecter

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
Last edited on
Topic archived. No new replies allowed.