Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Need help with C Language

Reply
Thread Tools

Need help with C Language

 
 
vinod.bhavnani@gmail.com
Guest
Posts: n/a
 
      08-01-2006
Hello all,

I have 2 qs about statements and their meanings in the C language.

first If i have an array named

int a[256]

and if i use a staement like

for(i=0;i<256;i++)
If(a[i])
{
}
how will this loop work? when will the IF loop be true and when will it
be false

the next qs is have is

:
if i have a statement array[j++]=3*bar[i];
what exactly is happ to j
does each time the loop increments and a new row of j is beaing
assigned a new value or what is happ


Thanks,
Vinod

 
Reply With Quote
 
 
 
 
Clever Monkey
Guest
Posts: n/a
 
      08-01-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> first If i have an array named
>
> int a[256]
>
> and if i use a staement like
>
> for(i=0;i<256;i++)
> If(a[i])
> {
> }
> how will this loop work? when will the IF loop be true and when will it
> be false
>

First, the nit-picking: This snippet will not compile, probably. There
is no standard keyword "If" in standard C.

The answer is whether or not the a[i] expression resolves to nonzero or
not. What is the value contained in a[i]? Only you know at this point.

> if i have a statement array[j++]=3*bar[i];
> what exactly is happ to j
> does each time the loop increments and a new row of j is beaing
> assigned a new value or what is happ
>

Post-increment. Check the c.l.c. FAQ <http://c-faq.com/>
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      08-01-2006
(E-Mail Removed) said:

> Hello all,
>
> I have 2 qs about statements and their meanings in the C language.
>
> first If i have an array named
>
> int a[256]
>
> and if i use a staement like
>
> for(i=0;i<256;i++)
> If(a[i])


C is a case-sensitive language. The 'if' keyword has a lower case i.

> {
> }
> how will this loop work? when will the IF loop be true and when will it
> be false


It depends on the value of a[i]. Since you didn't assign any values, the
value is indeterminate. But let's say you do something like this:

for(i = 0; i < 256; i++)
{
a[i] = i % 2;
}

and then this:

for(i = 0; i < 256; i++)
{
if(a[i])
{
printf("a[i] is true for %d\n", i);
}
}

then it will print:

a[i] is true for 1
a[i] is true for 3
a[i] is true for 5
...
all the way down to
...
a[i] is true for 253
a[i] is true for 255

> the next qs is have is
>
> :
> if i have a statement array[j++]=3*bar[i];
> what exactly is happ to j
> does each time the loop increments and a new row of j is beaing
> assigned a new value or what is happ


In the example you give,

array[j++] = 3 * bar[i];

can be re-written as:

array[j] = 3 * bar[i];
j = j + 1;

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
Ronald Bruck
Guest
Posts: n/a
 
      08-01-2006
In article <(E-Mail Removed). com>,
<(E-Mail Removed)> wrote:

> Hello all,
>
> I have 2 qs about statements and their meanings in the C language.
>
> first If i have an array named
>
> int a[256]
>
> and if i use a staement like
>
> for(i=0;i<256;i++)
> If(a[i])
> {
> }
> how will this loop work? when will the IF loop be true and when will it
> be false
>
> the next qs is have is
>
> :
> if i have a statement array[j++]=3*bar[i];
> what exactly is happ to j
> does each time the loop increments and a new row of j is beaing
> assigned a new value or what is happ


I am often amazed at reading questions like this. I remember when I
was learning C (and Fortran, and Mathematica, and...) When I had a
question like this, I'd program up a little example, change the
parameters, and see what I got. It may not be what the manual strictly
says it should be, but that's an even MORE informative fact about the
compiler (or other program). You learn best by figuring it out
yourself.

(I particularly remember puzzling through the copy protection in
Visicalc--for my own edification and admiration, not for nefarious
purposes--before realizing it was exploiting a BUG in the 6502
instruction set! What the code loop DID was different from what the
6502 designers promised it WOULD do.)

Has the habit of experimentation died? Has the Internet made us all
lazy?

In this case, of course, he has to put something IN his if... statement
to distinguish what happens. (And I suggest he NOT make it an If
statement, as posted. But he'd probably figure that out real quick.)
As for the j++ question, ... Oh, I give up. Hint: why is j++ called
POST increment? Second hint: it's not a USENET post.

--
Ron Bruck

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
 
Reply With Quote
 
vinod.bhavnani@gmail.com
Guest
Posts: n/a
 
      08-01-2006
hey ,

Thanks for all yr help .The actual piece ofcode is below:_ I was
wondering if someone could explain whats going on in the code below

int do_mval=0;

if( mask_dset == NULL ){
mmm = (byte *) malloc( sizeof(byte) * nvox ) ;
if( mmm == NULL )
return " \n*** Can't malloc workspace! ***\n" ;
memset( mmm , 1, nvox ) ; mcount = nvox ;
} else {

mmm = THD_makemask( mask_dset , miv , mask_bot , mask_top ) ;
if( mmm == NULL )
return " \n*** Can't make mask for some reason! ***\n" ;
mcount = THD_countmask( nvox , mmm ) ;


/*-- allocate an array to histogrammatize --*/

flim = mri_new( mcount , 1 , MRI_float ) ;
flar = MRI_FLOAT_PTR(flim) ;

/*-- load values into this array --*/

switch( DSET_BRICK_TYPE(input_dset,iv) ){
default:
free(mmm) ; mri_free(flim) ;
return "*** Can't use source dataset -- illegal data type!
***" ;

case MRI_short:{
short * bar = (short *) DSET_ARRAY(input_dset,iv) ;
float mfac = DSET_BRICK_FACTOR(input_dset,iv) ;
if( mfac == 0.0 ) mfac = 1.0 ;
if( do_mval ){
float val ;
for( ii=jj=0 ; ii < nvox ; ii++ ){
if( mmm[ii] ){
val = mfac*bar[ii] ;
if( val >= val_bot && val <= val_top ) flar[jj++] =
val ;
}
}
mval = jj ;
} else {
for( ii=jj=0 ; ii < nvox ; ii++ )
if( mmm[ii] ) flar[jj++] = mfac*bar[ii] ;
}
}


I understand most part of this code..Now the problem is

What exactly is array mmm being assigned to if the maskdatset is not
being assigned to it.
is mmm being assigned to an array of all 1's

if so
could someone explain what exactly is happ in the switch case
statement?

like in case there is no maskdset and mmm array has been set to all 1's
then what exactly does this piece of code do:
I mean is it necessary to put if(mmm[ii] loop before assignment
or is the if statement redudndant?


for( ii=jj=0 ; ii < nvox ; ii++ )
if( mmm[ii] ) flar[jj++] = mfac*bar[ii] ;

Richard Heathfield wrote:
> (E-Mail Removed) said:
>
> > Hello all,
> >
> > I have 2 qs about statements and their meanings in the C language.
> >
> > first If i have an array named
> >
> > int a[256]
> >
> > and if i use a staement like
> >
> > for(i=0;i<256;i++)
> > If(a[i])

>
> C is a case-sensitive language. The 'if' keyword has a lower case i.
>
> > {
> > }
> > how will this loop work? when will the IF loop be true and when will it
> > be false

>
> It depends on the value of a[i]. Since you didn't assign any values, the
> value is indeterminate. But let's say you do something like this:
>
> for(i = 0; i < 256; i++)
> {
> a[i] = i % 2;
> }
>
> and then this:
>
> for(i = 0; i < 256; i++)
> {
> if(a[i])
> {
> printf("a[i] is true for %d\n", i);
> }
> }
>
> then it will print:
>
> a[i] is true for 1
> a[i] is true for 3
> a[i] is true for 5
> ...
> all the way down to
> ...
> a[i] is true for 253
> a[i] is true for 255
>
> > the next qs is have is
> >
> > :
> > if i have a statement array[j++]=3*bar[i];
> > what exactly is happ to j
> > does each time the loop increments and a new row of j is beaing
> > assigned a new value or what is happ

>
> In the example you give,
>
> array[j++] = 3 * bar[i];
>
> can be re-written as:
>
> array[j] = 3 * bar[i];
> j = j + 1;
>
> --
> Richard Heathfield
> "Usenet is a strange place" - dmr 29/7/1999
> http://www.cpax.org.uk
> email: rjh at above domain (but drop the www, obviously)


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      08-01-2006
Ronald Bruck said:

<snip>

> Has the habit of experimentation died?


Do your own sociology homework.

> Has the Internet made us all lazy?


I would answer that, but apathy prevents me.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      08-01-2006
Ronald Bruck <(E-Mail Removed)> writes:

> I am often amazed at reading questions like this. I remember when I
> was learning C (and Fortran, and Mathematica, and...) When I had a
> question like this, I'd program up a little example, change the
> parameters, and see what I got. It may not be what the manual strictly
> says it should be, but that's an even MORE informative fact about the
> compiler (or other program). You learn best by figuring it out
> yourself.


Sometimes experimentation is valuable. Other times, it's just
misleading. What do you learn from an experiment that tests the
behavior of "i = i++"? You learn how that kind of undefined
behavior manifests on your compiler, today.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
 
Reply With Quote
 
Andrew Poelstra
Guest
Posts: n/a
 
      08-01-2006
On 2006-08-01, (E-Mail Removed) <(E-Mail Removed)> wrote:
> Hello all,
>
> I have 2 qs about statements and their meanings in the C language.
>
> first If i have an array named
>
> int a[256]
>


You mean "if I have an array named `a'...", right? Because you can't
have `int', spaces or brackets in a identifier name.

> and if i use a staement like
>
> for(i=0;i<256;i++)
> If(a[i])
> {
> }
> how will this loop work? when will the IF loop be true and when will it
> be false
>


Nope. It won't work.
a) You don't have an "IF loop" in that code. There's no "IF loop" in C.
b) What you do have, a "If" construct, isn't in C either.
c) What you /should/ have, a "if statement", is not part of C.
d) If you /did/ have a "if statement", it may never evaluate to false,
because you haven't initialized the array. Reading an uninitialized
array is Undefined Behavior, my friend.
e) Where did you define i? It isn't a char, is it?

> the next qs is have is


Please say `question'. It takes two seconds.

>:
> if i have a statement array[j++]=3*bar[i];
> what exactly is happ to j
> does each time the loop increments and a new row of j is beaing
> assigned a new value or what is happ
>


What loop? Where are array, j, bar, and i defined? "happ" is not a word.

--
Andrew Poelstra <website down>
To reach my email, use <email also down>
New server ETA: 42
 
Reply With Quote
 
Andrew Poelstra
Guest
Posts: n/a
 
      08-01-2006
On 2006-08-01, (E-Mail Removed) <(E-Mail Removed)> wrote:
> hey ,
>
> Thanks for all yr help .The actual piece ofcode is below:_ I was
> wondering if someone could explain whats going on in the code below
>


You are abusing the letter m. That's what's going on.

> int do_mval=0;
>
> if( mask_dset == NULL ){
> mmm = (byte *) malloc( sizeof(byte) * nvox ) ;

Assume mmm is a pointer, use
mmm = malloc (nvox * sizeof *mmm);

> if( mmm == NULL )
> return " \n*** Can't malloc workspace! ***\n" ;

Why are you returning a string literal? Is your function defined to
return a string literal? This snippit is not doing what you think it
is. http://www.c-faq.com and buy a copy of _The C Programming Language_
by Dennis Ritchie and Brian Kernighan (sp?).

> memset( mmm , 1, nvox ) ; mcount = nvox ;

That 1 would be better as "sizeof *mmm".

> } else {
>
> mmm = THD_makemask( mask_dset , miv , mask_bot , mask_top );

When you call functions you haven't defined, we can't tell you what
happens. We aren't psychic.

> if( mmm == NULL )
> return " \n*** Can't make mask for some reason! ***\n" ;

return sends a value back to the calling function. You know that, right?

<snipped much of the same>
>
> I understand most part of this code..Now the problem is
>


I don't think you do.

> What exactly is array mmm being assigned to if the maskdatset is not
> being assigned to it.
> is mmm being assigned to an array of all 1's
>


I dunno. What do all of your other functions do? What scope is mmm?



Finally, some netiquette:
1) Don't top post!
2) Don't forget to snip signatures.
Thanks for your consideration.

--
Andrew Poelstra <website down>
To reach my email, use <email also down>
New server ETA: 42
 
Reply With Quote
 
Andrew Poelstra
Guest
Posts: n/a
 
      08-01-2006
On 2006-08-01, Andrew Poelstra <(E-Mail Removed)> wrote:
> On 2006-08-01, (E-Mail Removed) <(E-Mail Removed)> wrote:
>> Hello all,
>>
>> I have 2 qs about statements and their meanings in the C language.
>>
>> first If i have an array named
>>
>> int a[256]
>>

>
> You mean "if I have an array named `a'...", right? Because you can't
> have `int', spaces or brackets in a identifier name.
>
>> and if i use a staement like
>>
>> for(i=0;i<256;i++)
>> If(a[i])
>> {
>> }
>> how will this loop work? when will the IF loop be true and when will it
>> be false
>>

>
> Nope. It won't work.
> a) You don't have an "IF loop" in that code. There's no "IF loop" in C.
> b) What you do have, a "If" construct, isn't in C either.
> c) What you /should/ have, a "if statement", is not part of C.

Ack! I meant "is not a loop". Sorry for the confusion.

> d) If you /did/ have a "if statement", it may never evaluate to false,
> because you haven't initialized the array. Reading an uninitialized
> array is Undefined Behavior, my friend.
> e) Where did you define i? It isn't a char, is it?
>
>> the next qs is have is

>
> Please say `question'. It takes two seconds.
>
>>:
>> if i have a statement array[j++]=3*bar[i];
>> what exactly is happ to j
>> does each time the loop increments and a new row of j is beaing
>> assigned a new value or what is happ
>>

>
> What loop? Where are array, j, bar, and i defined? "happ" is not a word.
>



--
Andrew Poelstra <website down>
To reach my email, use <email also down>
New server ETA: 42
 
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
writing a compiler for Monster language using C language Shravani C Programming 8 03-16-2008 09:36 PM
A language-agnostic language Ed Java 24 03-27-2006 08:19 PM
c is a low-level language or neither low level nor high level language pabbu C Programming 8 11-07-2005 03:05 PM
Using a Scripting Language as Your Scripting Language DaveInSidney Python 0 05-09-2005 03:13 AM
Python is the best and most popular general purpose scripting language; the universal scripting language Ron Stephens Python 23 04-12-2004 05:32 PM



Advertisments