Help - Problem with array

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2005
Posts: 15,431
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1471
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help - Problem with array

 
0
  #11
Jan 2nd, 2007
I compiled and ran the program -- I didn't get any runtime errors, but I only entered a couple time zones.

Your program is more than a little confusing. why are you putting just one character at a time in its own array element of query[] ? For example, if I enter 123 <Enter> the program puts query[0] = '1', query[1] = '2' and query[2] = '3'. Then if I enter 234 <Enter> the program puts query[3] = '2', query[4] = '3' and query[5] = '4'. At this point it has
query[0] = '1'
query[1] = '2'
query[2] = '3'
query[3] = '2'
query[4] = '3'
query[5] = '4'

Note that the above contain the ascii values for the numbers I entered, not the binary values. The letter '1' is NOT the same as the number 1. (google for "ascii chart" and you will discover why)

Next you extract the three values from the above array. '1', '2' and '3' are the first thee. Then you attempt to use them as the index into the db array. But db array has a maximum of 9 for each dimension, and you are attempting to access '1' (49 decimal).

You have an array overflow problem -- attemtping to access elements out of bounds. To correct this you need to convert the letters to binary. One way to do that is like this:
  1. hundred=filtered[i] - '0';
  2. ten=filtered[i+1] - '0';
  3. one=filtered[i+2] - '0';

Finally, you need to put a break statement at the end of each case.
Last edited by Ancient Dragon; Jan 2nd, 2007 at 5:51 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 7
Reputation: Siva_sbj is an unknown quantity at this point 
Solved Threads: 0
Siva_sbj Siva_sbj is offline Offline
Newbie Poster

Re: Help - Problem with array

 
0
  #12
Jan 2nd, 2007
Thank you Salem.

I have changed

int db[9][9][9] to int db[10][10][10]

and

for(i=0;i<=100;i++) to for(i=0;i<=99;i++).

That should fix the boundary error.

But still I get the same error at the same point.

Is there anyother way to read the array instead of

zone = db[hundred][ten][one];
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 7
Reputation: Siva_sbj is an unknown quantity at this point 
Solved Threads: 0
Siva_sbj Siva_sbj is offline Offline
Newbie Poster

Re: Help - Problem with array

 
0
  #13
Jan 2nd, 2007
Thank You very much Ancient Dragon. That was more than a help for me...

I knew I was using the ascii code instead of decimal value but I totally forgot it during the computation. I will reply after completing the program.

take care.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 7
Reputation: Siva_sbj is an unknown quantity at this point 
Solved Threads: 0
Siva_sbj Siva_sbj is offline Offline
Newbie Poster

Re: Help - Problem with array

 
0
  #14
Jan 2nd, 2007
It worked!!!!

I changed the ascii value to decimal by subtracting 48 from each element in filtered array.

My first miniproject in C is almost complete..

I will post the final code and file after giving the final touch.

thank you all...
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