Velocity Reviews > for and arrays

# for and arrays

Bill Cunningham
Guest
Posts: n/a

 07-22-2008
I understand this code.

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

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

Ben Bacarisse
Guest
Posts: n/a

 07-22-2008
"Bill Cunningham" <(E-Mail Removed)> writes:

> I understand this code.
>
> int a[5];
> int b;
> for (b=0;b<5;b=b+1)
> int a[b];

Did you try?

> This should take every element of the array a and set it to 1,2,3,4,5.
> Great.

No, a loop that "sets" things will usually have an assignment in its
body.

> 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.

I'd stick with 1 dimensional ones. If you are in this for the
challenge of learning C, then it is much better to master the easy
bits before moving on. If you have some objective in mind (i.e. you
want to write some specific program) then you should pick a language
with fewer pitfalls and peculiarities than C. In particular,
interpreted languages let you learn by trying things out which, I
suspect, is a style that would suit you.

--
Ben.

Bill Cunningham
Guest
Posts: n/a

 07-22-2008

"Ben Bacarisse" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Bill Cunningham" <(E-Mail Removed)> writes:
>
>> I understand this code.
>>
>> int a[5];
>> int b;
>> for (b=0;b<5;b=b+1)
>> int a[b];

>
> Did you try?

Well I tried and it compiled with no errors. Or atleast compiled anyway.
Yes this is an excercise to learn. All I do is an excercise to learn. What
1-500? How's this for another take.

int a[10];
int b;
for (b=0;b<10;b++) {

a[ b=b+1];
}

Bill

Ben Bacarisse
Guest
Posts: n/a

 07-22-2008
"Bill Cunningham" <(E-Mail Removed)> writes:

> "Ben Bacarisse" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> "Bill Cunningham" <(E-Mail Removed)> writes:
>>
>>> I understand this code.
>>>
>>> int a[5];
>>> int b;
>>> for (b=0;b<5;b=b+1)
>>> int a[b];

>>
>> Did you try?

>
> Well I tried and it compiled with no errors. Or atleast compiled
> anyway.

You must have found out by now that a program that compiles is like an
essay the spell-checks. It does not mean you've answered the question.

> Yes this is an excercise to learn.

Compiling it is not enough, then. The wonderful thing about
programming is that your program can tell you if you've got it right.

> All I do is an excercise to learn. What
> if one had an array of 500 ints and had the simple task of setting them
> 1-500? How's this for another take.
>
> int a[10];
> int b;
> for (b=0;b<10;b++) {
>
> a[ b=b+1];
> }

Let's assume that you intended to write a[500] rather than a[10]. You
need to move one character characters to turn the above into
a program that does what you want, but can you see which character
needs to move at to where?

[I still think that you should learn an interpreted language.]

--
Ben.

Keith Thompson
Guest
Posts: n/a

 07-22-2008
"Bill Cunningham" <(E-Mail Removed)> writes:
> I understand this code.

No, I'm afraid you don't.

> int a[5];
> int b;
> for (b=0;b<5;b=b+1)
> int a[b];
>
> This should take every element of the array a and set it to 1,2,3,4,5.

You seem to be thinking that the line "int a[b];" assigns a value to
an element of a. In fact, it doesn't do anything at all like that.
following. (It declares a VLA which hides the declaration of a.)

Just this once, I'll give you a freebie. If you want to set the
elements of a to 1, 2, 3, 4, 5, here's one way to do it:

int a[5];
int i;
for (i = 0; i < 5; i ++) {
a[i] = i + 1;
}

> 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.

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.

Here's a conceptual picture of your one-dimensional array, where each
'#' represents an element that can hold an int value:

+-+-+-+-+-+
|#|#|#|#|#|
+-+-+-+-+-+

And here's a corresponding picture for your "int a[5][3];":

+-+-+-+
|#|#|#|
+-+-+-+
|#|#|#|
+-+-+-+
|#|#|#|
+-+-+-+
|#|#|#|
+-+-+-+
|#|#|#|
+-+-+-+

But you shouldn't even be thinking about 2-dimensional arrays until
you have a firm grasp on 1-dimensional arrays.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Bill Cunningham
Guest
Posts: n/a

 07-23-2008

"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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]=i+1;

Bill

Barry Schwarz
Guest
Posts: n/a

 07-23-2008
On Tue, 22 Jul 2008 16:40:36 -0700, Keith Thompson <(E-Mail Removed)>
wrote:

>"Bill Cunningham" <(E-Mail Removed)> writes:
>> I understand this code.

>
>No, I'm afraid you don't.
>
>> int a[5];
>> int b;
>> for (b=0;b<5;b=b+1)
>> int a[b];
>>
>> This should take every element of the array a and set it to 1,2,3,4,5.

>
>You seem to be thinking that the line "int a[b];" assigns a value to
>an element of a. In fact, it doesn't do anything at all like that.
>following. (It declares a VLA which hides the declaration of a.)

Since there are no braces around the statement, it is a constraint
violation in C89 since declarations must precede statements. Even in
C99 it should be a duplicate definition of a.

Remove del for email

Barry Schwarz
Guest
Posts: n/a

 07-23-2008
On Tue, 22 Jul 2008 22:15:52 GMT, "Bill Cunningham" <(E-Mail Removed)>
wrote:

>I understand this code.

You are not even close as others have pointed out.

>
>int a[5];
>int b;
>for (b=0;b<5;b=b+1)
>int a[b];
>
> 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

What do you think the phrase "first element of 5" means? What about
"the second element of 3"? How many elements to you think this 2D
array has?

>to 1,2,3 ? I don't know how to work with a 2 dimensional array.

Don't even try till after you understand 1D arrays.

Remove del for email

Keith Thompson
Guest
Posts: n/a

 07-23-2008
Barry Schwarz <(E-Mail Removed)> writes:
> On Tue, 22 Jul 2008 16:40:36 -0700, Keith Thompson <(E-Mail Removed)>
> wrote:
>
>>"Bill Cunningham" <(E-Mail Removed)> writes:
>>> I understand this code.

>>
>>No, I'm afraid you don't.
>>
>>> int a[5];
>>> int b;
>>> for (b=0;b<5;b=b+1)
>>> int a[b];
>>>
>>> This should take every element of the array a and set it to 1,2,3,4,5.

>>
>>You seem to be thinking that the line "int a[b];" assigns a value to
>>an element of a. In fact, it doesn't do anything at all like that.
>>following. (It declares a VLA which hides the declaration of a.)

>
> Since there are no braces around the statement, it is a constraint
> violation in C89 since declarations must precede statements. Even in
> C99 it should be a duplicate definition of a.

It's also a constraint violation in C89 because it's a VLA. But I
didn't want to go into too much detail about why it's wrong. The way
to transform that into what Bill intended is to delete it and write a
correct line.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Bill Cunningham
Guest
Posts: n/a

 07-23-2008

"Ben Bacarisse" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> [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.

Bill

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Philipp Java 21 01-20-2009 08:33 AM shashi Perl Misc 17 04-13-2006 03:48 PM rir3760 C Programming 1 04-04-2005 06:29 AM Bill Reyn C++ 3 06-22-2004 12:01 PM kelvSYC C Programming 2 09-26-2003 06:52 AM