Question

****C PROGRAMMING**** Why isn't the determinant part of my code not running? I have included the...

****C PROGRAMMING****

Why isn't the determinant part of my code not running? I have included the attachments below and would greatly appreciate some help.

---------------------------------------

           case 6:
               printf("You chose determinate!\n\n");
               printf("Press 2 for 2x2\n OR \n Press 3 for 3x3 Matrix: ");
               scanf("%d", &detChoice);
               printf("\n");

               if (detChoice == 2) {
                   printf("Matrix: ");
                   scanf("%d", &detChoice);
                   printf("Enter elements in matrix of size 2x2: \n");

                  
                   printf("\n\nEnter the 4 elements of the array\n");
                   for (i = 0; i < 2; i++)
                       for (j = 0; j < 2; j++)
                           scanf("%d", &a[i][j]);

                   printf("\n\nThe entered matrix is: \n\n");
                   for (i = 0; i < 2; i++)
                   {
                       for (j = 0; j < 2; j++)
                       {
                           printf("%d\t", a[i][j]); // to print the complete row
                       }
                       printf("\n"); // to move to the next row
                   }

                   // finding the determinant of a 2x2 matrix
                   determinant = a[0][0] * a[1][1] - a[1][0] * a[0][1];
                   printf("\n\nDterminant of 2x2 matrix is : %d\n", a[0][0] * a[1][1], a[1][0] * a[0][1], determinant);

               }
               else if (detChoice == 3) {
                   printf("Matrix: ");
                   scanf("%d", &detChoice);
                   printf("Enter elements in matrix of size 3x3: \n");


                   printf("\n\nEnter the 9 elements of the array\n");
                   for (i = 0; i < 3; i++)
                       for (j = 0; j < 3; j++)
                           scanf("%d", &a[i][j]);

                   printf("\n\nThe entered matrix is: \n\n");
                   for (i = 0; i < 3; i++)
                   {
                       for (j = 0; j < 3; j++)
                       {
                           printf("%d\t", a[i][j]); // to print the complete row
                       }
                       printf("\n"); // to move to the next row
                   }

                   // finding the determinant of a 3x3 matrix
                   determinate = a[0][0] * ((a[1][1] * a[2][2]) - (a[2][1] * a[1][2])) - a[0][1] * (a[1][0]
                       * a[2][2] - a[2][0] * a[1][2]) + a[0][2] * (a[1][0] * a[2][1] - a[2][0] * a[1][1]);
                   printf("\nDeterminant of 3X3 matrix: %d", determinate);

               }
               else
                   printf("ERROR! Determinant is only available on a 2 by 2 or 3 by 3 Matricies.\n\n");
               break;

------------------------------

I need it for both 2 by 2 and 3 by 3 matricies depending on what the user is asking for. Thank you so much!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

There were certain changes that i did in the formula and also there were to extra scanf which were used out of not use. I am pasting the code that i made and after that i am pasting the code of your's with correction.

Code:

#include <stdio.h>

int main()
{
    int detChoice,i,j,determinant ;
     printf("You chose determinate!\n\n");
               printf("Press 2 for 2x2\n OR \nPress 3 for 3x3 Matrix: \n");
               scanf("%d", &detChoice);
               printf("\n");

               if (detChoice == 2) {
                   printf("Matrix: ");
                   //scanf("%d", &detChoice);
                   int a[2][2];
                   printf("Enter elements in matrix of size 2x2: \n");

                  
                   printf("\n\nEnter the 4 elements of the array\n");
                   for (i = 0; i < 2; i++)
                       for (j = 0; j < 2; j++)
                           scanf("%d", &a[i][j]);

                   printf("\n\nThe entered matrix is: \n\n");
                   for (i = 0; i < 2; i++)
                   {
                       for (j = 0; j < 2; j++)
                       {
                           printf("%d\t", a[i][j]); // to print the complete row
                       }
                       printf("\n"); // to move to the next row
                   }

                   // finding the determinant of a 2x2 matrix
                   determinant = ((a[0][0] * a[1][1]) - (a[1][0] * a[0][1]));
                   printf("\n\nDterminant of 2x2 matrix is : %d\n", determinant);

               }
               else if (detChoice == 3) {
                   printf("Matrix: ");
                   int a[3][3];
                   printf("Enter elements in matrix of size 3x3: \n");


                   printf("\n\nEnter the 9 elements of the array\n");
                   for (i = 0; i < 3; i++)
                       for (j = 0; j < 3; j++)
                           scanf("%d", &a[i][j]);

                   printf("\n\nThe entered matrix is: \n\n");
                   for (i = 0; i < 3; i++)
                   {
                       for (j = 0; j < 3; j++)
                       {
                           printf("%d\t", a[i][j]); // to print the complete row
                       }
                       printf("\n"); // to move to the next row
                   }
      
                   // finding the determinant of a 3x3 matrix
                   determinant  = (a[0][0] * (a[1][1] * a[2][2] - a[2][1] * a[1][2]))
                                    - 
                                    (a[0][1] * (a[1][0] * a[2][2] - a[2][0] * a[1][2]))
                                    +
                                    (a[0][2] * (a[1][0] * a[2][1] - a[2][0] * a[1][1]));
                   printf("\nDeterminant of 3X3 matrix: %d", determinant );

               }
               else
                   printf("ERROR! Determinant is only available on a 2 by 2 or 3 by 3 Matricies.\n\n");
              
    return 0;
}

