1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help in below program

Discussion in 'C' started by JustDoIt, Jul 21, 2016.

  1. JustDoIt

    JustDoIt New Member

    Joined:
    Jul 21, 2016
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Would very much appreciate help with the following:

    Using C Prog, Dev a prog that will play a number guessing game.The prog should allow users to enter their 3 numbers (between 0-9). You must store teh 3 numbers in a 1D array.

    You prog should use functions. The prog should display a simple menu to the user and each option in teh menu will be implemented by calling a separate function. You must use pointer notation to access array elements-NOT subscripts.

    1. Enter 3 selected numbers
    2. Display the contents of the 1D array containing the numbers you entered.
    3. Compare your chosen lotto numbers in teh 1D array with eh following winning numbers: 1,3, 5

    Return to the main() function how many numbers the use entered correctly and display this.

    4. Reverse the contents of the 1D array containing the numbers the user entered correctly and display this.

    5. Exit program

    After the function has completed, your program should return to the main menu and allow the user to select another option.

    Note:
    -Do not hardcode: use symbolic names where appropriate.
    -Do not use Global variables: use local variables and pass them as parameters to appropriate functions.
    -Kudos to well-written code: comments, indentations, whitespace, brackets
     

    Attached Files:

  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    What help do you need?
    Do you understand the requirements?
    What code have you written so far? Does it work? If not, what exactly is going wrong?
    Do you know that "THE" is not spelt T-E-H?
     
  3. JustDoIt

    JustDoIt New Member

    Joined:
    Jul 21, 2016
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    im a newbie here, thks so much for your reply!
    Here's the code i've done so far: but there seems to be a lot of problems with it.
    Would appreciate corrections to it, esp for pointers and user defined functions
    Code:
    #include <stdio.h>
    
    #define SIZE 3
    
    int main()
    {
    	// Choice entered by user
    	int num = 0;
    	
    	// Array of 3 numbers entered by user
    	int *array[SIZE] = {0};
    	    
    	// Array of winning numbers 1, 3, 5;
    	int *winning[SIZE] = {1, 3, 5};
    	    
    	// Reversed array of numbers user entered
    	int *reversedarray[SIZE] = {0};
    	
    	// Infinite loop
    	while(1) {
    		// Clear the console
    		system("cls");
    		
    		// Print menu
    	    printf("Math Quiz Game Menu:\n");
    		printf("1) Enter 3 selected numbers from keyboard\n");
    		printf("2) Display contents of 1-D array of entered numbers\n");
    		printf("3) Compare numbers with winning numbers (1,3, 5\n");
    		printf("4) Reverse contents of 1-D array entered\n");
    		printf("5) Exit program");
    		printf("Please enter your choice: ");
    	    
    		// Get the user's choice
    		int option = 0;
    		scanf("%d", &option);
    		
    		// Execute instructions
    		switch(option) {
    			// Enter selected numbers from keyboard
    			case 1 : {
    				printf("Enter selected numbers from keyboard (1 - 9): ");
    				scanf("%d", &num);
    				break;
    			}
    			// Display contents of 1D array
    			case 2 : {
    				int i = 0;
    				for(i = 0; i < ; i++) 
    				{
    					printf("%d", *array[i]);
    					scanf("%d", &*array[i]);
    				}
    				break;
    			}
    			// Compare entered numbers with winning numbers
    			case 3 : 
    			{
    				int i = 0;
    				int correct = 0;
    				int incorrect = 0;
    				for(i = 0; i < array; i++) 
    				{
    					if(*array[i] == *winningarray[i]) 
    				    {
    						correct++;
    					} //end if
    					else 
    					{
    						incorrect++;
    					}//end else
    				} //end for 
    				printf("You guessed %d questions correctly and %d questions incorrectly!\n", correct, incorrect);
    				break;
    			} // end case 3 
    			
    			
    			// Reverse contents of 1D array
    			case 4 : 
    			{
    			    int main()
    {
       int i, j, *array[SIZE], *reversedarray[SIZE];
     
       printf("Enter the array elements\n");
     
       for (i = 0; i < SIZE; i++)
          scanf("%d", &array[i]);
     
       /*
        * Copying elements into array b starting from end of array a
        */
     
       for (i = SIZE - 1; j = 0; i >= 0; i--, j++)
          *reversedarray[j] = *array[i];
     
       /*
        * Copying reversed array into original.
        * Here we are modifying original array, this is optional.
        */
     
       for (i = 0; i < SIZE; i++)
          *array[i] = *reversedarray[j];
     
       printf("Reverse array is\n");
     
       for (i = 0; i < SIZE; i++)
          printf("%d\n", *array[i]);
     
    			} //end case 4
    			break;
    			}
    			
    			
    			// Exit program
    			case 5 : {
    				printf("Exiting program...\n");
    				return 0;
    			}
    			// Invalid number
    			default : {
    				printf("Sorry, you have entered an invalid number!\n");
    			}
    		}
    		
    		// Pause before next iteration
    		system("pause");
    	}
    	
        return 0;
    }
     
    Last edited by a moderator: Jul 25, 2016
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    What problems, exactly? Does it compile? If not, what errors do you get?

    If it does build OK and run, presumably it doesn't give you the output you expected. What input did you give it, what output did you expect, and what output did you get?
     

Share This Page