Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #67891 > unrolled thread
| Started by | Alla _ <modelling.data@gmail.com> |
|---|---|
| First post | 2015-08-20 10:22 -0700 |
| Last post | 2015-08-21 04:07 -0700 |
| Articles | 17 — 8 participants |
Back to article view | Back to comp.lang.c
programming in c: array; for loop to initialize values Alla _ <modelling.data@gmail.com> - 2015-08-20 10:22 -0700
Re: programming in c: array; for loop to initialize values "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-08-20 10:29 -0700
Re: programming in c: array; for loop to initialize values Alla _ <modelling.data@gmail.com> - 2015-08-20 11:26 -0700
Re: programming in c: array; for loop to initialize values "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-08-20 11:37 -0700
Re: programming in c: array; for loop to initialize values Barry Schwarz <schwarzb@dqel.com> - 2015-08-20 16:47 -0700
Re: programming in c: array; for loop to initialize values Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-06 13:38 -0700
Re: programming in c: array; for loop to initialize values Barry Schwarz <schwarzb@dqel.com> - 2015-09-07 12:38 -0700
Re: programming in c: array; for loop to initialize values Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-07 23:00 -0700
Re: programming in c: array; for loop to initialize values jt@toerring.de (Jens Thoms Toerring) - 2015-08-20 17:32 +0000
Re: programming in c: array; for loop to initialize values Alla _ <modelling.data@gmail.com> - 2015-08-20 11:27 -0700
Re: programming in c: array; for loop to initialize values Keith Thompson <kst-u@mib.org> - 2015-08-20 12:44 -0700
Re: programming in c: array; for loop to initialize values "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-08-20 13:34 -0700
Re: programming in c: array; for loop to initialize values Alla _ <modelling.data@gmail.com> - 2015-08-21 04:03 -0700
Re: programming in c: array; for loop to initialize values Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-08-21 02:08 -0700
Re: programming in c: array; for loop to initialize values Keith Thompson <kst-u@mib.org> - 2015-08-21 10:22 -0700
Re: programming in c: array; for loop to initialize values James Kuyper <jameskuyper@verizon.net> - 2015-08-21 13:17 -0400
Re: programming in c: array; for loop to initialize values Alla _ <modelling.data@gmail.com> - 2015-08-21 04:07 -0700
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-08-20 10:22 -0700 |
| Subject | programming in c: array; for loop to initialize values |
| Message-ID | <43dff05d-eda8-4a4d-b9e5-4de3e9e9f21e@googlegroups.com> |
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!
[toc] | [next] | [standalone]
| From | "Rick C. Hodgin" <rick.c.hodgin@gmail.com> |
|---|---|
| Date | 2015-08-20 10:29 -0700 |
| Message-ID | <1efebca8-1f71-4bec-aa30-952948cb0c7b@googlegroups.com> |
| In reply to | #67891 |
On Thursday, August 20, 2015 at 1:23:02 PM UTC-4, Alla _ wrote:
> 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!
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
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-08-20 11:26 -0700 |
| Message-ID | <263c4fcf-662f-48e7-9545-639fbe410be5@googlegroups.com> |
| In reply to | #67892 |
> 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?
[toc] | [prev] | [next] | [standalone]
| From | "Rick C. Hodgin" <rick.c.hodgin@gmail.com> |
|---|---|
| Date | 2015-08-20 11:37 -0700 |
| Message-ID | <3171674b-fab7-4af5-865f-dbb75c4f09d4@googlegroups.com> |
| In reply to | #67898 |
On Thursday, August 20, 2015 at 2:27:06 PM UTC-4, Alla _ 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. > > > > 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? 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. Best regards, Rick C. Hodgin
[toc] | [prev] | [next] | [standalone]
| From | Barry Schwarz <schwarzb@dqel.com> |
|---|---|
| Date | 2015-08-20 16:47 -0700 |
| Message-ID | <p2pcta9k31grc385ktc73lrbo1n0h94ldg@4ax.com> |
| In reply to | #67898 |
On Thu, 20 Aug 2015 11:26:52 -0700 (PDT), Alla _
<modelling.data@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.
>>
>> 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?
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
[toc] | [prev] | [next] | [standalone]
| From | Tim Rentsch <txr@alumni.caltech.edu> |
|---|---|
| Date | 2015-09-06 13:38 -0700 |
| Message-ID | <kfna8szs2q9.fsf@x-alumni2.alumni.caltech.edu> |
| In reply to | #67915 |
Barry Schwarz <schwarzb@dqel.com> writes: > On Thu, 20 Aug 2015 11:26:52 -0700 (PDT), Alla _ > <modelling.data@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.
[toc] | [prev] | [next] | [standalone]
| From | Barry Schwarz <schwarzb@dqel.com> |
|---|---|
| Date | 2015-09-07 12:38 -0700 |
| Message-ID | <d4lquapmg47iebfnljtmapu1a6tp5qffnv@4ax.com> |
| In reply to | #69664 |
On Sun, 06 Sep 2015 13:38:06 -0700, Tim Rentsch
<txr@alumni.caltech.edu> wrote:
>Barry Schwarz <schwarzb@dqel.com> writes:
>
>> On Thu, 20 Aug 2015 11:26:52 -0700 (PDT), Alla _
>> <modelling.data@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.
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.
[toc] | [prev] | [next] | [standalone]
| From | Tim Rentsch <txr@alumni.caltech.edu> |
|---|---|
| Date | 2015-09-07 23:00 -0700 |
| Message-ID | <kfnk2s1mowf.fsf@x-alumni2.alumni.caltech.edu> |
| In reply to | #69837 |
Barry Schwarz <schwarzb@dqel.com> writes: > On Sun, 06 Sep 2015 13:38:06 -0700, Tim Rentsch > <txr@alumni.caltech.edu> wrote: > >> Barry Schwarz <schwarzb@dqel.com> writes: >> >>> On Thu, 20 Aug 2015 11:26:52 -0700 (PDT), Alla _ >>> <modelling.data@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. > > 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. There are lots of other cases in section 7. I posted an extensive list a few months ago in a response to Keith Thompson.
[toc] | [prev] | [next] | [standalone]
| From | jt@toerring.de (Jens Thoms Toerring) |
|---|---|
| Date | 2015-08-20 17:32 +0000 |
| Message-ID | <d3mh92F3c5nU1@mid.uni-berlin.de> |
| In reply to | #67891 |
Alla _ <modelling.data@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 ___ jt@toerring.de
\__________________________ http://toerring.de
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-08-20 11:27 -0700 |
| Message-ID | <63dadfe5-d703-415f-830d-f353f50ed1c6@googlegroups.com> |
| In reply to | #67893 |
On Thursday, August 20, 2015 at 9:32:30 PM UTC+4, Jens Thoms Toerring wrote:
> Alla _ <modelling.data@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 ___ jt@toerring.de
> \__________________________ http://toerring.de
Thank you! It's clear now.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-08-20 12:44 -0700 |
| Message-ID | <lnwpwp218m.fsf@kst-u.example.com> |
| In reply to | #67891 |
Alla _ <modelling.data@gmail.com> writes:
> 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
> }
(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) kst-u@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"
[toc] | [prev] | [next] | [standalone]
| From | "Rick C. Hodgin" <rick.c.hodgin@gmail.com> |
|---|---|
| Date | 2015-08-20 13:34 -0700 |
| Message-ID | <eea8c1b3-5c13-4920-aa7a-78c62a00651e@googlegroups.com> |
| In reply to | #67902 |
On Thursday, August 20, 2015 at 2:47:00 PM UTC-4, Keith Thompson wrote:
> Alla _ <modelling.data@gmail.com> writes:
> > 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
> > }
>
> (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.
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."
Best regards,
Rick C. Hodgin
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-08-21 04:03 -0700 |
| Message-ID | <535dfcee-e2ef-425a-b0d1-c71a16be60e0@googlegroups.com> |
| In reply to | #67902 |
On Thursday, August 20, 2015 at 10:47:00 PM UTC+4, Keith Thompson wrote:
> Alla _ <modelling.data@gmail.com> writes:
> > 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
> > }
>
> (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) kst-u@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"
Thank you, Keith. It makes sense to initialize only
garbage values to zero, leaving normal values as they are.
I'll try that.
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2015-08-21 02:08 -0700 |
| Message-ID | <f1ad4ae3-94e1-4f09-bd2c-2014ab6c8f2e@googlegroups.com> |
| In reply to | #67891 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-08-21 10:22 -0700 |
| Message-ID | <lnk2so1rqr.fsf@kst-u.example.com> |
| In reply to | #67927 |
Malcolm McLean <malcolm.mclean5@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.
--
Keith Thompson (The_Other_Keith) kst-u@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"
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2015-08-21 13:17 -0400 |
| Message-ID | <55D75D2F.2000506@verizon.net> |
| In reply to | #67951 |
On 08/21/2015 01:22 PM, Keith Thompson wrote: > Malcolm McLean <malcolm.mclean5@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.
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-08-21 04:07 -0700 |
| Message-ID | <c9185507-0a21-4d42-acd6-7666aeb58058@googlegroups.com> |
| In reply to | #67891 |
On Thursday, August 20, 2015 at 9:23:02 PM UTC+4, Alla _ wrote:
> 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!
Thank you everyone for your inputs and help.
It's much more clear to me now.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web