Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FindAverage
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int grade, average;

        private void button2_Click(object sender, EventArgs e)
        {
            grade = Int32.Parse(textBox1.Text);

            LstGrades.Items.Add(grade);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            average = LstGrades.Items. / LstGrades.Items.Count;
        }
    }
}
Posted
Updated 18-Nov-14 4:43am
v2
Comments
ZurdoDev 18-Nov-14 10:29am    
What is your question?
Nelek 18-Nov-14 10:44am    
How to make the average of the grades entered in listbox? ;P
ZurdoDev 18-Nov-14 10:54am    
See Solution 1. This is very basic. Just loop through the item and add them up and then divide by the count.

It may help you to write out pseudo code, meaning you write it out in short english sentences and then translate that into code.
Nelek 18-Nov-14 10:56am    
You didn't realize it was me, with a irony smily at the end, did you?
ZurdoDev 18-Nov-14 10:57am    
Oops. Nope, I did not.

[blushing/]

There are a couple of ways to do this, but the simplest is just to use a foreach loop:
C#
int total = 0;
foreach (object o in LstGrades.Items)
   {
   total += (int) o;
   }
And then divide by the count to get the average.
 
Share this answer
 
Comments
Manas Bhardwaj 18-Nov-14 10:44am    
Yes +5!
You can use LINQ
C#
var sum = LstGrades.Items.Cast<int>().Sum();
average = sum / LstGrades.Items.Count;

or simple loop
C#
int sum = 0;
foreach (var item in LstGrades.Items)
{
    int result;
    if(int.TryParse(item.ToString(), out result))
            sum += result;
}
average = sum / LstGrades.Items.Count;
 
Share this answer
 
v4
Comments
Manas Bhardwaj 18-Nov-14 10:44am    
Ditto +5!
DamithSL 18-Nov-14 10:56am    
:)
BillWoodruff 18-Nov-14 11:31am    
Post redacted because it reflected an incorrect assumption on my part about the solution given.
DamithSL 18-Nov-14 11:59am    
I haven't received any errors from my code, what is the error you get?
BillWoodruff 18-Nov-14 12:55pm    
Let me ask how you have created the ListBox Items. If you inserted integer values into the ListBox in code, then, yes, your first example will work.

Since the OP appears to be inserting integer values into the ListBox.Items, I think I should change my vote to #4 !

The potential problem I see is your using Cast to 'int because ListBox Items are by default ... if entered as Type 'string in the design-time editor ... Type 'object. You could, however, Cast to 'string, or 'object, and then the Linq will not break.

But, if you are going to use Linq, why not also use the 'Average function of Enumerable ?
What about this?

C#
var sum = 0;

for(var count = 0; count < LstGrades.Items.Count; count++)
{
    sum += int.Parse(LstGrades.Items[count].ToString());
}

var average = sum / LstGrades.Items.Count;
 
Share this answer
 
v3
Comments
Manas Bhardwaj 18-Nov-14 10:49am    
Dear Platinum Downvoter, considering that you have been here for long, wouldn't it be nice that you put a note for your downvote.

Your comment would really help to improve and see what's wrong.
DamithSL 18-Nov-14 10:55am    
I think int.parse is wrong :) it should be int.Parse, C# is case sensitive
5wd! ( I assume you will correct it)
Manas Bhardwaj 18-Nov-14 10:57am    
Thanks for noticing. Corrected!
BillWoodruff 18-Nov-14 11:32am    
Vote of #5 to counteract an unwarranted down-vote. Even if the vote was based on a spelling error, I don't think a down-vote was appropriate.
C#
// requires Linq

private double average;

private void btnGetAverage_Click(object sender, EventArgs e)
{
    average = lstGrades.Items
        .OfType<object>()
            .Select(o => Convert.ToInt32(o))
                .Average();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900