0

I want to try to get two arrays and sum them to another array, but it just doens't work, and I don't know why.

My code :

int v1[3],v2[3],v3[3];

for(int i  = 0 ; i < 3; i++) {
    printf("Type a number : \n");
    scanf("%d", &v1[i]);
}

for(int i  = 0 ; i < 3; i++) {
    printf("Type a number : \n");
    scanf("%d", &v2[i]);
}

for(int i  = 0 ; i < 3; i++) {
 v3[i] = v1[i] + v2[i];
 scanf("%d", &v3[i]);

 printf("Total : %d ", &v3[i]);
}

And when I get to type all the 6 numbers, he just don't show Total, and I have to type more to go to exit screen.

Screenshot :

8
  • 4
    Why do you need a scanf for the result array? ("scanf("%d", &v3[i]);") Commented May 31, 2017 at 3:47
  • I just wanted to show the result for the user. But as I am new to C, I am newbie to this stuff. Commented May 31, 2017 at 3:50
  • Read the answers more carefully. Commented May 31, 2017 at 3:55
  • The final result continue being the same as from the old code, I don't know what is happening. Even if I put the printf out of the for or inside and with the modifications that the answers said, it's the same result. Commented May 31, 2017 at 3:57
  • Your main problem here is that you either compiled your program with no warnings enabled or that you ignored the warning(s). Commented May 31, 2017 at 4:32

6 Answers 6

2

What do you exactly want? Summation of each element from two arrays into a new third array? That's it right?

int main(int argc, char** argv) {
    int v1[3],v2[3],v3[3];

    for(int i  = 0 ; i < 3; i++) {
      printf("Type a number for v1 :\t");
      scanf("%d", &v1[i]);

      printf("Type a number for v2 :\t");
      scanf("%d", &v2[i]);
      // Add here
      v3[i] = v1[i] + v2[i]; // Mind you that this could lead to integer overflow.
  }

  printf("\nResult Arr :\n");
  for(int i  = 0 ; i < 3; i++)
    printf("%d\n", v3[i]);
}
Sign up to request clarification or add additional context in comments.

4 Comments

It worked the code, thanks. But as I am new to C, I don't know what that /t means. Could you explain to me, please ?
And what this (int argc, char** argv) will do ? Sorry for the newbie question.
The int main(int argc, char **argv) is the form for the main() function when you use command line arguments. This code doesn't use command line arguments; the main function should be defined using int main(void) to indicate that it is ignoring any command line arguments. The \t (note the backslash, not forward slash /t) is a tab. That's covered in any basic book on C.
@JonathanLeffler Thanks for explaning to me, I started to learn C almost 3 months ago, so a lot of things are new to me.
2

What is the purpose of scanf inside the third for loop?

I think remove scanf inside third for loop:

 scanf("%d", &v3[i]);

Also, remove & in printf:

printf("Total : %d ", v3[i]);

Full code: This code working fine on GCC compiler.

#include <stdio.h>

int main()
{
        int v1[3],v2[3],v3[3], i;

        for(i  = 0 ; i < 3; i++)
        {
                printf("Type a number : \n");
                scanf("%d", &v1[i]);
        }

        for(i  = 0 ; i < 3; i++)
        {
                printf("Type a number : \n");
                scanf("%d", &v2[i]);
        }

        for(i  = 0 ; i < 3; i++)
        {
                v3[i] = v1[i] + v2[i];

                printf("Total : %d\n", v3[i]);
        }
}

4 Comments

It does not work the same way, because the end result does not have the actual sum of the two vectors.
@Novatec: your code didn't show that calculation, nor did the question mention it. Changing the rules isn't fair — in extreme cases, the question becomes a 'chameleon question', and they're bad.
Arguably, the code should check the return value from each of the surviving scanf() calls. But your basic diagnosis is correct.
@JonathanLeffler I am sorry for what I did here, your code too works, thanks for helping me. I am new to C, so for me, the third scanf would read the third array and would work, but as it seems, it don't. Again, thanks for the help.
0

Delete the third scanf and you don't want the ampersand before the v3 in the printf.

1 Comment

It does not work the same way, because the end result does not have the actual sum of the two vectors.
0

First, initialize your array so you won't have a garbage.

`int v1[3] = {0,0,0}, v2[3] = {0,0,0}, v3[3] = {0,0,0};`

in adding both arrays,

for(int i = 0; i < 3; i++){
    v3[i] = v1[i] + v2[i];
    printf("total: %d", v3[i]);
}

Comments

0

If you want to store result in v3, you need to remove scanf("%d", &v3[i]); from last loop

You need to change

printf("Total : %d ", &v3[i]);//you are passing address here, where printf expects value

to

printf("Total : %d ", v3[i]);

Also you should add a space before %d in each scanf so that it takes care of enter hit from previous input eg

scanf(" %d", &v1[i]);

3 Comments

Is still showing Total : 6487568. Even after I removed the scanf, why is that happening ? If I let &v3[i] he just show that, and If I remove, he shows the Total as 2.
You need to remove & from printf as well
It does not work the same way, because the end result does not have the actual sum of the two vectors.
0

It is because you added a scanf function in the last for loop which does the summation. So if you just remove that scanf line it will work great. Old code:

int v1[3],v2[3],v3[3]; for(int i = 0 ; i < 3; i++) { printf("Type a  number : \n"); scanf("%d", &v1[i]); } for(int i = 0 ; i < 3; i++) {  printf("Type a number : \n"); scanf("%d", &v2[i]); } for(int i = 0 ; i < 3; i++) { v3[i] = v1[i] + v2[i]; scanf("%d", &v3[i]);    printf("Total : %d ", &v3[i]); }

New code should be

int v1[3],v2[3],v3[3]; for(int i = 0 ; i < 3; i++) { printf("Type a number : \n"); scanf("%d", &v1[i]); } for(int i = 0 ; i < 3; i++) { printf("Type a number : \n"); scanf("%d", &v2[i]); } for(int i = 0 ; i < 3; i++) { v3[i] = v1[i] + v2[i];  printf("Total : %d ", &v3[i]); }

That should do it.

1 Comment

It does not work the same way, because the end result does not have the actual sum of the two vectors.

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.