Thread: Need a real help please!!

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    Need a real help please!!

    Hi, I'm applying and doing a code that's printing the upper-trinqular of the given matrix, I've successfully done the code for printing what I demanded, but there's one specific error that I need a real help in it which is whenever I enter a matrix initialized with value 0, (matrix[0][0]=0) my code prints wrong result and something unclear for me at all and it's just when I enter the first matrix's value as 0.


    here's my code:
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    
    float matrix[10][10], m;
    int i, j, k, n;
    void upper_triangularization() {
    	for (i = 0; i<n - 1; i++)
    		for (j = i + 1; j<n; j++) {
    			m = matrix[j][i] / matrix[i][i];
    			for (k = 0; k<n + 1; k++) {
    				matrix[j][k] = matrix[j][k] - (m*matrix[i][k]);
    			}
    		}
    } //upper_traingulisation
    void main() {
    	printf("Enter number. of column :: ");
    	scanf("%d", &n);
    	printf("Enter the augmented matrix: \n");
    	for (i = 0; i<n; i++)
    		for (j = 0; j<n ; j++)
    			scanf("%f", &matrix[i][j]);
    
    
    	upper_triangularization();
    
    
    	printf("The upper traingular matrix is : \n");
    
    
    	for (i = 0; i<n; i++) {
    		for (j = 0; j<n; j++)
    			printf("%f \t", matrix[i][j]);
    		printf("\n");
    	}
    }
    Any help please?

    thanks in advance!!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Grr... use local variables, not global variables! What exactly is your test input, expected output, and actual output?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    Grr... use local variables, not global variables! What exactly is your test input, expected output, and actual output?
    I tested matrix
    0 1
    1 0


    I tried ur suggestion about local and global...the same results.
    My problem occurred whenever I enter the first value as 0.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RyanC
    I tried ur suggestion about local and global...the same results.
    It isn't intended to give you different results. It is intended for you to write stylistically better code that will be easier to debug and maintain.

    Quote Originally Posted by RyanC
    My problem occurred whenever I enter the first value as 0.
    "What exactly is your test input, expected output, and actual output?"

    Have you checked for the possibility of division by zero?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Yes laser;
    I know it's prevented to divide on Zero..but not benefit because I got the same result..wrong printf

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RyanC
    I know it's prevented to divide on Zero..but not benefit because I got the same result..wrong printf
    That's too bad. I might have a chance at helping you debug if you described what the program is supposed to do and told me what exactly is your test input, expected output, and actual output, but since you refuse to so, I guess I'll have to give up. Thankfully, it is not my program, so I do not have to be bothered.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Laser I'm sorry about that...I didn't understand you very well...sorry about that.
    My program in brief is converting the given matrix to a upper-triangular matrix...
    And regarding to my problem is just occurring whenever I enter the value 0 in the first value of the given matrix

  8. #8
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    That's too bad. I might have a chance at helping you debug if you described what the program is supposed to do and told me what exactly is your test input, expected output, and actual output, but since you refuse to so, I guess I'll have to give up. Thankfully, it is not my program, so I do not have to be bothered.
    My test input is double numbers...
    I have tested
    0
    1
    1
    0
    And got the problem

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by RyanC View Post
    My test input is double numbers...
    I have tested
    0
    1
    1
    0
    And got the problem
    Again, this is like 1/3 of the question asked. To clarify what we want you to answer... imagine a perfect world. When you enter this information, what is supposed to come out? And when you answer that, what actually comes out? You know it's polite to have a reference, so we know we're doing the right thing, instead of you just depending on us to run your example and come up with the right answer.

  10. #10
    Banned
    Join Date
    Apr 2015
    Posts
    596
    The question is in abbreviation "You must find the upper-triangular of the given matrix by algebraically way and print the uppers triangular on the screen"

  11. #11
    Banned
    Join Date
    Apr 2015
    Posts
    596
    What I have done is working fine as expected unless one thing which is whenever I put the first value of the given matrix 0..the code starts printing weird things.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by RyanC View Post
    Yes laser;
    I know it's prevented to divide on Zero..but not benefit because I got the same result..wrong printf
    Well, your code failed a simple assertion:

    Code:
    #include <assert.h>
    
    void upper_triangularization() {
        for (i = 0; i<n - 1; i++)
            for (j = i + 1; j<n; j++) {
                assert(matrix[i][i] != 0.0 && "division by zero detected!");
                m = matrix[j][i] / matrix[i][i];
                for (k = 0; k<n + 1; k++) {
                    matrix[j][k] = matrix[j][k] - (m*matrix[i][k]);
                }
            }
    } //upper_traingulisation
    
    
    sh-4.3$ main                                                                                                                                                            
    
    Enter number. of column :: 2                                                                                                                                            
    
    Enter the augmented matrix:                                                                                                                                             
    
    0 1                                                                                                                                                                     
    
    1 0                                                                                                                                                                     
    
    main: main.c:9: upper_triangularization: Assertion `matrix[i][i] != 0.0 && "division by zero detected!"' failed.
    So even though you say it's prevented, it clearly is not. This would explain the -nans and -infs you get as output.

    What I resent is that you would not provide sample outputs yourself! How hard is it, really?!?

  13. #13
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by whiteflags View Post
    Well, your code failed a simple assertion:

    Code:
    #include <assert.h>
    
    void upper_triangularization() {
        for (i = 0; i<n - 1; i++)
            for (j = i + 1; j<n; j++) {
                assert(matrix[i][i] != 0.0 && "division by zero detected!");
                m = matrix[j][i] / matrix[i][i];
                for (k = 0; k<n + 1; k++) {
                    matrix[j][k] = matrix[j][k] - (m*matrix[i][k]);
                }
            }
    } //upper_traingulisation
    
    
    sh-4.3$ main                                                                                                                                                            
    
    Enter number. of column :: 2                                                                                                                                            
    
    Enter the augmented matrix:                                                                                                                                             
    
    0 1                                                                                                                                                                     
    
    1 0                                                                                                                                                                     
    
    main: main.c:9: upper_triangularization: Assertion `matrix[i][i] != 0.0 && "division by zero detected!"' failed.
    So even though you say it's prevented, it clearly is not. This would explain the -nans and -infs you get as output.

    What I resent is that you would not provide sample outputs yourself! How hard is it, really?!?
    I avoided that thing in my code after edition (didn't post it) but whatever it's still printing wrong printf ( I added a new condition for the possibility of 0, whenever there's 0 then divide conversely (0/a=0)

    I really in need of help, any clue?

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    In what way is it printing wrong? I am not a mind reader.

  15. #15
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by whiteflags View Post
    In what way is it printing wrong? I am not a mind reader.
    You are right.

    here's what I've done and I'm pretty much pleased to help me:
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include <assert.h>
    float matrix[10][10], m, temp[10];
    int i, j, k, n;
    
    
    void upper_triangularization() {
        for (i = 0; i<n - 1; i++)
            for (j = i + 1; j<n; j++) 
            {
                if (matrix[i][i] == 0) // if the matrix[i][i] =0 of the diagonal then the ratio of what m included is always 0, because 0/a=0 always //
                {
                    assert(matrix[i][i] != 0.0 && "division by zero detected!");
                    m = 0;
                }
                else
                    m = matrix[j][i] / matrix[i][i];
                for (k = 0; k < n + 1; k++) {
                        matrix[j][k] = matrix[j][k] - (m*matrix[i][k]);}
                }
    } //upper_traingulisation
    void main() {
        printf("Enter number. of column :: ");
        scanf("%d", &n);
        printf("Enter the augmented matrix: \n");
        for (i = 0; i<n; i++)
            for (j = 0; j<n ; j++)
                scanf("%f", &matrix[i][j]);
    
    
        upper_triangularization();
    
    
        printf("The upper traingular matrix is : \n");
    
    
        for (i = 0; i<n; i++) {
            for (j = 0; j<n; j++)
                printf("%f \t", matrix[i][j]);
            printf("\n");
        }
        getch();
    }

    I'm still getting wrong printf in one specific given matrix which is
    0 1
    1 0 and the printf must be
    1 0
    0 1
    but unfortunately it's printing something weird.
    Last edited by RyanC; 11-29-2015 at 05:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ and real languages
    By lruc in forum C++ Programming
    Replies: 42
    Last Post: 09-05-2008, 11:35 AM
  2. real uses of C++?
    By jlf029 in forum C++ Programming
    Replies: 13
    Last Post: 11-17-2005, 04:28 AM
  3. which is the real He-man?
    By correlcj in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 11-14-2002, 06:11 AM
  4. RPG(for real)
    By Paninaro in forum Game Programming
    Replies: 1
    Last Post: 06-22-2002, 05:48 PM
  5. Replies: 7
    Last Post: 12-12-2001, 10:28 AM