By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,378 Members | 3,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,378 IT Pros & Developers. It's quick & easy.

About double dimensional array initialisation

P: n/a
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[i];
c[i]=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 .

Jun 18 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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/

--
Er*********@sun.com
Jun 18 '07 #2

P: n/a
mukesh tiwari <mu****************@gmail.comwrites:
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} };

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 18 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.