Thread: Problem!!!

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    11

    Question Problem!!!

    Hi,

    I'm kind of new at this, but I've looked at the code over and over and can't figure out my problem.

    Code:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    
    /*
    **Assumes that each player has no more than 10 
    moves each and that NO player is indifferent to 
    the other player's move (i.e. player1's payoff for making ).
    **Assumes that all the characters are ints separated by spaces 
    and that the first two ints are non-negative.
    */
    main(){
    	FILE *file;     //Declare file pointer
    	int a, b, c, d, e, f, g, h, i, j, k, m, n, p; 	//Array indexes
    
    //~~~Read in file~~~//
    	int cols, rows;
    	int size = cols*rows;
    	int p1Payoffs[size/2], p2Payoffs[size/2];
    	
    	file = fopen("prisoners_dilema.txt", "r");
    	if(file==NULL){
    		printf("Sorry! The file could not be opened!\n");
    	} else{
    
    	//Find #cols & #rows
    	int fscanf(file, "%d %d", &cols, &rows);
    
    	//Error check
    	if(size > 100){printf("Sorry! The game is too large. Each player may have 10 or fewer plays.\n"); return;}
    
    	//Read in player's payoffs
    	for(a=0; a<size*2; a;){
    		int fscanf(file, "%d %d", &p1Payoffs[a], &p2Payoffs[a]);
    	}
    	}
    	fclose(file);
    
    //~~~Find Player's Best Responses~~~//
    	int indexP1[size], indexP2[size];
    	int p1Counter=0, p2Counter=0;
    
    	//P1's BRs (columns)
    	for(c=0; c<cols-1; c++){//for each columns
    		for(d=0; d<size-cols; d+cols){//and for each cell in that column
    			if(p1Payoffs[d]>p1Payoffs[d+cols]){indexP1[p1Counter] = d; p1Counter++;}
    			else if(p1Payoffs[d]==p1Payoffs[d+cols]){
    				indexP1[p1Counter] = d;
    				indexP1[p1Counter+1] = d+cols;
    				p1Counter = p1Counter+2;
    			}
    		}
    	}
    
    	//P2's BRs (rows)
    	for(e=0; e<rows-1; e++){//for each row
    		for(f=0; f<cols; f++){//and each cell in that row
    			if(p2Payoffs[f]>p2Payoffs[f+1]){indexP2[p2Counter] = f; p2Counter++;}
    			else if(p2Payoffs[f]==p2Payoffs[f+1]){
    				indexP2[p2Counter] = f;
    				indexP2[p2Counter+1] = f++;
    				p2Counter = p2Counter+2;
    			}
    		}
    	}
    
    //~~~Find Nash Equilibria~~~//
    	int p1Move[p1Counter], p2Move[p1Counter];
    	int m1Counter=0, m2Counter=0;
    	
    	//If cell indexes match...
    	for(g=0; g<p1Counter; g++){
    		for(h=0; h<p2Counter; h++){
    			if(indexP1[g]==indexP2[h]){
    				//Determine the row in which this occurs
    				for(i=1; i<rows+1; i++){
    					if(indexP1[g]<(i)*(size/rows){
    						p1Move[m1Counter] = i-1;
    						m1Counter = m1Counter+1;
    					}
    				}
    				//Determine the column in which this occurs
    				for(j=0; j<cols; j++){
    					if(indexP1[g]%cols==j){
    						p2Move[m2Counter] = j;
    						m2Counter = m2Counter+1;
    					}
    				}
    			}
    		}
    	}
    
    //~~~Print Normal Form Game~~~//
    	//Print column labels
    	for(k=0; k<cols; k++){
    		printf("   %d", k);
    	}
    	//Print row labels & payoffs
    	for(m=0; m<rows; m++){
    		printf("%d  ", m);
    		for(n=0; n<m1Counter; n++){
    			printf("%d,%d  ", p1Move[n], p2Move[n]);
    		}
    	}
    
    
    //~~~Print Nash Equilibria~~~//
    
         //Finish this after I can read in the file properly
    
    }


    Errors:

    Code:
    nashEquilibriumFinder.c: In function ‘main’:
    nashEquilibriumFinder.c:26: error: expected ‘)’ before string constant
    nashEquilibriumFinder.c:32: error: expected ‘)’ before ‘;’ token
    nashEquilibriumFinder.c:33: error: expected ‘)’ before string constant
    nashEquilibriumFinder.c:76: error: expected ‘)’ before ‘{’ token
    nashEquilibriumFinder.c:80: error: expected expression before ‘}’ token

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    	//Find #cols & #rows
    	int fscanf(file, "%d %d", &cols, &rows);
    That int makes that a function prototype, not a function call.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    11
    Thanks so much! I really appreciate the help!

    Unfortunately, this code also doesn't print when I compile and run it.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    11
    I don't get it. It just doesn't print anything!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > int size = cols*rows;
    C does not predict the future, so do this after you have read rows and cols.

    > for(a=0; a<size*2; a;)
    Since your arrays are size/2, running to size*2 is a big overstep.
    Or it would be if you had written a++ rather than a
    At the moment, it's an infinite loop going nowhere.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    11
    I bow to your superiority.

    Thanks for your help, I've been writing and rewriting code for this project for so long that it has almost all blurred together. To make it worse, I'm sick as a dog, but the end of semester will come no slower.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    11

    Exclamation Ack! More problems!

    This the code I have post-revision:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    /*
    **Assumes that each player has no more than 10 
    moves each and that NO player is indifferent to 
    the other player's move (i.e. player1's payoff for making ).
    **Assumes that all the characters are ints separated by spaces 
    and that the first two ints are non-negative.
    */
    
    
    main(){
    
    	FILE *file;
    	int a, b, c, d; 	//Array index
    
    //~~~Read in file~~~//
    	int cols, rows;
    	int size;
    	int p1Payoffs[size/2], p2Payoffs[size/2];
    	
    	file = fopen("prisoners_dilema.txt", "r");
    	if(file==NULL){
    		printf("Sorry! The file could not be opened!\n");
    	} else{
    
    	//Find #cols & #rows
    	fscanf(file, "%d %d", &cols, &rows);
    
    	//Error check
    	if(size > 100){printf("Sorry! The game is too large. Each player may have 10 or fewer plays.\n"); return;}
    
    	//Read in player's payoffs
    	for(a=0; a<size; a++){
    		fscanf(file, "%d %d", &p1Payoffs[a], &p2Payoffs[a]);
    	}
    	}
    
    	fclose(file);
    
    	size = cols*rows;
    
    
    //~~~Find Player's Best Responses~~~//
    	int indexP1[size], indexP2[size];
    	int p1Counter=0, p2Counter=0;
    
    	//P1's BRs (columns)
    	for(a=1; a<=rows; a++){
    		for(d=0; d<size-cols; d+cols){
    			if(p1Payoffs[d]>p1Payoffs[d+cols*a]){indexP1[p1Counter] = d; p1Counter++;}
    			else if(p1Payoffs[d]==p1Payoffs[d+cols]){
    				indexP1[p1Counter] = d;
    				indexP1[p1Counter+1] = d+cols;
    				p1Counter = p1Counter+2;
    			}
    		}
    	}
    
    	//P2's BRs (rows)
    	for(b=0; b<cols; b++){
    		for(a=0; a<cols; a++){
    			if(p2Payoffs[a]>p2Payoffs[a+b]){indexP2[p2Counter] = a+b; p2Counter++;}
    			else if(p2Payoffs[a]==p2Payoffs[a+b]){
    				indexP2[p2Counter] = a;
    				indexP2[p2Counter+1] = a++;
    				p2Counter = p2Counter+2;
    			}
    		}
    	}
    
    //~~~Find Nash Equilibria~~~//
    	int p1Move[p1Counter], p2Move[p1Counter];
    	int m1Counter=0, m2Counter=0;
    	
    	//If cell indexes match...
    	for(a=0; a<p1Counter; a++){
    		for(b=0; b<p2Counter; b++){
    			if(indexP1[a]==indexP2[b]){
    				//Determine the row in which this occurs
    				for(c=1; c<rows+1; c++){
    					if(indexP1[a]<c*(size/rows)){
    						p1Move[m1Counter] = c-1;
    						m1Counter = m1Counter+1;
    					}
    				}
    				//Determine the column in which this occurs
    				for(d=0; d<cols; d++){
    					if(indexP1[a]%cols==d){
    						p2Move[m2Counter] = d;
    						m2Counter = m2Counter+1;
    					}
    				}
    			}
    		}
    	}
    
    //~~~Print Normal Form Game~~~//
    	//Print column labels
    	for(a=0; a<cols; a++){
    		printf("\t%d", a);
    	}
    	//Print row labels & payoffs
    	for(b=0; b<rows; b++){
    		printf("%d\t", b);
    		for(c=0; c<m1Counter; c++){
    			printf("%d,%d\t", p1Move[c], p2Move[c]);
    			if (c==m1Counter-1) {printf("\n");}
    		}
    	}
    
    
    
    //~~~Print Nash Equilibria~~~//
    
    
    return 0;
    }
    I then get
    Code:
    Segmentation fault

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    //Error check
    	if(size > 100){printf("Sorry! The game is too large. Each player may have 10 or fewer plays.\n"); return;}
    and where do you calculate the size?

    also - if fopen is failed - no need to call fclose
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    11
    Both are good catches, but neither seems to fix the problem. Thanks for your help, though.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you're just going to have to post your latest code.

    Code:
    	int size;
    	int p1Payoffs[size/2], p2Payoffs[size/2];
    	
    	file = fopen("prisoners_dilema.txt", "r");
    	if(file==NULL){
    		printf("Sorry! The file could not be opened!\n");
    	} else{
    
    	//Find #cols & #rows
    	fscanf(file, "%d %d", &cols, &rows);
    
    	//Error check
    	if(size > 100){printf("Sorry! The game is too large. Each player may have 10 or fewer plays.\n"); return;}
    
    	//Read in player's payoffs
    	for(a=0; a<size; a++){
    		fscanf(file, "%d %d", &p1Payoffs[a], &p2Payoffs[a]);
    	}
    	}
    
    	fclose(file);
    
    	size = cols*rows;
    You STILL have an array overrun, and you're still using size in many places without initialising it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    11
    My latest code that produces a segmentation error:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    /*
    **Assumes that each player has no more than 10 
    moves each and that NO player is indifferent to 
    the other player's move (i.e. player1's payoff for making ).
    **Assumes that all the characters are ints separated by spaces 
    and that the first two ints are non-negative.
    */
    
    
    main(){
    
    	FILE *file;
    	int a, b, c, d; 	//Array index
    
    //~~~Read in file~~~//
    	int cols, rows;
    	int size;
    	int p1Payoffs[100], p2Payoffs[100];
    	
    	file = fopen("prisoners_dilema.txt", "r");
    	if(file==NULL){
    		printf("Sorry! The file could not be opened!\n");
    	} else{
    
    	//Find #cols & #rows
    	fscanf(file, "%d %d", &cols, &rows);
    	size = cols*rows;
    
    	//Error check
    	if(size > 100){printf("Sorry! The game is too large. Each player may have 10 or fewer plays.\n"); return;}
    
    	//Read in player's payoffs
    
    	for(a=0; a<size; a++){
    		fscanf(file, "%d %d", &p1Payoffs[a], &p2Payoffs[a]);
    	}
    
    	fclose(file);
    	}
    
    
    
    //~~~Find Player's Best Responses~~~//
    	int indexP1[size/2], indexP2[size/2];
    	int p1Counter=0, p2Counter=0;
    
    	//P1's BRs (columns)
    	for(a=1; a<=rows; a++){
    		for(d=0; d<size-cols; d+cols){
    			if(p1Payoffs[d]>p1Payoffs[d+cols*a]){indexP1[p1Counter] = d; p1Counter++;}
    			else if(p1Payoffs[d]==p1Payoffs[d+cols]){
    				indexP1[p1Counter] = d;
    				indexP1[p1Counter+1] = d+cols;
    				p1Counter = p1Counter+2;
    			}
    		}
    	}
    
    	//P2's BRs (rows)
    	for(b=0; b<cols; b++){
    		for(a=0; a<cols; a++){
    			if(p2Payoffs[a]>p2Payoffs[a+b]){indexP2[p2Counter] = a+b; p2Counter++;}
    			else if(p2Payoffs[a]==p2Payoffs[a+b]){
    				indexP2[p2Counter] = a;
    				indexP2[p2Counter+1] = a++;
    				p2Counter = p2Counter+2;
    			}
    		}
    	}
    
    //~~~Find Nash Equilibria~~~//
    	int p1Move[p1Counter], p2Move[p1Counter];
    	int m1Counter=0, m2Counter=0;
    	
    	//If cell indexes match...
    	for(a=0; a<p1Counter; a++){
    		for(b=0; b<p2Counter; b++){
    			if(indexP1[a]==indexP2[b]){
    				//Determine the row in which this occurs
    				for(c=1; c<rows+1; c++){
    					if(indexP1[a]<c*(size/rows)){
    						p1Move[m1Counter] = c-1;
    						m1Counter = m1Counter+1;
    					}
    				}
    				//Determine the column in which this occurs
    				for(d=0; d<cols; d++){
    					if(indexP1[a]%cols==d){
    						p2Move[m2Counter] = d;
    						m2Counter = m2Counter+1;
    					}
    				}
    			}
    		}
    	}
    
    //~~~Print Normal Form Game~~~//
    	//Print column labels
    	for(a=0; a<cols; a++){
    		printf("\t%d", a);
    	}
    	//Print row labels & payoffs
    	for(b=0; b<rows; b++){
    		printf("%d\t", b);
    		for(c=0; c<m1Counter; c++){
    			printf("%d,%d\t", p1Move[c], p2Move[c]);
    			if (c==m1Counter-1) {printf("\n");}
    		}
    	}
    
    
    
    //~~~Print Nash Equilibria~~~//
    
    
    return 0;
    }
    Sorry for all the dumbass mistakes. It seems I can't see the trees for the forest...

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    I get a compile error on

    Code:
    if(size > 100){printf("Sorry! The game is too large. Each player may have 10 or fewer plays.\n"); return;}
    You are "returning" in a function with no value and you have an "int" for a return in the prototype.

    Are you able to compile and run - Seems so based on your error - so WHERE do you get the fault?

    edit: Hmm.. seems my compiler decided it wanted an int (another problem). OK, my bad.

    BTW, it would benefit us all if you were to NOT put multiple statements on one line and also to clean up your indentation.
    Last edited by mike65535; 04-20-2011 at 05:11 AM.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    And... what does "prisoners_dilema.txt" look like?

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    OK I started cleaning up your indentation and it's immediately clear you don't exit gracefully if the .txt file doesn't exist.

  15. #15
    Registered User
    Join Date
    Apr 2011
    Posts
    11
    The formatting issues are my bad. I do that for my own ease of viewing while working. The latest code...

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    /*
    **Assumes that each player has no more than 10 
    moves each and that NO player is indifferent to 
    the other player's move (i.e. player1's payoff for making ).
    **Assumes that all the characters are ints separated by spaces 
    and that the first two ints are non-negative.
    */
    
    
    main(){
    
    	FILE *file;
    	int a, b, c, d; 	//Array index
    
    //~~~Read in file~~~//
    	int cols, rows;
    	int size;
    	int p1Payoffs[100], p2Payoffs[100];
    	
    	file = fopen("prisoners_dilema.txt", "r");
    	if(file==NULL){
    		printf("Sorry! The file could not be opened!\n");
    	} else{
    
    		//Find #cols & #rows
    		fscanf(file, "%d %d", &cols, &rows);
    		size = cols*rows;
    	
    		//Error check
    		if(size > 100){
    			printf("Sorry! The game is too large. Each player may have 10 or fewer plays.\n");
    		}
    	
    		//Read in player's payoffs
    	
    		for(a=0; a<size; a++){
    			fscanf(file, "%d %d", &p1Payoffs[a], &p2Payoffs[a]);
    		}
    	
    		fclose(file);
    	}
    
    
    
    //~~~Find Player's Best Responses~~~//
    	int indexP1[size/2], indexP2[size/2];
    	int p1Counter=0, p2Counter=0;
    
    	//P1's BRs (columns)
    	for(a=1; a<=rows; a++){
    		for(d=0; d<size-cols; d+cols){
    			if(p1Payoffs[d]>p1Payoffs[d+cols*a]){
    				indexP1[p1Counter] = d; p1Counter++;
    			}
    			else if(p1Payoffs[d]==p1Payoffs[d+cols]){
    				indexP1[p1Counter] = d;
    				indexP1[p1Counter+1] = d+cols;
    				p1Counter = p1Counter+2;
    			}
    		}
    	}
    
    	//P2's BRs (rows)
    	for(b=0; b<cols; b++){
    		for(a=0; a<cols; a++){
    			if(p2Payoffs[a]>p2Payoffs[a+b]){
    				indexP2[p2Counter] = a+b; p2Counter++;
    			}
    			else if(p2Payoffs[a]==p2Payoffs[a+b]){
    				indexP2[p2Counter] = a;
    				indexP2[p2Counter+1] = a++;
    				p2Counter = p2Counter+2;
    			}
    		}
    	}
    
    //~~~Find Nash Equilibria~~~//
    	int p1Move[p1Counter], p2Move[p1Counter];
    	int m1Counter=0, m2Counter=0;
    	
    	//If cell indexes match...
    	for(a=0; a<p1Counter; a++){
    		for(b=0; b<p2Counter; b++){
    			if(indexP1[a]==indexP2[b]){
    				//Determine the row in which this occurs
    				for(c=1; c<rows+1; c++){
    					if(indexP1[a]<c*(size/rows)){
    						p1Move[m1Counter] = c-1;
    						m1Counter = m1Counter+1;
    					}
    				}
    				//Determine the column in which this occurs
    				for(d=0; d<cols; d++){
    					if(indexP1[a]%cols==d){
    						p2Move[m2Counter] = d;
    						m2Counter = m2Counter+1;
    					}
    				}
    			}
    		}
    	}
    
    //~~~Print Normal Form Game~~~//
    	//Print column labels
    	for(a=0; a<cols; a++){
    		printf("\t%d", a);
    	}
    	//Print row labels & payoffs
    	for(b=0; b<rows; b++){
    		printf("%d\t", b);
    		for(c=0; c<m1Counter; c++){
    			printf("%d,%d\t", p1Move[c], p2Move[c]);
    			if (c==m1Counter-1) {
    				printf("\n");
    			}
    		}
    	}
    
    
    
    //~~~Print Nash Equilibria~~~//
    
    
    return 0;
    }

    ... and prisoners_dilema.txt is simply a text file containing
    Code:
    2 2 1 0 0 1 1 1 0 1

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  2. sturct/pointer problem, and fscanf problem
    By hiphop4reel in forum C Programming
    Replies: 6
    Last Post: 07-28-2008, 09:40 AM
  3. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  4. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM
  5. Texture Problem(I got the NeHe tut working, but I have a problem)
    By SyntaxBubble in forum Game Programming
    Replies: 2
    Last Post: 12-02-2001, 10:40 PM

Tags for this Thread