Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > About double dimensional array initialisation

Reply
Thread Tools

About double dimensional array initialisation

 
 
mukesh tiwari
Guest
Posts: n/a
 
      06-18-2007
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==
{
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 .

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      06-18-2007
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/

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      06-18-2007
mukesh tiwari <(E-Mail Removed)> writes:
> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To convert a one dimensional array into a two dimensional array using C amrutha0303 Software 0 08-03-2010 10:02 PM
Array of Array Initialisation Olaf P. C Programming 2 04-24-2007 02:10 PM
Initialisation of reference vs. initialisation of reference member Tim Clacy C++ 8 05-30-2006 06:14 PM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
How do copy Strings from a single dimensional array to double dimensional array Venkat C++ 4 12-05-2003 09:23 AM



Advertisments