CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: matrix

  1. #1
    Join Date
    Apr 2003
    Posts
    3

    matrix

    Jigglychu...help me out b

    Code:
    //Jason Park
    //Project 45
    
    #include <iostream.h>
    #include "apmatrix.h"
    #include <stdlib.h>
    #include <time.h>
    
    void insert(apmatrix <int> &, int, int);
    void print(apmatrix <int>, int, int);
    void large_pos(apmatrix <int>, int, int);
    void diagonal(apmatrix <int>, int, int);
    
    int main (void)
    {
    	apmatrix <int> A(5, 5);
    	int i = 0;
    	int j = 0;
    
    	srand(time(NULL));
    
    	insert(A, i, j);
    	print(A, i, j);
    	large_pos(A, i, j);
    	diagonal(A, i, j);
    
    	return 0;
    }
    
    void insert(apmatrix <int> & A, int i, int j)
    {
    	int value;
    
    	for(i = 0; i < 5; i++)
    	{
    		for(j = 0; j < 5; j++)
    		{
    			value = 1 + rand() % 100;
    			A[i][j] = value;
    		}
    	}
    }
    
    void print(apmatrix <int> A, int i, int j)
    {
    	int largest = 0;
    
    	for(i = 0; i < 5; i++)
    	{
    		for(j = 0; j < 5; j++)
    		{
    			cout << A[i][j] << "\t";
    		}
    			cout << endl;
    	}
    }
    
    
    void large_pos(apmatrix <int> A, int i, int j)
    {
    	int largest = 0;
    	int row, column;
    
    	for(i = 0; i < 5; i++)
    	{
    		for(j = 0; j < 5; j++)
    		{
    			if(largest < A[i][j])
    			{
    				largest = A[i][j];
    				row = i;
    				column = j;
    			}
    		}
    	}
    
    	cout << "The largest value of the matrix is: " << largest << endl;
    	cout << "Row coordinate is: " << row << endl;
    	cout << "Column coordinate is: " << column << endl;
    }
    
    void diagonal(apmatrix <int> A, int i, int j)
    {
    	int diag_sum = 0;
    
    		for (i; i < A.numrows(); i++)
    		{
    			for (j; j < A.numcols(); j++)
    			{
    				if (j == i)
    					diag_sum = diag_sum + A[i][j];
    			}
    		}
    
    			cout << "Sum of the diagonal values is: " << diag_sum << endl;
    }
    Last edited by Nightblade; April 2nd, 2003 at 09:48 PM.

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    First off, use #include <iostream> and not iostream.h. This has
    to come up at least 10 times every week.

    Secondly, post your code inside of [ c o d e ] and [ / c o d e ]
    segments, but without the extra spaces. This comes up often too.
    Edit: You have since reformatted your code.

    Lastly, we have no idea what you want us to help you with. I
    don't feel that I should have to copy all of your code and try to
    compile it. You should give a description of whatever problem you
    are having and then ask the appropriate question. From what
    you posted, I don't have any idea whether you're just getting a
    compiler error or a run-time/logic error ... and I'm not about to
    compile it to find out.

    --Paul

  3. #3
    Join Date
    Apr 2003
    Posts
    8
    no, he was just posting it here specifically so that i could help him out thats why he didn't specify

  4. #4
    Join Date
    Apr 2003
    Posts
    3
    sorry...just signed up for this forum...im a friend of Jigglychu and we are both in the same class with the terrible santa claus of a teacher... my question is with the last function...the diagonal addition...when i run it, it just gives the first value in the matrix (0,0)...when its supposed to add all the diagonal values of the matrix

  5. #5
    Join Date
    Apr 2003
    Posts
    8
    ok it works now

    Code:
    //Jason Park
    //Project 45
    
    #include <iostream.h>
    #include "apmatrix.h"
    #include <stdlib.h>
    #include <time.h>
    
    void insert(apmatrix <int> &);
    void print(apmatrix <int>);
    void large_pos(apmatrix <int>);
    void diagonal(apmatrix <int>);
    
    int main (void)
    {
    	apmatrix <int> A(5, 5);
    	int i = 0;
    	int j = 0;
    
    	srand(time(NULL));
    
    	insert(A);
    	print(A);
    	large_pos(A);
    	diagonal(A);
    
    	return 0;
    }
    
    void insert(apmatrix <int> & A)
    {
    	int value;
    
    	for(int i = 0; i < 5; i++)
    	{
    		for(int j = 0; j < 5; j++)
    		{
    			value = 1 + rand() % 100;
    			A[i][j] = value;
    		}
    	}
    }
    
    void print(apmatrix <int> A)
    {
    	int largest = 0;
    
    	for(int i = 0; i < 5; i++)
    	{
    		for(int j = 0; j < 5; j++)
    		{
    			cout << A[i][j] << "\t";
    		}
    			cout << endl;
    	}
    }
    
    
    void large_pos(apmatrix <int> A)
    {
    	int largest = 0;
    	int row, column;
    
    	for(int i = 0; i < 5; i++)
    	{
    		for(int j = 0; j < 5; j++)
    		{
    			if(largest < A[i][j])
    			{
    				largest = A[i][j];
    				row = i;
    				column = j;
    			}
    		}
    	}
    
    	cout << "The largest value of the matrix is: " << largest << endl;
    	cout << "Row coordinate is: " << row << endl;
    	cout << "Column coordinate is: " << column << endl;
    }
    
    void diagonal(apmatrix <int> A)
    {
    	int diag_sum = 0;
    
    		for (int i = 0; i < A.numrows(); i++)
    		{
    			for (int j = 0; j < A.numcols(); j++)
    			{
    				if (j == i)
    					diag_sum = diag_sum + A[i][j];
    			}
    		}
    
    			cout << "Sum of the diagonal values is: " << diag_sum << endl;
    }

  6. #6
    Join Date
    Apr 2003
    Posts
    3
    Thanks b!

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by Nightblade
    sorry...just signed up for this forum...im a friend of Jigglychu and we are both in the same class with the terrible santa claus of a teacher... my question is with the last function...the diagonal addition...when i run it, it just gives the first value in the matrix (0,0)...when its supposed to add all the diagonal values of the matrix
    When you post on a public forum, everyone who is on the forum is entitled to comment. CodeGuru isn't a chat room. Your subject of "matrix" will make participants in CodeGuru believe that the topic concerns using a matrix C++ library, or a problem with using such a library, etc. Instead, they are greeted with a bunch of code with no context.

    Why not both of you exchange e-mails if the intent is to send messages back and forth without anyone else participating? Also, shouldn't each of you fix your own work?

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Sep 2002
    Posts
    1,747
    Plus its still wrong... unless they don't really want to learn how to write c++...
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  9. #9
    Join Date
    Apr 2003
    Posts
    8
    Well, the thing is, he tried to fix his program and it wouldn't work, so as a last resort he asked me to help him, so i did. As to the email, he just wanted to check out this forum b/c our teacher is not very good, and he teaches the program like C. In the future, we will keep it to emails, but please understand that not everyone here is an expert programmer. In our case, we didnt even know what STL was until i posted a thread here.

    Even if the program is coded w/ deprecated tags and in noobish ways, it still fulfills its inteded purpose, as verified when i compile it. Please understand, we are only 16, and in an AP computer science course where our teacher isn't very good at all. He teaches coding in non-standard ways, and C++ more like C.

    So please, be nice

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured