Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
May 16th, 2013, 09:20 AM
 otisskiller
Registered User

Join Date: May 2013
Posts: 1
Time spent in forums: 20 m 33 sec
Reputation Power: 0
Voting game

Hello members, i have a probleme concerning the a voting game .
Here is the code :

#include<stdio.h>
#include<conio.h>
#define DIM 100
void vote(char contestants[],int n);
int sum(int n);
char contestants[]={'A','B','C','D'};
main()
{
srand(time(NULL));
vote(contestants,4);

getch(); }
void vote(char contestants[],int n) //Voting function
{
int verification[DIM][DIM]={0},marks[DIM]={0},s,u,v,e,i;
s=sum(n);
for(i=0;i<s;i++)
{ do //Choosing two differents index
{
u = rand( ) % n;
v = rand( ) % n;
}
while(u==v);

if(verification[u][v]!=1 && verification[v][u]!=1) //Checking whether he two contestants have already fought each other or not :
{
printf("Which one is better ? 1-%c ou 2-%c .\n Enter 1 or 2 to choose :",contestants[u],contestants[v]);
scanf("%d",&e);
if(e==1)
{
marks[u]=marks[u]+1;
}
else
{
marks[v]=marks[v]+1;
};

verification[u][v]=1;
verification[v][u]=1;
}
else
{
i--; //for repeating the instruction without incrementing the value of 'i'
};
}
int k;
for(k=0;k<n;k++)
{
printf("%c's mark is %d \n",contestants[k],marks[k]);
}
}
int sum(int n)
{
int v,i;
v=0;
for(i=1 ; i<=n ; i++)
{
v=v+i;
}
return v;
}

The probleme is that the program works for some contestants and stops . I really can't identify the problem.

Hope you understand my idea and help me identify the problem .

#2
May 16th, 2013, 01:18 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,170
Time spent in forums: 1 Month 3 Weeks 2 Days 10 h 27 m 9 sec
Reputation Power: 455
Don't put semi-colons after right braces. If you continue, it will someday cause you a horrid trouble.

Otherwise, include more header files to make the declarations known.

#include<time.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>

also, your initialization was invalid according to gcc. I used memset.

int
verification[DIM][DIM],
marks[DIM],
s,u,v,e,i;

memset(&(verification[0][0]),0,sizeof(verification));
memset(&(marks[0]),0,sizeof(marks));

Otherwise, I do not know what you expect your program should do.
__________________
[code]Code tags[/code] are essential for python code!

#3
May 16th, 2013, 01:45 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,866
Time spent in forums: 3 Months 1 Day 7 h 39 m 48 sec
Reputation Power: 2199
1. Use code tags! Otherwise your code listing will lose its formatting.

Code:
```#include<stdio.h>
#include<conio.h>
#define DIM 100
void vote(char contestants[],int n);
int sum(int n);
char contestants[]={'A','B','C','D'};
main()
{
srand(time(NULL));
vote(contestants,4);

getch();    }
void vote(char contestants[],int n) //Voting function
{
int verification[DIM][DIM]={0},marks[DIM]={0},s,u,v,e,i;
s=sum(n);
for(i=0;i<s;i++)
{  do                //Choosing two differents index
{
u = rand( ) % n;
v = rand( ) % n;
}
while(u==v);

if(verification[u][v]!=1 && verification[v][u]!=1) //Checking whether he two contestants have already fought each other or not :
{
printf("Which one is better ? 1-%c ou 2-%c .\n Enter 1 or 2 to choose :",contestants[u],contestants[v]);
scanf("%d",&e);
if(e==1)
{
marks[u]=marks[u]+1;
}
else
{
marks[v]=marks[v]+1;
};

verification[u][v]=1;
verification[v][u]=1;
}
else
{
i--; //for repeating the instruction without incrementing the value of 'i'
};
}
int k;
for(k=0;k<n;k++)
{
printf("%c's mark is %d \n",contestants[k],marks[k]);
}
}
int sum(int n)
{
int v,i;
v=0;
for(i=1 ; i<=n ; i++)
{
v=v+i;
}
return v;
}```

2. Do a proper job of formatting your code. Crappy formatting is crappy formatting. Be consistent in your indenting. And hiding close braces at the ends of lines is even worse than hiding open braces there.

3. Do not ignore warnings! Warnings are much more important than error messages! And do not attempt to run a program until you have cleared all warnings. If you do, then you have no idea what it's really doing.

Here is what I get when I try to compile your program with MinGW gcc:
Quote:
 C:TEST>gcc -Wall otis01.c otis01.c:8: warning: return-type defaults to `int' otis01.c: In function `main': otis01.c:9: warning: implicit declaration of function `srand' otis01.c:9: warning: implicit declaration of function `time' otis01.c:12: warning: control reaches end of non-void function otis01.c: In function `vote': otis01.c:15: warning: missing braces around initializer otis01.c:15: warning: (near initialization for `verification[0]') otis01.c:20: warning: implicit declaration of function `rand' otis01.c:46: parse error before `int' otis01.c:47: `k' undeclared (first use in this function) otis01.c:47: (Each undeclared identifier is reported only once otis01.c:47: for each function it appears in.) C:TEST>

The "implicit declaration of function" warnings tell you which functions you didn't #include the header file for. And you see warnings about your botched initialization on line 15 that b49P23TIvg told you about. There's also that misplaced declaration of k on line 46 -- given your secrecy about what language you're using, I'm assuming C rather than C++ or C99, so if you're using something other than C (which means C89 to programmers) then you need to say so.

Never ignore warnings!

Last edited by dwise1_aol : May 16th, 2013 at 01:49 PM.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Voting game