-
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
-
Code:
//Find #cols & #rows
int fscanf(file, "%d %d", &cols, &rows);
That int makes that a function prototype, not a function call.
Quzah.
-
Thanks so much! I really appreciate the help!
Unfortunately, this code also doesn't print when I compile and run it.
-
I don't get it. It just doesn't print anything!
-
> 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.
-
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.
-
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:
//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
-
Both are good catches, but neither seems to fix the problem. Thanks for your help, though.
-
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.
-
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...
-
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.
-
And... what does "prisoners_dilema.txt" look like?
-
OK I started cleaning up your indentation and it's immediately clear you don't exit gracefully if the .txt file doesn't exist.
-
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