Re: Help needed with arrays



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

.



Relevant Pages

  • Re: Comparing pointers to NULL
    ... Suppose our data is an array of records with each record having many ... In C it is natural to use a struct to represent a record and an ... in the body of a loop over record number the current record number ...
    (comp.lang.c)
  • Re: C code is not generating required results.
    ... int main ... the array by the size of an element. ... prevent the user of your program from entering more characters than ... want to loop. ...
    (comp.lang.c)
  • Re: Streaming files
    ... int lIndex = 0; ... Why are you opening and closing the file for each iteration of the loop? ... As you are increasing lIndex for every iteration in the loop, ... If you want to fit all that data into the array, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: read keyboard input and storing in an array?
    ... When in a loop, how do I simply ... The data returned by 'br.readLine' would always be a String so I've stored ... simply convert it to an int as follows: ... you could copy 'myInt' to your array. ...
    (comp.lang.java.help)
  • Re: Beginners prime number generator
    ... int main{ ... int nextprime = 0; ... is typically tested by being *less than* the length of the array. ... a sqrtcalculation in the outer loop and just compare counter ...
    (comp.lang.c)

Quantcast