943,694 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 527
  • C RSS
Sep 8th, 2009
0

how do you convert this into c

Expand Post »
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. }
Similar Threads
Reputation Points: 18
Solved Threads: 15
Junior Poster
ivatanako is offline Offline
150 posts
since Jul 2007
Sep 8th, 2009
0

Re: how do you convert this into c

Java is derived from C. You can use dynamic memory allocation.
Reputation Points: 188
Solved Threads: 44
Posting Pro
Majestics is offline Offline
554 posts
since Jul 2007
Sep 8th, 2009
-7

Re: how do you convert this into c

How is frameScores declared?
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Sep 8th, 2009
0

Re: how do you convert this into c

How is frameScores declared?
it is declared as an integer array. Same as temp.
Reputation Points: 18
Solved Threads: 15
Junior Poster
ivatanako is offline Offline
150 posts
since Jul 2007
Sep 8th, 2009
-7

Re: how do you convert this into c

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Sep 8th, 2009
0

Re: how do you convert this into c

>>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?
Reputation Points: 18
Solved Threads: 15
Junior Poster
ivatanako is offline Offline
150 posts
since Jul 2007
Sep 8th, 2009
-7

Re: how do you convert this into c

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. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Sep 8th, 2009
0

Re: how do you convert this into c

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
Reputation Points: 18
Solved Threads: 15
Junior Poster
ivatanako is offline Offline
150 posts
since Jul 2007

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: explanation related to structure
Next Thread in C Forum Timeline: A (hopefully) simple question...





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


Follow us on Twitter


© 2011 DaniWeb® LLC