943,589 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 2053
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 1st, 2007
0

Help - Problem with array

Expand Post »
I am writing a C program using Dev-CPP. It uses a 3 dimensional array to store around 350 values as shown below.

  1.  
  2. db[2][0][1]=3278;
  3. db[2][0][4]=2368;
  4. db[3][0][3]=6686;
  5. db[2][0][6]=7224;
  6. ..
  7. ..
  8. ..


I get a valid output when I read the array with the following code

  1. zone = db[2][0][1];
  2. printf("%d",zone);

but it stops abruptly with a error msg when i run a code like this...

  1. int h,t,o;
  2. h=2;
  3. t=0;
  4. o=1;
  5. zone = db[h][t][o];

The program stops exactly when the last line is executed... . It get compiled without any error

help me to read the array using the int values...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Siva_sbj is offline Offline
7 posts
since Jan 2007
Jan 1st, 2007
0

Re: Help - Problem with array

please post how the array was declared, or better yet post the entire function. what's the max value of each dimension?
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Jan 1st, 2007
0

Re: Help - Problem with array

I am sorry. I should have done that first..

  1. int db[9][9][9];

the maximum value is 7789 and the minimum value is 3278.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Siva_sbj is offline Offline
7 posts
since Jan 2007
Jan 1st, 2007
0

Re: Help - Problem with array

The values you put in the array are not relevent to your problem. I suspect the actual error is elsewhere in your program because what you posted looks ok. Without more code its impossible to say what your problem is.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Jan 1st, 2007
0

Re: Help - Problem with array

Either that, or you have some miniscule amount of RAM in your computer. What's your compiler? It's highly unlikely, but a crappy old compiler has much different limits than a modern one.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Jan 1st, 2007
1

Re: Help - Problem with array

Hello there.
Either that, or you have some miniscule amount of RAM in your computer. What's your compiler? It's highly unlikely, but a crappy old compiler has much different limits than a modern one.
Yes, an old compiler imposes a limit on the amount of memory that can be allocated to a single variable, but if that were the senario, the program execution would have halted at the point he has declared the three dimensional array.

I hope you realize that even though he hasn't stored any values initially in his array, the space has been nonetheless allocated to him. I don't think any failed attempt after the allocation is due to the old compiler.

And as far as RAM is concerned, he is allocating 729 * 4 bytes ~ 2.85 KB. I don't think this should be as such a problem...

Thank you.
Last edited by ~s.o.s~; Jan 1st, 2007 at 10:30 pm.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Jan 1st, 2007
0

Re: Help - Problem with array

Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
Hello there.

Yes, an old compiler imposes a limit on the amount of memory that can be allocated to a single variable, but if that were the senario, the program execution would have halted at the point he has declared the three dimensional array.

I hope you realize that even though he hasn't stored any values initially in his array, the space has been nonetheless allocated to him. I don't think any failed attempt after the allocation is due to the old compiler.

And as far as RAM is concerned, he is allocating 729 * 4 bytes ~ 2.85 KB. I don't think this should be as such a problem...
You're right - I guess I was thinking in terms of dynamic memory allocation...

Ancient Dragon is right - there's an error somewhere else in your program. Post your entire code so that we can criticize... I mean, correct, your code. :cheesy:
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Jan 1st, 2007
0

Re: Help - Problem with array

Hello there.
You're right - I guess I was thinking in terms of dynamic memory allocation...
Ah..just to clear the matters up...even if we dynamically allocated 2.9KB of memory, it wouldn't be such a problem, unless of course, you were doing realloc i.e. allocating memory during runtime and exceeding the limitations imposed by the current memory model of the compiler.

Thank you.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Jan 2nd, 2007
0

Re: Help - Problem with array

