Home>

### i do not know the program that displays the number of numerical values ​​that satisfy the condition

Nice to meet you, thank you for browsing.
Immediately, I am currently solving the problem of developing a site called "C language learned in a week".

In that, "A program that displays the number of numerical values ​​satisfying the following conditions by substituting and displaying random numbers from 0 to 100 for all components in an integer array variable a of length 5" "I made it."
There are the following three conditions.
(1) Number between 20 and 50
(2) A number greater than 80
(3) Number between 0 and less than 10

In the execution result example, it was written as follows.
Example of execution results
a  = 9 a  = 7 a  = 35 a  = 91 a  = 58
Number between 20 and 50: 1
Number greater than 80: 1
Number between 0 and 10: 2

Generating random numbers, assigning them to the array, and displaying them.
However, I do not know the program that displays the number of numerical values ​​that satisfy the condition.
Also, please tell me how to write the code.

Applicable source code
``````#include&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;
#include&lt;time.h&gt;
int main (void)
{
int a ;
int i;
srand ((unsigned) time (NULL));// Initialize random number
for (i = 0;i&lt;5;i ++)
{
a [i] = rand ()% 100 + 1;// get a random number between 1 and 100
printf ("a [% d] =% d \ n", i, a [i]);// Display 5 selected random numbers
}
if ((a [i]&gt;= 20) || (a [i]&lt;= 50))
{
printf ("% d from 20 to 50% \ n");
}
else
{
printf ("Number between 20 and 50" \ n ");
}
if (a [i]&gt;80)
{
printf ("numberd more than 80% d \ n");
}
else
{
printf ("0 number greater than 80 \ n");
}
if ((i&gt;= 0)&amp;&amp;(i&lt;10))
{
printf ("0 or more and less than 10% d \ n");
}
else
{
printf ("A number between 0 and less than 10 \ n");
}
return 0;
}``````
Tried

I couldn't come up with a program that displays the number of numerical values ​​that satisfy the following conditions, and I couldn't find anything that looked through the reference books and sites.

It's difficult to answer these questions.
I don't think it's helpful for the questioner if you write all of it
(1) Only numbers between 20 and 50
If you follow the questioner's program as much as possible, it will be as follows.

``````# include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main (void)
{
int a ;
int i;
int i20_50 = 0;
srand ((unsigned) time (NULL));// random number initialization
for (i = 0;i<5;i ++)
{
a [i] = rand ()% 100 + 1;// get a random number between 1 and 100
printf ("a [% d] =% d \ n", i, a [i]);// display 5 selected random numbers
if ((a [i]>= 20)&&(a [i]<= 50))
{
i20_50 + = 1;
}
}
printf ("Several% d from 20 to 50 \ n", i20_50);
return 0;
}``````

If ~ else How about inserting a counter (i ++) in each part of the statement and printing each variable at the end?

Variable`count`etc. should be prepared, and it should be incremented by 1 only when the condition is met while looping with`for`statements.

Since the length of the array, such as 5, is a constant, let's make it a`symbol constant`.

``````# include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define NUMBER 5
#define ITEM 3
int main (void)
{
int a [NUMBER];
int i;
int count [ITEM] = {0,0,0};
char text [ITEM]  = {"Number between 20 and 50", "Number greater than 80", "Number between 0 and less than 10"};
srand ((unsigned) time (NULL));// Initialize random number
for (i = 0;i<NUMBER;i ++) {
a [i] = rand ()% 100 + 1;// get a random number between 1 and 100
printf ("a [% d] =% d \ n", i, a [i]);// Display 5 selected random numbers
}
for (i = 0;i<NUMBER;i ++) {
if (20<= a [i]&&a [i]<= 50) {
count  ++;
}
else if (a [i]>= 80) {
count  ++;
}
else if (a [i]<10) {
count  ++;
}
}
for (i = 0;i<ITEM;i ++) {
printf ("% s:% d \ n", text [i], count [i]);
}
return 0;
}``````