for and arrays

K

Keith Thompson

Bill Cunningham said:
Keith I don't think my headers are lying. I'm running old ver 6 of OE.
slrn didn't work out for me. All the newsreaders I've tried as late I
haven't been able to lock onto my news server with.
I don't use trn at all.

And the presence of "@trnddc06" in a Message-ID really doesn't imply
the use of trn at all.
 
R

Richard

Bill Cunningham said:
Mark L Pappin said:
The line Keith quotes contains two string literals, "%i" and " ",
separated by nothing but whitespace ("%i"" " or "%i" " " would be
equivalent). The compiler has (as it is required to) merged them into
a single string literal "%i " and it is this which printf() was
passed.

[snip]

Mark,

I was thinking that the conversion specifiers and string literals are always
separated by a comma,

printf("%i"," ");

Was what I was think and , was printed. That further confused me. My text is
an O'reilly pocket referecne simply called "C" and as O'reilly books are
there is an animal on the front promenantly a cow. Above is the code I kept
trying. The comma between the 2 string literals.

Bill

The sherry is supposed to go in the trifle Bill, not down your gullet.

But 10 out of 10 for a wonderful stage act - it's bordering on the
beautiful at this stage. Listening to Santosh apologising to you had me
spilling my tea over some notes on my desk. You even managed to goad "Mr
Topic" Keith into discussing msgids and usenet posting headers!

LOL.

Classic stuff.
 
M

Mark L Pappin

Bill Cunningham said:
I was thinking that the conversion specifiers and string literals
are always separated by a comma,
printf("%i"," ");

Was what I was think

Format strings (which in turn contain conversion specifiers) usually
_are_ string literals.

Arguments to printf() beyond the first _must_ match their
corresponding conversion specifiers.

The specifier %i requires an int.
The specifier %s requires a string. (OK, fellow pedants - let's just
keep it simple for Bill at this time.)
and , was printed. That further confused me.

When you don't get what you expect, this should cause a light to come
on saying "Maybe I misunderstood, and need to figure out where I went
wrong". It shouldn't lead you to try removing a comma from the source
just because what you saw in the output included a comma.

At your present state of knowledge and apparent rate of acquisition,
DON'T just randomly try stuff. Post the complete program here along
with a precise description of what you expected and what you got,
along with any thoughts you might have about why.

mlp
 
B

Bill Cunningham

Mark L Pappin said:
Format strings (which in turn contain conversion specifiers) usually
_are_ string literals.

Arguments to printf() beyond the first _must_ match their
corresponding conversion specifiers.

The specifier %i requires an int.
The specifier %s requires a string. (OK, fellow pedants - let's just
keep it simple for Bill at this time.)


When you don't get what you expect, this should cause a light to come
on saying "Maybe I misunderstood, and need to figure out where I went
wrong". It shouldn't lead you to try removing a comma from the source
just because what you saw in the output included a comma.

At your present state of knowledge and apparent rate of acquisition,
DON'T just randomly try stuff. Post the complete program here along
with a precise description of what you expected and what you got,
along with any thoughts you might have about why.

mlp

#include <stdio.h>

int
main (void)
{
int i;

for (i = 0; i < 7; i++)
{

printf ("%i\n", i);
}
}

I indented this program using indent. It looks more readable to others
than my usual indentaion. All this does is print vertically the number 0-7.
I have no idea how to even start working with the line numbers to obtain
exponenials.

Bill
 
M

Mark L Pappin

Bill Cunningham said:
#include <stdio.h>

int
main (void)
{
int i;

for (i = 0; i < 7; i++)
{

printf ("%i\n", i);
}
}
I indented this program using indent. It looks more readable to
others than my usual indentaion.

It'll do. Whatever options you used to run indent, keep using them.
All this does is print vertically the number 0-7. I have no idea
how to even start working with the line numbers to obtain
exponenials.

OK, that's fine. Let's recap what you know:

0. A C program consists of a sequence of statements which are executed
one after the other.
A;
B;
C;
will run first A, then B, then C. Blocks of such sequences, wrapped
in {}, can usually each be considered equivalent to a single
statement.

1. You can print the value of an int variable named i with
printf("%i", i);

2. You can print a space with
printf(" ");

3. You can print a newline with
printf("\n");

4. You can do some sequence of things once for each value of i between
0 and 7 inclusive with
for (i = 0; i < 7; i++) {
E;
F;
G;
}


Now, the new bits:
5. You can calculate a new value by writing an arithmetic expression
involving variables, so for example to get the value of i plus 3,
i + 3
or, to get the value of i multiplied by i
i * i

