for and arrays

B

Bill Cunningham

[snipped much]
Very few of these goals (things to learn about) are language specific
and although learning C might help, it seems to me neither necessary
nor sufficient for achieving them.
What about taking something like an API like system calls, Glib, GTK+ or
something that is built in C and learn more and more about standard C.

I think that would either help or blow up in my face and confuse me
more.

Bill
 
N

Nick Keighley


don't snip so much. You originally said you wanted to:

"take every element of the array a and set it to 1,2,3,4,5"
where "a" was declared int a[5]
You don't even understand what a 2 dimensional array is.  Your
question assumes that it's a 5-element array followed by a 3-element
array.  It isn't.  It's a 5-element array, each of whose elements is a
3-element array of ints, for a total of 15 (5*3) ints.

    I understand if I am correct that each of the 5 elements have 3
elements. Yes. What I didn't understand was

    a=i+1;


that does what you wanted. I can't imagine how you can *not*
understand it!
I'm beginning to wonder if you have a completely wrong idea how
procedural
languages like C work. You don't appear to understand that the order
of
statements matters or how assignment works.

Consider
for (i = 0; i < 5; i++)
a = i + 1;

Now consider a simple assembler

store i 0 # store zero in i
load r1 i # load register 1 with contents of i
CMP: cmp r1 5 # campare i with 5
jge FI # exit it i >= 5
load r2 r1
inc r2 # r2 = i + 1
load r3 &a
add r3 r1 # r3 = &a
store r3 r2 # store i + 1 in a
inc r1
store i r1 # update i
jmp CMP
FI: nop

A basic compiler would optimise a lot of this away.





--
Nick Keighley

