"SIGSEGV ON THREAD", does anyone help me to solve, and also if possible, to complete the program?

0

It's the following, I do an info technician, and I'm learning C just two months, and I know little yet. Recently my teacher asked to make a program in which user entered with two number num, and that each position of two vectors n, were filled in decreasing form with each digit of that number. Ex: The user entered 284, the vector (of 100 positions) will have pos 100 = 0, a 99 = 0, (...) a 3 = 2, a 2 = 8 and a 1 = 4. And from there, we need to simulate an addition, a subtraction a division and a multiplication, working only with these vectors.

The output of the addition part would look something like this:

       1
[0][0][2][5][4]
+
[0][0][1][9][4]
=
[0][0][4][4][8]

Well complicated knowing that you can only work with the vector houses right?!

This is the problem, hence I am not able to proceed because of the error of the title, see my code:

    #include <stdio.h> 
int n[10], n2[10], num, nun, i=9, j=9, sv=0, on, res[4][20], opc[4][20];
main(){
        printf("Insert the 1st number: ");
        scanf("%d", &num);
        sv = num;
        while (num != 0)
    {
            n[i] = num%10;
            num = num/10; 
            i--;
    }
        num = sv;
        printf("Insert the 2nd number: ");
        scanf("%d", &nun);
        sv = nun;
        while (nun != 0)
        {
            n2[j] = nun%10;
            nun = nun/10; 
            j--;
        }
        nun = sv;
        printf("Select an operation (1=addition; 2=subtraction; 3=multiplication; 4=division): ");
        scanf("%d", &on);
        while((on>4)||(on<0)){
            printf("Try again: ");
            scanf("%d", &on);
        }
        for (j=0; j<4; j++){
            for (i=19; i>=0; i--){
                opc[j][i]=0;
            }
        }
        if (on==1)
        {
        for (i=10; i>=1; i++){
            if ((n[i] + n2[i] + opc[1][10+i]) <= 9)
            {
                opc[1][10+i] += (n[i] + n2[i]);
            }
            if ((n[i] + n2[i] + opc[1][10+i]) > 9)
            {
                opc[1][10+i] += (n[i] + n2[i])%10;
                opc[1][9+i] += (n[i] + n2[i])/10;
            }
        }
        for (i=0; i<=9; i++){
            printf("|%d|", n[i]);
        }
        printf("\n+\n");
        for (i=0; i<=9; i++){
            printf("|%d|", n2[i]);
        }
        printf("\n=\n");
            for (i=19; i>=0; i--){
        printf("|%d|", opc[1][i]);
        }
    }
    else
    {
        printf("Coming soon :)");
    }   
}

If someone can help me solve this error, or even suggest how I can do the rest of the program, I would be very grateful, I am already working on this for a week, doing the other exercises, but this is extremely difficult.     

asked by anonymous 19.06.2018 / 02:36

0 answers