Create an array of 10 random numbers in the range from -20 to 20. Determine the arithmetic mean of the positive elements of the array.

Update 1

I did this:

#include <ctime> #include <cstdlib> using namespace std; void main() { setlocale(0, "rus"); const int n = 10; int num[n]; int num_1 = 0; srand(time(0)); num_1 = rand() % 41 - 20; for (int i = 0; i < 10; i++) { cout << num_1<< endl; i++; } } 

Update 2

 setlocale(0, "rus"); const int n = 10; int num[n]; int num_1 = 0; srand(time(0)); num_1 = rand() % 41 - 20; for (int i = 0; i < 10; i++) { cout << num_1 << endl; } 

It is impossible to output all 10 numbers different.

  • @ ma1ina, According to the rules of the forum, questions should not be reduced to the decision or the completion of student assignments. Please clarify what you have done yourself and what did not work out. - DreamChild
  • 1. Transfer the code to the question and format it 2. But is it impossible what? - DreamChild
  • Question updated. - ma1ina
  • @ ma1ina again - transfer the code to the question and format it - it is very inconvenient to read it - DreamChild

1 answer 1

@ ma1ina , and they should not be so different in this way (even if you correct the error on your program).

After all, these are random numbers. Another number of m. whatever . The probability that it will be equal to the previous one (well, of course, if these are really random numbers) is 1/41.

-

If you need all 10 to be different, then you need to check, and if there was not the same before. The simplest (but not the most effective) solution is a comparison in the nested loop of a new number with those already stored.