Server rooms should be unfriendly! They should look dangerous,
they should be uncomfortably cold, they should be inexplicably noisy.
If you can arrange for the smell of burning transistors, that's good
too.
If anything, I'd rather see rackmounted servers designed in dark
foreboding
colors with lots of metal spikes sticking out of them.
Mike Sphar (on news//alt.sysadmin.recovery)
 
B

Bill Cunningham

[snip]
1. Copy and paste this code (between "/*begin1*/" and "/*end1*/") into
a new source file, compile, and run it. What output does it produce?

/*begin1*/
#include <stdio.h>
int main(void){
/* you are not expected to understand this - it is to
verify that you can and will follow instructions */
printf("%s%c%d\n",
"protest"+3,
"blank space"[5],
27/3-2*2*2);
return 0;
}
/*end1*/


2. Write a program to print a single line containing the greeting
"Hello, world!" followed by a newline character.

The results I got from your first program was a warning concerning no
newline character and the text

test 1

Here is the answer to #2 of your question.

#include <stdio.h>

int main(void) {
printf("hello world\n");
return 0;
}

Do you wish to correspond through private email ? I will post the answer to
your third question later.

Bill
 
B

Bill Cunningham

[snip]
The results I got from your first program was a warning concerning no
newline character and the text

test 1

Here is the answer to #2 of your question.

#include <stdio.h>

int main(void) {
printf("hello world\n");
return 0;
}

Oops. I forgot the ! at the end of the hello world.
#include said:
int main(void) {
printf("hello world!\n");
return 0;
}

There.

Bill
 
M

Mark L Pappin

Bill Cunningham said:
The results I got from your first program was a warning concerning no
newline character

You didn't get this warning when you ran it - you got it when you
compiled it. This is because your text editor does not end the final
line of the file it creates with a newline character. To eliminate
this warning (which merely adds noise to the compiler's output,
reducing the visibility of the messages you _should_ worry about) you
should ensure that each source file, when displayed on screen in your
editor, has an "extra" blank line at the end. (Any number of extra
blank lines at the end should make no difference to your code so feel
free to tap the Enter key a couple times after the final '}'.)
and the text

test 1

Perfect. Keep that program around (I recommend you save each answer
in an appropriately-named file, in this case "q1.c", for later
reference and review) and a little way down the track you will have
the knowledge to analyse _why_ that output is produced, when the
string "test 1" appears nowhere in the source.
Here is the answer to #2 of your question.

#include <stdio.h>

int main(void) {
printf("hello world\n");
return 0;
}

(with '!' added in a later post)

Close enough for jazz.

(While it's not vital for this example, remember that UPPER CASE and
lower case are distinct, both in C keywords and identifiers, and also
within strings. If you care enough to correct the missing '!', you
should also ensure case distinctions are preserved.)
Do you wish to correspond through private email ?

No - you owe it to the group to demonstrate the learning process
publicly (since you've previously wasted a lot of their time flailing
around), and as they see you improving may even be motivated to help.
Take that help in the spirit in which it is offered and everybody
wins.
I will post the answer to your third question later.

I look forward to it - you've made a good start and I'd like to see
you keep it up. (Analogous to "q1.c" above, save the 2 programs in
"q3a.c" and "q3b.c", and then paste them direct into your posting.)

Once we've dealt with each group of questions, I'll prepare a few
more, each building on what we've done previously. If at any stage
you don't understand something, I'll need you to describe what parts
of it you _do_ understand so I know what base I can build a new
explanation on. If you claim to understand (e.g. by not saying
otherwise) but actually don't, then your confusion will only deepen
because each part depends on what has come before.

mlp
 
R

Richard

Bill Cunningham said:
I understand this code.

int a[5];
int b;
for (b=0;b<5;b=b+1)
int a;

This should take every element of the array a and set it to 1,2,3,4,5.
Great. Now for the big question. How would you work this?

int a [5][3];

And make the first element of 5 all zeros and the second element of 3 equal
to 1,2,3 ? I don't know how to work with a 2 dimensional array.

Bill


Bill, instead of making such elementary mistakes time after time after
time why not just take my advice and step through your code with a
debugger for your system?

Surely you are trolling? You can surely not be so confused at this
stage?
 
B

Barry Schwarz

[snipped much]
Very few of these goals (things to learn about) are language specific
and although learning C might help, it seems to me neither necessary
nor sufficient for achieving them.
What about taking something like an API like system calls, Glib, GTK+ or
something that is built in C and learn more and more about standard C.

Don't even think about. You have been on chapter 1 for five years.
I think that would either help or blow up in my face and confuse me
more.

Until you have firm foundation in basic C, these add-ons will only
serve to confuse you even more than you are now.


Remove del for email
 
C

cooldude

I understand this code.

int a[5];
int b;
for (b=0;b<5;b=b+1)
int a;

    This should take every element of the array a and set it to 1,2,3,4,5.
Great. Now for the big question. How would you work this?

int a [5][3];

And make the first element of 5 all zeros and the second element of 3 equal
to 1,2,3 ? I don't know how to work with a 2 dimensional array.

Bill


hi,
i guess this is wat you want to do ..

for(i=0;i<5;i++)
for(j=0;j<3;j++)
a[j]=j;

ur 2D array has 5 rows 3 columns ( 5 x 2 ) 1st column is set to 0 2nd
to 1 3rd to 2 for each row..
 
L

lovecreatesbea...

Bill Cunningham said:
[I still think that you should learn an interpreted language.]
I've come so far with C now why stop ? Though I have even more to learn.
This C syntax is confusing. Bash and Perl are interesting though.

You might consider studying Python. Perl is largely a mish-mash of C,
AWK, shell scripting, and a few other languages. Python has a more
coherent design.

I think these are what I need to learn: C (C++ sometimes), Bourne
Shell, Makefile Scripting.
Personally, I'm a big fan of Perl, but I don't think
it's the language for you.

Last month, I wrote an automatic ssh and telnet script in Perl.
Because the files for rsh login on the destinate host may not be
available, I need to go to Perl and except. After I code an ssh and
telnet client in C, I can do the job without Perl. Perl is a mess.

Eric Steven Raymond said in his book: ``C and Python are semi-compact;
Perl, Java, Emacs Lisp, and shell are not.'' (http://www.faqs.org/docs/
artu/ch04s02.html#orthogonality)
 
B

Bill Cunningham

Ben Bacarisse said:
Bill, if it helps, this is thread you should be posting in. (I posted
this just to bump the thread).
Mark,

I don't know how to do this with spaces or for. So I will post another way
of doing it and I don't know how to include spaces. But the program works.

Bill

#include <stdio.h>

int main (void) {
char a[]={'0','1','2','3','4','5','6'};
printf("%s\n",a);
}

Bill
 
R

Richard

Bill Cunningham said:
Mark,

I don't know how to do this with spaces or for. So I will post another way
of doing it and I don't know how to include spaces. But the program
works.

You don't know how to include spaces?

ROTFLM. It just gets better.
Bill

#include <stdio.h>

int main (void) {
char a[]={'0','1','2','3','4','5','6'};
printf("%s\n",a);
}

Bill

Maybe you could describe, in words, what you are doing there? Did you
bother your arse to look at the data structures in a debugger yet as
advised?
 
B

Bill Cunningham

Richard said:
Bill Cunningham said:
Mark,

I don't know how to do this with spaces or for. So I will post another
way
of doing it and I don't know how to include spaces. But the program
works.

You don't know how to include spaces?

ROTFLM. It just gets better.
Bill

#include <stdio.h>

int main (void) {
char a[]={'0','1','2','3','4','5','6'};
printf("%s\n",a);
}

Bill

Maybe you could describe, in words, what you are doing there? Did you
bother your arse to look at the data structures in a debugger yet as
advised?

All I get from that thing is 'no stack'

Bill
 
R

Richard

Bill Cunningham said:
Richard said:
Bill Cunningham said:
I will post the answer to your third question later.

I look forward to it

Bill, if it helps, this is thread you should be posting in. (I posted
this just to bump the thread).

Mark,

I don't know how to do this with spaces or for. So I will post another
way
of doing it and I don't know how to include spaces. But the program
works.

You don't know how to include spaces?

ROTFLM. It just gets better.
Bill

#include <stdio.h>

int main (void) {
char a[]={'0','1','2','3','4','5','6'};
printf("%s\n",a);
}

Bill

Maybe you could describe, in words, what you are doing there? Did you
bother your arse to look at the data structures in a debugger yet as
advised?

All I get from that thing is 'no stack'

And of course you did not bother to follow the small tutorial that I
pointed you to? You're a waste of time. Actually you're not. You're a
super duper troll!
 
B

Ben Bacarisse

[I am re-posting my reply in case than means everything can be kept in
the one place.]

I don't know how to do this with spaces or for. So I will post another way
of doing it and I don't know how to include spaces. But the program
works.

You were unlucky. I have more luck, it seems. On my system I get:

0123456�-��H.��Pt���l��ЃH.��Pt��

Do you know why?
#include <stdio.h>

int main (void) {
char a[]={'0','1','2','3','4','5','6'};

I am surprised that:

char a[]={'0',' ','1',' ','2',' ','3',' ','4',' ','5',' ','6'};

did not occur to you. Knowing why you did not think of it is probably
more helpful than getting the program right. For example, did you
look for, and not find, a special escape sequence for a space
character (like there is for newline: '\n'). If so what did you do
when you did not find it? Maybe you simply did not know how to write
a space and you don't know any way to find such things out. I have a
feeling that examining these questions will be more helpful to you
than someone telling you how to write a space.
 
B

Bill Cunningham

Richard said:
And of course you did not bother to follow the small tutorial that I
pointed you to? You're a waste of time. Actually you're not. You're a
super duper troll!

Actually I bookmarked the tutorial and haven't got a chance to really
read over it. I have been writing several C utilities and they have been
working so when I pop them into gdb and try to run them they end without an
error code.

Bill
 
B

Bill Cunningham

You were unlucky. I have more luck, it seems. On my system I get:

0123456?-??H.??Pt???l???H.??Pt??

Do you know why?

No I have no idea. That looks like garbage on your printout.

Bill
 
B

Ben Bacarisse

You don't know how to include spaces?

ROTFLM. It just gets better.

I don't understand this. If you think Bill is clever fiction, why are
you replying with serious suggestions (see below)? If you think he is
genuinely struggling to learn, then why laugh at his attempts. It
seems cruel.
#include <stdio.h>

int main (void) {
char a[]={'0','1','2','3','4','5','6'};
printf("%s\n",a);
}

Bill

Maybe you could describe, in words, what you are doing there? Did you
bother your arse to look at the data structures in a debugger yet as
advised?

He said it worked so presumably if he'd looked he'd see what he expects
to see. For example, in gdb it shows:

(gdb) print a
$1 = "0123456"
(gdb) print a[6]
$2 = 54 '6'
(gdb) print a[7]
$3 = 0 '\0'

You have to know what's wrong to ask the right question:

(gdb) print sizeof a
$4 = 7

This is one problem with using a debugger to learn with -- it shows
you only what happens not what your code means.
 
R

Richard

Bill Cunningham said:
Actually I bookmarked the tutorial and haven't got a chance to really
read over it. I have been writing several C utilities and they have been
working so when I pop them into gdb and try to run them they end without an
error code.

Bill

Stop writing C utilities until you figure out strings. Trust me. use the
debugger to inspect your data structures.
 
R

Richard

Ben Bacarisse said:
I don't understand this. If you think Bill is clever fiction, why are
you replying with serious suggestions (see below)? If you think he is
genuinely struggling to learn, then why laugh at his attempts. It
seems cruel.

I can not make up my mind. I have given him lots of good advice. Until
he tries to follow some then I think a good laugh is in order...
#include <stdio.h>

int main (void) {
char a[]={'0','1','2','3','4','5','6'};
printf("%s\n",a);
}

Bill

Maybe you could describe, in words, what you are doing there? Did you
bother your arse to look at the data structures in a debugger yet as
advised?

He said it worked so presumably if he'd looked he'd see what he expects
to see. For example, in gdb it shows:

Why are you telling me what it looks like? I know what it looks like. He
needs to know what it looks like. The thing you have to remember with
Bill is that he heasn't a clue what "works" means since half the time he
doesnt appear to know what it is he wants to work.

It is up to HIM to use the tools. Not for others to tell him what the
tools will tell him.
(gdb) print a
$1 = "0123456"
(gdb) print a[6]
$2 = 54 '6'
(gdb) print a[7]
$3 = 0 '\0'

You have to know what's wrong to ask the right question:

(gdb) print sizeof a
$4 = 7

This is one problem with using a debugger to learn with -- it shows
you only what happens not what your code means.

To see the data is a major part of understanding that the code has
done. If he is serious that he doesn't know how to put spaces in then he
says some serious basic issues. And a debugger can show, down and dirty,
just what he is dealing with.
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top