0

I am a beginner. I'm trying to solve a pair-sum program. The program calls for the number of pairs being added and the actual pairs, then it returns the sum of each pair. I'm using an array to store the sum of each pair, but when I print out every element the last one is always wrong. This is what I have so far:

int c, val1, val2, x, i;

printf("Enter the number of pairs to sum: \n");
scanf("%d", &c);
int sum [c];
printf("Enter the pairs: \n");
for ( x = 0; x < (c - 1); ++x)
{
    scanf("%d", &val1); 
    scanf("%d", &val2);
    sum [x] = val1 + val2;
    val1 = 0;
    val2 = 0;
}

printf("The sum of each pair is: \n");
for (i = 0; i < c; ++i)
{
     printf("%d\t", sum[i]);
}
3
  • 1
    You have (c-1) in one loop, and not the other... Commented May 13, 2014 at 22:01
  • Incidentally I don't see why you need to care how many numbers there are...you could solve this by outputting the sum of each pair right after the user types it in. Commented May 13, 2014 at 22:09
  • @nneonneo he wants to not display the output until after all input has been entered Commented May 14, 2014 at 6:00

1 Answer 1

1

Change this

for ( x = 0; x < (c - 1); ++x)

to this:

for ( x = 0; x < c; ++x)

You are going until (c-1), where you print c times.

So, after the fix, your loop will be like this:

printf("Enter the pairs: \n");
for ( x = 0; x < (c - 1); ++x)
{
    scanf("%d %d\n", &val1, &val2 );
    sum [x] = val1 + val2;
    val1 = 0;        // Do you really need me?
    val2 = 0;        // and me?
}

You do not need to nullify val1 and val2 at the end of every loop.

Think the first time you enter the loop. These variables are uninitialized, but they get initialized by scanf().

Let's say the user typed 0 and 1, so your variables got these values respectively.

Then, you set them to zero.

Then, you go again inside the loop and the variables get assigned the input of the user.

And so on...

Think how this would work, if you would discard this step:

Then, you set them to zero.

It would be the same!

The only difference is that when you exit the loop, these variables will have what the user typed for the last run of the loop and not the zero values, but I think that's not a problem. :)

Sign up to request clarification or add additional context in comments.

4 Comments

There would be a difference if he types some words instead of numbers during the number input stage (all the number reads would fail and so it would use the last value set).
@MattMcNabb I do not think that this is what (s)he tried to do. I would say that (s)he thought he needed that for the input procedure as numbers.
Turns out something about scanf("%d %d\n... was messing up what was being recorded in each element of the array. (c - 1) was also incorrect, I don't know why I thought that would work. Thanks for the help!
@sushiknives, nice that you worked out it by yourself. This means that you tried, I am going to up-vote your question. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.