c - Calling a function within a loop -
i trying simple addition function take number x
array , add variable i
it, iteration variable inside loop. instead of adding each variable individually , producing output of :
3, 2, 7
it produces values of
3, 5, 13.
#include <stdio.h> int add(int x[], int y); int main(void) { int i; int a[3] = {3, 1, 5}; for(i=0; i<3; i++) { printf("%d \t", i); printf("%d, %d, equals %d \n", a[i], i, add(a[i], i)); } return 0; } int add(int x[], int y){ return x+y; }
try
int add(int x, int y){ return x+y; }
Comments
Post a Comment