473,324 Members | 2,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Need help with C Language

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

Aug 1 '06 #1
9 1593
vi************@gmail.com 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/>
Aug 1 '06 #2
vi************@gmail.com 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)
Aug 1 '06 #3
In article <11*********************@b28g2000cwb.googlegroups. com>,
<vi************@gmail.comwrote:
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
Aug 1 '06 #4
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:
vi************@gmail.com 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)
Aug 1 '06 #5
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)
Aug 1 '06 #6
Ronald Bruck <br***@math.usc.eduwrites:
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;}
Aug 1 '06 #7
On 2006-08-01, vi************@gmail.com <vi************@gmail.comwrote:
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
Aug 1 '06 #8
On 2006-08-01, vi************@gmail.com <vi************@gmail.comwrote:
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
Aug 1 '06 #9
On 2006-08-01, Andrew Poelstra <fa**********@wp.netwrote:
On 2006-08-01, vi************@gmail.com <vi************@gmail.comwrote:
>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
Aug 1 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: has | last post by:
<BLUSH> Careless talk costs lives, as they say. In my case, a throwaway comment that Python could trounce the notoriously underpowered and undersupported AppleScript language for "serious number...
5
by: mr.iali | last post by:
Hi Everyone I would like to get into software developent using a programming language like c++, java or pl/sql for oracle. I have no idea where to start from. Which language is there more...
39
by: Steven T. Hatton | last post by:
I came across this while looking for information on C++ and CORBA: http://www.zeroc.com/ice.html. It got me to wondering why I need two different languages in order to write distributed computing...
22
by: Rafia Tapia | last post by:
Hi all This is what I have in mind and I will appreciate any suggestions. I am trying to create a xml help system for my application. The schema of the xml file will be <helpsystem> <help...
2
by: sachin | last post by:
Hello Everybody I need some help regarding Natural Language Processing. I am designing a MT system from a SOV language to a SOV language. I need a parser which can find the root word...
34
by: Mark Kamoski | last post by:
Hi-- Please help. I need a code sample for bubble sort. Thank you. --Mark
40
by: apprentice | last post by:
Hello, I'm writing an class library that I imagine people from different countries might be interested in using, so I'm considering what needs to be provided to support foreign languages,...
22
by: the_grove_man | last post by:
I purchased a book titled "Pro ASP.NET 2.0" to get up to speed on web stuff because I ususally do Windows Form Applications.. But in the first chapters I was reading this week it brought to mind...
10
by: brooksr | last post by:
I know VB5/VBA very well but have not used VB to create web pages but need to do so. Can someone explain the purposes and differences between VBScript and VB.NET to create web pages? Also...
7
TRScheel
by: TRScheel | last post by:
I have seen this question appear many times in the forums, so I am writing this to hopefully help those who have this question. First, a history lesson! Your earlier languages compiled to assembly...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.