# For Loop

Page 1 of 1

## 2 Replies - 7291 Views - Last Post: 29 March 2008 - 05:58 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=47598&amp;s=58dc5232e41adfa5fee13d199d410e60&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 FemProgrammer

Reputation: 0
• Posts: 2
• Joined: 29-March 08

# For Loop

Posted 29 March 2008 - 07:48 AM

Hello All,

I am trying to write a for loop to calculate the total and average of the elements is this array, I have a feeling that I did something wrong. Please help!!!

Question:
double [] amts = {43.34, 12.95, 9.36, 27.50, 10.20, 4.52, 63.25, 2.15, 8.97};
int i = 0; double total = 0; double average = 0;

double [] amounts = {43.34, 12.95, 9.36, 27.50, 10.20, 4.52, 63.25, 2.15, 8.97};
Array.Reverse(amounts);
for ( i = 0; < amounts.Length; i++
Console.WriteLine("amounts [{0}] = {1}", i, amounts[i];

I also want to write a code that would print out the array in descending order..

Is This A Good Question/Topic? 0

## Replies To: For Loop

### #2 Martyr2

• Programming Theoretician

Reputation: 5509
• Posts: 14,497
• Joined: 18-April 07

## Re: For Loop

Posted 29 March 2008 - 08:58 AM

Keep an eye on your syntax. I noticed you forgot parenthesis etc in the lines you showed.

```double[] amounts = { 43.34, 12.95, 9.36, 27.50, 10.20, 4.52, 63.25, 2.15, 8.97 };
double sum = 0.0;

// Sort the array and then reverse it into descending order
Array.Sort(amounts);
Array.Reverse(amounts);

// Loop through the array
for (int i = 0; i < amounts.Length; i++) {
sum += amounts[i];
Console.WriteLine("amounts[{0}] = {1}", i, amounts[i]);
}

// Display the sum and average
Console.WriteLine("Sum is: " + sum);
Console.WriteLine("Average is: " + (sum / amounts.Length));

```

Enjoy!

"At DIC we be amount array looping code ninjas.... we are just plain loopy too."

This post has been edited by Martyr2: 29 March 2008 - 09:01 AM

### #3 FemProgrammer

Reputation: 0
• Posts: 2
• Joined: 29-March 08

## Re: For Loop

Posted 29 March 2008 - 05:58 PM

Martyr2, on 29 Mar, 2008 - 08:58 AM, said:

Keep an eye on your syntax. I noticed you forgot parenthesis etc in the lines you showed.

```double[] amounts = { 43.34, 12.95, 9.36, 27.50, 10.20, 4.52, 63.25, 2.15, 8.97 };
double sum = 0.0;

// Sort the array and then reverse it into descending order
Array.Sort(amounts);
Array.Reverse(amounts);

// Loop through the array
for (int i = 0; i < amounts.Length; i++) {
sum += amounts[i];
Console.WriteLine("amounts[{0}] = {1}", i, amounts[i]);
}

// Display the sum and average
Console.WriteLine("Sum is: " + sum);
Console.WriteLine("Average is: " + (sum / amounts.Length));

```

Enjoy!

"At DIC we be amount array looping code ninjas.... we are just plain loopy too."

Thank's Martyr2.. And Thanks For Pointing Out One of The Major Mistakes I Encounter All The Time!!