0

I want to create an array of 10 × 10, which contains '.' for each element. So I write as :

int A[10][10]={ '.','.','.','.',

(wait a minute I have to write 100 full stops and 100 commas)

               '.','.','.'}

Another method is to write '.', 10 times and then copy paste it 10 times, but still this takes time and I don't think this is the smartest method.

Is there a smarter method? I don't want to write so long full stops.

sehe
  • 374,641
  • 47
  • 450
  • 633
The Artist
  • 213
  • 1
  • 2
  • 7
  • 3
    You might want to read up on loops. – axiom Jun 10 '15 at 12:40
  • 1
    @unwind The "c++ lounge" is [Lounge](http://chat.stackoverflow.com/rooms/10/loungec) (_in response to now-deleted [comment thread](http://stackoverflow.com/questions/30756630/how-do-i-assign-cumbersome-repetitive-values-to-an-array/30756668#comment49566536_30756668)_) – sehe Jun 10 '15 at 13:08

3 Answers3

9

The only feasible way to initialize an array like this, is (unfortunately) by using a flood of macros:

#define ONE_DOT '.',
#define TWO_DOTS ONE_DOT ONE_DOT
#define FIVE_DOTS TWO_DOTS TWO_DOTS ONE_DOT
#define TEN_DOTS { FIVE_DOTS FIVE_DOTS },
#define TWENTY_DOTS TEN_DOTS TEN_DOTS
#define FIFTY_DOTS TWENTY_DOTS TWENTY_DOTS TEN_DOTS
#define ONE_HUNDRED_DOTS FIFTY_DOTS FIFTY_DOTS


int A[10][10] = { ONE_HUNDRED_DOTS };
Lundin
  • 195,001
  • 40
  • 254
  • 396
1

Use a for loop statement to assign all elements to '.' ----

char A[10][10];
int i,j;
for(i=0;i<10;i++)
  for(j=0;j<10;j++)
     A[i][j]='.';
Sourav Kanta
  • 2,727
  • 1
  • 18
  • 29
1

Loops are the thing You want to read about now.

The simple for loop will do that for you.

char A[10][10];
int i,j;
for ( i=0;i <10;++i){
     for ( j=0;j <10;++j)
            A[i][j]='.';
 }

For further justification google loops in c/c++

Sagar Kar
  • 177
  • 1
  • 2
  • 10