Home > Back-end >  Process finished with exit code -1073741571 (0xC00000FD)
Process finished with exit code -1073741571 (0xC00000FD)

Time:11-17

The aim of the exercise is to allocate n-lines of the triangle of tartar. My idea was to use pointers to pointers to allocate it. However when runned the process ends in this way: Process finished with

here is the code:

#include <stdio.h>
#include <stdlib.h>
int somma (int x) {
int sum=0;
for (int i=0; i<=x; i  ) {
  x=x-i;
  sum =x;
}
return sum;
} 


  void stampa (int **a, int x) {
    *(*(a 0) 0)=1;

    for (int j=1; j<x; j  ) {
        *(*(a 0) j)=0;
    }

    for (int i=1; i<x; i  ) {
        *(*(a i) 0)=1;
        for (int j=1; j<x; j  ) {
            *(*(a i) j)=*(*(a i-1) j-1) *(*(a i-1) j);
        }
    }
    for (int i=0; i<x; i  ) {
        for (int j=0; j<=i; j   ) {
            printf(" =", *(*(a i) j));

        }
        printf("\n");
    }
 }
int main() {
   int x, **mat=NULL;
   printf("Inserisci x:"); //insert x.
   scanf("%d", &x);
   int sum=somma(x);

 
   mat=(int**)malloc(sum*sizeof(int*));
   if (mat==NULL) {
       return 1;
   }
    stampa(mat, x); //print function.

  return 0;
}

CodePudding user response:

isn't *(*(a i-1) j) equal to a[-1] since you declared int i=0 in the loop just above?

CodePudding user response:

So, I took Tartaglia's triangle, I opened VS Code and I wrote an algorithm (It's night and I'm a beginner, don't scream). I hope it can help you. A couple of notes before the code:

  1. I use square brackets and array arithmetic, but you can use your preferred notation, it should work the same.
  2. The algorithm won't be great or the most efficient but it works.
  3. The program creates a x*x matrix (dinamycally allocating an array to arrays), then it puts the right numbers at the right place.
  4. The tartaglia function only allocates the needed numbers, but for easier code you can just fill the whole matrix.
  5. The matrix can be thought as watching the triangle after tilting your head to the right, so the first row basically corresponds to the right side of the triangle; the stampa function however prints the actual lines, so it loops through the matrix diagonally.

This is a basic program that works, so you have a base to work on. Remove what's useless and modify it as you need.

#include <stdio.h>
#include <stdlib.h>


void tartaglia(int **matrix, int x){
    int k=x-1; // I'm initializing only values I'll print, so the number of columns decreases for each row
    for (int i=1; i<x-1; i  ){
        for (int j=1; j<k; j  ){
            matrix[i][j] = (matrix[i][j-1])   (matrix[i-1][j]); // sums the two values above in the triangle
        }
        k--;
    }
}

void stampa(int **matrix, int x){
    for (int line=0; line<x; line  ){ //lines to print (go from 0 to x-1)
        int i = line;
        int j = 0;
        printf("\n");
        for (int z=0; z<=line; z  ){ // z= numbers in each line
            printf("%d ", matrix[i][j]);
            i--;
            j  ;
        }
    }
}

int main(){
    int x;
    //prompting input x, will be size of matrix (x*x)
    printf("Inserisci x:"); 
    scanf("%d", &x);

    //dynamically allocating pointer of pointers
    int **matrix = (int**) malloc(x*sizeof(int*));
    for (int i=0; i<x; i  ){
        matrix[i] = (int *) malloc(x*sizeof(int));
        for (int j=0; j<x; j  ){
            matrix[i][j] = 1; // just initializing the whole matrix
        }
    }

    tartaglia(matrix, x); //this function actually puts the values into the matrix
    stampa(matrix, x); //this function prints the values

}

Feel free to ask for any doubts.

  • Related