Output:

You chose determinate! Press 2 for 2x2 OR Press 3 for 3x3 Matrix: Matrix: Enter elements in matrix of size 3x3: Enter the 9 e

Now below is the code you gave with corrections:

  case 6: 
 printf("You chose determinate!\n\n");
               printf("Press 2 for 2x2\n OR \nPress 3 for 3x3 Matrix: \n");
               scanf("%d", &detChoice);
               printf("\n");

               if (detChoice == 2) {
                   printf("Matrix: ");
                   //scanf("%d", &detChoice);
                   int a[2][2];
                   printf("Enter elements in matrix of size 2x2: \n");

                  
                   printf("\n\nEnter the 4 elements of the array\n");
                   for (i = 0; i < 2; i++)
                       for (j = 0; j < 2; j++)
                           scanf("%d", &a[i][j]);

                   printf("\n\nThe entered matrix is: \n\n");
                   for (i = 0; i < 2; i++)
                   {
                       for (j = 0; j < 2; j++)
                       {
                           printf("%d\t", a[i][j]); // to print the complete row
                       }
                       printf("\n"); // to move to the next row
                   }

                   // finding the determinant of a 2x2 matrix
                   determinant = ((a[0][0] * a[1][1]) - (a[1][0] * a[0][1]));
                   printf("\n\nDterminant of 2x2 matrix is : %d\n", determinant);

               }
               else if (detChoice == 3) {
                   printf("Matrix: ");
                   int a[3][3];
                   printf("Enter elements in matrix of size 3x3: \n");


                   printf("\n\nEnter the 9 elements of the array\n");
                   for (i = 0; i < 3; i++)
                       for (j = 0; j < 3; j++)
                           scanf("%d", &a[i][j]);

                   printf("\n\nThe entered matrix is: \n\n");
                   for (i = 0; i < 3; i++)
                   {
                       for (j = 0; j < 3; j++)
                       {
                           printf("%d\t", a[i][j]); // to print the complete row
                       }
                       printf("\n"); // to move to the next row
                   }
      
                   // finding the determinant of a 3x3 matrix
                   determinant  = (a[0][0] * (a[1][1] * a[2][2] - a[2][1] * a[1][2]))
                                    - 
                                    (a[0][1] * (a[1][0] * a[2][2] - a[2][0] * a[1][2]))
                                    +
                                    (a[0][2] * (a[1][0] * a[2][1] - a[2][0] * a[1][1]));
                   printf("\nDeterminant of 3X3 matrix: %d", determinant );

               }
               else
                   printf("ERROR! Determinant is only available on a 2 by 2 or 3 by 3 Matricies.\n\n");
    break;

Feel free to reach out to me in case of any doubt or clarification. I will be more than happy to reply.

You chose determinate! Press 2 for 2x2 OR Press 3 for 3x3 Matrix: Matrix: Enter elements in matrix of size 3x3: Enter the 9 elements of the array 10 20 30 17 13 21 19 29 31 The entered matrix is: 10 17 19 20 13 29 30 21 Determinant of 3x3 matrix: 2760 ... Program finished with exit code O Press ENTER to exit console.

