| | |
how do you convert this into c
![]() |
•
•
Join Date: Jul 2007
Posts: 77
Reputation:
Solved Threads: 5
this is a java code, i need to convert it into C.
im having problem with
frameScores = temp
it is throwing a incompatible types errors
here is my C code
C Syntax (Toggle Plain Text)
private void addFrame (int toAdd) { int[ ] frameScores = new int[0]; totalScore = totalScore + toAdd; if (frameScores.length < lastFrameNumber) { int [ ] temp = new int [frameScores.length+1]; for (int k=0; k<frameScores.length; k++) { temp[k] = frameScores[k]; } temp[frameScores.length] = totalScore; frameScores = temp; } }
im having problem with
frameScores = temp
it is throwing a incompatible types errors
here is my C code
C Syntax (Toggle Plain Text)
void addFrame(int toAdd) { totalScore = totalScore + toAdd; if (sizeof(frameScores) < lastFrameNumber) { int temp[sizeof(frameScores)+1]; for (int k=0; k<sizeof(frameScores); k++) { temp[k] = frameScores[k]; } temp[sizeof(frameScores)] = totalScore; frameScores = temp; } }
>>int temp[sizeof(frameScores)+1];
That line is wrong. If frameScores is declared as
Well that will correct that line, but it still will not correct the line
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.
•
•
Join Date: Jul 2007
Posts: 77
Reputation:
Solved Threads: 5
•
•
•
•
>>int temp[sizeof(frameScores)+1];
That line is wrong. If frameScores is declared asint 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 beint temp[sizeof((frameScores)/sizeof(frameScores[0]))+1];
Well that will correct that line, but it still will not correct the lineframeScores = temp;because the two arrays are not the same size.
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.
C Syntax (Toggle Plain Text)
int* frameScores= {0}; int frameSize = 0; // size of frameScores int totalScore = 0; int lastFrameNumber = 0; void addFrame(int toAdd) { totalScore = totalScore + toAdd; if (frameSize < lastFrameNumber) { frameScores = (int *)realloc(frameScores,(frameSize+1) * sizeof(int)); frameScores[frameSize] = totalScore; ++frameSize; } } int main() { }
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.
•
•
Join Date: Jul 2007
Posts: 77
Reputation:
Solved Threads: 5
•
•
•
•
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.
C Syntax (Toggle Plain Text)
int* frameScores= {0}; int frameSize = 0; // size of frameScores int totalScore = 0; int lastFrameNumber = 0; void addFrame(int toAdd) { totalScore = totalScore + toAdd; if (frameSize < lastFrameNumber) { frameScores = (int *)realloc(frameScores,(frameSize+1) * sizeof(int)); frameScores[frameSize] = totalScore; ++frameSize; } } int main() { }
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
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> #include<string.h> const int ROLLING_FIRST_BALL = 0; const int ROLLING_SECOND_BALL =1; const int STRIKE_LAST_BALL =2; const int TWO_CONSEC_STRIKE =3; const int STRIKE_2_BALLS_AGO =4; const int SPARE_LAST_BALL =5; class Scorer{ private: int rollingFrame; int totalScore; int frameScores[]; int state; int firstBallInFrame; int lastFrameNumber; void addFrame(int toAdd) { totalScore = totalScore + toAdd; if (sizeof(frameScores) < lastFrameNumber) { int temp[sizeof(frameScores)+1]; for (int k=0; k<sizeof(frameScores); k++) { temp[k] = frameScores[k]; } temp[sizeof(frameScores)] = totalScore; frameScores = temp; } } public: Scorer(int frameCount){ lastFrameNumber = frameCount; rollingFrame = 1; totalScore = 0; frameScores[0]; state = ROLLING_FIRST_BALL; } int frameNumber( ) { if (sizeof(frameScores) == lastFrameNumber) { return lastFrameNumber+1;// game is over } else if (rollingFrame > lastFrameNumber) { return lastFrameNumber;// we're in last frame } else { return rollingFrame; } } int scoreSoFar(){ if(sizeof(frameScores) == lastFrameNumber) return frameScores[lastFrameNumber-1]; else return totalScore; } bool gameIsOver(){ return frameNumber() > lastFrameNumber; } int *roll (int ball) { if(state == ROLLING_FIRST_BALL) { if(ball == 10) { rollingFrame++; state = STRIKE_LAST_BALL; }else { firstBallInFrame = ball; state=ROLLING_SECOND_BALL; } }else if(state == ROLLING_SECOND_BALL) { if(firstBallInFrame + ball == 10) { rollingFrame++; state = SPARE_LAST_BALL; }else { rollingFrame++; addFrame(firstBallInFrame + ball); state = ROLLING_FIRST_BALL; } }else if(state==SPARE_LAST_BALL) { addFrame(10 + ball); if(ball == 10) { rollingFrame++; state = STRIKE_LAST_BALL; }else { firstBallInFrame = ball; state = ROLLING_SECOND_BALL; } }else if(state == STRIKE_LAST_BALL) { if(ball==10) { rollingFrame++; state = TWO_CONSEC_STRIKE; }else { firstBallInFrame = ball; state = STRIKE_2_BALLS_AGO; } } else if(state == TWO_CONSEC_STRIKE) { addFrame(20 + ball); if(ball == 10) { rollingFrame++; }else { firstBallInFrame = ball; state = STRIKE_2_BALLS_AGO; } } else if(state == STRIKE_2_BALLS_AGO) { addFrame(10 + firstBallInFrame + ball); if(firstBallInFrame + ball == 10) { rollingFrame++; state=SPARE_LAST_BALL; }else { totalScore = totalScore + firstBallInFrame + ball; rollingFrame++; addFrame(firstBallInFrame + ball); state = ROLLING_FIRST_BALL; } }else { //invalid state: state } return frameScores; } };
Last edited by ivatanako; Sep 8th, 2009 at 10:55 pm. Reason: added my c code
![]() |
Similar Threads
- download pdf file, convert to html, in PHP since working in Linux (PHP)
- convert int to string (C)
- How to convert wav to mp3 using VB? (Visual Basic 4 / 5 / 6)
- cannot convert from 'double' to 'float [6][3]' (C)
- problem with convert jumbled text file to unjumbled text file (C)
- Need help to convert Int64 to Base36 ? (PHP)
- VC++:convert File IStream to hex and store...? (C++)
Other Threads in the C Forum
- Previous Thread: explanation related to structure
- Next Thread: A (hopefully) simple question...
| Thread Tools | Search this Thread |
adobe api array arrays bash binarysearch calculate centimeter char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators initialization intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation pattern pdf performance pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scanf scheduling scripting segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions test testautomation unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h






