Thread: Selecting and marking a value on an array

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    4

    Exclamation Selecting and marking a value on an array

    I'm currently having difficulties on selecting and marking a value that had been on selected on a 2d array.


    Example: Before selecting the value the array should look like this.


    Code:
    # # # # # # # # # # # #
    #  1         2        #
    #               4     #
    #  6     2        8   #
    #                     #
    #       1   3    5    #
    #                     #
    #   3          6      #
    #         4       5   #
    #     7               #
    #          7     8    #
    # # # # # # # # # # # #

    Example:After Selecting the value the array should look like this, assume the user select number 1 as the value.

    Code:
    # # # # # # # # # # # #
    #  [1]   9   2        #
    #               4     #
    #  6     2        8   #
    #                     #
    #       1   3    5    #
    #                     #
    #   3          6      #
    #         4       5   #
    #  9  7               #
    #          7     8    #
    # # # # # # # # # # # #


    My current code:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include <time.h>
    
    
    void print_array(int arr[12][12], int ar[144]){
    
    
    int i,j,m=0;
    
    
    for(i=0;i<38;i++)
    {
        printf("#");
    }
    printf("\n");
     for(i=0;i<12;i++) 
        {
                for(j=0;j<14;j++)
                {
                    if(j==0)
                    {
                        printf("#");
                    }
                    else if(j==13)
                    {
                        printf("#");
                    }
                    else
                    {
                
                        arr[i][j-1] = ar[m++];
                            if(arr[i][j-1] == 0)
                            {
                                printf("   ");
                            }
                            else
                            {
                                printf(" %d " , arr[i][j-1]);
                            }
                    }                    
                 }
            printf("\n");
        }
        for(i=0;i<38;i++)
    {
        printf("#");
    }
    printf("\n");
    }
    
    
    
    
    
    
    void shuffle(int arr[12][12], int ar[144]){
    
    
    int t,j,i;
    
    
         srand ( time(NULL) );
        for(i=144-1;i>0;i--)     
        {
            
             j=rand() % (i+1);
             t=ar[j];
             ar[j] = ar[i];
             ar[i] = t;
        }
    
    
    
    
    
    
    }
    
    
    
    
    //error the whole selectnumber seems to have problem
    void selectnumber(int arr[12][12], int ar[144], int *num, int *x, int *y)
    {
    int i, j, test=0;
    int temp;
    printf("please select a number from 1-8:\n");
    fflush(stdin);
    scanf("%d", &temp);
    
    
    arr[*y][*x]=' ';
    arr[*y+1][*x]=' ';
    
    
    for (j=0; j<=12; j++)
    {
        for (i=0; i<=12; i++)
        {
            if(arr[j][i]==temp && test==0)
            {
            *y = j;
            *x = i;
            test = 1;
            }
        }
    }
    
    
    arr[*y][*x]='-';
    arr[*y+1][*x]='-';
    
    
    *num=temp;
    
    
    }
    
    
    
    
    
    
    int main()
    {
        int array[12][12]={0};
        int randvalues[144]={1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8};
        int gx,gy;
        int ginput;
        
      
    
    
        shuffle(array,randvalues);
        print_array(array,randvalues);
    
    
        selectnumber(array, randvalues, &ginput, &gy, &gx);
        
     
    
    
        return 0;
    }

    Since my array consist two types of array therefore i'm very confused on how to do it, please kindly help me

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I think it's just a matter of finding the temp number they chose in this part in the 2d-array:
    Code:
    printf("please select a number from 1-8:\n");
    fflush(stdin);
    scanf("%d", &temp);
    And displaying it with printf("[%d]", arr[y][x]); or something similar.

    Did you know that fflush(stdin); may not work?
    Question 12.26a

  3. #3
    Registered User
    Join Date
    May 2016
    Posts
    4
    Quote Originally Posted by whiteflags View Post
    I think it's just a matter of finding the temp number they chose in this part in the 2d-array:
    Code:
    printf("please select a number from 1-8:\n");
    fflush(stdin);
    scanf("%d", &temp);
    And displaying it with printf("[%d]", arr[y][x]); or something similar.

    Did you know that fflush(stdin); may not work?
    Question 12.26a
    hmmm correct me if i'm wrong..
    your method seems like getting the user to input the number and display it on an array, right?
    what i'm trying to achieve is, when the user enter a number, the program will find the value which is already in the array.
    since i'm having a function of printing the entire array, i cant print the array in selectnumber function

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The select number needs to find the temp number in the array... so somewhere a comparison like temp == arr[y][x] should be true, and then you print the arr[y][x] in the manner I showed.

  5. #5
    Registered User
    Join Date
    May 2016
    Posts
    4
    i'm currently blank and blur.. have no idea on how to do it =(

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    OK, then simplify first until you get the idea. Pretend that the number is 1. Your task is to call printf("[%d]", arr[y][x]); when you first find 1 in arr. When you can do that, the next step is to make it work for the number called temp.

  7. #7
    Registered User
    Join Date
    May 2016
    Posts
    4
    okay mate, i'll think about it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Marking assistant
    By walay234 in forum C Programming
    Replies: 1
    Last Post: 10-17-2011, 04:00 AM
  2. Selecting pivot in sorted array.
    By infantheartlyje in forum General Discussions
    Replies: 4
    Last Post: 10-14-2011, 09:43 AM
  3. marking a selection
    By megafiddle in forum General Discussions
    Replies: 2
    Last Post: 08-22-2011, 08:36 PM
  4. selecting an array
    By ekarapanos in forum C Programming
    Replies: 6
    Last Post: 07-05-2003, 08:22 AM
  5. A simple question about selecting elements in an array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 10:37 PM

Tags for this Thread