Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
Hello, I am now learning Multi- Dim Arrays. My $180 text book was misplaced and I have one on its way for much cheaper as a replacement. This means that I am relying on what I find on the internet to teach me about them.

I followed an example, tutorial http://www.youtube.com/watch?v=JNmZvdTKWrQ and wrote this code. But when I compile it I get an error message ("Incompatible types, got "ShortString", expected "Char")
Code:
Program mArray(input,output);

var
	suit : array[1..4] of char;
	rank : array[1..13] of char;
	cards : array[1..4,1..13] of char;
	x,y : integer;
	
begin
	suit[1] := 'A';
	suit[2] := 'B';
	suit[3] := 'C';
	suit[4] := 'D';
	
	rank[1] := '1';
	rank[2] := '2';
	rank[3] := '3';
	rank[4] := '4';
	rank[5] := '5';
	rank[6] := '6';
	rank[7] := '7';
	rank[8] := '8';
	rank[9] := '9';
	rank[10] := 'D';
	rank[11] := 'A';
	rank[12] := 'C';
	rank[13] := 'S';
	
	for x := 1 to 4 do
		begin
			for y := 1 to 13 do
			begin
				cards[x,y] := suit[x] + rank[y];
			end;					
		end;
end.

The only thing I did different from the tutorial that I can see was to make my Arrays Chars and not Strings. Strings are a part of Turbo Pascal and we can't use them for our class so I converted it to a Char. Each of my Chars has only 1 symbol.

Any ideas?

Thanks

-Lars
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,425
A sea of green
1. What line is it producing that error message for?

2. This seems wrong:
Code:
cards[x,y] := suit[x] + rank[y];
Both suit and rank are declared as arrays of type char. What does it mean to add one char to another char? If the result isn't also a char, then it seems nonsensical that it should be assigned to cards[x,y], an array also declared with a base type of char.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
What chown33 mentioned (the likely case of char + char yielding a ShortString instead of a char seems to be an issue), i think the real problem is logic. If you have an array that is 4 rows by 13 columns you have 52 elements. Great, that's the size of a deck of cards. But your type is just char. So you have 52 chars. You want to store info about a card there, but you only have one character to do it. This isn't the end of the world, if you're willing to simply say that row 1 will be hearts, row 2 will be spades, row 3 will be diamonds, row 4 will be clubs, but you won't have that information STORED in that array position.

You could use a third dimension that is 2 long, the first char for the suit the second for the card description.

What are you going to use the cards char array for? Just for display? You already have two arrays that you can use a nested loop over to do such a display. Is it for tracking what's been dealt and to who? If so you need not store the card info there, and char is probably not the datatype you need.

-Lee
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
You found the line, that was the line the compiler was complaining about. This is how he showed it in the demo video. I seems odd that he is using an arrhythmic operator on 2 Chars. The goal is to populate the array using both for loops for the suit of cards and the number of the card. Why there is an arrhythmic operator to do this I don't get?

EDIT: Thanks Lee, I get it kinda. What I am doing is to just learn about MDA right now. I have a take home quiz due tomorrow night and I don;t have the book handy to read up on it so I am trying to learn from the internet till my new book comes.

I thought in a MDA each array was still a separate Array so in a 2D array suit would store the value 'D' and Rank, for example '8', would be stored in another 1 byte slot. The MDA would display it as 'D' '9'. or is it trying to store it as 'D9', which is not possible as a Char?

-Lars
 
Last edited:

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
You found the line, that was the line the compiler was complaining about. This is how he showed it in the demo video. I seems odd that he is using an arrhythmic operator on 2 Chars. The goal is to populate the array using both for loops for the suit of cards and the number of the card. Why there is an arrhythmic operator to do this I don't get?

EDIT: Thanks Lee, I get it kinda. What I am doing is to just learn about MDA right now. I have a take home quiz due tomorrow night and I don;t have the book handy to read up on it so I am trying to learn from the internet till my new book comes.

I thought in a MDA each array was still a separate Array so in a 2D array suit would store the value 'D' and Rank, for example '8', would be stored in another 1 byte slot. The MDA would display it as 'D' '9'. or is it trying to store it as 'D9', which is not possible as a Char?

-Lars

With a multidimensional array you don't get N entries at each position where N is the number of dimensions. You get one entry for each position. It may help to think of multidimensional array as a bunch of arrays laid end to end. If your array is 4x13 it's just 4 sets of 13 chars laid end to end. Really just a 52 element array that allows a different means of accessing the elements to fit your need. If you need to store suit and card type you'd need a 4x13x2 array of characters. For the last dimension you could use the first char for the suit, second for the value. Something like:

cards[x,y,1] = suits[x]
cards[x,y,2] = rank[y]

In your innermost loop.

-Lee
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,425
A sea of green
I seems odd that he is using an arrhythmic operator on 2 Chars.

Was he using Char types? I thought you said he was using string types.
A string consisting of a single char is still a string type, not a char type.

What does the + operator really mean when applied to strings? Does it mean "arithmetic addition" or does it mean "concatenation"? Concatenation would result in an ordered arrangement of the operands in sequence: e.g. 'C' + 'a' + 't' is the ordered Char sequence 'Cat', i.e. a string.

I think your fundamental problem is you don't fully understand what types are involved, or what implicit type conversions might be involved (e.g. chars promoted to ShortStrings), so what you might have seen demonstrated using string types doesn't mean the same thing when applied to simple scalar types like integers or reals.

One difficulty here is that the compiler you're using seems to accept operands and operators that are forbidden by your teacher. That is, the compiler will happily accept and compile code for things that are considered errors in your coursework. That means it is entirely possible to write programs that compile and run, and appear to be "right", but which are wrong when the constraints of the course are applied. And I don't see any way to tell the compiler it shouldn't accept the extended types like string or shortstring, which means you will always be manually checking your code to make sure it doesn't accidentally use types that are forbidden by fiat instead of by the compiler.
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
Thanks guys,
The program worked once I changed the cards array to a String instead of a Char. I was thinking of a 2D Array like the game of BattleShip. So if I called out D4 it would go down the row till it found D then across to 4 and display what was in that container.

Chown33 - I see what you mean by Concatenation. So it is not a problem by storing the Chars since the Chars are stored in there own containers like, x4,y6 is 'C', x4,y7 is 'A' and x4,y8 is 'T'. But when I write out to the console as C+A+T I am displaying it as a string which it can't do since I said it was a Char MD Array?

Lee - This through me off with the deck of cards as you explained it. I would need 4x13x2. This would lead to 104 total containers a deck of cards is just 4 x 13 4 suits of 13 numbers. O wait... I think I got what you meant.... 4x13x2 the first array of 52 would hold the numbers 1 to 13, 4 different times.The second would hold just the suits 13 X 4. I see that. If my MDA was of type STRING then each container could hold something like 'A1', 'A2' and so on.

So for a deck of cards that are stored in 2 Char Arrays one for the number symbol from A - K and the other suits, 'S', 'D', 'H', 'C' I would have to display them as (ARRAY1[x] Concatenation ARRAY2[y] ) for 'A1'?
 

Bill McEnaney

macrumors 6502
Apr 29, 2010
295
0
1. What line is it producing that error message for?

2. This seems wrong:
Code:
cards[x,y] := suit[x] + rank[y];
Both suit and rank are declared as arrays of type char. What does it mean to add one char to another char? If the result isn't also a char, then it seems nonsensical that it should be assigned to cards[x,y], an array also declared with a base type of char.
Yes, trying add a character to another character is a mistake when you're writing Pascal programs. But it's an understandable mistake, too, because C would let you add characters to each other.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.