here is the code.. so that you could correct....
this is a program to find time zone from area code...
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void db_init(void);/*function to initialise db[9][9][9] to zero*/
  5. void db_init1(void);/*function to initialise db[9][9][9] to with the database*/
  6. int db[9][9][9]; /*array to store the value*/
  7. int n;
  8. int zone;
  9. int main()
  10. {
  11. db_init();/*function to initialise db[9][9][9] to zero*/
  12. db_init1();/*function to initialise db[9][9][9] to with the database*/
  13. int hundred,ten,one;
  14. int query[100];/*array to get input from user*/
  15. int filtered[100];/*array to store the only the number in the input from user*/
  16. int i=0,j,k;
  17.  
  18. for(i=0;i<=100;i++)/*function to initialise filtered to zero*/
  19. filtered[i]=0;
  20. i=0;
  21.  
  22.  
  23. printf("Hi! Thank you for using Siva's time zone locator.\nPress any key to continue....");
  24. getch();
  25. zone = db[2][0][1];/*not part of program. only to check*/
  26. printf("%d",zone);/*not part of program. only to check*/
  27. getch();
  28. system("CLS");
  29.  
  30.  
  31. printf("Please enter upto 20 three digit area codes and type \\ and hit enter\n");
  32.  
  33. /* \ is to indicate end of input*/
  34.  
  35.  
  36.  
  37. while(i!=100 && (query[i]=getchar())!=92)/*to get input from user*/
  38. {
  39. ++i;
  40. }
  41.  
  42.  
  43. n=i-1;/* -1 to exclude the \ at the end of the array*/
  44. k=0;
  45. for(i=0;i<=n;i++)
  46. {
  47. j=query[i];
  48. if (j>47 && j<58)/* to move the element in query[] to filtered[] if its a number*/
  49. {
  50. filtered[k]=j;
  51. ++k;
  52. }
  53. else
  54. ;
  55. }
  56. for (i=0;i<=k;i)
  57. {
  58. hundred=filtered[i];
  59. ten=filtered[i+1];
  60. one=filtered[i+2];
  61.  
  62. putchar(hundred);
  63. putchar(ten);
  64. putchar(one);/*works great till here*/
  65. zone = db[hundred][ten][one];/*problem starts here*/
  66.  
  67. switch (zone)
  68. {
  69. case 3278:
  70. printf("\tEastern Standard Time Zones");
  71. case 2368:
  72. printf("\tCentral Standard Time Zones");
  73. case 6686:
  74. printf("\tMountain Standard Time Zones");
  75. case 7224:
  76. printf("\tPacific Standard Time Zones");
  77. case 2389:
  78. printf("\tEastern & Central Time Zones");
  79. case 2689:
  80. printf("\tCentral & Mountain Time Zones");
  81. case 6289:
  82. printf("\tMountain & Central Time Zones");
  83. case 6789:
  84. printf("\tMountain & Pacific Time Zones");
  85. case 2289:
  86. printf("\tAleutian & Alaskan Time Zones");
  87. case 2852:
  88. printf("\tAtlantic Time Zones");
  89. case 6389:
  90. printf("\tNewfoundland Time Zone (Canadian)");
  91. case 7289:
  92. printf("\tPacific & Alaskan Time Zones");
  93. case 7789:
  94. printf("\tSamoan Standard Time Zones");
  95. default:
  96. printf("invalid code");
  97. }
  98.  
  99. i=i+3;
  100. }
  101.  
  102.  
  103.  
  104.  
  105.  
  106. return 0;
  107.  
  108.  
  109. }
  110. void db_init(void)
  111. {
  112. /*function to initialise db[9][9][9] to zero*/
  113. /*using a seperate function because programs stops after this if used in main()*/
  114. int i,j,k;
  115. for (i=0;i<=9;i++)
  116. {
  117. for (j=0;j<=9;j++)
  118. {
  119. for (k=0;k<=9;k++)
  120. {
  121. db[i][j][k]=0;
  122. }
  123. }
  124. }
  125.  
  126. }
  127. void db_init1(void)
  128. {
  129. /*function to initialise db[9][9][9] to with the database*/
  130. /*using a seperate function because programs stops after this if used in main()*/
  131. db[2][0][1]=3278;/*Eastern Time Zone*/
  132. db[2][0][2]=3278;
  133. db[2][0][3]=3278;
  134. db[2][0][7]=3278;
  135. db[2][1][2]=3278;
  136. db[2][1][5]=3278;
  137. db[2][1][6]=3278;
  138. db[2][2][6]=3278;
  139. db[2][2][9]=3278;
  140. db[2][3][1]=3278;
  141. db[2][3][4]=3278;
  142. db[2][3][9]=3278;
  143. db[2][4][0]=3278;
  144. db[2][4][8]=3278;
  145. db[2][5][2]=3278;
  146. db[2][6][7]=3278;
  147. db[2][6][9]=3278;
  148. db[2][7][6]=3278;
  149. db[2][8][9]=3278;
  150. db[3][0][1]=3278;
  151. db[3][0][2]=3278;
  152. db[3][0][4]=3278;
  153. db[3][0][5]=3278;
  154. db[3][1][3]=3278;
  155. db[3][1][5]=3278;
  156. db[3][2][1]=3278;
  157. db[3][3][0]=3278;
  158. db[3][3][6]=3278;
  159. db[3][3][9]=3278;
  160. db[3][4][5]=3278;
  161. db[3][4][7]=3278;
  162. db[3][5][1]=3278;
  163. db[3][5][2]=3278;
  164. db[3][8][6]=3278;
  165. db[4][0][1]=3278;
  166. db[4][0][4]=3278;
  167. db[4][0][7]=3278;
  168. db[4][1][0]=3278;
  169. db[4][1][2]=3278;
  170. db[4][1][3]=3278;
  171. db[4][1][6]=3278;
  172. db[4][1][8]=3278;
  173. db[4][1][9]=3278;
  174. db[4][3][4]=3278;
  175. db[4][3][8]=3278;
  176. db[4][4][0]=3278;
  177. db[4][4][3]=3278;
  178. db[4][5][0]=3278;
  179. db[4][7][0]=3278;
  180. db[4][7][5]=3278;
  181. db[4][7][8]=3278;
  182. db[4][8][4]=3278;
  183. db[5][0][2]=3278;
  184. db[5][0][8]=3278;
  185. db[5][1][3]=3278;
  186. db[5][1][4]=3278;
  187. db[5][1][6]=3278;
  188. db[5][1][7]=3278;
  189. db[5][1][8]=3278;
  190. db[5][1][9]=3278;
  191. db[5][4][0]=3278;
  192. db[5][5][1]=3278;
  193. db[5][6][1]=3278;
  194. db[5][6][7]=3278;
  195. db[5][7][0]=3278;
  196. db[5][7][1]=3278;
  197. db[5][8][5]=3278;
  198. db[5][8][6]=3278;
  199. db[6][0][3]=3278;
  200. db[6][0][6]=3278;
  201. db[6][0][7]=3278;
  202. db[6][0][9]=3278;
  203. db[6][1][0]=3278;
  204. db[6][1][3]=3278;
  205. db[6][1][4]=3278;
  206. db[6][1][6]=3278;
  207. db[6][1][7]=3278;
  208. db[6][3][1]=3278;
  209. db[6][4][6]=3278;
  210. db[6][4][7]=3278;
  211. db[6][4][9]=3278;
  212. db[6][7][8]=3278;
  213. db[7][0][3]=3278;
  214. db[7][0][4]=3278;
  215. db[7][0][5]=3278;
  216. db[7][0][6]=3278;
  217. db[7][1][6]=3278;
  218. db[7][1][7]=3278;
  219. db[7][1][8]=3278;
  220. db[7][2][4]=3278;
  221. db[7][2][7]=3278;
  222. db[7][3][2]=3278;
  223. db[7][3][4]=3278;
  224. db[7][4][0]=3278;
  225. db[7][5][4]=3278;
  226. db[7][5][7]=3278;
  227. db[7][7][0]=3278;
  228. db[7][7][2]=3278;
  229. db[7][7][4]=3278;
  230. db[7][8][1]=3278;
  231. db[7][8][6]=3278;
  232. db[8][0][2]=3278;
  233. db[8][0][3]=3278;
  234. db[8][0][4]=3278;
  235. db[8][1][0]=3278;
  236. db[8][1][3]=3278;
  237. db[8][1][4]=3278;
  238. db[8][1][9]=3278;
  239. db[8][2][8]=3278;
  240. db[8][3][5]=3278;
  241. db[8][4][3]=3278;
  242. db[8][4][5]=3278;
  243. db[8][4][8]=3278;
  244. db[8][5][6]=3278;
  245. db[8][5][7]=3278;
  246. db[8][5][9]=3278;
  247. db[8][6][0]=3278;
  248. db[8][6][2]=3278;
  249. db[8][6][3]=3278;
  250. db[8][6][5]=3278;
  251. db[8][7][6]=3278;
  252. db[8][7][8]=3278;
  253. db[9][0][4]=3278;
  254. db[9][0][5]=3278;
  255. db[9][0][8]=3278;
  256. db[9][1][0]=3278;
  257. db[9][1][2]=3278;
  258. db[9][1][4]=3278;
  259. db[9][1][7]=3278;
  260. db[9][1][9]=3278;
  261. db[9][3][7]=3278;
  262. db[9][4][1]=3278;
  263. db[9][4][7]=3278;
  264. db[9][5][4]=3278;
  265. db[9][5][9]=3278;
  266. db[9][7][3]=3278;
  267. db[9][7][8]=3278;
  268. db[9][8][0]=3278;
  269. db[9][8][9]=3278;
  270. db[2][0][4]=2368;/*Central Time Zone */
  271. db[2][0][5]=2368;
  272. db[2][1][0]=2368;
  273. db[2][1][4]=2368;
  274. db[2][1][7]=2368;
  275. db[2][1][8]=2368;
  276. db[2][2][4]=2368;
  277. db[2][2][5]=2368;
  278. db[2][2][8]=2368;
  279. db[2][5][1]=2368;
  280. db[2][5][4]=2368;
  281. db[2][5][6]=2368;
  282. db[2][6][2]=2368;
  283. db[2][7][0]=2368;
  284. db[2][8][1]=2368;
  285. db[3][0][9]=2368;
  286. db[3][1][2]=2368;
  287. db[3][1][4]=2368;
  288. db[3][1][6]=2368;
  289. db[3][1][8]=2368;
  290. db[3][1][9]=2368;
  291. db[3][2][0]=2368;
  292. db[3][2][5]=2368;
  293. db[3][3][4]=2368;
  294. db[3][3][7]=2368;
  295. db[3][6][1]=2368;
  296. db[4][0][2]=2368;
  297. db[4][0][5]=2368;
  298. db[4][0][9]=2368;
  299. db[4][1][4]=2368;
  300. db[4][1][7]=2368;
  301. db[4][3][0]=2368;
  302. db[4][3][2]=2368;
  303. db[4][6][9]=2368;
  304. db[4][7][9]=2368;
  305. db[5][0][1]=2368;
  306. db[5][0][4]=2368;
  307. db[5][0][7]=2368;
  308. db[5][1][2]=2368;
  309. db[5][1][5]=2368;
  310. db[5][6][3]=2368;
  311. db[5][7][3]=2368;
  312. db[5][8][0]=2368;
  313. db[6][0][1]=2368;
  314. db[6][0][8]=2368;
  315. db[6][1][2]=2368;
  316. db[6][1][5]=2368;
  317. db[6][1][8]=2368;
  318. db[6][3][0]=2368;
  319. db[6][3][6]=2368;
  320. db[6][4][1]=2368;
  321. db[6][5][1]=2368;
  322. db[6][6][0]=2368;
  323. db[6][6][2]=2368;
  324. db[6][8][2]=2368;
  325. db[7][0][8]=2368;
  326. db[7][1][2]=2368;
  327. db[7][1][3]=2368;
  328. db[7][1][5]=2368;
  329. db[7][3][1]=2368;
  330. db[7][6][3]=2368;
  331. db[7][6][9]=2368;
  332. db[7][7][3]=2368;
  333. db[8][0][6]=2368;
  334. db[8][1][5]=2368;
  335. db[8][1][6]=2368;
  336. db[8][1][7]=2368;
  337. db[8][3][0]=2368;
  338. db[8][3][2]=2368;
  339. db[8][4][7]=2368;
  340. db[8][7][0]=2368;
  341. db[9][0][1]=2368;
  342. db[9][0][3]=2368;
  343. db[9][1][3]=2368;
  344. db[9][1][8]=2368;
  345. db[9][2][0]=2368;
  346. db[9][3][1]=2368;
  347. db[9][3][6]=2368;
  348. db[9][4][0]=2368;
  349. db[9][5][2]=2368;
  350. db[9][5][6]=2368;
  351. db[9][7][2]=2368;
  352. db[9][7][9]=2368;
  353. db[9][8][5]=2368;
  354. db[3][0][3]=6686;/*Mountain Time Zone */
  355. db[3][0][7]=6686;
  356. db[3][8][5]=6686;
  357. db[4][0][3]=6686;
  358. db[4][0][6]=6686;
  359. db[4][3][5]=6686;
  360. db[5][0][5]=6686;
  361. db[7][1][9]=6686;
  362. db[7][2][0]=6686;
  363. db[7][8][0]=6686;
  364. db[8][0][1]=6686;
  365. db[9][7][0]=6686;
  366. db[2][0][6]=7224; /*Pacific Time Zone*/
  367. db[2][0][9]=7224;
  368. db[2][1][3]=7224;
  369. db[2][5][3]=7224;
  370. db[3][1][0]=7224;
  371. db[3][2][3]=7224;
  372. db[3][6][0]=7224;
  373. db[4][0][8]=7224;
  374. db[4][1][5]=7224;
  375. db[4][2][5]=7224;
  376. db[5][0][3]=7224;
  377. db[5][0][9]=7224;
  378. db[5][1][0]=7224;
  379. db[5][3][0]=7224;
  380. db[5][5][9]=7224;
  381. db[5][6][2]=7224;
  382. db[6][0][4]=7224;
  383. db[6][1][9]=7224;
  384. db[6][2][6]=7224;
  385. db[6][5][0]=7224;
  386. db[6][6][1]=7224;
  387. db[7][0][2]=7224;
  388. db[7][0][7]=7224;
  389. db[7][1][4]=7224;
  390. db[7][6][0]=7224;
  391. db[7][7][5]=7224;
  392. db[7][7][8]=7224;
  393. db[8][0][5]=7224;
  394. db[8][1][8]=7224;
  395. db[8][3][1]=7224;
  396. db[8][5][8]=7224;
  397. db[9][0][9]=7224;
  398. db[9][1][6]=7224;
  399. db[9][2][5]=7224;
  400. db[9][4][9]=7224;
  401. db[9][5][1]=7224;
  402. db[9][7][1]=7224;
  403. db[7][6][5]=2389;/*central and eastern time zone*/
  404. db[2][1][9]=2389;
  405. db[2][6][0]=2389;
  406. db[3][1][7]=2389;
  407. db[4][2][3]=2389;
  408. db[5][7][4]=2389;
  409. db[8][0][7]=2389;
  410. db[8][1][2]=2389;
  411. db[8][5][0]=2389;
  412. db[9][0][6]=2389;
  413. db[6][2][0]=2689;/*Central & Mountain Time Zones*/
  414. db[3][0][6]=2689;
  415. db[3][0][8]=2689;
  416. db[6][0][5]=2689;
  417. db[7][0][1]=6289;/*Mountain & Central Time Zones*/
  418. db[7][8][5]=6289;
  419. db[9][1][5]=6289;
  420. db[2][0][8]=6789;/*Mountain & Pacific Time Zones*/
  421. db[2][5][0]=6789;
  422. db[4][8][0]=6789;
  423. db[5][2][0]=6789;
  424. db[5][4][1]=6789;
  425. db[6][0][2]=6789;
  426. db[6][2][3]=6789;
  427. db[9][2][8]=6789;
  428. db[9][0][7]=2289;/*Aleutian & Alaskan Time Zones*/
  429. db[2][4][2]=2852;/*Atlantic Time Zone */
  430. db[2][4][6]=2852;
  431. db[2][6][4]=2852;
  432. db[2][6][8]=2852;
  433. db[2][8][4]=2852;
  434. db[3][4][0]=2852;
  435. db[4][4][1]=2852;
  436. db[4][7][3]=2852;
  437. db[5][0][6]=2852;
  438. db[6][6][4]=2852;
  439. db[7][5][8]=2852;
  440. db[7][6][7]=2852;
  441. db[7][8][4]=2852;
  442. db[7][8][7]=2852;
  443. db[8][0][9]=2852;
  444. db[8][2][9]=2852;
  445. db[8][6][8]=2852;
  446. db[8][6][9]=2852;
  447. db[9][0][2]=2852;
  448. db[9][3][9]=2852;
  449. db[8][0][8]=4292;
  450. db[7][0][9]=6389;/*Newfoundland Time Zone*/
  451. db[8][6][7]=7289;/*Pacific through Alaskan */
  452. db[6][8][4]=7789;/*Samoan Standard Time Zone */
  453. }

I am using dev-c++ ver 4.9.9.2 to develop this program.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Siva_sbj is offline Offline
7 posts
since Jan 2007
Jan 2nd, 2007
1

Re: Help - Problem with array

> for(i=0;i<=100;i++)
There are many MANY examples of you accessing outside the bounds of your arrays.

more - db[9][7][0]=6686;
and more - db_init()
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: not for repeat cards again!
Next Thread in C Forum Timeline: Hey!! Need Help With A Pseudocode Problem, Help Please





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC