Thread: 1d to 2d array

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    26

    1d to 2d array

    Hi
    I'm doing a wheel of fortune program in which the player scores are accumulated and tabulated in a 1d array player_turns[]={0,0,0}. The game has three rounds, and I don't know how to print the final score and declare a winner. I figure that it must have to do with converting the 1d array to a 2d array such as results[round_num][player_num]. Could someone please help or guide me how to do this. Thanks.

    Code:
    int main(int argc, char* argv[]){
    	char phrase[NAME_LENGTH];
    	char puzzle[NAME_LENGTH];
    	char clue[NAME_LENGTH];
    	int puzzle_length;
    	int round;
    	
    	char num_players[5];
    	int solved = 0;
    	int current_player;
    	int player_scores[NUM_PLAYERS] = {0, 0, 0};
    	int a=0;
    	
    
     	srand(time(0));
    	current_player=rand()%3+1;
    	printf("\n");
    	printf("Over 50,000 in cash and prizes just waiting to be won!\n");
    	printf(" Here on Wheel ... of ... Fortune!\n");
    	printf(" (loud drum roll and music...)\n");
    	printf("\n");
     
    	/* Get the number of players */
    	while (a< 1 || a > 3){
    		printf("\nHow many players will be playing the game (1-3)? ");
    		if(fgets(num_players,5,stdin)){
    			sscanf(num_players, "%d", &a);
    			}
    		printf("\n");
    		} 
    
    	/* Get the puzzle */
    	
    	for(round=0; round<3; ++round){
    	puzzle_length = initialize_array(phrase, puzzle, clue);
    	
    	current_player = rand()% a;
     
    	while (solved == 0) {
    		print_score(a, player_scores);
    		solved = player_turn(current_player, player_scores, phrase, puzzle, clue, puzzle_length);
    				if (solved == 0){
    					++current_player;
    				if (current_player == a){
    				current_player = 0;
    				}
    			}
    		}
    			solved=0;
    			current_player = rand()% a;			
    	}
    		return(0);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why would you need a two-dimensional array? You might want two arrays -- one for the scores for the current round, and one for the "total" scores that gets added to when someone solves a puzzle.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yeah 2-d arrays are my personal pet peeve, but they sometimes (and I mean very rarely) serve their purpose. In any event, a struct sounds better suited for your needs this time 'round.

    Example:
    Code:
    struct score
    {
       int player
       int points;
    };
    I was about to make it a linked list, but since we are talking about something with fixed rounds you'd just make an array of struct score objects.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    26
    So would I just need to declare another array similar to player_turn have it set to 0,0,0 and have player_turn added to it after each round. I really have no clue where to start. Thanks for taking the time to read this.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's pretty much how the show works (of course, except that only the winner keeps the money), so yes.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    26
    Would I then have to somehow break up that array to find out who was the winner?

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well... if anything, perhaps you could just ditch the struct, and only keep track of the players. Afterall, Wheel of Fortune is cumulative as far as winning goes. So whoever has the highest at the end of the game wins. That or stick with the structs and just add each round together. But yeah, on second thought, all you need is one array.

    int points[NUMBER_OF_PLAYERS]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiplying a 2D array by a 1D array
    By youngvito in forum C Programming
    Replies: 14
    Last Post: 06-12-2009, 03:50 PM
  2. malloc 1d string array
    By s_siouris in forum C Programming
    Replies: 3
    Last Post: 07-07-2008, 09:20 AM
  3. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  4. 1D and 2D Arrays
    By Rajin in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2005, 06:23 PM
  5. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM