Recursion and sum

Asked

Viewed 102 times

1

I am making a program in which you need a recursion according to the exercise that sums a vector of two matrices and sums the rows after the recursion and then add the columns to 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);    
    }
}
  • I don’t understand exactly what you want, how to pass the exercise you said (enunciation).

  • The header <conio.h> is not standard. Do not use anything that is set there. I take it from your includes.

1 answer

1

#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 metes i to 1 and right after that you put the 0. You don’t need the first assignment.

b) The first time the cycle is run, the value of i will be 0 (is what’s in the for). So the first thing the cycle does is to thermonymize the function with the 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 gets here (see note b)... The function recursion() is defined as accepting an array type parameter. The parameter array[i] + array[i + 1] is of the type int that is not compatible with the type expected by the function. Again: you have to be aware of the type of variables. The return causes the function to end.

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

f) with for (i = 0 ; i <= 3; i++) { /* ... */ } the cycle will be executed 4 times. The normal form of a for is first an assignment, then a comparison with < (nay <=) and finally the increment. When you see a different shape 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 no value has been assigned yet. You cannot use it as an array index.

h) arraytemp[j] has kind int; array[j] array type 3 int. You cannot assign an array to a type object int. Watch out for the types of objects you use

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

Happy Coding

Browser other questions tagged

You are not signed in. Login or sign up in order to post.