Note that there are some explanatory texts on larger screens.

plurals
  1. POFind the largest sum of 3 consecutive digits from number
    primarykey
    data
    text
    <p>I have a task to write the C program that finds the largest sum of 3 consecutive digits in numbers. I wrote it, but I have trouble: the program seems to work fine, but instead of correct answer it types some strange numbers. For example, if input is "560315", the response is "155". However, if I type "560415" the answer is still "155" and if I type "561315" the response changes to "156". Is it a problem with encoding? Here's the code:</p> <pre><code>#include &lt;stdio.h&gt; int isempt(int a) { if ((a==' ')||(a=='\n')||(a=='\t')) { return 1; } else return 0; } main() { int a, b, c, d, e, i, maxsum; a = 0; b = 0; c = 0; d = 0; maxsum = 0; i = 0; int counter = 0; //the variables "numsearch" and "inside" indicate whether program is in the number or outside it int numsearch = 1; int inside = 0; while ((i = getchar())!=EOF) { if (numsearch==1) { if (isempt(i)==0) { numsearch = 0; inside = 1; a = i; counter++; } } else if ((isempt(i)==0)&amp;&amp;(inside==1)) { if (counter == 1) { b = i; counter++; } else if (counter == 2) { c = i; counter++; } else if (counter == 3) { d = i; maxsum = a+b+c; if ((b+c+d) &gt; maxsum) { maxsum =( b+c+d); } a =b; b = c; c = d; counter++; } else if (counter == 4) { d = i; if ((b+c+d)&gt;maxsum) { maxsum = b+c+d; } a=b; b=c; c=d; } } else if ((counter&gt;=3)&amp;&amp;(isempt(i)==1)) { printf("\n%d\n", maxsum ); counter = 0; numsearch = 1; inside = 0; a = 0; b = 0; c = 0; d = 0; } else { counter = 0; numsearch = 1; inside = 0; a = 0; b = 0; c = 0; d = 0; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload