Recursion and sum

0

I'm doing a program in which you need a recursion according to the exercise that sums up a vector of two arrays and sums the lines after recursion and then adds the columns following my code

#include <stdio.h>
#include <conio.h>

int recursion(int array[3]){

    int i  = 1;

    for(i=0;i<3;i++){

        if(i == 0){
            return 0;
        }
        else if(array == 1){
            return 1;
        }

        return recursion(array[i]+array[i+1]);
        printf("%d",array[i]);
    }
}



int main ( ) {
    int array[3][3];
    int arraytemp[3];

    int i,j;
    for(i=0;i<=3;i++){
        arraytemp[j]=0;
        for(j=0;j<=3;j++){
            scanf("%d", & array[i][j]);
            arraytemp[j]=array[j];
        }
        recursion(arraytemp);    
    }
}
    
asked by anonymous 18.03.2016 / 01:28

1 answer

0
#include <stdio.h>
#include <conio.h>

int recursion(int array[3]){

    int i  = 1;                                // ==> nota a)

    for(i=0;i<3;i++){                          // ==> nota a)

        if(i == 0){                            // ==> nota b)
            return 0;
        }
        else if(array == 1){                   // ==> nota c)
            return 1;
        }

        return recursion(array[i]+array[i+1]); // ==> nota d)
        printf("%d",array[i]);                 // ==> nota e)
    }
}



int main ( ) {
    int array[3][3];
    int arraytemp[3];

    int i,j;
    for(i=0;i<=3;i++){                         // ==> nota f)
        arraytemp[j]=0;                        // ==> nota g)
        for(j=0;j<=3;j++){                     // ==> nota f) outra vez
            scanf("%d", & array[i][j]);
            arraytemp[j]=array[j];             // ==> nota h)
        }
        recursion(arraytemp);    
    }
}                                              // ==> nota i)

Notes:

a) First you get i a 1 and soon after you get to 0 . You do not need the first assignment.

b) The first time the loop is executed, the value of i will be 0 (that's what for ). So the first thing the loop does is to terminate the function with return of this instruction.

c) The program never arrives here (see note b)). If it arrived it would compare an array (the array with name array ) with an integer. Arrays and integers are not comparable! You have to be more attentive to the things you compare.

d) The program never arrives here (see note b)) ... The recursion() function is defined as accepting an array parameter. The array[i] + array[i + 1] parameter is of type int which is not compatible with the type expected by the function. Once again: you have to be careful about the type of variables. The return causes the function to terminate.

e) The program never arrives here (see note b) and note d)) ...

f) with for (i = 0 ; i <= 3; i++) { /* ... */ } the cycle will run 4 times. The normal form of for is first an assignment, then a comparison with < (not <= ) and finally the increment. When you see a different form, it is soon to suspect that something is wrong. The normal version of your cycle would be for (i = 0; i < 3; i++) { /* ... */ } .

g) Why j ? ???? The variable j has not yet been assigned any value. You can not use it as an index of an array.

h) arraytemp[j] has type int ; array[j] has array type of 3 int. You can not assign an array to an object of type int . Pay attention to the types of objects you use

i) Turn on the maximum warnings of your compiler. Pay attention to these warnings.

Happy Coding

    
18.03.2016 / 10:14