stuck on task 14

Back to General discussions forum

crashmatusow     2016-01-03 00:50:52

hey guys i'm trying to distribute through the modulate and hitting a dead end. (code in c)

include <stdio.h>

int main(int argc, char* argv[]) {

char op[100];
int x,stepcount;
int y[100];
stepcount = 0;
scanf("%d", &x);
for(;;) {
    scanf("%c %d", &op[stepcount], &y[stepcount]);
    if(op[stepcount] == '%') {stepcount++;break;}
    stepcount++;
}
int term = y[stepcount-1];
for(int i = 0; i < stepcount; i++) {
    if(op[i] == '*'){x = x * y[i] % term;}
    if(op[i] == '+'){x = (x + y[1]) % term;}
}
x = x % term;
printf("%d", x);

return 0;

}

Quandray     2016-01-03 08:44:53
User avatar

Hi,

After your second scanf, try adding the following line, so you can see what you are reading

printf("Read %c %d\n",op[stepcount],y[stepcount]);
Matthew Cole     2016-01-05 04:11:24

Hi crashmatusow!

One thing I will also suggest is that now is the time to learn the power of a debugger. Quandray has told you to insert what's called a trace print. This can work for simple problems, but eventually your code will be littered with these traces for difficult problems/bugs. Since CodeAbbey is output sensitive (ie. printing too much is just as wrong as not printing the right thing), forgetting to remove just one of these traces can cause you to fail the problem. Oops!

Therefore, I recommend learning a debugger. It's well worth your time investment.

Here are the canonical command line oriented debuggers: (Disclaimer: I'm a contributor to the LLVM/LLDB project, so I'm partial to that one.)

  • Linux/Unix/Mac OS X and Windows(with moderate effort): GDB and a tutorial
  • Mac OS X (preinstalled since XCode 3.1 and OS X Leopard 10.5)/Linux and Unix (with easy effort)/Windows (with easy to moderate effort): LLDB and a tutorial

But what if you hate the command line? We've got graphical debugger interfaces built on GDB, LLDB or the Visual Studio IDE!

If you had used a debugger on your particular question, you'd have seen that line 7 is not doing what you think it should be doing, and is thus breaking your control flow in line 12. How to fix it? I leave that up to you...

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