For Loop

For loop to calculate the total and average of the elements in this ar

Page 1 of 1

2 Replies - 7291 Views - Last Post: 29 March 2008 - 05:58 PM Rate Topic: -----

#1 FemProgrammer   User is offline

  • New D.I.C Head

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

For Loop

Post icon  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!!!

Thanks in Advance


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;


My Answer:
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   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5509
  • View blog
  • 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." :snap:

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

Was This Post Helpful? 0
  • +
  • -

#3 FemProgrammer   User is offline

  • New D.I.C Head

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

Re: For Loop

Posted 29 March 2008 - 05:58 PM

View PostMartyr2, 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." :snap:


Thank's Martyr2.. And Thanks For Pointing Out One of The Major Mistakes I Encounter All The Time!! :^: :) :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1