Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

programming in c: array; for loop to initialize values

226 views
Skip to first unread message

Alla _

unread,
Aug 20, 2015, 1:23:02 PM8/20/15
to
Hello!

I am at chapter 6 of Programming in C, and
here is the task:

Modify Program 6.1 so that the elements of the
array are initially set to zero. Use a for loop to perform
the initialization.

I don't understan what is being asked. I would be
grateful for explanation - what exactly shall I do
with the program 6-1.c; does "elements of the array are
initially set to zero" mean that each element like
values[0], etc, should become values [0] = 0, values [2] = 0, etc?
I hope if I understand this part, I will be able to
understand how to use for loop for this initialization.

Here is program 6-1.c

#include <stdio.h>

int main (void)
{
int values[10];
int index;

values[0] = 97;
values[2] = -100;
values[5] = 350;
values[3] = values[0] + values[5];
values[9] = values[5] / 10;
values[2]--;

for (index = 0; index < 10; index++)
printf ("values[%i] = %i\n", index, values[index]);

return 0
}

Thank you!

Rick C. Hodgin

unread,
Aug 20, 2015, 1:29:38 PM8/20/15
to
Yes. Up above you have:

values[0] = 97;

And to initialize to 0 you would have:

values[0] = 0;

But, it means that instead of manually typing them out using
"values[N] = 0;" you would instead use your loop variable to
reference the element, and populate all of them from your one
source code line via the iterative loop, rather than a host
of separate lines each populating its value.

Best regards,
Rick C. Hodgin

Jens Thoms Toerring

unread,
Aug 20, 2015, 1:32:30 PM8/20/15
to
Alla _ <modelli...@gmail.com> wrote:
> I am at chapter 6 of Programming in C, and
> here is the task:

> Modify Program 6.1 so that the elements of the
> array are initially set to zero. Use a for loop to perform
> the initialization.

> I don't understan what is being asked. I would be
> grateful for explanation - what exactly shall I do
> with the program 6-1.c; does "elements of the array are
> initially set to zero" mean that each element like
> values[0], etc, should become values [0] = 0, values [2] = 0, etc?

Yes, exactly. All of them are to be set to 0 (instead to
a lot of different values as it's done in the original
program). And for that you're supposed to use a for loop,
like the one used later for prinitng the values

> I hope if I understand this part, I will be able to
> understand how to use for loop for this initialization.

> Here is program 6-1.c

> #include <stdio.h>

> int main (void)
> {
> int values[10];
> int index;
>
> values[0] = 97;
> values[2] = -100;
> values[5] = 350;
> values[3] = values[0] + values[5];
> values[9] = values[5] / 10;
> values[2]--;
>
> for (index = 0; index < 10; index++)
> printf ("values[%i] = %i\n", index, values[index]);
>
> return 0
> }
Regards, Jens
--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de

Alla _

unread,
Aug 20, 2015, 2:27:06 PM8/20/15
to

> But, it means that instead of manually typing them out using
> "values[N] = 0;" you would instead use your loop variable to
> reference the element, and populate all of them from your one
> source code line via the iterative loop, rather than a host
> of separate lines each populating its value.
>
> Best regards,
> Rick C. Hodgin

Thank you. Do I understand correctly that all
elements should be equal to 0 when the program
finally runs, thus I get a set of 10 values all equal zero?

Alla _

unread,
Aug 20, 2015, 2:28:07 PM8/20/15
to
Thank you! It's clear now.

Rick C. Hodgin

unread,
Aug 20, 2015, 2:37:39 PM8/20/15
to
If that's all the assignment requires, then your program should
run and the loop should set each value to zero, resulting in all
of them being zero once the loop exits.

Keith Thompson

unread,
Aug 20, 2015, 2:47:00 PM8/20/15
to
(You're missing a semicolon on the return statement. If you're going to
post code, it's important to copy-and-paste the actual code that you've
compiled. Otherwise minor errors can creep in -- and in some cases such
minor errors can obscure what you're asking about and make it more
difficult for us to help. The missing semicolon is relatively innocuous
and easy to fix; I'll have to assume that's the only error.)

Later, you wrote:

> Thank you. Do I understand correctly that all
> elements should be equal to 0 when the program
> finally runs, thus I get a set of 10 values all equal zero?

That's a question about interpreting the wording of the exercise, not
about C, but I'll try to answer it anyway.

In the existing program, the initial values of the "values" array are
garbage. *Some* of those values are updated by the assignments; others
are left alone. The program then prints all the values, including the
garbage ones. (Strictly speaking this is undefined behavior; in
practice, it will probably just print the garbage values, which may or
may not vary from one execution to the next.)

The problem statement says that the array elements are *initially* set
to zero. I think what this means is that you should:

1. Define the array (that's already done);

2. Use a for loop to set all the array elements to zero;

3. Assign values to some elements (overwriting the zero values you
assigned in step 2) (that's already done); and

4. Print the values of all the elements of the array.

When I run the original program, the output starts with:

values[0] = 97
values[1] = 32542
values[2] = -101

where 32542 is the garbage value that happened to be in memory. You'll
probably see something else. The modified program's output should start
with:

values[0] = 97
values[1] = 0
values[2] = -101

That's my interpretation. You shouldn't take my word for it.
Re-read the problem statement and decide whether it's consistent
with my interpretation.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Rick C. Hodgin

unread,
Aug 20, 2015, 4:34:36 PM8/20/15
to
There are CONSTRAINTS on the initialization:

"Modify Program 6.1 so that the ELEMENTS of the
array are initially set to zero. USE A FOR LOOP TO PERFORM
THE INITIALIZATION."

Barry Schwarz

unread,
Aug 20, 2015, 7:47:47 PM8/20/15
to
Read the instructions again.

"Modify Program 6.1 so that the elements of the array are initially
set to zero. Use a for loop to perform the initialization."

The absence of any further direction implies the rest of the program
should remain unchanged.

There are two ways to initialize an array to 0:

The first is to specify a single value (in this case 0) in the
definition of the array. When there are more elements in the array
than initializers, all remaining elements are automatically set to 0.

The second is to specify an initialization value for each element
(in this case all the values would be 0).

The instructions quoted above use the word initialization in the
common sense, not the technical one. When one uses a for loop to set
each element to a value, this is an assignment, not an initialization
as the standard uses the term. Since the first sentence specifies
what to do, the inconsistency introduced by the second sentence can be
removed by replacing "the initialization" with "this task".

So your task is two fold:

Construct a for loop that will set each array element to 0.

Decide where to place the for loop in the program.

--
Remove del for email

Malcolm McLean

unread,
Aug 21, 2015, 5:09:01 AM8/21/15
to
On Thursday, August 20, 2015 at 6:23:02 PM UTC+1, Alla _ wrote:
>
> Modify Program 6.1 so that the elements of the
> array are initially set to zero. Use a for loop to perform
> the initialization.
>
> I don't understan what is being asked. I would be
> grateful for explanation.
>
It's a bit confusing.
Arrays start off with all elements random garbage, unless you tell
the compiler differently.
By stepping through the array and going

for(i=0;i<10;i++)
values[i] = something;

we initialise it with a for loop. Elements 0-9 are now "something"

However you can't easily use a for loop to initialise an array to
97, -100, 350 and so on, or other pattern with no rhyme or reason.
You can easily initialise to all zero (something ==-0), all ones
(something == 1), or a pattern like 0, 1, 2, ... (something == i),
0, 2, 4, ... (something == i * 2), 0, 1, 0, 1, ... (something = i % 2)
and so on. You quite often need regular patterns like these in real
functions.

Alla _

unread,
Aug 21, 2015, 7:03:56 AM8/21/15
to
Thank you, Keith. It makes sense to initialize only
garbage values to zero, leaving normal values as they are.
I'll try that.

Alla _

unread,
Aug 21, 2015, 7:07:21 AM8/21/15
to
Thank you everyone for your inputs and help.
It's much more clear to me now.

Keith Thompson

unread,
Aug 21, 2015, 12:16:44 PM8/21/15
to
Malcolm McLean <malcolm...@btinternet.com> writes:
[...]
> It's a bit confusing.
> Arrays start off with all elements random garbage, unless you tell
> the compiler differently.
[...]

A quibble: it's *arbitrary* garbage, not random garbage. You can't
meaningfully predict the initial values of an uninitialized array, but
if you try to use those values as random numbers it won't turn out well.
It's even possible that the initial contents will happen to be all zeros.

James Kuyper

unread,
Aug 21, 2015, 1:17:47 PM8/21/15
to
On 08/21/2015 01:22 PM, Keith Thompson wrote:
> Malcolm McLean <malcolm...@btinternet.com> writes:
> [...]
>> It's a bit confusing.
>> Arrays start off with all elements random garbage, unless you tell
>> the compiler differently.
> [...]
>
> A quibble: it's *arbitrary* garbage, not random garbage. You can't
> meaningfully predict the initial values of an uninitialized array, but
> if you try to use those values as random numbers it won't turn out well.
> It's even possible that the initial contents will happen to be all zeros.

It's not only possible, but disproportionately likely, because there are
some systems that zero-initialize it by default.

Tim Rentsch

unread,
Sep 6, 2015, 4:38:15 PM9/6/15
to
Barry Schwarz <schw...@dqel.com> writes:

> On Thu, 20 Aug 2015 11:26:52 -0700 (PDT), Alla _
> <modelli...@gmail.com> wrote:
>
>>> But, it means that instead of manually typing them out using
>>> "values[N] = 0;" you would instead use your loop variable to
>>> reference the element, and populate all of them from your one
>>> source code line via the iterative loop, rather than a host
>>> of separate lines each populating its value.
>>
>> Thank you. Do I understand correctly that all
>> elements should be equal to 0 when the program
>> finally runs, thus I get a set of 10 values all equal zero?
>
> Read the instructions again.
>
> "Modify Program 6.1 so that the elements of the array are initially
> set to zero. Use a for loop to perform the initialization."
>
> The absence of any further direction implies the rest of the program
> should remain unchanged.
>
> There are two ways to initialize an array to 0:
>
> The first is to specify a single value (in this case 0) in the
> definition of the array. When there are more elements in the array
> than initializers, all remaining elements are automatically set to 0.
>
> The second is to specify an initialization value for each element
> (in this case all the values would be 0).
>
> The instructions quoted above use the word initialization in the
> common sense, not the technical one. When one uses a for loop to set
> each element to a value, this is an assignment, not an initialization
> as the standard uses the term. [snip]

Actually the Standard uses the term "initialize" (in various forms)
in both senses, ie, both for initializers and things like assignment.

Barry Schwarz

unread,
Sep 7, 2015, 3:39:23 PM9/7/15
to
I could find only two uses in n1570 of the root initialize in
association with assignment:

6.3.2.1-2 uses "uninitialized" to mean not initialized and not
assigned a value.

Footnote 158 says clause-1 initializes the for loop. It odes not
say it initializes any of the variables in the clause.

Everywhere else, it appears to mean what I described.

Tim Rentsch

unread,
Sep 8, 2015, 2:00:22 AM9/8/15
to
There are lots of other cases in section 7. I posted an
extensive list a few months ago in a response to Keith
Thompson.
0 new messages