About double dimensional array initialisation

M

mukesh tiwari

Hi everybody i want to know about array initialisation .
Today i was solving a problem and here is my code

#include<cstdio>
#include<cstring>
int l;
int div(char *a,int d)
{
int k;
char c[120];
k=a[0];
c[0]=a[0]/d;
for(int i=1;i<l;i++)
{
k=10*(k-d*c[i-1])+a;
c=k/d;
}
return(k%d);
}


int main()
{

char m[120],n[100];
int k,j,p;//value[10][10];
int value[10][10]={{0},{0},{6,2,4,8},{1,3,9,7},{6,4},{0},{0},
{1,7,9,3},{6,8,4,2},{1,9}};

while(scanf("%s%s",m,n) && (m[0]!='0' || n[0]!='0') )
{
l=strlen(m);
p=m[l-1]-'0';//last digit
l=strlen(n);
for(j=0;j<l;j++)
n[j]=n[j]-'0';
if(n[0]==0)
printf("1\n");
else if(p==0 || p==1 || p==5 || p==6)
printf("%d\n",p);
else if(p==2 || p==3 || p==7 || p==8)
{
k=div(n,4);
printf("%d\n",value[p][k]);
}
else
{
k=div(n,2);
printf("%d\n",value[p][k]);
}
}
}

now my problem is
int k,j,p,value[10][10];
value[10][10]={{0},{0},{6,2,4,8},{1,3,9,7},{6,4},{0},{0},{1,7,9,3},
{6,8,4,2},{1,9}};
was giving me compiler (gcc) error and when i initialise with
declaration it worked fine as in my program . Plz some one tell me
why is it so .
 
E

Eric Sosman

mukesh tiwari wrote On 06/18/07 15:42,:
Hi everybody i want to know about array initialisation .
Today i was solving a problem and here is my code

#include<cstdio>
#include<cstring>

You may find better answers in comp.lang.c++, second
door on the left where that noisy party is in progress
(the party-goers aren't just loaded, they're overloaded).

But *if* you'd like to write C instead, the question
boils down to
now my problem is
int k,j,p,value[10][10];
value[10][10]={{0},{0},{6,2,4,8},{1,3,9,7},{6,4},{0},{0},{1,7,9,3},
{6,8,4,2},{1,9}};
was giving me compiler (gcc) error and when i initialise with
declaration it worked fine as in my program . Plz some one tell me
why is it so .

... and this turns out to be Question 8.3 in the
comp.lang.c Frequently Asked Questions (FAQ) list at

http://www.c-faq.com/
 
K

Keith Thompson

mukesh tiwari said:
Hi everybody i want to know about array initialisation .
Today i was solving a problem and here is my code

#include<cstdio>
#include<cstring>

Those are C++ headers. Either write C or post to comp.lang.c++.

But as it turns out, the rest of your program appears to be valid C
(apart from the error you're asking about).

Incidentally, the way you format your code makes it very difficult to
read. Use consistent indentation. I use 4 columns for each level;
some poeple use up to 8. Avoid tab characters for code posted to
Usenet; try to use just spaces for indentation. (I suspect your own
tab settings are different from mine. I see your code with 8-column
tabstops, which is normal.

[snip]
now my problem is
int k,j,p,value[10][10];
value[10][10]={{0},{0},{6,2,4,8},{1,3,9,7},{6,4},{0},{0},{1,7,9,3},
{6,8,4,2},{1,9}};
was giving me compiler (gcc) error and when i initialise with
declaration it worked fine as in my program . Plz some one tell me
why is it so .

Your first line declares the objects k, j, p, and value; it doesn't
initialize them.

Your second line is not an initialization, it's an assignment. In an
assignment, the right hand side has to be an expression; an
initializer can be either an exression or a more complex construct
such as the one yoy're trying to use here.

You need to make the initialization part of the declaration of value:

int k, j, p;
int value[10][10] =
{ {0},
{0},
{6, 2, 4, 8},
{1, 3, 9, 7},
{6, 4},
{0},
{0},
{1, 7, 9, 3},
{6, 8, 4, 2},
{1, 9} };
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top