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");
}