Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   matrices (http://www.go4expert.com/forums/matrices-t16979/)

xyzabc 20Apr2009 00:03

matrices
 
I just had a question about a project I am working on. I am supposed to design a program that will solve up to fifty simultaneous equations using gauss jordan.

I just was wondering what would be the best way to find the "PIVOT"(the largest absolute value number in the first column). Once you have found the "pivot" I then need to interchange that row with row 1. Any suggestions?

Code:

#include<iostream>
using namespace std;
 
int main()
{
    double x[50],a[50][50],c[50];
    int i,j,n;
   
       
    cout <<"\n\nPlease enter the number of rows/variables\n";
    cin >> n;
 
    cout<<"\nLet's now enter the row and column values\n"<<endl;
    cout<<endl;
 
for (i=1;i<=n;i++)
 {
  for (j=1;j<=n;j++)
  {
  cout <<"Row "<<i<<", Column "<<j<<"\n";
  cin >> a[i][j];
  }
 
 cout << "Please enter the constant for row "<<i<<"\n";
 cin >> c[i];
       
 }
 cout <<"\n\nThe matrix entered is as follows:\n";
 for (i=1;i<=n;i++)
 {
        cout<<"\n";
  for (j=1;j<=n;j++)
  {
  cout <<a[i][j]<<"\t";
  }
 cout<<" ="<<c[i]<<"\n";
  }

system("pause");
return 0; 
   
   
}


xpi0t0s 20Apr2009 03:11

Re: matrices
 
best way = probably the only way: start at the first row, record the value, see if the value in the second row is higher, then the 3rd, and repeat until you get to the end of the column. When the value you're looking at exceeds the value stored, update the value stored and where you found the new value.

xyzabc 20Apr2009 08:50

Re: matrices
 
Quote:

Originally Posted by xpi0t0s (Post 45933)
best way = probably the only way: start at the first row, record the value, see if the value in the second row is higher, then the 3rd, and repeat until you get to the end of the column. When the value you're looking at exceeds the value stored, update the value stored and where you found the new value.

Ok that sounds easy enough...I am guessing I would need to use a for loop to do this? How would I go about looking at each individual value in the row?

xpi0t0s 20Apr2009 12:08

Re: matrices
 
Something like this would do the trick:
Code:

max <= a[0][0]
loc <= 0
for i = 1..49
  if a[i][0] > max
    max <= a[i][0]
    loc <= i

Don't forget C arrays are zero based, and int a[50] defines an array from a[0]..a[49].
So your loop above starting from 1 and ending at n will not work (actually it will work for all n<=1<=49 but at n=50 there will be undefined behaviour because a[50] doesn't exist).

Code:

for (i=1;i<=n;i++)
 {
  for (j=1;j<=n;j++)
  {
  cout <<"Row "<<i<<", Column "<<j<<"\n";
  cin >> a[i][j];
  }

Hmm, at n=50 that will be 2500 values. This will be spectacularly tedious. Are the array values in a file anywhere? As a starting point this is fine but if the values are in a file you'll need to extend the code to read the values from a file instead.

xyzabc 20Apr2009 19:29

Re: matrices
 
Quote:

Hmm, at n=50 that will be 2500 values. This will be spectacularly tedious. Are the array values in a file anywhere? As a starting point this is fine but if the values are in a file you'll need to extend the code to read the values from a file instead.
Thank you!!

Yes I knew about the 0...49 but thanks for reminding me.

-------Hmm, at n=50 that will be 2500 values. This will be spectacularly tedious.------


Yes this will be very very tedious if you would ever have to enter a 50x50 matrix :cuss: and even a 10x10 takes some time.

The instructions were to allow the user to tell how many variables they desire and allow them to enter all of the values into the program and then use gauss jordan to solve the simultaneous system.


-------------Are the array values in a file anywhere?---------
No the array values aren't in a file because they aren't predetermined values as mentioned above. The user will have to enter their own values.

This would however be a good test I think to check to see if the program would compute the matrices correctly without having to type in values over and over. I definately don't want to be the one to type in all of those values.

xyzabc 22Apr2009 04:29

Re: matrices
 
Quote:

Originally Posted by xpi0t0s (Post 45949)
Something like this would do the trick:
Code:

max <= a[0][0]
loc <= 0
for i = 1..49
  if a[i][0] > max
    max <= a[i][0]
    loc <= i


Is there any way to check to determine the greatest value that is chosen to be the pivot using this sample code you provided? How would you swap the rows? Does anyone know a good source that may have good matrix operation help?

xpi0t0s 22Apr2009 12:10

Re: matrices
 
> Is there any way to check to determine the greatest value that is chosen to be the pivot using this sample code you provided?

I don't understand the question. The code does both; it tells you what the maximum value is (max) and what row it is on (loc).

> How would you swap the rows?

Same way I would swap the values of two variables, but done with arrays in a loop. This is one way to swap two variables; it uses a 3rd for temporary storage:
Code:

int a=10, b=20, c;
c=a; // make a safe copy of a
a=b; // overwrite a with b
b=c; // overwrite b with the safe copy of a
printf("%d %d",a,b); // will display 20,10


xyzabc 22Apr2009 20:36

Re: matrices
 
Quote:

Originally Posted by xpi0t0s (Post 46119)
> Is there any way to check to determine the greatest value that is chosen to be the pivot using this sample code you provided?

I don't understand the question. The code does both; it tells you what the maximum value is (max) and what row it is on (loc).

> How would you swap the rows?

Same way I would swap the values of two variables, but done with arrays in a loop. This is one way to swap two variables; it uses a 3rd for temporary storage:
Code:

int a=10, b=20, c;
c=a; // make a safe copy of a
a=b; // overwrite a with b
b=c; // overwrite b with the safe copy of a
printf("%d %d",a,b); // will display 20,10


Thanks...ok let me try to explain what I meant. That question wasn't worded correctly.. I meant to say how would you use a cout<< statement to actually check what the value is?

xpi0t0s 22Apr2009 21:02

Re: matrices
 
That one wasn't worded correctly either, I guess. Presumably you've tried cout << value; ?
If so how is that not what you want?

But I don't see how "how would you use cout to check a value" can be an explanation for "how do you swap the rows" when an answer to "how do you swap the rows" makes you realise that "how do you swap the rows" isn't what you meant.

I'm confused...

xyzabc 22Apr2009 21:17

Re: matrices
 
Quote:

Originally Posted by xpi0t0s (Post 46149)
That one wasn't worded correctly either, I guess. Presumably you've tried cout << value; ?
If so how is that not what you want?

But I don't see how "how would you use cout to check a value" can be an explanation for "how do you swap the rows" when an answer to "how do you swap the rows" makes you realise that "how do you swap the rows" isn't what you meant.

I'm confused...

I think I am now becoming confused as well. Let me try again.

Yes I have tried to do cout<<value;

for example.. The program asks how many variables do I want to solve for and I type 2. It is going to then ask me for Row 1 column 1 etc....

If I type in

5 9 = 12
8 7 = 15

Obviously the largest value in the first column is 8.

Since this value is being used as my pivot. The row that the pivot is in, now needs to be interchanged with row 1.

All I wanted to do was use a cout<< statement to make 100% sure that the program is selecting the correct value.

I am just not sure what that "value" needs to be. It's going to be a single value from the first column of the matrix obviously in this case 8, but without a variable actually assigned to that value. How would you use a cout<< statement?

I really hope that made more sense. Sorry for the confusion.


All times are GMT +5.5. The time now is 03:04.