Reputation: 103
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 :
Upvotes: 0
Views: 38180
Reputation: 34568
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]);
}
}
Upvotes: 2
Reputation: 211
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]);
}
Upvotes: 2
Reputation: 21
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.
Upvotes: 0
Reputation: 4044
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]);
Upvotes: 0
Reputation: 83
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]);
}
Upvotes: 0
Reputation: 3661
Delete the third scanf and you don't want the ampersand before the v3 in the printf.
Upvotes: 0