Suppose that a bird watching club has collected data on the number of sightings of 20 different
bird species in each of 100 different cities. The data has been stored in an two dimensional array
as:
int sightings[20][100];
The value stored in sightings [i][j] is the number of times that a bird belonging to
species number i was sighted in city number j.
(i)
Write a code segment that will compute and print the total number of bird sightings for
city number 0, then for city number 1, and so forth, up to city number 99.
(ii)
Let's say that a species is endangered if it has been sighted in only 4 cities or fewer.
(Species number i was sighted in city number j if sightings[i][j] is greater than zero.) Write
a code segment that will print a list of all the species that are endangered.
1)
for (int j = 0; j < 100; j++)
{
for (int i = 1; i < 20; i++)
{
sightings[0][j] += sightings[i][j];
}
cout << "For city number " << j << " total number of bird sightings: " << sightings[0][j] << endl;
}
2)
int k[20];
for (int j = 0; j < 20; j++)
{
k[j]=0;
}
for (int j = 0; j < 20; j++)
{
for (int i = 0; i < 100; i++)
{
if (sightings[j][i] > 0)
{
k[j] += 1;
}
}
if (k[j] < 4)
{
cout << "The species " << j << " is threatened with extinction.\n";
}
}
Comments
Leave a comment