for and arrays

B

Bill Cunningham

Mark,

I appreciate your help but please understand, in dealing with me you will
need patience. Because of my limitations and I do have k&r2.

Bill
 
B

Bill Cunningham

How about this. I don't know if the source is what you wanted, but the
binary does what I think you're looking for.

#include <stdio.h>
int
main (void)
{
int i;
for (i = 0; i < 7; i++)

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

Bill
 
B

Bill Cunningham

That will work, but I suggest you look closely at K&R2, page 9. Look
specifically at the program that occupies the upper half of the page. More
specifically, look at the loop. More specifically still, look at the
printf call inside that loop. What do you notice about the format string?

It's a while loop Richard and the format specifier is a tab. I would
have to go back and read Mark's question but I don't remember anything about
a tab. He said spaces.

Bill
 
B

Bill Cunningham

Bill Cunningham Writes:

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

[snip]
Forget the tab. What *else* do you notice about it? Hint: count the %
signs
and the number of parameters coming after the format string.
There are 2 % one for each variable. But that's not what's confusing me
here. It's the , 's and the number of commas and where they all go. Commas I
see and not in the format specification at all. But there are commas
separating the variables.

Bill
 
S

santosh

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

Then:

printf("i = %i\ti^2 = %i\ti^3 = %i\n", i, i*i, i*i*i);

Can you understand the above?
 
S

santosh

Bill said:
[snip]
Forget the tab. What *else* do you notice about it? Hint: count the %
signs
and the number of parameters coming after the format string.
There are 2 % one for each variable. But that's not what's confusing
me here. It's the , 's and the number of commas and where they all go.
Commas I see and not in the format specification at all. But there are
commas separating the variables.

Each argument in a function call needs to be separated by a comma. The
first argument to printf (and the only mandatory argument) is the
format string itself (i.e., the "blahblah" thing). The subsequent
arguments are needed only if the format string (the first argument)
contains any conversion specifiers (i.e., a % character, example %i
etc.).

So in this printf call:

printf("hello");

there is only one argument, one that is mandatory, the string
literal "hello". No more arguments are needed because this string
literal does not have any conversion specifiers.

In this call:

printf("hello %s", "hello");

there are two arguments separated by the comma, both of them string
literals. There are two arguments because the first argument, the
format string, contains a conversion specifier %s. Whenever you have a
conversion specifier printf will expect a corresponding additional
argument to convert and print (omitting unneccessary complications
here). The %s conversion specifier expects a char* argument that points
to a string. A string literal serves perfectly.

Similarly:

printf("%d %d %d", a, b, c);

Answer these questions concerning the above statement:

1.) How many arguments have we supplied to printf here? How did you
derive this answer?

2.) Identify the first argument. What's it called?

3.) Looking at the first argument alone, can you tell if printf will
expect further arguments? If so, how many? How did you determine this,
explain your reasoning?
 
S

santosh

Bill said:
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 ?

This is a pubic group Bill. Anyone can post anything is response to you.
While we all understand your stated problems (and many even believe
it), still there is bound to occur a post or two that you may find
either unhelpful or confusing.

If you want to converse only with Mark then I suggest that you carry on
your tutorial through private mail.
 
M

Mark L Pappin

Bill Cunningham said:
Also I do have K&R2. FYI

Then start again:

- work through each of the exercises in Chapter 1
- do NOT move on to an exercise until you have successfully completed
the previous one
- when you have completed ALL of the exercises in Chapter 1
successfully, go back to the start of Chapter 1 and work through all
of the exercises AGAIN, without looking at your previous solutions to
those exercises, and again: only move on when you have COMPLETED each

- when you have completed ALL of the exercises in Chapter 1 for the
second time, look at your solutions side by side (kill some trees for
this), and compare them. If you think that your second attempt was
better than your first (remember, both versions have been successfully
completed), go back and work through Chapter 1 AGAIN, and do this
comparison again.

- when you're happy that you can solve all the questions posed in
Chapter 1 without hardship, and that you have tried several different
ways of doing so and have run out of "better" ideas, then move on to
Chapter 2 and follow EXACTLY the same process.

- when you have worked through all of the exercises in K&R2
sequentially and got several fully functioning answers for each one,
put all of those printouts and saved files into a locked filing
cabinet and do the whole thing again (including all of the little
internal loops)

