#include(stdio.h) #include(conio.h) #include(stdlib.h) main() { clrscr(); int i,j,k,z[100]; for (i=0;i<100;i=i+1) z[i]=rand()%100; for (i=0;i<100;i=i+1) if(z[i]<50) j=j+1; else k=k+1; printf("%d\\n",j,k); getch(); return 0; } 

This program should divide the number 100 into two sections ... i.e. to 0 <50 and 50 <100, from each site take a random number and display them on the screen. But instead, it gives out some kind of silly numbers. Tell me, please, where is my mistake or what have I done wrong?

  • one
    @Nikitka, Remove the # signs at the beginning of each line of code, then use the 101010 button to edit the code. - Nicolas Chabanovsky

2 answers 2

To get new pseudo-random numbers with each new program start, at the beginning put the line

 srand(time(NULL)); 

Getting the first number (0-49):

 int r1 = rand() % 50; 

Getting the second number (from 50 to 100):

 int r2 = rand() % 51 + 50; 

    And it seemed to me that meant something like

     #include <stdio.h> #include <stdlib.h> void main() { // Массивы чисел int z[100]; // Исходный int l[100]; // Меньше 50 int g[100]; // Больше 50 // Заполняем случайными числами for(int i=0; i<100; i++) z[i] = rand() % 100 + 1; int ls = 0, gs = 0; // Разделяем for(int i=0; i<100; i++) if(z[i]<50) l[ls++] = z[i]; else g[gs++] = z[i]; // Выводим на экран printf("Less than 50: "); for(int i = 0; i<ls; i++) printf("%d ", l[i]); printf("\n\nGreater or equal than 50: "); for(int i = 0; i<gs; i++) printf("%d ", g[i]); } 
    • Thanks, all versions helped a lot ... - Nikitka