Problem 14 C

Back to Problem Solutions forum

MontBlanc     2015-08-12 19:36:19

So I'm not sure what I'm doing wrong... When I printf I seem to be doing my additions and multiplications correctly. I also retrieve the number with which I do the modulo successfully.

Here's my code :

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

    int main()
    {
        int c; double a; char b='0';
        scanf("%lf\n",&a);
        do{
           if(b=='+') a=a+c;
           else if(b=='*') a=a*c;
           scanf("%c %d\n",&b,&c);
        }while(b!='%');
        a=fmod(a,c);
        k=floor(a+0.5);
        printf("%d",k);
        return 0;
    }

I had to use a double type because other types do not support such big numbers, I got negative numbers with long.

Christopher Matthews     2015-08-13 07:45:49

There have been several posts in the forum about this very problem, if you search back a bit. However, the point of this problem is to understand how to use modulo to keep a number within certain "bounds", called Z/NZ. When you take a number modulo n, the number you get back is always guaranteed to be less than n. That said, you can use modulo any time throughout your algorithm to keep the number within bounds. You can set up a type of checking system to decide when the number is large enough to perform modulo, or you could just perform the modulo at each step without checking. The result will be the same. You will probably find that the best way to do this is to read all the lines in before starting your algorithm.

I hope I have been of some help. You will see these types of situations a lot as a programmer, so it is a must that you learn to understand it. You can read about modular arithmetic and Z/NZ on Code Abbey and Wikipedia.

-- Christopher P. Matthews

Christopher Matthews     2015-08-13 07:50:48

One more thing: I've looked at some of your solutions, and I would suggest writing small functions to accomplish specific tasks and building your program that way, rather than doing everything in main. For one, it is very hard for another person reading your code to understand what is going on. For two, when you use functions, it adds a logical flow to the program that makes it much easier to deduce what is going on. Also, use comments!

-- Christopher P. Matthews

MontBlanc     2015-08-13 12:14:12

Thank you very much for your reply. I thought about using modulo for every operation, the problem is that I would have to browse through all values first, but then I have no idea how to "reset" my scanf to the beginning. Well, as I you would have guessed (even more so by checking my 'solutions', haha...) I'm quite the beginner at programming. Anyhow, I'll look into it.

Thanks again for your time.

Christopher Matthews     2015-08-14 06:26:27

You're quite welcome. See, the problem is not "reseting" scanf; rather, you want to read in all lines of stdin, and store then in an array, rather than read through all of them every iteration. It will save you a lot of time and computing power.

I'm glad I could be of help.

-- Christopher P. Matthews

OldSchool     2015-09-17 08:15:38

EN: I wonder, who implemented an algorithm, that solves this problem with these initial data.
RU: Мне интересно у кого реализован алгоритм, который решит Эту задачу с такими исходными данными :)

(((176896932454305448757139039745644361794723334152282364694666
* 23)
+ 8)
% 1054)

Guy Gervais     2015-09-17 09:59:23
User avatar

666?

OldSchool     2015-09-17 10:45:00

One more: 1792475871468546560184376981937546814325108469057381567414320846758435690512476571046589784158076465784378765714058178432758168457873779143
%1054

Guy Gervais     2015-09-17 12:54:16
User avatar

No problem, it's 499.

I posted my updated solution which deals with an arbitrarily large starting value without resorting to a BigInteger library.

Vadim Tukaev     2015-09-17 15:52:18

MontBlanc, googling "singly linked list in C language".

OldSchool     2015-09-17 16:22:52

Guy Gervais, you are Right :) 666 and 499

Rodion (admin)     2015-09-18 17:05:27
User avatar

Sad thing - but people coding in Python often even do not realize it is a hard problem for they have long arithmetics just out of the box. So OldSchool you should not take them into account :)

Guy Gervais     2015-09-18 17:14:16
User avatar

If I'm not mistaken, OldSchool wrote the beginning of a "BigInteger" class using strings in VB6. It certainly isn't the easiest way to solve this problem, but it's pretty impressive in it's own way.

Rodion (admin)     2015-09-19 05:33:26
User avatar

>but it's pretty impressive in it's own way.

Oh yes - I've done this myself few times in my life and I remember that especially implementing division operation is quite a boring thing - so I also was impressed by OldSchool's solution :)

OldSchool     2015-09-19 11:11:03

:)

Please login and solve 5 problems to be able to post at forum