Welcome to Dream.In.Code
Become a C# Expert!

Join 150,417 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,062 people online right now. Registration is fast and FREE... Join Now!




For Loop

 
Reply to this topicStart new topic

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

FemProgrammer
29 Mar, 2008 - 06:48 AM
Post #1

New D.I.C Head
*

Joined: 29 Mar, 2008
Posts: 2

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..

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: For Loop
29 Mar, 2008 - 07:58 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 314 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

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

csharp

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." decap.gif

This post has been edited by Martyr2: 29 Mar, 2008 - 08:01 AM
User is offlineProfile CardPM
+Quote Post

FemProgrammer
RE: For Loop
29 Mar, 2008 - 04:58 PM
Post #3

New D.I.C Head
*

Joined: 29 Mar, 2008
Posts: 2

QUOTE(Martyr2 @ 29 Mar, 2008 - 08:58 AM) *

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

csharp

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." decap.gif


Thank's Martyr2.. And Thanks For Pointing Out One of The Major Mistakes I Encounter All The Time!! icon_up.gif smile.gif biggrin.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:15PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month