Thread: [HELP] Eigenvalues in C

  1. #1
    Registered User
    Join Date
    Aug 2012
    Location
    Caloocan, Metro Manila, Philippines
    Posts
    17

    [HELP] Eigenvalues in C

    Guys I have an assignment on programming that needs to implement the eigenvalues algorithm in C. The maximum MATRIX is 5X5 and the minimum is 2X2.


    My code already checks the upper half of the matrix (X) and the Diagonalization Values (O). I left with the lower right of the matrix (Y).


    O X X X
    X O X Y
    X X O Y
    X Y Y O

    Here is what I got so far.
    Code:
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int
    main(void)
    {
        int num, col, row, i, j, sum = 0, p = 1;
        char stream[256];
    
    
        // correct input from the user
        printf("Enter the dimension of your matrix [2-5]: ");
        do
        {
            char *line = fgets(stream, 256, stdin);
    
    
            char c;
            if (sscanf(line, " %d %c", &num, &c) == 1)
            {
                system("cls");
                if (num > 5)
                    printf("Dimension too big!\nRetry [2-5]: ");
                else if (num < 2)
                    printf("Dimension too small!\nRetry [2-5]: ");
            }
            else
            {
                system("cls");
                printf("Invalid input!\nEnter the dimension of your matrix [2-5]: ");
            }
        }
        while (num > 5 || num < 2);
    
    
        // create the matrix based on the size of input
        int matrix[num][num];
    
    
        system("cls");
        printf("Enter the matrix:\n\n");
        for (i = 0; i < num; i++)
            for (col = 0; col < num; col++)
                scanf("%d", &matrix[i][col]);
    
    
        // print out the diagonalized values
        printf("\nDiagonalization Values: ");
        for (i = 0; i < num; i++)
            printf("%d ", matrix[i][i]);
    
    
        // check the upper-left half of the matrix
        printf("\nThe constant is: ");
        for (i = 0; i < num; i++)
        {
            int cnt = 0, p = 1;
            if (i < num)
                for (row = 0; row < num; row++)
                {
                    col = (num - 1) - row;
                    if (row != col - i && col - i >= 0)
                    {
                        p *= matrix[row][col - i];
                        cnt++;
                    }
                }
            if (cnt != 0)
                sum += p;
        }
    
    
        // check the lower-right half of the matrix
        for (j = 1; j < num - 1; j++)
        {
            int cnt = 0, p = 1;
            if (j < num - 1)
                for (row = 1, col = (num - 1); row < num; row++, col--)
                {
                    if (row != col)
                    {
                        p *= matrix[row][col];
                        cnt++;
                    }
                }
            if (cnt != 0)
                sum += p;
        }
        printf("\n\n%d", sum);
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Unfortunately "help" isn't a question, and leaves no clue what you are having a problem with. We're not going to extend your program to include another part of it that you haven't finished. That's doing your work for you.

    If you have a specific problem, ask away. "Help" won't do, however.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Location
    Caloocan, Metro Manila, Philippines
    Posts
    17
    Quote Originally Posted by Adak View Post
    Unfortunately "help" isn't a question, and leaves no clue what you are having a problem with. We're not going to extend your program to include another part of it that you haven't finished. That's doing your work for you.

    If you have a specific problem, ask away. "Help" won't do, however.
    I already finished it, sorry...

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That's the second time you've done this, I believe.

    Please don't post a "problem", if you don't have a problem, or have already solved the problem.

    That's really annoying. I don't know how others feel about it, but I won't be responding to your queries for awhile. I don't understand that kind of thinking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Library for Eigenvalues and Eigenvectors
    By ioan1ioan in forum C Programming
    Replies: 5
    Last Post: 03-15-2012, 09:46 PM
  2. c code for eigenvectors and eigenvalues for 4x4 matrix
    By sanjana in forum C++ Programming
    Replies: 2
    Last Post: 10-11-2011, 02:52 PM
  3. Finding Eigenvalues and Eigenvectors of a matrix
    By Zeeshan in forum C++ Programming
    Replies: 3
    Last Post: 06-13-2008, 02:50 AM
  4. Eigenvalues for 2x2 Matrix
    By dcwang3 in forum C Programming
    Replies: 18
    Last Post: 02-18-2008, 08:35 PM