Здравствуйте! Хочу написать код, где программа с помощью функций высчитывает средний вес спортсменов заданной страны из массивы структур. Проблема заключается в том, что я не могу разобраться, как корректно прописать аргументы, хотя без функций код работает.
#include <iostream>
#include <locale.h>
#include <string.h>
#include <ctype.h>
#define N 2
using namespace std;
float ves(char, struct);
int main()
{
setlocale(LC_ALL, "Rus");
struct SPORT
{
char surname[10];
char country[10];
int year;
float result;
float height;
float weight;
}mas[N];
int *ptr_mas = &mas;
int i;
for(i = 0; i < N; i++)
{
cout<<"Введите фамилию спортсмена"<<' '<<i+1<<endl;
cin>>mas[i].surname;
cout<<"Введите страну спортсмена"<<' '<<i+1<<endl;
cin>>mas[i].country;
cout<<"Введите год рождения спортсмена"<<' '<<i+1<<endl;
cin>>mas[i].year;
cout<<"Введите результат спортсмена"<<' '<<i+1<<endl;
cin>>mas[i].result;
cout<<"Введите рост спортсмена"<<' '<<i+1<<endl;
cin>>mas[i].height;
cout<<"Введите вес спортсмена"<<' '<<i+1<<endl;
cin>>mas[i].weight;
}
cout<<"Фамилия\t\t";
cout<<"Страна\t\t";
cout<<"Год\t\t";
cout<<"Результат\t";
cout<<"Рост\t";
cout<<"Вес\t"<<endl;
for(i = 0; i < N; i++)
{
cout<<mas[i].surname<<"\t\t";
cout<<mas[i].country<<"\t\t";
cout<<mas[i].year<<"\t\t";
cout<<mas[i].result<<"\t\t";
cout<<mas[i].height<<"\t";
cout<<mas[i].weight<<"\t"<<endl;
}
char strana[10];
cout<<"Назовите страну:";
cin>>strana;
cout<<"Средний вес по стране равен:"<<ves(char strana, struct SPORT);
return 0;
}
float ves(char strana, struct SPORT )
{
int kol = 0;
float ves1 = 0;
int i;
for(*ptr_mas = &mas; *ptr_mas < N; ptr_mas++)
{
if(!strcmp(mas[i].country, strana))
{
ves1 = ves1 + mas[i].weight;
kol++;
}
}
sr = ves1/kol;
return sr;
}
Спасибо.
float ves(char strana, struct SPORT *mas);
float ves(char strana, struct SPORT *mas)
{
int kol = 0;
float ves1 = 0;
int i;
float sr;
/* for(ptr = mas ; ptr < ptr + N; ptr++)
{
if(!strcmp(*ptr, strana))
{
ves1 = ves1 + mas[i].weight;
kol++;
}
}*/
sr = ves1/kol;
return sr;
}
struct SPORT
{
char surname[10];
char country[10];
int year;
float result;
float height;
float weight;
}mas[N];
int *ptr = mas;
int main()
{
setlocale(LC_ALL, "Rus");
char strana[10];
cout<<"Назовите страну:";
cin>>strana;
//cout<<"Средний вес по стране равен:"<<ves(strana, *ptr)<<endl;
return 0;
}