Go Down

Topic: Distance Program (Read 4769 times) previous topic - next topic

Grumpy_Mike

Do you mean that in place of printing 132 you print out "one hundred and thirty two"?

elevatedsounds

No I want it to print out the corresponding word  in the array
Code: [Select]
char distance(char room1,char room2)
  {
    char distance[7][7]=
    {
      {0,0,0,0,0,0,0},
      {0,'saturn','mercury','urunus','sun','earth','pluto'},
      {0,'notes','delete','sum','ligit','monkey','money'},
      {0,'nail','nail','destroy','laugh','rich','plant'},
      {0,'bed','clock','drapes','fallacies','click','door'},
      {0,'mouse','spoon','dog','toes','head'},
      {0,'elephant','table','mouse','star','ratio','magic'},
     
    };
    return distance [room1][room2];
  }

I'm not sure how to make that happen correctly though

PaulS

You have an array of characters. 'mercury' is NOT a character. A character variable can hold a single character.

Technically, this is not entirely accurate, but practically, as used on an Arduino, it is.
The art of getting good answers lies in asking good questions.


Utopia


David, Thank you very much.


You're quite welcome - I'm sorry it took so many questions to arrive at the solution, but I'm delighted it works for you.

elevatedsounds

Hey David if I want to recall stored names for example
Code: [Select]
{
      {0,0,0,0,0,0,0},
      {0,'saturn','mercury','urunus','sun','earth','pluto'},
      {0,'notes','delete','sum','ligit','monkey','money'},
      {0,'nail','nail','destroy','laugh','rich','plant'},
      {0,'bed','clock','drapes','fallacies','click','door'},
      {0,'mouse','spoon','dog','toes','head'},
      {0,'elephant','table','mouse','star','ratio','magic'},
     
    };

What would I have to do?

liuzengqiang


Hey David if I want to recall stored names for example
Code: [Select]
{
      {0,0,0,0,0,0,0},
      {0,'saturn','mercury','urunus','sun','earth','pluto'},
      {0,'notes','delete','sum','ligit','monkey','money'},
      {0,'nail','nail','destroy','laugh','rich','plant'},
      {0,'bed','clock','drapes','fallacies','click','door'},
      {0,'mouse','spoon','dog','toes','head'},
      {0,'elephant','table','mouse','star','ratio','magic'},
     
    };

What would I have to do?


First get rid of the zeros. Next define what a recall means. There is no standard meaning of recall in C so without you telling up what it means, especially what input it takes and where/what output it should be making, you're asking a question which can not be answered.
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

AWOL

#22
Aug 01, 2011, 08:58 am Last Edit: Aug 01, 2011, 09:10 am by AWOL Reason: 1
http://arduino.cc/forum/index.php/topic,68037.0.html

Cross-posting is unhelpful and time-wasting.
Please don't do it.

Topics merged 08:10 BST 1 Aug 11

Utopia


Hey David if I want to recall stored names ... What would I have to do?


character arrays are a bit trickier - first, you pass back a char* instead of a char (which is only one character long), and second, you have to watch out for scope - take for instance this code:
Code: [Select]
const char* namez(int item1,int item2)
{
  static char *info[6][6]=
  {
    {"saturn","mercury","urunus","sun","earth","pluto"},
    {"notes","delete","sum","ligit","monkey","money"},
    {"nail","nail","destroy","laugh","rich","plant"},
    {"bed","clock","drapes","fallacies","click","door"},
    {"mouse","spoon","dog","toes","head"},
    {"elephant","table","mouse","star","ratio","magic"}
  };
  return info[item1-1][item2-1];
}


First off, you are passing back a char *, not a char, since a char is only one character, and you want the whole string, so you pass a pointer; also, you are calling it const (constant), because you shouldn't change anything you get back (like changing 'mouse' to 'mice'). We've renamed the function to be more descriptive than 'distance', and changed the indexes from char to int (strictly speaking, they could be char because the computer knows to change them to numbers, but it's confusing, since indexes are all integer).

At the bottom we add this line
   
Code: [Select]
return info[item1-1][item2-1];
Because as liudr said, you don't need all the zeroes, and this saves space: because the only way into the array is through your function, we can do this subtraction of one, and no one outside the function knows about it (or cares) - it still works as usual, with indexes of 1-6

Finally, the array: we use static here so the data stays around, and a char * because they are strings, not individual single characters. When you enter a function, everything inside it is newly created, and then destroyed when you leave (there are a few exceptions, but they don't count here). So if you returned a pointer to this array, the array would disappear, and the pointer would be meaningless (sometimes the function data isn't destroyed right away, so you think everything is ok, but a little later it may get wiped out and you won't know why there's a problem). The reason this wasn't a problem with the distance array is because integers can be passed back in a function, but these strings cannot - instead, the pointer to them is passed back. However, making them static keeps them around for the whole program, and so this function will work.

One more thing - you'll notice the last entry is missing a comma at the end - it's not needed since it's the last.

I hope this explains it well enough - char arrays are quite a bit different from number arrays, and they can be tricky the first time around.

Go Up