- when you have worked through all of K&R2 twice and then some, you
_will_ be able to program in C (or you'll be able to fake it
convincingly, and honestly that'll do).


While working through these steps, feel free to post your code here
for critique or assistance - if you provide sufficient detail of what
you expect it to do and where its behaviour deviates from that, plus
details of the steps you've taken already to isolate the problem, then
people will be more inclined to help you.


But those people won't be me. I'm tired of dealing with you, Bill,
and I've decided that my sanity is worth more to me than your
amusement.

mlp
 
M

Mark L Pappin

Bill Cunningham said:
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, this is Usenet. I can't control what other people post, and if
your flailing about drives them to hand you the answer on a plate just
to make you shut up, there's nothing I can do about it. I'm trying to
structure the progression of these exercises so that each one builds
on what you've already demonstrated you can regurgitate - please tell
me as soon as possible if I've taken too big a step for you to follow
- for everybody's sake don't post some random crap and expect us to
believe it was a genuine attempt that you thought would solve the
problem. The horribly broken printf() call was almost enough to
convince me that you're a troll who's enjoying stringing me along.

In the last 24 hours you've produced a lot more verbiage without
saying much.

printf ("%i\n", i, " " , "%i\n", i*i, "%i\n" , " " , "%i\n" , i*i*i);
^
Caret 1. points to format specifier

No, it doesn't. There are tab characters involved there somewhere,
because the carets don't line up with anything meaningful in that
line. Make sure that your posts (including code) contain no tab
characters in future. And you managed to screw up the matching
between attributions and quoting depth. ... Ahh, this article got
posted several times.

....
You said 3 lines so the \n character 3 times.

No, I didn't say "3 lines". I said:

4a. Write a program to print 7 lines, each containing the line
number (start at 0), the square of the line number, and the cube of
the line number, separated by spaces.

I said "7 lines". Total.
Each of those 7 lines had to contain 3 things, separated by spaces.
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.

With all due respect Bill: I don't care. Some people and some
occupations are incompatible - I'm never going to be a gymnast, for
example, no matter how much I stamp my feet and say "I wanna".
Programming a computer requires mental activity - if yours is impaired
then you're at a disadvantage before you start. Unless you are truly
outstanding (and I've seen no evidence of that) you will remain
blocked here. Perhaps you need to find another hobby (I trust that
you've not been considering this as a career direction) - take up
building model ships out of toothpicks, or something.


If you believe that you are as extraordinary as you would need to be
to break through the shackles which so far seem to be binding you, see
my other followup, to your "K&R2" posting.


Apart from that, have a nice life. I'm done here.

mlp
 
C

Chris M. Thomasson

Bill Cunningham said:
Bill Cunningham Writes:

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.

What does your doctor say when you tell him that the medication your taking
for mental illness is making you mentally ill?
 
R

Richard Bos

santosh said:
It still seems suspicious to me, but I may be making accusations without
clear evidence. In any case, regardless of the "msg-ID" issue, based on
an overall consideration of his posts to clc, I believe Bill C is a
troll. IMO of course.

Frankly, I don't see that it matters whether he is a troll, or someone
with a learning problem _and_ the accompanying bad attitude that means
he refuses to do something about his learning problem. Either way,
trying to help him is bad for both the newsgroup and Bill himself.

Richard
 
S

soscpd

Hello Bill, List

printf("%i \n",i);
Hum. Now I didn't expect that. I expected the numbers to be printed
horizontally.

Read the follow URL's. I'm sure you can fix the issue after that.

http://en.wikipedia.org/wiki/Printf
http://www.pixelbeat.org/programming/format_specs.html

If, after that, you still can't deal with...

#include <stdio.h>
#include <stdlib.h>

#define MAX_LOOP 7

int main(void)
{
int i;

for(i=0;i<MAX_LOOP;i++)
{
if(i>=MAX_LOOP)
{
printf("%i\n",i);
return EXIT_SUCCESS;
}
printf("%i ",i);
}
return EXIT_FAILURE;
}


Rafael
 
S

soscpd

Hello Bill, List



Read the follow URL's. I'm sure you can fix the issue after that.

http://en.wikipedia.org/wiki/Printfhttp://www.pixelbeat.org/programming/format_specs.html

If, after that, you still can't deal with...

#include <stdio.h>
#include <stdlib.h>

#define MAX_LOOP 7

int main(void)
    {
        int i;

        for(i=0;i<MAX_LOOP;i++)
            {
                if(i>=MAX_LOOP)
                    {
                        printf("%i\n",i);
                        return EXIT_SUCCESS;
                    }
                printf("%i ",i);
            }
        return EXIT_FAILURE;
    }

Rafael

Ok... the > isn't really necessary (but cut him off demand a
replacement with another =), but is harmless here.

#include <stdio.h>
#include <stdlib.h>

#define MAX_LOOP 7

int main(void)
{
int i;

for(i=0;i<MAX_LOOP;i++)
{
if(i==MAX_LOOP)
{
printf("%i\n",i);
return EXIT_SUCCESS;
}
printf("%i ",i);
}
return EXIT_FAILURE;
}

Rafael
 
M

Mabden

Richard Bos said:
Frankly, I don't see that it matters whether he is a troll, or someone
with a learning problem _and_ the accompanying bad attitude that means
he refuses to do something about his learning problem. Either way,
trying to help him is bad for both the newsgroup and Bill himself.

Well, you can say that about a lot of situations. I feel you still need to
try. When I was posting some questions about arcane stuff, people constantly
said I was a troll. I was just trying to pin down my exact question. It took
an author of the K&R and Mr. Keith (the Other one) to finally satisfy my
quest for knowledge. I encourage people to try not to assume you know the
full scope of the question from a couple of first posts that you've seen a
million times; the poster might be trying to gather their ideas for the
FIRST time and not realize what a bunch of pedantic bastards you all are
(and I mean that in the way you think I mean it, but with a brotherly hug at
the end)

I have been very stubborn on issues in this group, over and over again, and
patience and consideration have helped me, And made me a nicer poster (not
that THAT will last ;))

You know what, I shouldn't even be posting this. I don't know even the back
story. Or anything about this Bill guy.

Oops.... I hit send... damn it!

Uuuuhh... where's the unsend button...
 
B

Bill Cunningham

santosh said:
Then:

printf("i = %i\ti^2 = %i\ti^3 = %i\n", i, i*i, i*i*i);

Can you understand the above?

Oh my goodness no. The carets look like exponents. That's about all I
get.

Bill
 
N

Nigel

    Oh my goodness no. The carets look like exponents. That's about all I
get.

Bill

Bill, the first parameter in printf is a string. Every character in it
will be printed unless it is an escape sequence or a format
placeholder (i.e. %i, which is for printing an integer). So, the %i is
not being printed, it will print i (the second argument) as an
integer. As for \t, that is a escape sequence which stands for the TAB
character.
 

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,816
Messages
2,569,715
Members
45,503
Latest member
TraceyP38

Latest Threads

Top