简体   繁体   中英

Find Quantity of Biggest integers in N integers in C

I want to do this code that tells you the number of (n) integers that are bigger (or equal) than a (k) input.

So for example:

input:
4 15
12 
6 
15
24

output:
2

So the 4 is the number of integers the user is going to input and the 15 is the k number, now the output is the quantity of numbers that are bigger than k.

What I have of code is this:

#include<stdio.h>

int main()
{
    int n, k, i;
    int c, d;     
    scanf(" %d",&n);
    scanf("%d", &k);

    for(i=1;i<=n;i++)
    {
        scanf("%d",&c);
        if (c[i]>k)
            c[i]=d;
    }
    printf("%d", d);      
    return 0;
}

As you can see my code sucks, I don't know how to find the integers that are bigger than k and to print the quantity, not the numbers themselves. Any help is really appreaciated. Thanks.

Not sure why you are trying to reference c as an array. That is not needed. Try this:

int main() 
{
   int n, k, i, c;
   int count = 0;

   scanf(" %d",&n);
   scanf("%d", &k);

   for(i=1;i<=n;i++)
   {
     scanf("%d",&c);
     if (c > k)
       count++;
   }

   printf("%d", count); 
   return 0
}

Also, I would rename your variables to something more meaningful, such as numEntries , checkValue , etc.

Far less elegant solution, but one that keeps the value you need for some further use.. OldProgrammer did it much simpler and more pretty.

int main()
{   
  int num, s, i, cnt = 0;
  printf("please input number of integers and int to compare with\n");
  scanf("%d %d", &s, &num);
  int arr[s];
  for(i = 0; i < s; i++)
  {
    printf("Please input %d. number", i+1);  
    scanf("%d", &arr[i]);
  }

  for(i = 0; i < s; i++)
  {
    if(arr[i] >= num)
      cnt++;
  }
   //at this point cnt holds the value you need

  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM