GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 25-May-2004, 22:01
mgdpetter mgdpetter is offline
New Member
 
Join Date: May 2004
Location: Sydney
Posts: 6
mgdpetter is on a distinguished road
Question

problems with 2d arrays..


Hi, im stuck again...

Small problem...
multidimensional arrays.
Can i first create a 2 dimensional array of characters like this:

CPP / C++ / C Code:
char string[7][7];

and then in some other function initialize it?
I tried this, but no luck...

CPP / C++ / C Code:
strcpy(string, {{' ',' ',' ',' ',' ',' ',' '},
                     {' ',' ',' ',' ',' ',' ',' '},
                     {' ',' ',' ',' ',' ',' ',' '},
                     {' ',' ',' ',' ',' ',' ',' '},
                     {' ',' ',' ',' ',' ',' ',' '},
                     {' ',' ',' ',' ',' ',' ',' '},
                     {' ',' ',' ',' ',' ',' ',' '}});


Is there any other way??
  #2  
Old 25-May-2004, 22:34
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Do you know how to loop yet? A common and effective way of initializing arrays of any dimension is to use a "for loop" to go through the array setting each element to the value of your desire.

CPP / C++ / C Code:
int main()
{
  char string[7][7];

  for (int x = 0; x < 7; x++)
    for (int y = 0; y < 7; y++)
      string[x][y] = '\0'; // NULL character

  return 0;
}
__________________
-Aaron
  #3  
Old 25-May-2004, 22:35
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
Here is one way, but not the easy way I think:
CPP / C++ / C Code:
char string[7][7];// = {NULL};
for(int i=0;i<7;i++)
for(int j=0;j<7;j++)
	string[i][j]=NULL;
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #4  
Old 25-May-2004, 22:36
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Beat you
__________________
-Aaron
  #5  
Old 25-May-2004, 22:39
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
yes u did,I'm one minute late... Damn, my typing is still slow...
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #6  
Old 26-May-2004, 02:17
mgdpetter mgdpetter is offline
New Member
 
Join Date: May 2004
Location: Sydney
Posts: 6
mgdpetter is on a distinguished road

thanx, but doesnt work..


Quote:
Originally Posted by aaroncohn
Do you know how to loop yet? A common and effective way of initializing arrays of any dimension is to use a "for loop" to go through the array setting each element to the value of your desire.

CPP / C++ / C Code:
int main()
{
  char string[7][7];

  for (int x = 0; x < 7; x++)
    for (int y = 0; y < 7; y++)
      string[x][y] = '\0'; // NULL character

  return 0;
}

That works if i want to fill every element with NULL characters, but i want to do something else.. first i want to create an array ,which is [7][7], and then i take a character and use a switch statement to check what character that is and then initialize the array accordingly.
My code looks a bit like this:
CPP / C++ / C Code:

char a[ROWS][COLUMNS]={{' ',' ',' ',' ',' ',' ',' '},
                       {' ','@','@','@',' ',' ',' '},
                       {' ',' ',' ',' ','@',' ',' '}
                       {' ',' ',' ',' ','@',' ',' '},
                       {' ',' ','@','@','@',' ',' '}
                       {' ','@',' ',' ','@',' ',' '},
                       {' ','@','@','@','@',' ',' '}};
      
   
char b[ROWS][COLUMNS]={{' ','@',' ',' ',' ',' ',' '},
                       {' ','@',' ',' ',' ',' ',' '},
                       {' ','@',' ',' ',' ',' ',' '},
                       {' ','@','@','@',' ',' ',' '},
                       {' ','@',' ',' ','@',' ',' '},
                       {' ','@',' ',' ','@',' ',' '},
                       {' ','@','@','@','@',' ',' '}};

in other words, im trying to represent every lower case letter as a 2d array representation of that character built of @.
But i dont want to initialize every 26 letters, but rather declare only one array and then have some kind of switch statement to decide how to initialize it. Can it be done?
  #7  
Old 26-May-2004, 02:38
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
If this is what you want, I could give an idea of how to do this.

first, take a piece of paper and draw a 7X7 grid and fill in the '@' so that it makes and 'a'. Now, List down all the position in the array which contains the '@'. In your prev post, the char a contains '@' in the following positions:

(1,1),(1,2),(1,3),(2,4),(3,4),(4,2),(4,3),(4,4),(5 ,1),(5,4),(6,1),(6,2),(6,3),(6,4).

do the same with b,c,d...z

Now, put all the positions for each char in a struct or array or even a file.. chose what suits your need.

CPP / C++ / C Code:
char chr;
cout<<"Enter a char :";
cin>>chr;
switch (chr)
{
  case 'a':
  //here u get the list of position for the the char 'a' and put '@' at the
  //corresponding position in your a[][] array.
  break;
}
// thats just a pseudokod to guide,try to program and if you have a problem with that, post it here!!
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #8  
Old 26-May-2004, 21:19
mgdpetter mgdpetter is offline
New Member
 
Join Date: May 2004
Location: Sydney
Posts: 6
mgdpetter is on a distinguished road

aha..


That worked very well. thank you.
Now i only have one small little problem left..
im using
CPP / C++ / C Code:
 scanf()
to make the user type in a string and then convert that string to the combinations of @ that you showed me how to do.
Its just that if the user types two strings separated by ' ' (blank space) the scanf function stores the second string as well, (or something like that), and that causes problems for me...
Is it possible to remove any remaining parts of the string, i've read something about
CPP / C++ / C Code:
fflush()
but dont't know if/how it works...
thanx
  #9  
Old 26-May-2004, 22:54
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
Well, I use scanf and fflush but there was a thread where another member suggested that I shouldnot be using it cause it may or may not work..

so you can use the fgets ot getline function or even the >> ooperator to get input.

but just how to use scanf and fflush:
CPP / C++ / C Code:
  scanf("%s",array[i]);
  fflush(stdin);
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #10  
Old 26-May-2004, 23:10
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by mgdpetter
That worked very well. thank you.
Now i only have one small little problem left..
im using
CPP / C++ / C Code:
 scanf()
to make the user type in a string and then convert that string to the combinations of @ that you showed me how to do.
Its just that if the user types two strings separated by ' ' (blank space) the scanf function stores the second string as well, (or something like that), and that causes problems for me...
Is it possible to remove any remaining parts of the string, i've read something about
CPP / C++ / C Code:
fflush()
but dont't know if/how it works...
thanx
fflush() is undefined for the input stream, and scanf() has lots of problems.

Since all you want to do is read a string, use fgets(). It's the best you can get for string input.

Are you saying the space is causing problems? Why should it cause trouble?
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
GeForce FX 5600 problems Peacemaker Computer Hardware Forum 24 18-Jul-2004 06:50
Chaintech Geforce 5600 FX problems bartster74 Computer Hardware Forum 8 04-May-2004 14:16
need help with passing 3 arrays into a function tommy69 C Programming Language 14 07-Apr-2004 01:22
CD Burner Problems Joe9995 Computer Hardware Forum 0 07-Feb-2004 11:47
CD Burner problems dan_wood Computer Hardware Forum 3 27-Nov-2003 20:12

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:03.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.