-1

I'm currently working on code that reads in a sequence of integers in the form of m1, n1, m2, n2, until I input a zero, and it prints the sum of m * n. Here is what I have so far:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main()
{
    int m, n, i, sum = 0;
    bool plus = true;
    scanf("%d", &m);
    scanf("%d", &n);
    for(i = m; i <= n; i++)
    {
            sum = sum + (m * n);
            if(!plus)
            {
                putchar('+');
            }
            printf("%d*%d", m, n);
            plus = false;
    }
    printf("=%d\n", sum);
    return 0;
}

If I type in 1, 1, 0, it prints out 1 * 1 = 1, but if I were to type in, 1, 2, 3, 4, 0, it prints out 1*2+1*2=4. I'm just confused on how I get it to calculate 1*2 and 3*4.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 2
    You only read two numbers. `for` loop seems to be useless here, you need a `while` loop checking the input values. You really should *seriously* refine your logic. – Eugene Sh. Mar 19 '18 at 14:19
  • I think you may encounter this problem as well: https://stackoverflow.com/q/5240789/10077 – Fred Larson Mar 19 '18 at 14:20
  • 1
    When you've fixed this problem, don't forget to go back and check the return value from `scanf()` before reading `m` or `n`. – Toby Speight Mar 19 '18 at 14:44

4 Answers4

2

you never read the 3rd and 4th arguments as the scanf calls are outside the loop. If you put them and the plus initializtion inside the loop, and correct the loop initilization and termination conditions your code should work (If I am not missing anything else).

Ofir
  • 8,194
  • 2
  • 29
  • 44
1

I'm currently working on code that reads in a sequence of integers in the form of m1, n1, m2, n2, until I input a zero

It seems the approach you are using in whole is wrong. You should not enter the variables m and n. At least I do not see where 0 is entered and checked in your code.

What you are doing is calculating

sum = sum + (m * n);

n - m + 1 times in the loop. Because m and n correspondingly were entered like 1 and 2 you got 4 (1 * 2 + 1 * 2).

I can suggest the following approach.

#include <stdio.h>

int main(void) 
{
    printf( "Enter a sequence of integers (0 - exit): " );

    long long int sum = 0;
    int value, prev_value;
    unsigned int i;

    i = 0;
    while ( scanf( "%d", &value ) == 1 && value != 0 )
    {
        if ( i % 2 )
        {
            if ( i != 1 ) printf( " + " );
            printf( " %d * %d ", prev_value, value );
            sum += ( long long int )prev_value * value;
        }
        else
        {
            prev_value = value;
        }

        ++i;
    }

    if ( i % 2  )
    {
        if ( i != 1 ) printf( " + " );
        printf( "%d", prev_value );
        sum += prev_value;
    }

    printf( " = %lld\n", sum );

    return 0;
}

The program output might look like

Enter a sequence of integers (0 - exit):  1 2 3 4 0
1 * 2  +  3 * 4  = 14
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

You don't want a for loop like that, as you want to loop an indefinite amount. My recommendation is to use while (true), and break from the loop when you reach the termination condition (or when scanf() fails to read both inputs):

#include <stdio.h>
#include <stdbool.h>

int main()
{
    int sum = 0;
    char const *plus = "";
    while (true)
    {
        int m, n;
        if (scanf("%d%d", &m, &n) != 2)
            break;
        if (m == 0 || n == 0)
            break;

        sum += m * n;
        printf("%s%d*%d", plus, m, n);
        plus = "+";
    }
    printf("=%d\n", sum);
}

A couple of other improvements in there: I've reduced the scope of m and n to be inside the loop, and I've changed plus to refer to the actual string to insert, rather than indirecting through a boolean.

Arguably, you might want to separate the scanf() back into the two calls you had, so that the terminating 0 need not be paired:

    if (scanf("%d", &m) != 1 || m == 0)
        break;
    if (scanf("%d", &n) != 1 || n == 0)
        break;
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
-1
#include <stdio.h>
#include <stdlib.h>

int main(void)
{

  int sum, m, n;

  if (!scanf("%d", &m))
    return 0;

  while (m)
  {
    if (!scanf("%d", &n))
      return 0;

    sum += m * n;

    if (!scanf("%d", &m))
      return 0;
  }

  printf("%d", sum);
}

Try this. I wrote it quickly, but it should work. Code also should check if number of arguments is correct (situation when user input is X 0 or X X X 0 etc.). Issues spotted in your code:

  1. User input outside the loop
  2. No scanf() returns check
  • Naming it "full solution" is quite exaggerated. It's just a while loop with few scanf() functions in it. There is still a lot of space to make it complete and reliable. – Bartek Woźniak Mar 19 '18 at 15:04