Problem output of function
void find_winner(int array[]){
int i,questions[3],j=0;
for (i=0; i<3; i++){
questions[i]=1;
}
for (i=0; i<3; i++){
while (array[j+1]!=1){
questions[i]++;
j++;
}
}
for (i=0; i<3; i++){
printf("%d",questions[i]);
}
}
This function should find the winner of a game,that is the one that answered at the most questions.For example,if there is an array
[1]
[2]
[3]
[1]
[2]
[1]
[2]
[3]
the first player answered three questions,the second one answered 2 questions and the third one three.so,there are two winners,the player one and the player three.
My problem is that it just prints the number of questions that the first two players answered.What have I done wrong?
I hope someone can help me!!!
Re: Problem output of function
not to sound like a smart a$$ but thats what your code should do with the way you have it coded. One way to get it to do what you want it to do would be to have a variable in the second for loop (declared before entering the loop so it is accessible from the third for loop also) that kept track of the most questions answered, then on the third for loop you could have it only print the elements in the array that equaled the variable.
Re: Problem output of function
I have tried it with a variable,but it also wasn't accessible from the third loop.Although the last player answered just three questions,the variable I used got the number 22,and it shoulb be 3,at this case :confused:
Re: Problem output of function
Shouldn't it be that player 1 answered 3, player 2 answered 3 and player 3 answered 2?
First initialise questions to 0. Loop through array (using i?) and do questions[array[i] - 1]++; This will then give you the scores for the players (assuming array contains values in the range 1 - 3 (you should check). Then just iterate through questions to find the element(s) with the highest score and its index + 1 is(are) the winners. When I get access to my MSVS again I'll knock you up some code. In the meantime I hope this helps.
Re: Problem output of function
Try this
Code:
#include <iostream>
using namespace std;
#define NO_PLAYER 3
void find_winner(int ans[]) {
int players[NO_PLAYER] = {0},
j = 0,
max = 0;
while (ans[j] >= 1 && ans[j] <= NO_PLAYER) {
int pl = ans[j] - 1;
players[pl]++;
if (players[pl] > max)
max = players[pl];
j++;
}
for (int i = 0; i < NO_PLAYER; i++)
if (players[i] == max)
cout << "player " << i + 1 << " is a winner!" << endl;
}
int main()
{
int answers[] = {1, 2, 3, 1, 2, 1, 2, 3, 0};
find_winner(answers);
return 0;
}
The end of the answers is indicated by a 0.
Re: Problem output of function
Nice,but the numbers of the array answers do not correspond to the players that answer each time a question...
They correspond to the question each player has to answer...For example,if we have the array
answers[] = {1, 2, 3, 4, 5, 6, 1, 1, 2, 0};
the first player answers at 6 question,the second at 1,the third at 2...How can I do this???? :ehh:
Re: Problem output of function
Sorry - I misunderstood your question. Do the number sequence always start at 1 and do they increment by 1 until they are reset to 1? So the first 1 corresponds to player 1 and the second 1 corresponds to player 2 etc?
Re: Problem output of function
Re: Problem output of function
OK. Try this.
Code:
#include <iostream>
using namespace std;
void find_winner(int ans[]) {
int noplay = 0,
i = 0,
pl = -1,
max = 0;
while (ans[i] > 0)
if (ans[i++] == 1)
noplay++;
int *play = new int[noplay];
for (int j = 0; j < noplay; j++)
play[j] = 0;
i = 0;
while (ans[i] > 0) {
if (ans[i++] == 1)
pl++;
play[pl]++;
if (play[pl] > max)
max = play[pl];
}
for (int j = 0; j < noplay; j++)
if (play[j] == max)
cout << "player " << j + 1 << " winner" << endl;
delete [] play;
}
int main()
{
int answers[] = {1, 2, 3, 1, 2, 1, 2, 3, 0};
//int answers[] = {1, 2, 3, 4, 5, 6, 1, 1, 2, 0};
find_winner(answers);
return 0;
}
Re: Problem output of function
I have runned the program,using this function:
Code:
void find_winner(int ans[]) {
int noplay=0,i=0,pl=-1,max=0,*play,j;
while (ans[i] > 0){
if (ans[i++] == 1) noplay++;
}
printf("%d\n",noplay);
for (j = 0; j < noplay; j++)
play[j] = 0;
i=0;
while (ans[i] > 0) {
if (ans[i++] == 1)
pl++;
play[pl]++;
if (play[pl] > max)
max = play[pl];
}
for (j = 0; j < noplay; j++)
if (play[j]==max)
printf("player %d winner",j+1);
}
but I get a segmentation fault...Why does it happen??? :confused:
Re: Problem output of function
Now I runned it using this code:
Code:
void find_winner(int ans[]) {
int noplay=0,i=0,pl=-1,max=0,play[3],j;
for (j = 0; j <3; j++)
play[j] = 0;
i=0;
while (ans[i] > 0) {
if (ans[i++] == 1)
pl++;
play[pl]++;
if (play[pl] > max)
max = play[pl];
}
for (j = 0; j <3; j++)
if (play[j]==max)
printf("player %d winner",j+1);
}
I gave at all the players the same number of questions,but the output is just : player 3 winner
How can I change this code so that if all the players are winners that they will all be printed???
Re: Problem output of function
You are not using the code I posted! Your code in post #10 fails because play is a pointer to an integer and hasn't been initialised. That's what the new did in my original code.
Your code in post #11 when run on my system produces the output
player 1 winnerplayer 3 winner
Try changing the printf statement to
Code:
printf("player %d winner\n",j+1);
Re: Problem output of function
Yes,it has a right result if there are two winners,but not if there are 3 :/
Re: Problem output of function
If you want to use an array in the function, then the code can be shorted to
Code:
void find_winner(int ans[]) {
int noplay = 0,
i = 0,
pl = -1,
max = 0,
play[3] = {0},
j;
while (ans[i] > 0) {
if (ans[i++] == 1)
pl++;
play[pl]++;
if (play[pl] > max)
max = play[pl];
}
for (j = 0; j < 3; j++)
if (play[j] == max)
printf("player %d winner\n", j + 1);
}
On my system with the input 1, 2, 3, 1, 2, 1, 2, 3, 0 this gives as output
player 1 winner
player 3 winner
Note that if there are more than 3 players, this program will crash! My code in post #9 dealt with an unknown number of players.
Re: Problem output of function
Quote:
Originally Posted by
mathmari
Yes,it has a right result if there are two winners,but not if there are 3 :/
With the input of {1, 2, 3, 1, 2, 3, 1, 2, 3, 0}, my program produces
player 1 winner
player 2 winner
player 3 winner
What input data are you using for which the program produces incorrect output?