6. You don't have to store such a value anywhere explicitly - you can
use it almost anywhere you could use the name of a variable, and then
instead of the value of the variable being used you'll use the new
calculated value. For example, you can print the value of an i plus 3
with
printf("%i", i+3);
(compare this with (1) above). You _can_ store a calculated value if
you want but we'll worry about that later.


So: since the variable i holds the line number that the question asks
for, how could you calculate the square of the line number? How could
you print that new value? What about the cube?

If you think you can answer the 3 questions above, try modifying the
program you posted here to complete the answer to q4a.

mlp
 
K

Keith Thompson

Bill Cunningham said:
#include <stdio.h>

int
main (void)
{
int i;

for (i = 0; i < 7; i++)
{

printf ("%i\n", i);
}
}

I indented this program using indent. It looks more readable to others
than my usual indentaion.

All this does is print vertically the number 0-7.
I have no idea how to even start working with the line numbers to obtain
exponenials.

The exercise Mark gave you, as I recall, said to print the square of
the number. That's just the number multiplied by itself. For
example, the square of i is i*i.

I don't remember exactly what the exercise was; this thread has gotten
so long I can't easily find it ("easily" meaning within the time and
effort I'm willing to expend right now).
 
B

Bill Cunningham

Well here's my latest attempt. I just get the line numbers.

#include <stdio.h>

int
main (void)
{
int i;

for (i = 0; i < 7; i++)
{

printf ("%i\n", i, " " , "%i\n", i*i, "%i\n" , " " , "%i\n" , i*i*i);
}

}

Bill
 
M

Mark L Pappin

Bill Cunningham said:
Well here's my latest attempt. I just get the line numbers.
printf ("%i\n", i, " " , "%i\n", i*i, "%i\n" , " " , "%i\n" , i*i*i);

Please explain in detail the logic you used to construct this argument
list. If you did not use logic to construct it, go away now.


printf() takes a single format string followed by an optional list of
things to print as specified in that format string.

You've supplied a format string of
"%i\n"
(which says "print an int and a newline") and a list of things, the
first of which is an int. Thus, printf() does exactly as you've asked
and prints that int and a newline and nothing more.


Look at the problem specification again.
On each line you need to print a number of things:
- an int, which is the line number
- a space
- an int, which is the square of the line number
- a space
- an int, which is the cube of the line number
- a newline

This means that you need a format string (the first argument to
printf()) that specifies 3 ints, 2 spaces, and a newline; and the
other arguments need to provide values for the the %-specified
conversions.

So: what format string would tell printf() to print
- an int
- a space
- an int
- a space
- an int
- a newline
?

Don't guess - use the reference material you have available to figure
it out.

mlp
 
R

Richard

Mark L Pappin said:
It'll do. Whatever options you used to run indent, keep using them.

Better don't. Do it by hand. Understand how to indent your own
code. programming, like building a wall, requires skill and pride in
ones work. Recommending "indent" is like recommending an automatic code
generator IMO. If Bill isnt a troll and he just types in any old rubbish
and then expects tools to make it readable only when he posts here then
he has no pride in his work and, frankly, no chance of ever cutting the
mustard.
OK, that's fine. Let's recap what you know:

0. A C program consists of a sequence of statements which are executed
one after the other.
A;
B;
C;
will run first A, then B, then C. Blocks of such sequences, wrapped
in {}, can usually each be considered equivalent to a single
statement.

This is very naive and also wrong. it can not be considered anything of
the kind. It is a scope. In that block anything could or could not be
executed.

1. You can print the value of an int variable named i with
printf("%i", i);

2. You can print a space with
printf(" ");

3. You can print a newline with
printf("\n");

4. You can do some sequence of things once for each value of i between
0 and 7 inclusive with
for (i = 0; i < 7; i++) {
E;
F;
G;
}

You could also have conditional if/else flows.
Now, the new bits:
5. You can calculate a new value by writing an arithmetic expression
involving variables, so for example to get the value of i plus 3,
i + 3
or, to get the value of i multiplied by i
i * i

6. You don't have to store such a value anywhere explicitly - you can
use it almost anywhere you could use the name of a variable, and then
instead of the value of the variable being used you'll use the new
calculated value. For example, you can print the value of an i plus 3
with
printf("%i", i+3);
(compare this with (1) above). You _can_ store a calculated value if
you want but we'll worry about that later.


So: since the variable i holds the line number that the question asks
for, how could you calculate the square of the line number? How could
you print that new value? What about the cube?

If you think you can answer the 3 questions above, try modifying the
program you posted here to complete the answer to q4a.

mlp

--
 
S

santosh

Bill said:
Well here's my latest attempt. I just get the line numbers.

#include <stdio.h>

int
main (void)
{
int i;

for (i = 0; i < 7; i++)
{

printf ("%i\n", i, " " , "%i\n", i*i, "%i\n" , " " , "%i\n" ,
i*i*i);
}

}

You really are priceless Bill! Replace that printf statement of yours
with the sequence I give below:

printf("%i\t", i);
printf("%i\t", i*i);
printf("%i\n", i*i*i);

Can you understand this? Until you understand what the three statements
above do, do *not* proceed further.
 
C

Chris M. Thomasson

Bill Cunningham said:
Well here's my latest attempt. I just get the line numbers.

#include <stdio.h>

int
main (void)
{
int i;

for (i = 0; i < 7; i++)
{

printf ("%i\n", i, " " , "%i\n", i*i, "%i\n" , " " , "%i\n" , i*i*i);
}

}

You are a C expert.
 
K

Keith Thompson

Richard said:
Better don't. Do it by hand. Understand how to indent your own
code. programming, like building a wall, requires skill and pride in
ones work. Recommending "indent" is like recommending an automatic code
generator IMO. If Bill isnt a troll and he just types in any old rubbish
and then expects tools to make it readable only when he posts here then
he has no pride in his work and, frankly, no chance of ever cutting the
mustard.

I disagree. If he gets used to the way his code looks after it's been
run through indent, it's likely (well, possible anyway) that he'll
start writing it that way in the first place. In the meantime, it
gives him one less thing to worry about, which in his case would seem
to be a good idea.

[...]
This is very naive and also wrong. it can not be considered anything of
the kind. It is a scope. In that block anything could or could not be
executed.

No, it's quite correct. A block is a statement. See C99 6.8.2.

[...]
You could also have conditional if/else flows.

Yeah, and while loops, and do-while loops, and gotos, and switch
statements, and longjmp(), and ....

Do you really think that piling on more details is helpful?

[...]
 
B

Bartc

Bill Cunningham said:
Well here's my latest attempt. I just get the line numbers.
for (i = 0; i < 7; i++)
{

printf ("%i\n", i, " " , "%i\n", i*i, "%i\n" , " " , "%i\n" , i*i*i);
}

}

If you have yet to learn Programming, then C probably isn't the best choice
to start with. Try BASIC instead.

It will do everything you appear to want to do in C, eg:

10 FOR I=0 TO 6
20 PRINT I, I*I, I*I*I
30 NEXT I

But if battling with C is just a kind of therapy, then carry on.
 
S

Serve Lau

Keith Thompson said:
I disagree. If he gets used to the way his code looks after it's been
run through indent, it's likely (well, possible anyway) that he'll
start writing it that way in the first place. In the meantime, it
gives him one less thing to worry about, which in his case would seem
to be a good idea.

It also helps that his questions dont get sidetracked in endless indentation
debates.

Its mad fun seeing a doped up guy learning C!
 
B

Bill Cunningham

santosh said:
You really are priceless Bill! Replace that printf statement of yours
with the sequence I give below:

printf("%i\t", i);
printf("%i\t", i*i);
printf("%i\n", i*i*i);

Can you understand this? Until you understand what the three statements
above do, do *not* proceed further.

Well yeah I understand what that does above but the way it was put it
have to be on the same line.

Bill
 
B

Bill Cunningham

printf ("%i\n", i, " " , "%i\n", i*i, "%i\n" , " " , "%i\n" , i*i*i);
^ ^ ^ ^ ^ ^ ^
^
Caret 1. points to format specifier
Caret 2. space
Caret 3. format specifier
Caret 4. square
Caret 5. format specifier
Caret 6. space
Caret 7. format specifier
Caret 8. i cubed.

All this I didn't realize to not be on the same space. You said 3 lines so
the \n character 3 times.

Please remember Mark that... Well I don't know if you know or not but
you should so I will speak. My mind is half gone. In that I mean
concentration is limited. I'm on about 6 psychotropics legally for mentall
illness.

I also thought you meant 3 lines printed so that's why the 3 \n
specifiers.

Bill
 
B

Bill Cunningham

Mark,

I appreciate everyone who cares' involvement in this but giving me the
answers like Santosh did below might not be best. If we would've talked I
probably would've figured it out but now Santosh gave me the answer. Do you
agree that others' input should be hints and not answers ?

Bill
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top