how do you convert this into c

Reply

Join Date: Jul 2007
Posts: 77
Reputation: ivatanako is an unknown quantity at this point 
Solved Threads: 5
ivatanako ivatanako is offline Offline
Junior Poster in Training

how do you convert this into c

 
0
  #1
Sep 8th, 2009
this is a java code, i need to convert it into C.

  1. private void addFrame (int toAdd) {
  2. int[ ] frameScores = new int[0];
  3. totalScore = totalScore + toAdd;
  4. if (frameScores.length < lastFrameNumber) {
  5. int [ ] temp = new int [frameScores.length+1];
  6. for (int k=0; k<frameScores.length; k++) {
  7. temp[k] = frameScores[k];
  8. }
  9. temp[frameScores.length] = totalScore;
  10. frameScores = temp;
  11. }
  12. }

im having problem with

frameScores = temp

it is throwing a incompatible types errors

here is my C code
  1. void addFrame(int toAdd)
  2. {
  3. totalScore = totalScore + toAdd;
  4. if (sizeof(frameScores) < lastFrameNumber) {
  5. int temp[sizeof(frameScores)+1];
  6. for (int k=0; k<sizeof(frameScores); k++)
  7. {
  8. temp[k] = frameScores[k];
  9. }
  10. temp[sizeof(frameScores)] = totalScore;
  11. frameScores = temp;
  12. }
  13. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 108
Reputation: Majestics is an unknown quantity at this point 
Solved Threads: 7
Majestics Majestics is offline Offline
Junior Poster

Re: how do you convert this into c

 
0
  #2
Sep 8th, 2009
Java is derived from C. You can use dynamic memory allocation.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,412
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: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: how do you convert this into c

 
-7
  #3
Sep 8th, 2009
How is frameScores declared?
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: Jul 2007
Posts: 77
Reputation: ivatanako is an unknown quantity at this point 
Solved Threads: 5
ivatanako ivatanako is offline Offline
Junior Poster in Training

Re: how do you convert this into c

 
0
  #4
Sep 8th, 2009
Originally Posted by Ancient Dragon View Post
How is frameScores declared?
it is declared as an integer array. Same as temp.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,412
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: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: how do you convert this into c

 
-7
  #5
Sep 8th, 2009
>>int temp[sizeof(frameScores)+1];
That line is wrong. If frameScores is declared as int frameScores[5] then the above declaration will create an array of 5*sizeof(int) = 20+1 elements. What you want is 6 elements, so the line should be int temp[sizeof((frameScores)/sizeof(frameScores[0]))+1];

Well that will correct that line, but it still will not correct the line frameScores = temp; because the two arrays are not the same size.
Last edited by Ancient Dragon; Sep 8th, 2009 at 10:31 pm.
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: Jul 2007
Posts: 77
Reputation: ivatanako is an unknown quantity at this point 
Solved Threads: 5
ivatanako ivatanako is offline Offline
Junior Poster in Training

Re: how do you convert this into c

 
0
  #6
Sep 8th, 2009
Originally Posted by Ancient Dragon View Post
>>int temp[sizeof(frameScores)+1];
That line is wrong. If frameScores is declared as int frameScores[5] then the above declaration will create an array of 5*sizeof(int) = 20+1 elements. What you want is 6 elements, so the line should be int temp[sizeof((frameScores)/sizeof(frameScores[0]))+1];

Well that will correct that line, but it still will not correct the line frameScores = temp; because the two arrays are not the same size.
thank you. any suggestion on how i could correct it?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,412
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: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: how do you convert this into c

 
-7
  #7
Sep 8th, 2009
If you are attempting to add a new element to frameScores array, such as increase its size from 5 to 6 elements, then make frameScores a pointer and reallocate its size.
  1. int* frameScores= {0};
  2. int frameSize = 0; // size of frameScores
  3. int totalScore = 0;
  4. int lastFrameNumber = 0;
  5.  
  6. void addFrame(int toAdd)
  7. {
  8. totalScore = totalScore + toAdd;
  9. if (frameSize < lastFrameNumber)
  10. {
  11. frameScores = (int *)realloc(frameScores,(frameSize+1) * sizeof(int));
  12. frameScores[frameSize] = totalScore;
  13. ++frameSize;
  14. }
  15. }
  16.  
  17. int main()
  18. {
  19. }
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: Jul 2007
Posts: 77
Reputation: ivatanako is an unknown quantity at this point 
Solved Threads: 5
ivatanako ivatanako is offline Offline
Junior Poster in Training

Re: how do you convert this into c

 
0
  #8
Sep 8th, 2009
Originally Posted by Ancient Dragon View Post
If you are attempting to add a new element to frameScores array, such as increase its size from 5 to 6 elements, then make frameScores a pointer and reallocate its size.
  1. int* frameScores= {0};
  2. int frameSize = 0; // size of frameScores
  3. int totalScore = 0;
  4. int lastFrameNumber = 0;
  5.  
  6. void addFrame(int toAdd)
  7. {
  8. totalScore = totalScore + toAdd;
  9. if (frameSize < lastFrameNumber)
  10. {
  11. frameScores = (int *)realloc(frameScores,(frameSize+1) * sizeof(int));
  12. frameScores[frameSize] = totalScore;
  13. ++frameSize;
  14. }
  15. }
  16.  
  17. int main()
  18. {
  19. }
this is what i was trying to convert, the last page where the java code is

http://www.cs.berkeley.edu/~clancy/w...es/bowling.pdf

the framescore was declared as private with and it size is int[0]. I am new to java, and so far i was able to convert probably around 85% of the code, i was stuck with this part wherein the array from temp is being transfered into framescores.

and if you might find it interesting, here's my C code

  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<string.h>
  4.  
  5. const int ROLLING_FIRST_BALL = 0;
  6. const int ROLLING_SECOND_BALL =1;
  7. const int STRIKE_LAST_BALL =2;
  8. const int TWO_CONSEC_STRIKE =3;
  9. const int STRIKE_2_BALLS_AGO =4;
  10. const int SPARE_LAST_BALL =5;
  11.  
  12. class Scorer{
  13. private:
  14. int rollingFrame;
  15. int totalScore;
  16. int frameScores[];
  17. int state;
  18. int firstBallInFrame;
  19. int lastFrameNumber;
  20. void addFrame(int toAdd)
  21. {
  22. totalScore = totalScore + toAdd;
  23. if (sizeof(frameScores) < lastFrameNumber) {
  24. int temp[sizeof(frameScores)+1];
  25. for (int k=0; k<sizeof(frameScores); k++)
  26. {
  27. temp[k] = frameScores[k];
  28. }
  29. temp[sizeof(frameScores)] = totalScore;
  30. frameScores = temp;
  31. }
  32. }
  33. public:
  34. Scorer(int frameCount){
  35. lastFrameNumber = frameCount;
  36. rollingFrame = 1;
  37. totalScore = 0;
  38. frameScores[0];
  39. state = ROLLING_FIRST_BALL;
  40. }
  41. int frameNumber( )
  42. {
  43. if (sizeof(frameScores) == lastFrameNumber) {
  44. return lastFrameNumber+1;// game is over
  45. } else if (rollingFrame > lastFrameNumber) {
  46. return lastFrameNumber;// we're in last frame
  47. } else {
  48. return rollingFrame;
  49. }
  50. }
  51. int scoreSoFar(){
  52. if(sizeof(frameScores) == lastFrameNumber)
  53. return frameScores[lastFrameNumber-1];
  54. else
  55. return totalScore;
  56. }
  57. bool gameIsOver(){
  58. return frameNumber() > lastFrameNumber;
  59. }
  60. int *roll (int ball)
  61. {
  62. if(state == ROLLING_FIRST_BALL)
  63. {
  64. if(ball == 10)
  65. {
  66. rollingFrame++;
  67. state = STRIKE_LAST_BALL;
  68. }else
  69. {
  70. firstBallInFrame = ball;
  71. state=ROLLING_SECOND_BALL;
  72. }
  73. }else if(state == ROLLING_SECOND_BALL)
  74. {
  75. if(firstBallInFrame + ball == 10)
  76. {
  77. rollingFrame++;
  78. state = SPARE_LAST_BALL;
  79. }else
  80. {
  81. rollingFrame++;
  82. addFrame(firstBallInFrame + ball);
  83. state = ROLLING_FIRST_BALL;
  84. }
  85. }else if(state==SPARE_LAST_BALL)
  86. {
  87. addFrame(10 + ball);
  88. if(ball == 10)
  89. {
  90. rollingFrame++;
  91. state = STRIKE_LAST_BALL;
  92. }else
  93. {
  94. firstBallInFrame = ball;
  95. state = ROLLING_SECOND_BALL;
  96. }
  97. }else if(state == STRIKE_LAST_BALL)
  98. {
  99. if(ball==10)
  100. {
  101. rollingFrame++;
  102. state = TWO_CONSEC_STRIKE;
  103. }else
  104. {
  105. firstBallInFrame = ball;
  106. state = STRIKE_2_BALLS_AGO;
  107. }
  108. }
  109. else if(state == TWO_CONSEC_STRIKE)
  110. {
  111. addFrame(20 + ball);
  112. if(ball == 10)
  113. {
  114. rollingFrame++;
  115. }else
  116. {
  117. firstBallInFrame = ball;
  118. state = STRIKE_2_BALLS_AGO;
  119. }
  120. }
  121. else if(state == STRIKE_2_BALLS_AGO)
  122. {
  123. addFrame(10 + firstBallInFrame + ball);
  124. if(firstBallInFrame + ball == 10)
  125. {
  126. rollingFrame++;
  127. state=SPARE_LAST_BALL;
  128. }else
  129. {
  130. totalScore = totalScore + firstBallInFrame + ball;
  131. rollingFrame++;
  132. addFrame(firstBallInFrame + ball);
  133. state = ROLLING_FIRST_BALL;
  134. }
  135. }else
  136. {
  137. //invalid state: state
  138. }
  139. return frameScores;
  140. }
  141. };
Last edited by ivatanako; Sep 8th, 2009 at 10:55 pm. Reason: added my c code
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC