small program

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

I have rewritten and rewritten and written again this small program. Can
anyone that knows what I am try to achieve tell me what I'm doing wrong?
This version prints nothing. As was written before I got this to stdout

0 0

#include <stdio.h>

int main()
{
int ar[128] = { 0 };
int b[128] = { 0 };
int i, j;
for (i = 0; i < 127; ++i) {
for (j = 0; j < 127; ++j);
while (i < 127 && j < 127)
printf("%d %d\n", b, ar[j]);
}
}

I can't take it anymore I want it to work. Here is what I'm trying to do

0 0
1 127
2 126
3 125

....

Bill
 
J

jacob navia

Bill Cunningham a écrit :
I have rewritten and rewritten and written again this small program. Can
anyone that knows what I am try to achieve tell me what I'm doing wrong?
This version prints nothing. As was written before I got this to stdout

0 0

#include <stdio.h>

int main()
{
int ar[128] = { 0 };
int b[128] = { 0 };
int i, j;
for (i = 0; i < 127; ++i) {
for (j = 0; j < 127; ++j);


You see the nasty semicolon at the end of your second
"for" statement ???????

LOOK CLOSER!

That means that the whole statement is understood as:

for (j=0; j<127; ++j)
; // EMPTY STATEMENT!!!!!!

This means that the whole "j" loop is an empty loop, and
at the exit of that empty loop j is 127, so the
while never catches!

CONCLUSION:

ALWAYS WRITE BRACES IN YOUR FOR STATEMENTS!!!
 
B

Bill Cunningham

LOOK CLOSER!

That means that the whole statement is understood as:

for (j=0; j<127; ++j)
; // EMPTY STATEMENT!!!!!!

This means that the whole "j" loop is an empty loop, and
at the exit of that empty loop j is 127, so the
while never catches!

CONCLUSION:

ALWAYS WRITE BRACES IN YOUR FOR STATEMENTS!!!

So then for is never like while for example.

while(){

body; }

or

while()
statement;

While works both ways. Is there never a semicolon after for then?

Bill
 
M

Morris Keesan

for (i = 0; i < 127; ++i) {
for (j = 0; j < 127; ++j);
while (i < 127 && j < 127)
printf("%d %d\n", b, ar[j]);
}

Look at this carefully. Assume that you've fixed the stray semicolon which
makes the inner for loop do nothing.

for (i = 0; i < 127; ++i)
for (j = 0; j < 127; ++j)
while (i < 127 && j < 127)


How many times is that inmost "while" loop going to execute?
Is a "while" really what you want here?
(Hint: when you first encounter the while condition, i and j are
both equal to 0. Can anything in the body of the while cause
the condition to become untrue?
 
K

Keith Thompson

Bill Cunningham said:
I have rewritten and rewritten and written again this small program. Can
anyone that knows what I am try to achieve tell me what I'm doing wrong?
This version prints nothing. As was written before I got this to stdout

0 0

So you're telling us that some previous version of this program, which
you haven't shown us, prints "0 0". What do you expect us to do with
that information? (That's not a request to see the previous version.)
#include <stdio.h>

int main()

Ok, but "int main(void)" is better.
{
int ar[128] = { 0 };
int b[128] = { 0 };

You initialize your two rather oddly named arrays to all zeros. You
never assign any other values to them. What are they for?
int i, j;
for (i = 0; i < 127; ++i) {
for (j = 0; j < 127; ++j);

The above loop has no body. What is it intended to do? (I know what
it actually does, and there's a much more straightforward way to do
it.)
while (i < 127 && j < 127)
printf("%d %d\n", b, ar[j]);
}


The above loop will never execute. If it did, it would never
terminate, since the values of i and j don't change.
}

I can't take it anymore I want it to work. Here is what I'm trying to do

0 0
1 127
2 126
3 125

...

What exactly does the "..." represent? You're printing two columns of
numbers; the first column increments starting at 0, the second starts
at 0, then jumps to 127, then decrements on each line. I can guess
that the next line would be "4 124", but I can't guess how many lines
you want to print or what the last lines should look like.

I can think of no plausible output, similar to what you show, for
which an array would be either necessary or appropriate. It should be
a simple loop with just a few integer variables.

Step 1: Decide what output you want, and describe it exactly.

Step 2: Throw away your current code.

Step 3: Write a new program to produce the output you want. This
should be easier than fixing the numerous problems in your current
code.
 
B

Bill Cunningham

How many times is that inmost "while" loop going to execute?
Is a "while" really what you want here?
(Hint: when you first encounter the while condition, i and j are
both equal to 0. Can anything in the body of the while cause
the condition to become untrue?

I thought at first that an outer for loop would iterate from 0 to 127
and an inner I wanted to set from 127 to 0. Ok I see now. Let me try this
again. Maybe I don't need a while. When I get ahold of kandr2 sections can
be cited to me on this.

Bill
 
S

Seebs

I have rewritten and rewritten and written again this small program. Can
anyone that knows what I am try to achieve tell me what I'm doing wrong?
This version prints nothing. As was written before I got this to stdout

for (i = 0; i < 127; ++i) {
for (j = 0; j < 127; ++j);
while (i < 127 && j < 127)
printf("%d %d\n", b, ar[j]);
}


Well, be glad it does nothing, because if it didn't do nothing, it'd
do everything.

You have two problems here.

The first is your for loop. Here is the complete for loop:
for (j = 0; j < 127; ++j);

The body of the for loop is the ";" at the end.

So, after this executes, j will be 127.

Your next statement is a while loop. Since j is 127, it exits. If j were
less than 127, though, it would run FOREVER, because i and j never change
inside the while loop.

-s
 
K

Keith Thompson

jacob navia said:
Bill Cunningham a écrit :
I have rewritten and rewritten and written again this small
program. Can anyone that knows what I am try to achieve tell me what
I'm doing wrong? This version prints nothing. As was written before
I got this to stdout

0 0

#include <stdio.h>

int main()
{
int ar[128] = { 0 };
int b[128] = { 0 };
int i, j;
for (i = 0; i < 127; ++i) {
for (j = 0; j < 127; ++j);


You see the nasty semicolon at the end of your second
"for" statement ???????

LOOK CLOSER!

That means that the whole statement is understood as:

for (j=0; j<127; ++j)
; // EMPTY STATEMENT!!!!!!

This means that the whole "j" loop is an empty loop, and
at the exit of that empty loop j is 127, so the
while never catches!

CONCLUSION:

ALWAYS WRITE BRACES IN YOUR FOR STATEMENTS!!!

Agreed -- but that's hardly the only problem with the program.

The most obvious "fix" for the empty for loop would be to have it
enclose the following while loop. This would give you a triple
nested loop, which is just what you'd want for traversing a
3-dimensional array, but makes no sense at all for the desired
output.

As I suggested elsethread, the shortest path from the posted program
to a correct program involves deleting everything and starting
from scratch.

Bill, I don't usually post solutions, but here's a simple program that
does something similar to what you seem to be trying to do:

#include <stdio.h>
int main(void)
{
int i;
for (i = 0; i <= 128; i ++) {
printf("%d %d\n", i, 128 - i);
}
return 0;
}

No arrays, no nested loops.

If you have a program that doesn't work, adding random stuff is rarely
the answer.
 
L

Lew Pitcher

I thought at first that an outer for loop would iterate from 0 to 127
and an inner I wanted to set from 127 to 0.

Bill, it sounds to me like you are just /guessing/ at the possible outcome,
and not actually /thinking/ about it.
Ok I see now. Let me try this
again. Maybe I don't need a while. When I get ahold of kandr2 sections can
be cited to me on this.

You really should try to develop your program in steps. Only develop the
part that you *understand*, and leave the rest for later. Once you have the
developed part working properly, you can tackle the part that you haven't
done, and divide it into a part that you understand, and a part that you
don't. See the pattern?

/* stepwise refinement, version 1 */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i;

for (i = 0; i < 128; ++i) /* i varies from 0 to 127 */
{
/* do something with an ever-increasing i */
}
return EXIT_SUCCESS;
}

======================================================

/* stepwise refinement, version 2 */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, j;

for (i = 0; i < 128; ++i) /* i varies from 0 to 127 */
{
/* do something with an ever-increasing i */
for (j = 127; j > -1; --j) /* j varies from 127 to 0 */
{
/* do something with an ever-decreasing j and a constant i */
}
}
return EXIT_SUCCESS;
}

======================================================

/* stepwise refinement, version 3 */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, j;
int ar[128] = {0} /* 128 ints, each set to zero */
b[128] = {0}; /* 128 ints, each set to zero */

for (i = 0; i < 128; ++i) /* i varies from 0 to 127 */
{
/* do something with an ever-increasing i */
for (j = 127; j > -1; --j) /* j varies from 127 to 0 */
{
printf("%d %d\n",b,ar[j]);
}
}
return EXIT_SUCCESS;
}



--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
S

Squeamizh

    I have rewritten and rewritten and written again this small program. Can
anyone that knows what I am try to achieve tell me what I'm doing wrong?
This version prints nothing. As was written before I got this to stdout

0 0

#include <stdio.h>

int main()
{
    int ar[128] = { 0 };
    int b[128] = { 0 };
    int i, j;
    for (i = 0; i < 127; ++i) {
        for (j = 0; j < 127; ++j);
        while (i < 127 && j < 127)
            printf("%d %d\n", b, ar[j]);
    }

}

I can't take it anymore I want it to work. Here is what I'm trying to do

0 0
1 127
2 126
3 125

...


I am amazed by you guys who continue to write well-intentioned helpful
replies to Bill Cunningham. Seriously, folks, what's it going to take
for you all to realize you're being played?
 
L

Lew Pitcher

On October 6, 2009 15:42, in comp.lang.c, Squeamizh ([email protected])
wrote:
[snip]
I am amazed by you guys who continue to write well-intentioned helpful
replies to Bill Cunningham.

Nope. If our replies were /just/ to Bill C, then email would do, and you'd
never see them here. *Because* we post replies here, *anyone* who wishes to
understand the craft of programming in C can learn from them, not just Bill
C.
Seriously, folks, what's it going to take
for you all to realize you're being played?

Bill /may/ be "playing" us, but it doesn't matter. Did you learn something
from the exchange (besides your supposition about Bill)? I'm sure that,
even if you didn't, *someone* is learning something from these exchanges.

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
S

Seebs

I am amazed by you guys who continue to write well-intentioned helpful
replies to Bill Cunningham. Seriously, folks, what's it going to take
for you all to realize you're being played?

Who cares?

They're newbie questions of the sort that newbies always encounter, and
there are plenty of people reading who will benefit from seeing them
explained.

-s
 
J

jacob navia

Squeamizh a écrit :
I have rewritten and rewritten and written again this small program. Can
anyone that knows what I am try to achieve tell me what I'm doing wrong?
This version prints nothing. As was written before I got this to stdout

0 0

#include <stdio.h>

int main()
{
int ar[128] = { 0 };
int b[128] = { 0 };
int i, j;
for (i = 0; i < 127; ++i) {
for (j = 0; j < 127; ++j);
while (i < 127 && j < 127)
printf("%d %d\n", b, ar[j]);
}

}

I can't take it anymore I want it to work. Here is what I'm trying to do

0 0
1 127
2 126
3 125

...


I am amazed by you guys who continue to write well-intentioned helpful
replies to Bill Cunningham. Seriously, folks, what's it going to take
for you all to realize you're being played?


Well, I realized that when I said

"Write braces around "for" statements"

and then he answers

Ahh "while" statements are different?

Then, Mr heathfield comes to play and corrects me...

For the interface for the containers library I proposed
I received exactly zero answers. For my whole proposals there
were far less answers and interest than "to help Bill"...
 
S

Seebs

For the interface for the containers library I proposed
I received exactly zero answers. For my whole proposals there
were far less answers and interest than "to help Bill"...

You've gotten substantial feedback, but it's at a much different level.

You're never gonna get any buy-in to a proposed interface which uses
camelcase. I doubt anyone will ever support standardization of that
container library simply because it's top-heavy and doesn't map well
onto typical C use cases; languages which need tools like that mostly
already have them.

-s
 
J

Joachim Schmitz

jacob said:
Squeamizh a écrit :
I have rewritten and rewritten and written again this small
program. Can anyone that knows what I am try to achieve tell me
what I'm doing wrong? This version prints nothing. As was written
before I got this to stdout 0 0

#include <stdio.h>

int main()
{
int ar[128] = { 0 };
int b[128] = { 0 };
int i, j;
for (i = 0; i < 127; ++i) {
for (j = 0; j < 127; ++j);
while (i < 127 && j < 127)
printf("%d %d\n", b, ar[j]);
}

}

I can't take it anymore I want it to work. Here is what I'm trying
to do 0 0
1 127
2 126
3 125

...


I am amazed by you guys who continue to write well-intentioned
helpful replies to Bill Cunningham. Seriously, folks, what's it
going to take for you all to realize you're being played?


Well, I realized that when I said

"Write braces around "for" statements"

and then he answers

Ahh "while" statements are different?

Then, Mr heathfield comes to play and corrects me...


Pardon? I don't read RH's reply to BC that way at all.
RH corrects BC's understanding of your post in statiting that you were not
talking about a language rule but a style rule.

Bye, Jojo
 
P

Phil Carmody

Lew Pitcher said:
On October 6, 2009 15:42, in comp.lang.c, Squeamizh ([email protected])
wrote:
[snip]
I am amazed by you guys who continue to write well-intentioned helpful
replies to Bill Cunningham.

Nope. If our replies were /just/ to Bill C, then email would do, and you'd
never see them here. *Because* we post replies here, *anyone* who wishes to
understand the craft of programming in C can learn from them, not just Bill
C.

But the level of advice has almost never got above "don't poke forks
in your eye". I'd be amazed if _anyone_ has learnt _anything_ from
any responses to Bill's malformed messes.

Phil
 
B

Bill Cunningham

As I suggested elsethread, the shortest path from the posted program
to a correct program involves deleting everything and starting
from scratch.

Bill, I don't usually post solutions, but here's a simple program that
does something similar to what you seem to be trying to do:

#include <stdio.h>
int main(void)
{
int i;
for (i = 0; i <= 128; i ++) {
printf("%d %d\n", i, 128 - i);
}
return 0;
}

No arrays, no nested loops.

I hit my forehead with my palm and came to this solution which I will
post and is untested.

#include <stdio.h>

int main() /*int is needed I guess and void is optional */

{
int a[128]={0};
int b[128]={0};
int i,j;
for (i=0;i<127;++i)
{
for (j=128;j!=0;--j)
printf("%i %i\n",a,b[j]);
}
}

I don't know if that would work or not.

Bill
 
B

Bill Cunningham

Lew Pitcher said:
Bill, it sounds to me like you are just /guessing/ at the possible
outcome,
and not actually /thinking/ about it.


You really should try to develop your program in steps. Only develop the
part that you *understand*,

Unfortunately that might not be much.

and leave the rest for later. Once you have the
developed part working properly, you can tackle the part that you haven't
done, and divide it into a part that you understand, and a part that you
don't. See the pattern?

/* stepwise refinement, version 1 */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i;

for (i = 0; i < 128; ++i) /* i varies from 0 to 127 */
{
/* do something with an ever-increasing i */
}
return EXIT_SUCCESS;
}

======================================================

/* stepwise refinement, version 2 */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, j;

for (i = 0; i < 128; ++i) /* i varies from 0 to 127 */
{
/* do something with an ever-increasing i */
for (j = 127; j > -1; --j) /* j varies from 127 to 0 */
{
/* do something with an ever-decreasing j and a constant i */
}
}
return EXIT_SUCCESS;
}

======================================================

/* stepwise refinement, version 3 */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, j;
int ar[128] = {0} /* 128 ints, each set to zero */
b[128] = {0}; /* 128 ints, each set to zero */

for (i = 0; i < 128; ++i) /* i varies from 0 to 127 */
{
/* do something with an ever-increasing i */
for (j = 127; j > -1; --j) /* j varies from 127 to 0 */
{
printf("%d %d\n",b,ar[j]);
}
}
return EXIT_SUCCESS;
}


Yes.


Bill
 
K

Keith Thompson

Bill Cunningham said:
Keith Thompson said:
As I suggested elsethread, the shortest path from the posted program
to a correct program involves deleting everything and starting
from scratch.

Bill, I don't usually post solutions, but here's a simple program that
does something similar to what you seem to be trying to do:

#include <stdio.h>
int main(void)
{
int i;
for (i = 0; i <= 128; i ++) {
printf("%d %d\n", i, 128 - i);
}
return 0;
}

No arrays, no nested loops.

I hit my forehead with my palm and came to this solution which I will
post and is untested.
[untested code snipped]
I don't know if that would work or not.

Gosh, how might you *find out* whether it would work, hmm?

Why would you post code that you haven't bothered to run?
Why would you post code that repeats most of the blatant errors
you've already made?

Throw it away. Start again. Define the problem clearly before
writing a single line of code (I still don't know what you're trying
to do). Don't add an array unless you *know* what it's going to
be used for. Don't add a loop unless you *know* what it's going
to be used for.

Don't waste our time posting code that you haven't bothered to test.
If you have a question, ask it. "Here's some garbage code that
may or may not work" is not a question.

If you're not a deliberate troll (and I've given you the benefit
of the doubt far longer than most other people here have), you're
doing a good imitation of one.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top