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 22-Aug-2006, 03:41
swedenguy swedenguy is offline
New Member
 
Join Date: Aug 2006
Posts: 8
swedenguy is on a distinguished road

how to initialize a char matrix matrix with empty elements?


hello guys!
i have a problem (don't know why) and need some help
I have a matrix se part of the code below
CPP / C++ / C Code:
int i, j;
	char matrix[3][3];
	for(i=0;i<=9; i++) {
		if(bo->square[i].piece_type==1) /*bo->square[i].piece_type return an integer */
			  matrix[i/3][i%3]='+';
		else if (bo->square[i].piece_type==2) /
			matrix[i/3][i%3]='*';
		else matrix[i/3][i%3]=' ';
		}
but i get a run time error when i have the last statement matrix[i/3][i%3]=' ';
If a remove this statement everything is correct but i get some strange (random character) at the empty matrix element when i print it on the screen
Can somebody tell me how do i intialize an element of the matrix with an empty character ' ' without geting this error ?
  #2  
Old 22-Aug-2006, 10:17
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,147
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: how to initialize a char matrix matrix with empty elements?


Quote:
Originally Posted by swedenguy
hello guys!
i have a problem (don't know why)

One way to figure it out is to look at the code How many elements are there in the array? How many times does it go through the loop? (A run time error when you are assigning an value to an array element may be an indication that you are going beyond the limits of the array.)

If you can't see it, then make the program tell you what it is working on:

CPP / C++ / C Code:
    for (i = 0; i <= 9; i++) {
        printf("i / 3 = %d, i %% 3 = %d\n", i/3, i%3);
    .
    .
    .
    }

Sometimes I just break out the problem area into a separate small program to make sure nothing else is screwing up the works:

CPP / C++ / C Code:
#include <stdio.h>

int main()
{
    int i;
    char matrix[3][3];

    for (i = 0; i <= 9; i++) {
        printf("i / 3 = %d, i %% 3 = %d\n", i/3, i%3);
        /*matrix[i / 3][i % 3] = ' '; */
    }
    return 0;
}

Or, you could simply use the matrix in a more "natural" way: Instead of trying to calculate index values, you could use nested loops:

CPP / C++ / C Code:
    int i, j;
    char matrix[3][3];

    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            printf("i = %d, j = %d\n", i, j);
            matrix[i][j] = ' ';
        }
    }

Regards,

Dave
  #3  
Old 22-Aug-2006, 10:27
swedenguy swedenguy is offline
New Member
 
Join Date: Aug 2006
Posts: 8
swedenguy is on a distinguished road

Re: how to initialize a char matrix matrix with empty elements?


Quote:
Originally Posted by davekw7x
One way to figure it out is to look at the code How many elements are there in the array? How many times does it go through the loop? (A run time error when you are assigning an value to an array element may be an indication that you are going beyond the limits of the array.)

If you can't see it, then make the program tell you what it is working on:

CPP / C++ / C Code:
    for (i = 0; i <= 9; i++) {
        printf("i / 3 = %d, i %% 3 = %d\n", i/3, i%3);
    .
    .
    .
    }

Sometimes I just break out the problem area into a separate small program to make sure nothing else is screwing up the works:

CPP / C++ / C Code:
#include <stdio.h>

int main()
{
    int i;
    char matrix[3][3];

    for (i = 0; i <= 9; i++) {
        printf("i / 3 = %d, i %% 3 = %d\n", i/3, i%3);
        /*matrix[i / 3][i % 3] = ' '; */
    }
    return 0;
}

Or, you could simply use the matrix in a more "natural" way: Instead of trying to calculate index values, you could use nested loops:

CPP / C++ / C Code:
    int i, j;
    char matrix[3][3];

    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            printf("i = %d, j = %d\n", i, j);
            matrix[i][j] = ' ';
        }
    }

Regards,

Dave
yes you are right Dave. Thank you for sharing your experience/knowledge with you. The error was as "ususal" uín this case when going beyond the array in my case for (i = 0; i <= 9; i++) should be for (i = 0; i < 9; i++)
  #4  
Old 22-Aug-2006, 11:37
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,432
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

Re: how to initialize a char matrix matrix with empty elements?


Please do not double post. I wasted time answering your other post. One post is enough.
__________________

Definition: Politics
Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
  #5  
Old 22-Aug-2006, 11:43
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,147
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: how to initialize a char matrix matrix with empty elements?


Quote:
Originally Posted by swedenguy
yes you are right Dave. Thank you for sharing your experience/knowledge with you. The error was as "ususal" uín this case when going beyond the array in my case for (i = 0; i <= 9; i++) should be for (i = 0; i < 9; i++)

Yes, that works.

However...

Why not just use the nested loops. That way there's not much question about what it is doing (I suggest that it's likely to be more efficient in terms of machine code and time as well as debugging time.)

Regards,

Dave
 
 

Recent GIDBlogCompress Your Web Site by gidnetwork

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
how to initialize a char matrix matrix with empty elements? swedenguy C Programming Language 1 22-Aug-2006 11:15
Combining Vectors and References Frankg C++ Forum 7 14-Jan-2006 06:17
Memory cannot be read? dlare9 C Programming Language 3 16-Nov-2005 07:03
Debug Assertion Failed! dlare9 C Programming Language 3 13-Nov-2005 23:18
Reading non ASCII with read() Atomical C Programming Language 8 13-Sep-2005 14:30

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

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


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