-1

This might be a silly question, but I am unable to find the solution. Function mat is not being called after giving input to variable n.

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

int mat(int n)
{
    printf("hello");
    int temp = n, count = 0;
    while (temp != 0)
    {
        temp = n % 10;
        switch (temp)
        {
        case 1:
            count += 2;
            break;
        case 7:
            count += 3;
            break;
        case 4:
            count += 4;
            break;
        case 2:
        case 3:
        case 5:
            count += 5;
            break;
        case 6:
        case 0:
        case 9:
            count += 6;
            break;
        case 8:
            count += 7;
            break;
        }
    }
    return count;
}

int main(void)
{

    int t, n, h;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d", &n);
    
        h = mat(n);
        printf("%d\n", h);
    }
}

I think something is wrong with scanf but don't know what it is. this program was to give the output for number of matches being used for particular number.

Aconcagua
  • 24,880
  • 4
  • 34
  • 59
prajwal
  • 7
  • 4
  • 4
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – RoQuOTriX Aug 30 '22 at 06:26
  • 3
    Looks like infinite loop. After `temp = n % 10;` add `n = n / 10;` – Fe2O3 Aug 30 '22 at 06:26
  • 1
    C and C++ are two *very* different languages. Please don't spam irrelevant tags, only the tags of the language you're actually program in. – Some programmer dude Aug 30 '22 at 06:27
  • And besides a debugger, even some [rubber duck debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging) could help. – Some programmer dude Aug 30 '22 at 06:28
  • sorry , didn't know what tags more to mention. 5 was minimum – prajwal Aug 30 '22 at 06:29
  • Not directly related, but `t` is a *signed* (!) `int` – you should check for *negative* input to avoid unexpected results (always expect the dumb user...). – Aconcagua Aug 30 '22 at 06:30
  • What value are you entering for `t`?? – Peter Aug 30 '22 at 06:32
  • Rather than apologising you might just remove the surplus tag – I did so for you, dropping `C++` as the code rather hints to C (explicit `void` as parameter list, C headers instead of C++ ones). If I erred, sorry – then please fix yourself ;) – Aconcagua Aug 30 '22 at 06:33
  • @Fe2O3 Is right, but you can have that much simpler: Drop that `temp` variable entirely, you do not re-use the original value of `n` anyway, so you can just work on that one. I personally would prefer a `for` loop in this specific case: `for(;n; n/= 10) { switch(n % 10) { /* body of your switch statement as has been */ } }` – Aconcagua Aug 30 '22 at 06:37
  • 1
    Debugging print statements should have a newline at the end of the format string — the output may not appear in a timely fashion if it does not. You should test the return value from `scanf()` to ensure it read data — and when things are going wrong, you should print the value it read to ensure the program received what you expected. Are you missing a statement such as ``n /= 10;`` in the loop? – Jonathan Leffler Aug 30 '22 at 06:39
  • By the way: A digit of 0 adds 6 to the count – so when the *initial* value of `n` already is 0 – what would be the expected result? 0 – then you don't need to add anything – or 6 – then you need to catch the special case: `int mat(int n) { if(n == 0) { return 6; } /* rest of function */ }` – Aconcagua Aug 30 '22 at 06:42

1 Answers1

0

Forgive me. A common mistake of beginners is to "get into the weeds" without properly considering the nature of a problem. Lines and lines of code are written that fiddle with particular micro-aspects of the problem.

From the OP code, it appears you want to sum various amounts to a total based on the digits found in the user's number. Since 0-9 are "contiguous", a contiguous array of those values (with duplicates) can serve and reduce the amount of code that is copy/paste/adapted. You will spend many more hours READING code than writing code. Try to make the code as simple to understand (and reliable) as possible. Potential bugs will LEAP OFF THE SCREEN when you do this and your code will be appreciated by others.

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

int mat( int n ) {
    printf( "hello\n" );

    // contiguous array of 10 values (some repeated)
    int incr[] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 };
    int count = 0;

    // sum from array values based on digits of value received
    do {
        count += incr[ n % 10 ];
    } while( n /= 10 );

    return count;
}

int main( void ) {
    int t = 0; // ALWAYS initiaialise variables
    scanf( "%d", &t );

    while( t-- ) {
        int n = 0; // declare variables close to use
        scanf( "%d", &n );
    
        // No need for 'h'. print returned value directly.
        printf( "%d  ==>  %d\n", n, mat( n ) );
    }
}
Fe2O3
  • 6,077
  • 2
  • 4
  • 20
  • 1
    I was trying to do this problem [link](https://www.codechef.com/viewsolution/72723987), and finally figured out the answer, but I am highly grateful to u for taking the time to find out and write a solution for it. – prajwal Aug 31 '22 at 08:03
  • @prajwal Learning is a journey. `:-)` – Fe2O3 Aug 31 '22 at 08:12