CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20
  1. #16
    Join Date
    Apr 2013
    Posts
    45

    Re: Problem output of function

    I give these values(the same like you):
    1
    2
    3
    1
    2
    3
    1
    2
    3
    but I get the result player 3 winner

  2. #17
    Join Date
    Apr 2013
    Posts
    45

    Re: Problem output of function

    I gave the values:
    1
    2
    3
    1
    2
    3
    1
    2
    3
    but I just get the result player 3 winner

  3. #18
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Problem output of function

    Quote Originally Posted by mathmari View Post
    I gave the values:
    1
    2
    3
    1
    2
    3
    1
    2
    3
    but I just get the result player 3 winner
    What about the terminating 0? The program will continue reading data until it finds the 0. If there is no terminating 0 specified the program will read data until it finds one somewhere in memory - which will make player 3 always the winner!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #19
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Problem output of function

    If you want to use a fixed size matrix for play - rather than dynamically allocated at run time - then the code below will check for array bound overflow. Note the final terminating 0 in the test data in main.

    There must be someway for find_winner to know when the end of array ans is reached. I've used 0 as the terminating number. Another way of doing this would be to have an extra parameter to find_winner that specifies the number of elements in array ans. However, this could be prone to error. IMO having a terminating 0 is preferable.

    Code:
    #include <stdio.h>
    
    #define NO_PLAYER	3
    
    void find_winner(int ans[]) {
    int	noplay = 0,
    	i = 0,
    	pl = -1,
    	max = 0,
    	j,
    	play[NO_PLAYER] = {0};
    
    	while (ans[i] > 0) {
    		if (ans[i++] == 1)
    			 if (++pl >= NO_PLAYER)
    				 break;
    
    		play[pl]++;
    		if (play[pl] > max)
    			max = play[pl];
    	}
    
      	for (j = 0; j < NO_PLAYER; j++)
    		if (play[j] == max)
    			printf("player %d  winner\n", j + 1);
    }
    
    int main()
    {
    int answers[] = {1, 2, 3, 1, 2, 3, 1, 2, 3, 0};
    
    	find_winner(answers);
    	return 0;
    }
    This code produces the output
    player 1 winner
    player 2 winner
    player 3 winner
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #20
    Join Date
    Apr 2013
    Posts
    45

    Talking Re: Problem output of function

    Thank you veeery much!!!!!!!!!

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured