Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Keith Thompson <kst-u@mib.org> |
| Newsgroups | comp.lang.c |
| Subject | Re: programming in c: array; for loop to initialize values |
| Date | Thu, 20 Aug 2015 12:44:57 -0700 |
| Organization | None to speak of |
| Lines | 96 |
| Message-ID | <lnwpwp218m.fsf@kst-u.example.com> (permalink) |
| References | <43dff05d-eda8-4a4d-b9e5-4de3e9e9f21e@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Injection-Info | mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="30334"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+GpwM2ba2E+Yf6UYoXxOw+" |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) |
| Cancel-Lock | sha1:lLCjoGsOLlTFJ8rYjoKGKNJNW2M= sha1:gzxKNDj7iq75OeVtIszdv1EIpXE= |
| Xref | csiph.com comp.lang.c:67902 |
Show key headers only | View raw
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"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web