this is a java code, i need to convert it into C.

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

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;
            }     
    }

How is frameScores declared?

it is declared as an integer array. Same as temp.

>>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.

Edited 7 Years Ago by Ancient Dragon: n/a

>>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?

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.

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()
{
}

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.

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()
{
}

this is what i was trying to convert, the last page where the java code is

http://www.cs.berkeley.edu/~clancy/web/case.studies/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

#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;
}
};

Edited 7 Years Ago by ivatanako: added my c code

This article has been dead for over six months. Start a new discussion instead.