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.

2
  • 3
    You might want to read up on loops. Commented Jun 10, 2015 at 12:40
  • 1
    @unwind The "c++ lounge" is Lounge<C++> (in response to now-deleted comment thread) Commented Jun 10, 2015 at 13:08

3 Answers 3

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 };
Sign up to request clarification or add additional context in comments.

2 Comments

It almost looks like song lyrics.
@Quentin Feel free to use the code as lyrics for your next mainstream pop hit :)
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]='.';

1 Comment

This isn't initialization, however. For example, what if A would be const?
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++

5 Comments

So, for each i between 0 and -1... well that's off to a good start.
@Quentin i is intiger type, what do u mean by between 0 and -1??
Your for loops go from 0 included to 0 excluded, i.e [0, 0). They won't ever be entered.
Thats was a typo for 10 .... i honestly overlooked it.... Anyway i dont mind downvote to my answer but help me improve! Thanks
This isn't initialization, however. For example, what if A would be const?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.