-2

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.

Jose Ricardo Bustos M.
  • 8,016
  • 6
  • 40
  • 62

2 Answers2

2

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.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
OldProgrammer
  • 12,050
  • 4
  • 24
  • 45
  • It worked! Thanks a lot. One question, how does count++ work on the code without using another for? Sorry if it is a stupid question, im just new to this. And thanks again. – Redspark77 Oct 28 '15 at 01:27
  • Not sure I understand. ++ is the auto-increment operator. So it adds 1 to the existing value of count each time the condition ( c>k) is met. Spend some time learning how to use a debugger, and step through the code to see what happens. – OldProgrammer Oct 28 '15 at 01:33
2

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;
}
Rorschach
  • 734
  • 2
  • 7
  • 22