Add a comment
Know the answer?
Add Answer to:
****C PROGRAMMING**** Why isn't the determinant part of my code not running? I have included the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • System Programming in C

    Explain what the problem is within the program. Fix the problem when you create a child process per column. The code is given below. So basically, after the child processes have successfully excuted their code, the final print statement does not give the correct values. It prints the original matrix rather than the multiplied matrix.#include  #include  #include  #include  int main(int argc, char *argv[]){ int row = 0; int column = 0; row = atoi(argv[1]); column = atoi(argv[2]); int *A = ...

  • I am trying to add a string command to my code. what am i doing wrong?...

    I am trying to add a string command to my code. what am i doing wrong? #define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <stdio.h> #include <string.h> #include <math.h> int main(void) {    int i, j;    int rowA, colA, rowB, colB;    int A[10][10], B[10][10];    int sum[10][10];    char str1[10];    printf("This is a matrix calculator\n");    //read in size from user MATRIX A    printf("Enter in matrix A....\n");    printf("\t#row = ");    scanf("%d", &rowA);    printf("\t#col = ");   ...

  • C program-- the output is not right please help me to correct it. #include <stdio.h> int...

    C program-- the output is not right please help me to correct it. #include <stdio.h> int main() { int arr[100]; int i,j,n,p,value,temp; printf("Enter the number of elements in the array: \n "); scanf("%d",&n); printf("Enter %d elements in the array: \n",n); for(i=0;i<n;i++) { printf("\nelement %d: ",i); scanf("\n%d",&arr[i]); } printf("\nEnter the value to be inserted: \n "); scanf("\n%d",&value); printf("The exist array is: \n"); for(i=0;i<n;i++) { printf("%d",arr[i]); } p=i; for(i=0;i<n;i++) if(value<arr[i] ) { p = i; break; } arr[p]=value; printf("\n"); for (i =...

  • I'm having trouble sorting this square matrix (a 2d array that has number of rows and...

    I'm having trouble sorting this square matrix (a 2d array that has number of rows and same number of column n x n) using row-wise approach in C programming. Please help me program this in C to sort it using row-wise approach, here is the code: #include <stdio.h> #define MAX 100 int main() { int mat[MAX][MAX]; int i, j, m, n; int rowsum, columnsum, diagonalsum; int k; int magic = 0; int transpose[MAX][MAX]; printf("Enter the # of rows and columns...

  • #include "stdio.h" int main() { float array[5][3],rowsum=0.0,colsum=0.0; int i,j; for(i=0;i<5;i++){ printf("Enter the %d elements of array:\n",i);...

    #include "stdio.h" int main() { float array[5][3],rowsum=0.0,colsum=0.0; int i,j; for(i=0;i<5;i++){ printf("Enter the %d elements of array:\n",i); for(j=0;j<3;j++){ scanf("%f",&array[i][j]); } } printf("Given table data:\n"); for(i=0;i<5;i++){ for(j=0;j<3;j++){ printf(" %0.1f",array[i][j]); } printf("\n"); } for(i=0;i<5;i++){ for(j=0;j<3;j++){ rowsum=rowsum+array[i][j]; } printf("sum of the %d row is %0.1f\n",i,rowsum); rowsum=0; } for(j=0;j<3;j++){ colsum+=array[j][i]; printf("Sum of %d coloumn is %0.1f\n",j,colsum); colsum=0; } return(0); } can someone help me to get the correct row sum and column sum in c program

  • I need some with this program, please look at the changes I need closely. Its running...

    I need some with this program, please look at the changes I need closely. Its running correctly, but I need to do a few adjustments here is the list of changes I need: 1. All of the integers on a single line, sorted in ascending order. 2. The median value of the sorted array on a single line 3. The average of the sorted array on a single line Here is the program: #include<stdio.h> #include<stdlib.h> /* This places the Adds...

  • Debug the following matrix program in C: // Program to read integers into a 3X3 matrix...

    Debug the following matrix program in C: // Program to read integers into a 3X3 matrix and display them #include <stdio.h> void display(int Matrix[3][3],int size); int main(void) {         char size;         double Matrix[size][size+1];         printf("Enter 9 elements of the matrix:\n");         int i;         for (i = 0: i <= size: i++)     {       int j = 0;       for (; j <= size++; j++){         scanf("%d", matrix[i--][4])       }     }         Display(Matrix,9);         return 0; void...

  • Hello, I have my piece of C programming code and I have a pointer initialized to...

    Hello, I have my piece of C programming code and I have a pointer initialized to point to my array and I need help to display the contents of my array using a while loop or do-while loop. The stop condition should be when the pointer reaches '\0'. The code is below: #include <stdio.h> int main () {    char array[80]; printf("Enter a string: "); scanf("%[^\n]", &array); printf("%s\n", array); char * p;    p = array;         }

  • Need help with this C program? I cannot get it to compile. I have to use...

    Need help with this C program? I cannot get it to compile. I have to use Microsoft Studio compiler for my course. It's a word game style program and I can't figure out where the issue is. \* Program *\ // Michael Paul Laessig, 07 / 17 / 2019. /*COP2220 Second Large Program (LargeProg2.c).*/ #define _CRT_SECURE_NO_DEPRECATE //Include the following libraries in the preprocessor directives: stdio.h, string.h, ctype.h #include <stdio.h> /*printf, scanf definitions*/ #include <string.h> /*stings definitions*/ #include <ctype.h> /*toupper, tolower...

  • C Programming Question Hi, I have the following code and in my submission I'm supposed to...

    C Programming Question Hi, I have the following code and in my submission I'm supposed to only include function definitions (i.e. implementations) in the file. However, I'm not NOT required to write the main, struct definitions and function prototypes. Could someone help me fix this code? Text Version: #include<stdio.h> #include<stdlib.h> #include <string.h> struct ip_address { int octet_1; int octet_2; int octet_3; int octet_4; }; typedef struct ip_address ip_address_t; void print_ip_address(ip_address_t ip1){ printf("%d.%d.%d.%d", ip1.octet_1,ip1.octet_2,ip1.octet_3,ip1.octet_4); } int is_valid(ip_address_t ip1){ if(ip1.octet_1 < 0...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT