USA Linux Users Group Forum Index
Log in Register FAQ Memberlist Search USA Linux Users Group Forum Index Album

matrix problem C++

 
Post new topic   Reply to topic   printer-friendly view    USA Linux Users Group Forum Index » Shell Scripting and Programming
View previous topic :: View next topic  
Author Message
Ephemeral
Advanced Member


Joined: 26 Jun 2024
Posts: 671
Location: UK

PostPosted: Thu Apr 16, 2024 1:34 pm    Post subject: matrix problem C++ Reply with quote

keep getting problems with this matrix im tring to implement in c++
Code:

int main()
{

const int ROWS = 3;
const int COLUMNS = 3;

int matrix1[ROWS][COLUMNS];


matrix1 = {{1, 1, 1},
{1, 1, 1},
{1, 1, 1}};


cant see whats going wrong here... Confused

any help appreciated


Back to top
View user's profile Send private message
Ephemeral
Advanced Member


Joined: 26 Jun 2024
Posts: 671
Location: UK

PostPosted: Thu Apr 16, 2024 1:50 pm    Post subject: Reply with quote

Code:
int matrix1[ROWS][COLUMNS] = {{1, 1, 1},
{1, 1, 1},
{1, 1, 1}};


Solved it with this...

Any other solutions would be welcomed though


Back to top
View user's profile Send private message
nukes
Linux Guru


Joined: 29 Aug 2024
Posts: 4558

PostPosted: Thu Apr 16, 2024 2:02 pm    Post subject: Reply with quote

well, { {1,1,1},{1,1,1},{1,1,1} } would be a int**.

While in terms of passing references around, pointers and arrays are equivalent - not in terms of the actual storage class.

the value of the matrix1 reference is constant, which is why you can't reassign it (it's not a pointer, it's the actual data).

it would be like saying something like

int x=1;
&x = 0xdeadbeef;



_________________
Gentoo x86-64 2.6.29.1
FreeBSD 7-CURRENT
Arch x86 2.6.30
Back to top
View user's profile Send private message Visit poster's website AIM Address MSN Messenger
Ephemeral
Advanced Member


Joined: 26 Jun 2024
Posts: 671
Location: UK

PostPosted: Thu Apr 16, 2024 11:21 pm    Post subject: Reply with quote

thanks for that

Yes i was thinking i must have got something fundamentally wrong (or missed)...

This is my first forray into OO programming having spent a lot of time in Matlab (a weakly typed language)...

Looks like i need to spend some time reading about Pointers in C++

Trying to teach myself some C++ at home


Back to top
View user's profile Send private message
platinummonkey
Advanced Member


Joined: 01 Mar 2024
Posts: 732
Location: Texas

PostPosted: Fri Apr 17, 2024 4:21 am    Post subject: Reply with quote

Ephemeral wrote:

This is my first forray into OO programming having spent a lot of time in Matlab (a weakly typed language)...


ugh... matlab... x.x It's.... "unique"... x.x So great it makes you applaud their work using your head and your desk Razz



_________________
desktop - FreeBSD 7.2
laptop & server - Archlinux i686 kernel26 2.6.32.10-1
- TAMULinux-2.0.2-ALPHA
USB Boot - Archlinux i686 kernel26 2.6.32.10-1 USB boot
Back to top
View user's profile Send private message Visit poster's website AIM Address
Ephemeral
Advanced Member


Joined: 26 Jun 2024
Posts: 671
Location: UK

PostPosted: Tue Apr 21, 2024 2:11 pm    Post subject: Reply with quote

Back with a new problem Laughing Very Happy

Basically im trying to write my first proper function in c++

i want to do something like this


Code:
matrixmultiply(matrix1, matrix2)



matrix 1 and 2 could be any matrix... both will be float though



Then i use this code to multiply the two to give matrix3 and then read it out in the console

Code:
int n,m;

for(int i=0;i<4;i++)
   {
      for (int k=0;k<4;k++)
      {
         matrix3[i][k] = 0;
         for(int j=0;j<4;j++)
         {
            matrix3[i][k] += (matrix1[i][j] * matrix2[j][k]);
         }
      }
   }

for (n = 0; n<ROWS; n++)
{
   for (m = 0; m<COLUMNS; m++)
   {
cout << matrix3[n][m];
   }
   cout << endl;
}


Back to top
View user's profile Send private message
masinick
Linux Guru


Joined: 03 Apr 2024
Posts: 8615
Location: Concord, NH

PostPosted: Tue Apr 21, 2024 2:59 pm    Post subject: Reply with quote

Other than the cout statement, most of what you are doing are the kinds of things that can be implemented in the C programming language as well. If you have the classic C text from Prentice-Hall Software Series, "The C Programming Language, by Brian W. Kernighan and Dennis M. Ritchie (the author of C), provides a terse, functional description of how arrays and pointers work. If you either own that book, can borrow it, or have some kind of on-line way to read it, I recommend it, even though you are looking at C++ rather than C. The kind of thing you are doing is handled identically in C except for the I/O, which differs between C and C++. It is a relatively short book by reference book standards, with just over 200 pages of content, and it is considered one of, if not THE, classic C programming text.



_________________
Brian Masinick
Distros: SimplyMEPIS
sidux - no CAPS!, antiX, Debian
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
Ephemeral
Advanced Member


Joined: 26 Jun 2024
Posts: 671
Location: UK

PostPosted: Sat Apr 25, 2024 11:26 am    Post subject: Reply with quote

Yeah, thanks, ive spent a while trawling through for info on the book..

It does seem the majority of people still hold it with high regard...

Maybe its time to get that melting credit card out again Very Happy


Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    USA Linux Users Group Forum Index » Shell Scripting and Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All content © 2024-2009 - Usa Linux Users Group
This forum is powered by phpBB. © 2024-2009 phpBB Group
Theme created by phpBBStyles.com and modified by Crouse