1

I am writing a program in C language which is an India Game called Thambola(similar to Bingo). In this game the user gets a 3x9 ticket and the computer asks the user to enter the number which the second program(this picks numbers randomly from 1-90) picked. If the entered number exists in the ticket, the number should be changed to 'x' meaning the number has been stricken off. I need help here. How to replace an already printed number with 'x'? i read this C - Remove and replace printed items but it doest help because i have 27 numbers to be changed.Please help me. Here is the part of the code:

int number,i,j;
const char x='x';

printf("\nEnter the number:");
scanf("%d",&number); // number entered by the user from the Picker

for (i=0;i<3;i++)
    for (j=0;j<9;j++)
        {if (ticket[i][j]==number)
            ticket[i][j]=x;
printf("%d",ticket[i][j]); //if the input number is present in the ticket, this should change it to 'x

    }

getchar();

}

Community
  • 1
  • 1
user1445848
  • 11
  • 1
  • 5
  • Use ncurses? http://en.wikipedia.org/wiki/Ncurses – Marc B Jun 09 '12 at 06:37
  • 1
    I would write a function that clears the screen and re-displays the entire board. Then call that function every time you need to update something. Its quick and easy. A better but more complicated way would be curses. – jedwards Jun 09 '12 at 06:48
  • How to use clrscr(); in Visual C++ 2010? – user1445848 Jun 09 '12 at 06:52

2 Answers2

0

Main problem of your code snippet is your logic behind assigning values to variables.

As anyone can see, you have an array of integers but you want to assign a char to one of its elements. Actually you can do it by casting, but the result will be the ASCII value of the 'x'.

{if (ticket[i][j]==number) // number is an integer
            ticket[i][j]=x; // x is a char

Since 'x' has the ASCII value of 120,which is out of range from the possible numbers in the ticket, you can safely cast 'x' to its integer value and then assign it to its array element. In printing, if you see 120 print 'x'.

In the situations where your char's integer value is in the range of the other possible integer values, pick another integer value and treat this value as 'x' in logic flow (for example, pick 0 for the corresponding integer, if 0 comes print 'x').

You can use ncurses or simply print a ticket state then clear the screen just before printing modified ticket for output.

Seçkin Savaşçı
  • 3,446
  • 2
  • 23
  • 39
  • can u explain how to use 'x' in place of a integer? i am a newbie :) – user1445848 Jun 11 '12 at 03:00
  • in c, every char is a corresponding integer value defined in ASCII table. You can simply cast it, for example: `int y = (int)your_char;` BTW here is the link for ascii table http://www.asciitable.com/ – Seçkin Savaşçı Jun 11 '12 at 07:46
0
public class sample1 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        List<int[]> outer = new ArrayList<int[]>(9);
        List<int[]> inner = new ArrayList<int[]>(9);
        int[][] final_arr = new int[9][18];
        int[][][] final_arr2 = new int[6][3][9];


        int[][] multi = new int[][]{
            {2,3,5,7,9,12,13,15,17},
            {1,3,4,5,6,7,11,14,16,18},
            {1,2,4,6,8,9,12,13,17,18},
            {3,5,7,9,10,11,12,14,15,16},
            {1,4,6,7,8,10,11,13,14,18},
            {2,3,5,8,9,12,13,15,16,17},
            {1,2,4,6,7,10,11,13,17,18},
            {1,3,5,8,9,12,13,15,16,18},
            {2,4,6,7,9,10,11,14,15,16,17}
        };

        for( int i=0;i<9;i++){
            outer.add(multi[i]);
        }

        for(int x=0;x<9;x++){
            int [] temp = new int[18];
                for(int k=0;k<outer.get(x).length;k++){
                    //System.out.print(outer.get(x)[k]-1 +" ");
                    int row = outer.get(x)[k]-1;
                    temp[row]=1;
                }
                //System.out.println();
            inner.add(temp);
            }
        System.out.println();
        int count=1;
        for(int i=0;i<9;i++){
            List<Integer> temp = new ArrayList<Integer>();
            for(int k=0;k<outer.get(i).length;k++){
                temp.add(count);
                count++;
            }
            Collections.shuffle(temp);
            int index_of_temp=0;
            for(int j=0;j<18;j++){
                if(inner.get(i)[j]==1){
                    inner.get(i)[j]=temp.get(index_of_temp);
                    index_of_temp++;
                }
                //System.out.print(inner.get(i)[j]+ " ");
            }
            //System.out.println();
        }

        for(int i=0;i<9;i++){
            for(int j=0;j<18;j++){
                final_arr[i][j]=inner.get(i)[j];
            }
        }
        System.out.println();

        for(int i=0;i<18;i++){
            for(int j=0;j<9;j++){
              //System.out.print(final_arr[j][i]+ " ");
                final_arr2[i/3][i%3][j]=final_arr[j][i];
            }

        }
        for(int k=0;k<6;k++){
            for(int i=0;i<3;i++){
                for(int j=0;j<9;j++){
                    System.out.print(final_arr2[k][i][j]+ " ");
                }
                System.out.println();
            }
            System.out.println();
        }
    }

}
Pang
  • 9,564
  • 146
  • 81
  • 122
Sunil
  • 1