Home > OS >  Count number of triplets with products equal to given number
Count number of triplets with products equal to given number

Time:09-29

The count value is not being printed, the program takes the input but it wont print the count value. What is the reason for this ?

#include<stdio.h>
#include<stdlib.h>
int main()
{
  int n,m,count=0;;
  scanf("%d",&n);
  int arr[n];
  for(int i=0;i<n;i  )
    scanf("%d",&arr[i]);
  scanf("%d",&m);
  for(int i=0;i<n-2;i  )
    for(int j=i 1;i<n-1;j  )
      for(int k=j 1;k<n;k  )
        if(arr[i]*arr[j]*arr[k]==m)
          count=count 1;
  printf("%d",count);
}

CodePudding user response:

Your printf is evidently there and since it's a well-tested function that is known to function properly, we can safely assume that the program never reached there. So, possible causes (will start with cases that we can evidently dismiss in this case, but the aim is to show you the thought process in general):

Earlier end

A return statement, for example can end the execution of a function, so, if there was a return somewhere, maybe inside the block of a conditional, that would explain this. But there is no such thing in your code.

Condition

It could be the case that your printf is inside a conditional that evaluates to false and the program just never reaches there. That's not the case here either, at least if we can assume that the code you have shared is your actual code. Note that you have a conditional, but the printf outside the conditional should execute independently of its result.

Error

A syntax or other error can cause program crash, but we can exclude that as well here, because you would have error messages and you presumably would share it.

Long execution

That's certainly possible, presuming that n is very large and you do not wait until all loops finish their job.

Infinite cycle

You may have an end condition for one of the loops that will always be true. Take a look at

for(int j=i 1;i<n-1;j  )

which changes j at each step, compares i with n. Since the inner loop never changes i or n, this is an infinite cycle. You need to fix this and see whether this was the only problem. If the result is still not shown at the end, then look at the reasons above.

CodePudding user response:

There is no trailing newline in printf("%d",count); which may on some systems prevent the output from appearing on the console. For example on unix systems, the output will be followed by the shell prompt on the same line, which can cause the user to not see it.

Modify this statement as printf("%d\n", count);

But the main problem is the second loop: for(int j=i 1;i<n-1;j ) where you use the wrong index in the test expression. Use for(int j=i 1;j<n-1;j ) instead.

As a rule of thumb, use spaces around binary operators to improve readability of the code and test the return value of scanf() to detect invalid input.

Here is a modified version:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int n, m, count = 0;

    if (scanf("%d", &n) != 1 || n <= 0) {
        printf("invalid input\n")
        return 1;
    }
    int arr[n];
    for (int i = 0; i < n; i  ) {
        if (scanf("%d", &arr[i]) != 1) {
            printf("invalid input\n")
            return 1;
        }
    }
    if (scanf("%d", &m)) {
        printf("invalid input\n")
        return 1;
    }
    for (int i = 0; i < n - 2; i  ) {
        for (int j = i   1; i < n - 1; j  ) {
            for (int k = j   1; k < n; k  ) {
                if (arr[i] * arr[j] * arr[k] == m)
                    count  = 1;
            }
        }
    }
    printf("%d\n", count);
    return 0;
}
  •  Tags:  
  • c
  • Related