-1

Ok, so I need to input a matrix of n elements and to replace each composite number with the closest prime number. I made functions for reading the matrix, showing the matrix, finding if a number is prime or not and finding the closest prime number to a number and they work.

Here is what I did and the problem is that the replacement does not work and I get the error: Process terminated with status -1073741510 (0 minute(s), 18 second(s))

#include <stdio.h>
#include <stdlib.h>
int n;

int readMatrix(int **matrix)
{
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            printf("matrix[%d][%d]=", i, j);
            scanf("%d", &matrix[i][j]);
        }
    }
    return matrix;
}

void showMatrix(int **matrix)
{
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

int prime(int a)
{
    int c = 0;
    for (int i=1; i<a; i++) {
        if (a % i == 0) {
            c++;
        }
    }
    if (c == 1) {
        return 1;
    } else return 0;
}

int nearPrime(int b)
{
    int lp, bp, ok = 0, p;
    lp = b - 1;
    bp = b + 1;
    while (ok != 1) {
        if (prime(lp) == 1) {
            ok = 1; break;
        }
        lp--;
    }
    ok = 0;
    while (ok != 1) {
        if (prime(bp) == 1) {
            ok = 1; break;
        }
        bp++;
    }
    if ((b-lp) < (bp-b)) {
        p = lp;
    } else p = bp;
    return p;
}

int main()
{
    int **matrix, aux;
    printf("n=");
    scanf("%d", &n);
    matrix = malloc(n*sizeof(int *));
    if (matrix == NULL) exit(1);
    for (int i=0; i<n; i++) {
        matrix[i] = malloc(n*sizeof(int));
        if (matrix[i] == NULL) exit(1);
    }
    readMatrix(matrix);
    showMatrix(matrix);
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            if (prime(matrix[i][j]) == 0 && matrix[i][j] != 1) {
                matrix[i][j] = nearPrime(matrix[i][j]);
            }
        }
    }
    showMatrix(matrix);
    for (int i=0; i<n; i++) free(matrix[i]);
    free(matrix);
    return 0;
}

Can you tell me why it is not working?

UPDATE

I think I solved it. When checking if a matrix number is prime I also added the condition that it needs to be different than 1 because the program will return 0 when checking if 1 is prime number and it needs to return 1 since 1 is actually a prime number.

Thanks for all the tips!

Ghost
  • 401
  • 3
  • 15
  • Doesn't the compiler tells you what's wrong? It should be shouting a lot of warnings at you, listen to the compiler, don't ignore its warnings. – Some programmer dude Jan 21 '19 at 12:38
  • The only warning I get is the one I wrote down. – Ghost Jan 21 '19 at 12:39
  • 1
    Also please [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). With a debugger you can catch crashes as they happen, to learn when and where in your code the crash happened. A debugger will then let you examine values of variables to make sure they seem legit and valid. – Some programmer dude Jan 21 '19 at 12:40
  • Then your compiler is too polite. Make it throw a `-Wall` of warnings at you. I.e. use that commandline option. – Yunnosch Jan 21 '19 at 12:40
  • 3
    *One* major problem: `matrix[i] = malloc(n*sizeof(char));` Does that seem correct, to allocate memory for an array of `char`, and then treat it as an array of `int`? – Some programmer dude Jan 21 '19 at 12:41
  • @Someprogrammerdude I did not even see that mistake. But still, it does not work, same error. – Ghost Jan 21 '19 at 12:42
  • @iBug Yes, I'm sorry. I updated it. – Ghost Jan 21 '19 at 12:44
  • As mentioned by @Yunnosch, please rebuild with more warning enabled. Start by reading the warnings you get, trying to understand what they mean and how to fix the root causes of them (and not simply add some casting to silence the compiler). That will go a long way in making the code more robust. – Some programmer dude Jan 21 '19 at 12:45
  • @Someprogrammerdude I tried it running more times, with more examples. From what I saw, the problem is when I input 1 as a matrix element. I will have a look why. – Ghost Jan 21 '19 at 12:47
  • 1
    Or you could skip all this and do it correctly instead. [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays) – Lundin Jan 21 '19 at 12:47
  • `array = malloc(n * sizeof(array[0]))` avoids the problem of using the wrong type. – molbdnilo Jan 21 '19 at 12:50
  • Actually your prime function does *not* return 1 if the number is 1, since in that case the loop is zero-trip and c is 0, so it will return 0 (as it should). – Tom Karzes Jan 21 '19 at 12:51
  • @TomKarzes Hmm, you are right. Ok, I used `prime(1)` in main and it returns 0 as you said. I don't know what is the problem then since adding `&&matrix[i][j] != 1` solved it. I think the problem is that it returns 0 and it should return 1. – Ghost Jan 21 '19 at 12:54

1 Answers1

1

I think I may have found the issue in your code. The problem lies in your nearPrime function. It will work for most numbers that it takes in as an argument but that is not the case if you input the number 1 for this function. Consider what will happen if it does take in the number 1.

Then, your variable lp = (1 - 1) = 0. When you then further input this into the prime function, it will not return 1 because of the way it is implemented. You then keep on decreasing this number if no prime is found and since the number is now negative, it will never even enter in the for loop in the prime function and the prime function will then always return 0. Thus, you will get stuck in the while(ok != 1) loop for a really long time and that is why your process terminates. To fix this, make sure you check that lp != 0 before proceeding to enter the loop. Since you are also checking for the nearest prime, you also need to check if lp is 0 before returning a value. In short, make the following change to your code.

int nearPrime(int b)
{
    int lp, bp, ok = 0, p;
    lp = b - 1;
    bp = b + 1;
    if (lp != 0)
    {
        while (ok != 1) {
            if (prime(lp) == 1) {
                ok = 1; break;
            }
            lp--;
        }
    }

    ok = 0;
    while (ok != 1) {
        if (prime(bp) == 1) {
            ok = 1; break;
        }
        bp++;
    }
    if (((b - lp) < (bp - b)) && lp != 0) {
        p = lp;
    }
    else p = bp;
    return p;
}

One other thing: your readMatrix function seems to expect a return type of int but you are returning the argument matrix, which is of type int**. Furthermore, in your main code, you are not actually doing anything with your returned value so probably change the return type of your readMatrix function to void (and don't return matrix, of course).

Alaiko
  • 185
  • 2
  • 7