Hi!
I have a problem, what i cant solve...I have to write a program, that settles if the most frequent birds peculiar to an area were the same. The number of areas is N. We note the number of the birds. The number of the species is M.
I have the maximum values, I can print them on the console, but I don' know, how to work with them in the next step, where I should examine, wheter they are the same. Does someone have a good idea how to continue the code? Shall I redirect the output into a new array (what I don't know, how to do), or is there an easyer solution? Here's the code, what i currently have.
(Sorry for my bad english.)

``````#include <iostream>
#include <windows.h>
#include <string>

using namespace std;

const int maxn=100;
const int maxm=100;

void input_size(int &n, int &m);
void input_names(string city[maxn], string species[maxm], int n, int m);
void input_array(int x[maxn][maxm], int n, int m, string city[maxn], string species[maxm]);
void max(const int x[maxn][maxm], int n, int m, string city[maxn], string species[maxm]);
void waitforkey();

int main()
{
int x[maxn][maxm];
int n, m;
string city[maxn];
string species[maxm];
input_size(n, m);
input_names(city, species, n, m);
input_array(x, n, m, city, species);
max(x, n, m, city, species);
waitforkey();
}
void input_size(int &n, int &m)
{
string tmp;
bool error;
do
{
cout << "Number of cites? [0.." << maxn <<"] "; cin >> n;
error=cin.fail() || n<=0 || n>maxn;
if (error)
{
cout << "Error." << endl;
cin.clear(); getline(cin,tmp,'\n');
}
}
while (error);
do
{
cout << "Number of birdspecies: [0.." << maxm <<"] "; cin >> m;
error=cin.fail() || m<=0 || m>maxm;
if (error)
{
cout << "Error." << endl;
cin.clear(); getline(cin,tmp,'\n');
}
}
while (error);
cout << endl;
}
void input_names(string city[maxn], string species[maxm], int n, int m)
{
string tmp;
bool error;
do
{
cout << "Enter the name of the cityes! (one name in one row)" << endl;
for (int i=0; i<n; ++i)
{
cin >> city[i];
}
error=cin.fail();
if (error)
{
cout << "Error." << endl;
cin.clear(); getline(cin,tmp,'\n');
}
}
while (error);
cout << endl;
do
{
cout << "Please enter the name of the species! (one name in one row)" << endl;
for (int i=0; i<m; ++i)
{
cin >> species[i];
}
error=cin.fail();
if (error)
{
cout << "Error." << endl;
cin.clear(); getline(cin,tmp,'\n');
}
}
while (error);
cout << endl;
}
void input_array(int x[maxn][maxm], int n, int m, string city[maxn], string species[maxm])
{
string tmp;
bool error;
cout << "Number of inspected birds: " << endl;
for(int i = 0;i < n;++i)
{
cout << city[i] <<endl;
for(int j = 0;j < m;++j)
{
do
{
cout << species[j] << ": ";
cin >> x[i][j];
error=cin.fail();
if (error)
{
cout << "Error." << endl;
cin.clear(); getline(cin,tmp,'\n');
}
}
while (error);
}
cout << endl;
}
cout << "--------------------------------------------------" << endl;
}
void max(const int x[maxn][maxm], int n, int m, string city[maxn], string species[maxm])
{
int bird, maxim;
cout<< "Maximums: "<<endl;
for (int i=0; i<n; ++i)
{
maxim=x[i][0];
bird=0;
for (int j=0; j<m; ++j)
{
if (x[i][j]>maxim)
{
maxim=x[i][j];
bird=j;
}
}
cout << species[bird] << " (" << maxim << " " << city[i] << ")"<< endl;
}
cout << endl;
}
void waitforkey()
{
system("pause");
}``````

## All 6 Replies

Can you try and explain the problem a little more? I can make out that you populate an array of cities and an array of species but what are you doing with the array 'x' (as an aside 'x' is not a useful name for a variable, please give it a name which makes some sense) ? And what do you want to compare when you have the maximum values?

The "x-array" contains the values of the birds. The main problem is, that i have to compare the birds, wheter the most frequent bird in every city was the same bird. With the max values i have the name of the birds, so i can compare the names, so i can find out, if the statement was corrent or incorrect. But i don't know, how to redirect the names into an array to make the comparsion easier. Probably there are better methods, but i'm beginner (learning c++ for 2 months).

So basically all you want to check is the most frequent bird in all cities was same ?
Instead of having 'maxim' as in integer you should declare it as an array and populate the maximum value of each city in the array and then check if each element in the array is same. something like

``````int maxim[number of cities]
for(int i=0;i<number of cities;i++){
int maximum = getMaximumForCity(i);
maxim[i] = maximum;
}

//maxim has the maximums for all cities, now just iterate and compare each value with previous value.``````

You can use std::algorithm also but I'm not sure if you have studied those so I'm suggesting the simplest way to go about it.

I implemented it, but i have a question: how can i use this: getMaximumForCity(i). I don't know, how to declare it, or shall i use the maximum for it, what i've written?

Ok what I meant is when you are calculating max , store the maximum of each city in an array, like int maxim[number of cities] and then use that array to check if it has all same elements or different.

Ok, i made some tries, but it's not better. I think, i should use the index of the bird name, not the name...

``````void max(const int x[maxn][maxm], int n, int m, string city[maxn], string species[maxm])
{
int bird, maxim, k;
cout<< "Maximums: "<<endl;
for (int i=0; i<n; ++i)
{
maxim=x[i][0];
bird=0;
for (int j=0; j<m; ++j)
{
if (x[i][j]>maxim)
{ maxim=x[i][j];
k=j;
}
}
cout<<species[k]<<endl;
}

if(species[k]==species[k++])
{
cout<<"The"<<species [k]<<" was the most frequent bird in every city.";
}
else
{
cout<<"Not the same bird was the most frequent.";
}
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.