The Char Data Type with 1 vs 2 Dimensional Array

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2008
Posts: 32
Reputation: salman213 is an unknown quantity at this point 
Solved Threads: 0
salman213 salman213 is offline Offline
Light Poster

The Char Data Type with 1 vs 2 Dimensional Array

 
0
  #1
Jun 20th, 2008
Hey, here are two codes and with one slight difference they do different things
CODE 1
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. char array[2];
  6. int i=0;
  7. while (i<2)
  8. {
  9. cin>>array[i];
  10. i++;
  11. }
  12. cout<<array[0]<<' '<<array[1];
  13. }
EXAMPLE OUTPUT:
  1. two words
  2. t w


CODE 2
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. char array[2][100];
  6. int i=0;
  7. while (i<2)
  8. {
  9. cin>>array[i];
  10. i++;
  11. }
  12. cout<<array[0]<<' '<<array[1];
  13. }
EXAMPLE OUTPUT:
  1. two words
  2. two words




How is that occurring, by adding an extra dimension to array it takes both words. What is going on....lol


Thanks!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,740
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 739
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: The Char Data Type with 1 vs 2 Dimensional Array

 
1
  #2
Jun 20th, 2008
>How is that occurring, by adding an extra dimension to array it takes both words.
It helps to know that the native string type in C++ is actually an array of char, and cin's >> operator is pretty smart about knowing what types you give it. When array[i] is an array of char, cin>>array[i]; will read a word into it. when array[i] is a single char, cin>>array[i]; will read a single character into it.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: The Char Data Type with 1 vs 2 Dimensional Array

 
1
  #3
Jun 20th, 2008
You need to read a basic tutorials on arrays... Seeing this is c++ have a look at std::strings as well.
Last edited by iamthwee; Jun 20th, 2008 at 5:14 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 32
Reputation: salman213 is an unknown quantity at this point 
Solved Threads: 0
salman213 salman213 is offline Offline
Light Poster

Re: The Char Data Type with 1 vs 2 Dimensional Array

 
0
  #4
Jun 20th, 2008
is it possible to think of it as a matrix?

for example

  1. char array[2][2];

Isn't this what we are forming
  1. 0 1
  2. 0
  3.  
  4. 1

If we do
  1. cin>> array[0]
and we input the word: "WHAT"
how is that being placed in to the matrix I drew.

To display the word I played around and found I can do this:
  1. cout<< array[0][0]<<array[0][1]<<array[0][2]<<array[0][3]
But I thought the maximum it can go to was array [1][1] ?? So how can those arrays be defined.

I mean the only combinations I thought were possible according to a tutorial I read were

array[0][0]
array[0][1]
array[1][0]
array[1][1]
Last edited by salman213; Jun 20th, 2008 at 5:53 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: The Char Data Type with 1 vs 2 Dimensional Array

 
0
  #5
Jun 20th, 2008
Originally Posted by salman213 View Post
is it possible to think of it as a matrix?

for example

  1. char array[2][2];

Isn't this what we are forming
  1. 0 1
  2. 0
  3.  
  4. 1

If we do
  1. cin>> array[0]
and we input the word: "WHAT"
how is that being placed in to the matrix I drew.

To display the word I played around and found I can do this:
  1. cout<< array[0][0]<<array[0][1]<<array[0][2]<<array[0][3]
But I thought the maximum it can go to was array [1][1] ?? So how can those arrays be defined.

I mean the only combinations I thought were possible according to a tutorial I read were

array[0][0]
array[0][1]
array[1][0]
array[1][1]

When the compiler saw this line:

  1. char array[2][2];

it set aside memory for 2 x 2 = 4 bytes of contiguous memory. Let's say it stored array[0][0] at the following address in memory:

  1. 0xaa0000

The four characters would be stored at the following addresses:

  1. array[0][0] 0xaa0000
  2. array[0][1] 0xaa0001
  3. array[1][0] 0xaa0002
  4. array[1][1] 0xaa0003

If you are referring to the element array[i][j], the computer figures out which address in memory is being referred to by this mathematical equation:

  1. address = address of array[0][0] + offset

where offset is calculated as:

  1. (i times number of rows) + j

In your case the number of rows = 2, so offset is:

  1. offset = 2i + j

So in the case of array[0][3], i = 0 and j = 3, so:

  1. offset = 0 * 2 + 3 = 3

The address of array[0][3] is thus:

  1. 0xaa0000 + 3 = 0xaa0003

which is the address of array[1][1], so array[1][1] and array[0][3] are equivalent here, so when you displayed array[0][3], you were really displaying what was stored at array[1][1], which is 'T'.

However, even though you can get away with it, it's probably not a great habit to get into and you should probably only reference the elements array[0][0], array[0][1], array[1][0], and array[1][1].
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 32
Reputation: salman213 is an unknown quantity at this point 
Solved Threads: 0
salman213 salman213 is offline Offline
Light Poster

Re: The Char Data Type with 1 vs 2 Dimensional Array

 
0
  #6
Jun 20th, 2008
alright thanks alot for explaining so clearly.

cool..
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC