Re: Help needed with arrays
- From: "Arman Sahakyan" <armancho_x@xxxxxxxxxx(donotspam)>
- Date: Thu, 3 Nov 2005 01:33:03 -0800
Hi Paul,
You have made some efforts in comarision with your
previous codes; now your code runs. But first let me
make some general comments;
* To be more accurate, define main function to
return a value - 0:
int main()
{
....
return 0;
}
* To be more simplified and abvious, declare
each variable in a separate line:
int num[NUM_ITEMS];
int i;
int total = 0;
////
"Paul Warnack" wrote:
> "=?Utf-8?B?QXJtYW4gU2FoYWt5YW4=?=" <armancho_x@xxxxxxxxxx(donotspam)> wrote
> in news:8136A30C-67B9-47F5-AC4E-8BE427746D74@xxxxxxxxxxxxx:
>
> > Hi all
> >
> >
> > "David Wilkinson" wrote:
> >>
> >> Always post code that will compile.
> >>
> >> Why don't you at least try to use your array?
> >>
> >
> > Really good idea to let Paul solve his problem himself.
> > But it sounds Paul is a beginner, and I hope you, David, don't
> > mind if I provide a working code :-):
> >
>
> snip -> code trimmed
>
> First I would like to apologize for my lack of apparent effort, I had
> worked on this problem for 3 hours before I posted and I was very
> aggravated. I am currently studying electronics and I have found the
> thought process required of programming to be very different than that of
> my electronics classes. However, I have produced the following code which
> satisfies the majority of my assignment.
>
> #include <stdio.h>
> #include <conio.h>
>
> #define NUM_ITEMS 10
>
> main()
>
> {
>
> int num[NUM_ITEMS], i, total=0;
>
> for ( i=0; i<NUM_ITEMS; i++ )
>
> scanf( "%d", &num[i] );
>
> total = 0;
>
> for ( i=0; i<NUM_ITEMS; i++ )
>
> total += num[i];
>
> printf ( "Total is %3d\n", total );
>
> getch();
>
> }
>
> The problem I have now is writing a loop statement to fill the array
> instead of the array being populated by the user. Any hints/websites would
> be appreciated. :
Here I cannot understand why you are not able to do it
yourself; the loop that you've wrote (the one that collects
nums from a user) is not easier than one that simply fills
the array (without a user). Maybe you're asking for a
different thing... If it is so, clearify your Q.
Anyway, the loop code may look like so:
for (i = 0; i < NUM_ITEMS; i ++)
num[i] = i;
Note, you may change the value assigned to num[i];
it could be any integer value.
> Thank You to those who responded. I have a newfound respect for
> programmers.
Thanks!
> BTW thanks for the code, I plan on picking it apart to see how you did it.
You are very welcome.
--
======
Arman
.
- References:
- Re: Help needed with arrays
- From: Paul Warnack
- Re: Help needed with arrays
- Prev by Date: Re: File Summary
- Next by Date: Re: Character pointers?
- Previous by thread: Re: Help needed with arrays
- Next by thread: time function
- Index(es):
Relevant Pages
|