| View previous topic :: View next topic |
| Author |
Message |
Heartless I post too much
Reputation: 0
Joined: 03 Dec 2006 Posts: 2436
|
Posted: Thu Apr 10, 2008 2:40 pm Post subject: [C++]String Matrix? |
|
|
I'm not 100% sure they are called matrixs, but this is what one would look like.
| Code: | | int Matrix[100][100]; |
Is there even a thing as string matrixs? It work when I compiled this code...
| Code: | #include <iostream>
using namespace std;
int main()
{
char Matrix[100][100];
return 0;
} |
But when I tried to assign it something it won't work.
| Code: |
#include <iostream>
using namespace std;
int main()
{
char Matrix[100][100];
for (int i = 0; i < 100; i++)
{
Matrix[100][i] = "Something";
}
return 0;
}
|
What am I doing wrong?
_________________
What dosen't kill you, usually does the second time. |
|
| Back to top |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Thu Apr 10, 2008 2:52 pm Post subject: |
|
|
String Matrix[100][100];
if you want it to hold a string
for char,
Matrix[100][i] = 'A';
Should work.
|
|
| Back to top |
|
 |
killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Thu Apr 10, 2008 3:06 pm Post subject: |
|
|
You have the array at 100 , 100. Which the index starts at 0.
| Code: |
Matirx[99][i] = "Something";
|
There is another problem. Matrix is declared like this
| Code: |
char Matrix[100][100];
|
This only allows you to hold one char. You need to make it a pointer or else you will get an error.
| Code: |
char *Matrix[100][100] = { 0 };
|
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Thu Apr 10, 2008 8:24 pm Post subject: |
|
|
Firstly since I don't think you fully understand what a matrix is (not trying to insult you just stating what I observed.)
http://en.wikipedia.org/wiki/Matrix_%28mathematics%29
Heres quick example of a string matrix:
| Code: | #include <iostream>
#include <string>
#include <vector>
std::string strStringMatrix[10][10];
int main()
{
char szInput[255] = {0};
for( int x=0;x<10;x++ )
for( int y=0;y<10;y++ ){
sprintf_s( szInput, sizeof(szInput), "Testing %dx%d", x, y );
strStringMatrix[x][y] = std::string( szInput );
std::cout << "[" << x << "x" << y << "] " << strStringMatrix[x][y].c_str() << std::endl;
}
return 0;
} |
_________________
- Retired. |
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Fri Apr 11, 2008 4:58 am Post subject: |
|
|
| HornyAZNBoy wrote: | | Code: |
#include <iostream>
using namespace std;
int main()
{
char Matrix[100][100];
for (int i = 0; i < 100; i++)
{
Matrix[100][i] = "Something";
}
return 0;
}
|
What am I doing wrong? | The second dimension is for the letters... Matrix[i] = "Something"; would be a bit more closer, but that doesn't work either, since strings can't be set that way. However this DOES work: | Code: | #include <iostream>
#include <string>
int main(int argc, char **argv)
{
std::string test[3][3];
for(int j=0; j<3; ++j)
for(int i=0; i<3; ++i)
test[j][i] = "asd";
for(int j=0; j<3; ++j) {
for(int i=0; i<3; ++i)
std::cout << test[j][i] << " ";
std::cout << std::endl;
}
return EXIT_SUCCESS;
} | A bit similar to Wiccaan's code, but w/e :P Of course you could do the printing in the same loop, but IMO this demonstrates a bit better the matrix.
A bit offtopic, since you were talking about matrices, but here's a hint: if you need no more than one dimension (the 2nd one is for the chars) I recommend using std::vector..
|
|
| Back to top |
|
 |
|