adding all array elements

loophey please tell me how to add all array elements inside a for loop

Page 1 of 1

5 Replies - 20760 Views - Last Post: 04 August 2006 - 01:13 PM Rate Topic: -----

#1 proaspirer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 29-July 06

adding all array elements

Posted 29 July 2006 - 07:23 AM

hey please tell me how to add all array elements inside a for loop

This post has been edited by proaspirer: 29 July 2006 - 07:24 AM

Is This A Good Question/Topic? 1

Replies To: adding all array elements

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: adding all array elements

Posted 29 July 2006 - 09:53 AM

Can you post the code you have completed so far?

Basically you need to determine the length of the array, so that the FOR loop executes the correct number of times. Then access each elements value in the array by using the counter variable as the subscript for the array. Store each value from the array into a running total variable.

This post has been edited by jayman9: 29 July 2006 - 10:06 AM

Was This Post Helpful? 0
  • +
  • -

#3 chubbykelly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 02-August 06

Re: adding all array elements

Posted 04 August 2006 - 12:50 PM

r we allowed to post the codes here? bcoz i can offer some help this time.
Was This Post Helpful? 0
  • +
  • -

#4 frog  Icon User is offline

  • unleashed
  • member icon

Reputation: 2
  • View blog
  • Posts: 683
  • Joined: 26-March 06

Re: adding all array elements

Posted 04 August 2006 - 12:55 PM

not unless some effort is seen

forum rules

This post has been edited by frog: 04 August 2006 - 12:57 PM

Was This Post Helpful? 0
  • +
  • -

#5 chubbykelly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 02-August 06

Re: adding all array elements

Posted 04 August 2006 - 01:00 PM

ok....

View Postfrog, on 4 Aug, 2006 - 12:55 PM, said:

not unless some effort is seen

forum rules

Was This Post Helpful? 0
  • +
  • -

#6 chubbykelly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 02-August 06

Re: adding all array elements

Posted 04 August 2006 - 01:13 PM

1.you will need to set the desired array size first.
2.to make use of the elements of the array a for loop statement is needed.
3. since array index in c starts at 0, the initial value of ur counter(may serve as ur index as well) shld be set to zero and the input and sum of the elements shld be done while counter< the size

say u want an array with 5 elements

ur for stmt should look like this:
for (ctr=0;ctr<5;ctr++)

4. u will need scanf to hold the values being entered by the user
5. dont forget to store it in the array element not just a variable ok?
6. then u can compute for the sum
7. after u complete the iteration, you can now print the sum of the elements of ur array


hope this can be of help to u.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1