ATTN C People: Error passing arrays in Func

Discussion in 'OT Technology' started by Disguy, May 2, 2007.

  1. Disguy

    Disguy Batman > Moon Knight OT Supporter

    Joined:
    Oct 26, 2004
    Messages:
    1,061
    Likes Received:
    0
    I have written my program for my final but I have run into a problem when I created a separate func to do my prints. I don't know what I am doing wrong.

    Here is the code.
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    void printStatement (float win[], float loss[], int pointsFor[], int pointsAgainst[]);
    int main()
    
    {
    	// Four teams, user number of weeks. Percentage wins, points for / against.
    
    	int weeks, homePoints, awayPoints, home, away, count, subCount, predict;
    	int pointsFor [4] = {0,0,0,0};
    	int pointsAgainst[4] = {0,0,0,0};
    	float win[4] = {0,0,0,0};
    	float loss[4] = {0,0,0,0};
    	int complexPointsHome =0;
    	int complexPointsAway =0;
    
    
    	printf("Enter the number of weeks: ");
    	scanf("%i", &weeks);
    
    	// Set up a loop for the number of weeks such that two games are played each week.
    	// If team 1 and 2 are playing teams 3 and 4 must play.
    	for (count =0; count < weeks; count++)
    	{
    		if (count = 2);
    			printStatement(win, loss, pointsFor, pointsAgainst);
    
    		for (subCount = 0; subCount < 2; subCount++)
    		{
    
    		predict =0;
    
    		if (count > 2)
    			{
    			printf("Would you like me to predict the winner? 0-NO, 1-YES: ");
    			scanf(" %i", &predict);
    			}
    
    		printf("\nEnter the Away team: ");
    		scanf("%i", &away);
    
    		printf("\nEnter the Home team: ");
    		scanf("%i", &home);
    
    		if (predict ==1)
    			{
    				printf("\n\t\tSimple Prediction\n");
    				if (win[home]>win[away])
    					printf("I predict Team %i will win!", home);
    				else if (win[home] < win[away])
    					printf("I predict Team %i will win!", away);
    				else printf("It's to close to tell!");
    
    				printf("\n\t\tComplex Prediction\n");
    
    				complexPointsHome++;
    
    				if (win[home]>win[away])
    					complexPointsHome++;
    
    				else if (win[home] < win[away])
    					complexPointsAway++;
    
    				if (pointsFor[home]>pointsFor[away])
    					complexPointsHome++;
    
    				else if(pointsFor[home]<pointsFor[away])
    					complexPointsAway++;
    
    				if (pointsAgainst[home]<pointsAgainst[away])
    					complexPointsHome++;
    
    				else if (pointsFor[home]<pointsFor[away])
    					complexPointsAway++;
    
    
    				if (win[home]>win[away])
    					printf("I predict Team %i will win!", home);
    				else if (win[home] < win[away])
    					printf("I predict Team %i will win!", away);
    				else printf("It's to close to tell!");
    
    
    			}
    
    		if (predict = 0);
    			{
    				printf("\nEnter the Home team's points: ");
    				scanf("%i", &homePoints);
    				pointsFor[home] =homePoints + pointsFor[home];
    				pointsAgainst[away] = homePoints +pointsAgainst[away];
    
    
    				printf("\nEnter the Away team's points: ");
    				scanf("%i", &awayPoints);
    				pointsAgainst[home] = awayPoints+pointsAgainst[home];
    				pointsFor[away] =awayPoints+pointsFor[away];
    
    				if (homePoints > awayPoints)
    					{
    						win[home]++;
    						loss[away]++;
    					}
    				else
    					{
    						win[away]++;
    						loss[home]++;
    					}
    					//repeat for second game, which will include the other two teams.
    			}
    
    		}
    
    	}
    
    
    	return 0;
    }
    
    void printStatement (float win[], float loss[], int pointsFor[], int pointsAgainst[]);
    {
    	int weeks, pCount;
    	float percent[4] = {0,0,0,0};
    
    
    weeks = (win[0]+loss[0], win[1]+loss[1], win[2]+loss[2], win[3]+loss[3])/4;
    
    	printf("\nThere are %i weeks so far.\n" , weeks);
    
    	for(pCount =0; pCount < 4; pCount++)
    	{
    		printf("Team %i wins %.0f losses %.0f\n", pCount, win[pCount], loss[pCount]);
    
    		if (win[pCount] == weeks)
    			percent[pCount] = 1;
    
    		else if (win[pCount] == 0)
    			percent[pCount] =0;
    
    		else percent[pCount] = win[pCount]/ weeks;
    			printf("percent %i = %.2f / %i = %.2f\n", pCount,  win[pCount], weeks, percent[pCount]);
    
    	}
    
    	printf("\nLeague standings after %i weeks:\n", weeks);
    	printf("Team 0 %.0f - %.0f \n", win[0], loss[0]);
    	printf("Team 1 %.0f - %.0f \n", win[1], loss[1]);
    	printf("Team 2 %.0f - %.0f \n", win[2], loss[2]);
    	printf("Team 3 %.0f - %.0f \n", win[3], loss[3]);
    
    	printf("\nTeam 0 %i - %i \n", pointsFor[0], pointsAgainst[0]);
    	printf("Team 1 %i - %i \n", pointsFor[1], pointsAgainst[1]);
    	printf("Team 2 %i - %i \n", pointsFor[2], pointsAgainst[2]);
    	printf("Team 3 %i - %i \n", pointsFor[3], pointsAgainst[3]);
    
    	printf("\nTeam 0 %.0f%% \n", (percent[0]*100));
    	printf("Team 1 %.0f%%\n", (percent[1]*100));
    	printf("Team 2 %.0f%% \n", (percent[2]*100));
    	printf("Team 3 %.0f%%\n", (percent[3]*100));
    
    }
    
    
    
     
  2. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    what output are you getting and what output do you expect?
     
  3. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Code:
    if (count = 2);
        printStatement(win, loss, pointsFor, pointsAgainst);
    
    If you're getting more output than expected, or an infinite loop, my guess would be the argument to if.
     
  4. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    1. Arrays must always be passed by reference. I don't remember whether using [] brackets in the argument list automatically includes an implicit reference.

    2. Check your function header syntax.

    I'm not going to tell you more than that. It's your damn final, not mine.
     
  5. Disguy

    Disguy Batman > Moon Knight OT Supporter

    Joined:
    Oct 26, 2004
    Messages:
    1,061
    Likes Received:
    0
    Error project.c: 122 unrecognized declaration
    Warning project.c: 127 no type specified. Defaulting to int
    Error project.c: 127 undeclared identifier 'win'
    Error project.c: 127 type error: pointer expected
    Error project.c: 127 undeclared identifier 'loss'
    Error project.c: 127 type error: pointer expected
    Error project.c: 127 type error: pointer expected
    Error project.c: 127 type error: pointer expected
    Error project.c: 127 type error: pointer expected
    Error project.c: 127 type error: pointer expected
    Error project.c: 127 type error: pointer expected
    Error project.c: 127 type error: pointer expected
    Error project.c: 127 initializer must be constant
    Warning project.c: 129 no type specified. Defaulting to int
    Error project.c: 129 syntax error; found "\12There are %i weeks ... expecting ')'
    Error project.c: 129 skipping "\12There are %i weeks ... `,' `weeks'
    Error project.c: 129 redefinition of 'printf'
    Error c:\lcc\include\stdio.h: 132 Previous definition of 'printf' here
    Error project.c: 131 unrecognized declaration
    Warning project.c: 131 no type specified. Defaulting to int
    Error project.c: 131 syntax error; found `=' expecting ')'
    Warning project.c: 131 no type specified. Defaulting to int
    Error project.c: 131 Syntax error; missing semicolon before `<'
    Error project.c: 131 skipping `<'
    Error project.c: 131 too many errors
    1 error

    Tool completed with exit code 1
     
  6. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    I suspect an unmatched curly brace, parenthesis, etc. before it gets to the print function.
     
  7. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    Did you not read what I just told you?
     
  8. Disguy

    Disguy Batman > Moon Knight OT Supporter

    Joined:
    Oct 26, 2004
    Messages:
    1,061
    Likes Received:
    0
    Nevermind for some reason I had a semi colon next to the function header
     
  9. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    Because you copied it from the prototype, most likely. That's a good reason to put opening { on the same line as the block header instead of on the next line; it forces you to pay attention to how the line with the block header on it is terminated.

    e.g.:

    if (something){
    do this;
    }

    instead of:

    if (something)
    {
    do this
    }
     

Share This Page