Home>

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 [0] = 9 a [1] = 7 a [2] = 35 a [3] = 91 a [4] = 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.
Please give us your advice.
Also, please tell me how to write the code.

Applicable source code
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main (void)
{
    int a [5];
    int i;
    srand ((unsigned) time (NULL));// Initialize random number
    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))
    {
        printf ("% d from 20 to 50% \ n");
    }
    else
    {
        printf ("Number between 20 and 50" \ n ");
    }
    if (a [i]>80)
    {
        printf ("numberd more than 80% d \ n");
    }
    else
    {
        printf ("0 number greater than 80 \ n");
    }
    if ((i>= 0)&&(i<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.

  • Answer # 1

    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 [5];
        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;
    }

  • Answer # 2

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

  • Answer # 3

    Variablecountetc. should be prepared, and it should be incremented by 1 only when the condition is met while looping withforstatements.

    Since the length of the array, such as 5, is a constant, let's make it asymbol 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] [30] = {"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 [0] ++;
          }
          else if (a [i]>= 80) {
              count [1] ++;
          }
          else if (a [i]<10) {
              count [2] ++;
          }
      }
      for (i = 0;i<ITEM;i ++) {
          printf ("% s:% d \ n", text [i], count [i]);
      }
      return 0;
    }

  • Answer # 4

    If you want to count, loop from the beginning to the end of the array and write the code to check and count the value