The growth of students in a class is represented as an array. Determine the average height of boys and girls. Print a message who is taller girls or boys.

I realized that I needed to set an array (3 above - boys, 3 below - girls), calculate the average value and compare, but I always have some errors. I can not even properly display the array on the screen, although I did the following:

#include <iostream> #include <string> using namespace std; int main() std::string students[6]={ "170", "171", "189", "160", "156", "159"}; cout<<students; return 0; } 

I just can not understand what's the matter then.

  • one
    1. How do you distinguish boys from girls? - qulinxao
  • Yes, it's just the numbers. Numbers 1,2,3 - height of boys. 4,5,6 - growth of girls - Treaq
  • I can not understand why he does not want to display an array on the screen. I wrote the "cout" command, but he does not want to display the results, although he did follow the pattern - Treaq
  • @Treaq, when you ask a question, describe (except for your wishes) what you really see on the screen (not everything is possible, but only relevant messages). In your case (for a start), it would be necessary to inform you that the program * simply does not compile *. (or (if compiled, and its output is different from the expected), bring the actual code in question). - This is to answer @ fori1ton about the missing brace. - avp

3 answers 3

Here are the most serious mistakes:

  1. Missed the opening brace after main()
  2. If you hope that the line is cout << students; displays the contents of the array, then you are mistaken. The variable students is a pointer to the beginning of the array, so only the address in memory that the first element of the array is located will be displayed. If you want to output the elements of an array, you will have to loop and output one by one.

And a couple more comments about the mansions:

  1. If you have registered using namespace std; then it is not necessary to explicitly specify the std when declaring the string array.
  2. Still, it would be better if the growth of boys and girls will be stored in different arrays.
  3. Why do you store numbers in string ? Especially if you need to calculate their arithmetic average. float or on int would suit far better.
  • did everything according to the model, namely "using namespace std;" and "string": c Thank you, I will try - Treaq
  • 3
    @Treaq, try not to do on the model, but first read some textbook on the language, delve into its basic structures, and only then write on it. Doing the pattern you will learn nothing. - fori1ton

av_boy: = (students [1] + students [2] + students [3]) / 3;

writeln ("average age of boys =", av_boy);

by analogy for girls.

  • I myself know this (by the way, you wrote it down in pascal, didn't it?), I cannot understand why even my code contains an error and how to fix it. I need to find through the array, preferably all the code, not cuts. - Treaq
  • Well, at least you write what mistakes, we are not wizards) - Avtostopom_do_Raya
  • i019.radikal.ru/1406/33/ecd4f00740det.jpg I thought you would stuff the compiler, all the same the picture is not right) - Treaq
  • one
    The compilation error corrects the addition of the first / second line of such #include <iostream> But it is unlikely that the program will output can be considered the correct result. - KoVadim
  • So it is. The very first one stands - Treaq
 int students[6] = {170, 171, 189, 160, 156, 159}; float boy = 0, girl = 0; cout<<"Мальчики:"; for(int i = 0; i < 3; ++i) { printf(" %d",students[i]); boy += students[i]; } boy /= 3; printf("\nСредний рост мальчиков: %f\n", boy); ... if (boy > girl) cout<<"Мальчики выше"; ...