6 Replies - 2232 Views - Last Post: 16 March 2010 - 05:53 AM Rate Topic: -----

#1 Shelo483   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 01-April 09

Array of Objects

Posted 13 March 2010 - 07:14 AM

Hi Guyz,

I am busy with a program which would prompt for a name and 3 judge marks for each person.
For each person the program should find the 2 highest marks(judge marks) out of the 3 marks entered and find the average of those two highest marks and display them.
After you entered in all inputs the program should display the persons name as well as the 3 judges marks and the average of the 2 highest marks for each person.
It should also display the student with the highest average e.g.(Winner)

Output example
Name Judge1 Judge2 Judge3 Average
Jane 85 72 34 78.5
John 65 79 42 72

Winner
Jane 78.5

The good thing is that I am done with the program and everything else.


**The only problem is that i need to to now use an array of objects (eg array of Person objects). Each Person object will contain the Person Name, Judge1 mark, Judge 2 mark and Judge 3 mark.**(Need to create a class called Person)
Has anyone have an idea on how i would do this.
Thanks

Here is my program

namespace ExampleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] StName = new string[20];
            int[,] JudgesMarks = new int[20,3];
            int[] FinalMarks = new int[20];
            int counter = 0;

            // Priming read of Student number

            Console.WriteLine("Enter student number(-99 to stop): ");
            counter = int.Parse(Console.ReadLine());

            // Loop for students

            while (counter != 99)
            {
               
                Console.WriteLine("Enter Student Name: ");
                StName[counter-1] = Console.ReadLine();
                // Enter marks
                Console.WriteLine("Enter Judge 1: ");
                JudgesMarks[counter-1,0] = int.Parse(Console.ReadLine());
                Console.WriteLine("Enter Judge 2: ");
                JudgesMarks[counter-1,1] = int.Parse(Console.ReadLine());
                Console.WriteLine("Enter Judge 3: ");
                JudgesMarks[counter-1,2] = int.Parse(Console.ReadLine());

                // Calculate Average
                // Check for top 2 marks

                if(JudgesMarks[counter-1,0] <= JudgesMarks[counter-1,1] && JudgesMarks[counter-1,0] <= JudgesMarks[counter-1,2])
                    FinalMarks[counter-1] = (JudgesMarks[counter-1,1] + JudgesMarks[counter-1,2])/2;
                else
                    if(JudgesMarks[counter-1,1] <= JudgesMarks[counter-1,0] && JudgesMarks[counter-1,1] <= JudgesMarks[counter-1,2])
                    FinalMarks[counter-1] = (JudgesMarks[counter-1,0] + JudgesMarks[counter-1,2])/2;
                    else
                        FinalMarks[counter-1] = (JudgesMarks[counter-1,0] + JudgesMarks[counter-1,1])/2;

                Console.WriteLine("Enter Student No: ");
                counter = int.Parse(Console.ReadLine());
            }
            // List the students
            string msg ="";
            string maxName = "";
            int max = 0, count = 0; ;
            int judge1 = 0, judge2 = 0, judge3 = 0;
            for (int i = 0; i < 20; ++i)
            {
                if(StName[i] != null) 
                {
                    msg+=StName[i] + " ";
                    msg+=FinalMarks[i] + "\n";
                    if(FinalMarks[i] > max)
                    {
                        max = FinalMarks[i];
                        maxName = StName[i];
                    }
                    judge1 += JudgesMarks[i, 0];
                    judge2 += JudgesMarks[i, 1];
                    judge3 += JudgesMarks[i, 2];
                    count++;

                }
            }
            //Display in messagebox
            msg += "Judge 1: " + judge1 / count + "\n";
            msg += "Judge 2: " + judge2 / count + "\n";
            msg += "Judge 3: " + judge3 / count + "\n";
            MessageBox.Show(msg,"List of Students");

            string msg2 = "";
            msg2 += "Winner is " + maxName + " with " + max;
            MessageBox.Show(msg2,"Winner");


                    
                            

        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Array of Objects

#2 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Array of Objects

Posted 13 March 2010 - 08:01 AM

Creating the Person is as simple as this

namespace ExampleApp
{
	/// <summary>
	/// Description of Person.
	/// </summary>
	public class Person
	{
		public string Name{get;set;}
		public int Judge1{get;set;}
		public int Judge2{get;set;}
		public int Judge3{get;set}
		
		public Person()
		{
		}
	}
}



With the class made see if you can implement it into your existing application. If you run into trouble post (use this thread instead of opening a new thread) the code you're using and what issues you're having :)
Was This Post Helpful? 0
  • +
  • -

#3 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7506
  • View blog
  • Posts: 15,556
  • Joined: 16-October 07

Re: Array of Objects

Posted 13 March 2010 - 08:13 AM

Parallel arrays are bad. If someone taught them to you, forget them.

Here's some code without them:
class Student {
	public string name;
	public int[] judgesMarks = new int[3];
	public int finalMarks;
}



Actually, that's not the best class, but it's a start. This whole business with counter-1 is pretty miserable, though.

Here's just a little more code:
class Student {
	public string name;
	public int[] judgesMarks = new int[3];
	// wait, this is an average?
	// why a variable?
	//public int finalMarks;
	public double getAverage() {
		int total, lowest;
		total = lowest = judgesMarks[0];
		for(int i=1; i<judgesMarks.Length; i++) {
			total += judgesMarks[i];
			if (judgesMarks[i]<lowest) { lowest = judgesMarks[i]; }
		}
		return ((double)(total-lowest))/(judgesMarks.Length-1);
	}
}

Student GetStudentFromUser() {
	Student student = new Student();
	Console.WriteLine("Enter Student Name: ");
	student.name = Console.ReadLine();
	for(int i=0; i<judgesMarks.Length; i++) {
		Console.WriteLine("Enter Judge " + (i+1) + ": ");
		student.judgesMarks[i] = int.Parse(Console.ReadLine());
	}
	return student;
}



I've probably offered too much. I'd also use List<Student> instead of Student[] if you're allowed.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#4 Shelo483   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 01-April 09

Re: Array of Objects

Posted 14 March 2010 - 01:46 AM

Thanks guyz

I'll see with what i can come up and post my final solution.

Thanks
Was This Post Helpful? 0
  • +
  • -

#5 Shelo483   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 01-April 09

Re: Array of Objects

Posted 15 March 2010 - 07:34 AM

Hi Guyz

I started all over with my program and came up with a solution.
I works quite fine, but need opinions on where i could improve a little.

Student[] student = new Student[100];
        private int count = 0;
        private int counter = 1;
        private string message = "StudentNO\tName\tJudge1\tJudge2\tJudge3\tFinalMark\n\n";
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lblCount.Text = counter.ToString();
        }

        private void enterMarksToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (txtStNo.Text == "" || txtName.Text == "" || txtJudge1.Text == "" || txtJudge2.Text == "" || txtJudge3.Text == "")
            {
                MessageBox.Show("Please Do Not Leave Any Fields Empty!", "Warning");
            }
                        else if (Convert.ToDouble(txtJudge1.Text) < 0 || Convert.ToDouble(txtJudge1.Text) > 100)
            {
                MessageBox.Show("Please Note: Mark should be between 0 and 100!", "Warning");
            }
            else if (Convert.ToDouble(txtJudge2.Text) < 0 || Convert.ToDouble(txtJudge2.Text) > 100)
            {
                MessageBox.Show("Please Note: Mark should be between 0 and 100!", "Warning");
            }
            else if (Convert.ToDouble(txtJudge3.Text) < 0 || Convert.ToDouble(txtJudge3.Text) > 100)
            {
                MessageBox.Show("Please Note: Mark should be between 0 and 100!", "Warning");
            }
           
            else
            {
                student[count] = new Student(txtStNo.Text, txtName.Text, int.Parse(txtJudge1.Text), int.Parse(txtJudge2.Text), int.Parse(txtJudge3.Text));
                student[count].CalculateFinalMark();

                
                //message +=  count.ToString() + "\t\t" + studName[count-1] + "\t" + judgeMark1 + "\t" + judgeMark2 + "\t" + judgemark3 + "\t" + finalMark[count-1] + "\n";
                count++;
                counter++;
                
                lblCount.Text = counter.ToString();
                txtStNo.Text = "";
                txtName.Text = "";
                txtJudge1.Text = "";
                txtJudge2.Text = "";
                txtJudge3.Text = "";

            }
        }

        private void endJudgingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int maximum = 0;
            string maximumName = "";
            
            double judge1 = 0, judge2 = 0, judge3 = 0;
            for (int i = 0; i < count; ++i)
            {
                
                if (student[i].Finalmark > maximum)
                {
                    maximum = student[i].Finalmark;
                    maximumName = student[i].Name;
                }
                judge1 += student[i].Judge1;
                judge2 += student[i].Judge2;
                judge3 += student[i].Judge3;


                message += student[i].StudNo + "\t\t" + student[i].Name + "\t" + student[i].Judge1 + "\t" + student[i].Judge2 + "\t" + student[i].Judge3 + "\t" + student[i].Finalmark +
                    "\n";
            }
            message += "\nJudges Averages:\n\n";
            message += "Judge 1: " + judge1 / count + "\n";
            message += "Judge 2: " + judge2 / count + "\n";
            message += "Judge 3: " + judge3 / count + "\n\n";
            
            message += "Winner is " + maximumName + "  " + maximum;


            MessageBox.Show(message, "Judging Results");
            
        }
 private void lblCount_TextChanged(object sender, EventArgs e)
        {

            if (int.Parse(lblCount.Text) == 11)
            {
                MessageBox.Show("Exceeded maximum number of students!", "Warning");
                enterMarksToolStripMenuItem.Enabled = false;
                
                
                
            }
        }




Student Class
class Student
    {
        private string name;
        private string studNo;
        private int judge1;
        private int judge2;
        private int judge3;
        private int finalMark;

        public Student(string no, string n, int mark1, int mark2, int mark3)
        {
            studNo = no;
            name = n;
            judge1 = mark1;
            judge2 = mark2;
            judge3 = mark3;
        }
        public string StudNo
        {
            get
            {
                return studNo;
            }

        }
        public string Name
        {
            get
            {
                return name;
            }
        }
        public int Judge1
        {
            get
            {
                return judge1;
            }
            set
            {
                judge1 = value;
            }
        }
        public int Judge2
        {
            get
            {
                return judge2;
            }
            set
            {
                judge2 = value;
            }
        }
        public int Judge3
        {
            get
            {
                return judge3;
            }
            set
            {
                judge3 = value;
            }

        }
        public int Finalmark
        {
            get
            {
                return finalMark;
            }
            set
            {
                finalMark = value;
            }
        }
        public void CalculateFinalMark()
        {
            if(judge1 <= judge2 && judge1 <= judge3)
                finalMark = (judge2 + judge3) / 2;
            else if(judge2 <= judge1 && judge2 <= judge3)
                finalMark = (judge1 + judge3) / 2;
            else
                finalMark = (judge1 + judge2) / 2;
        }

            
    
    
    }



Thanks
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Array of Objects

Posted 15 March 2010 - 11:08 AM

In your Student class this property is read-only
public string StudNo
{
    get
      {
          return studNo;
      }
}



Which means this (in your constructor) should cause an error

studNo = no;



If you're wanting to set the value of StudNo from outside your Student class it also needs a setter as well
Was This Post Helpful? 0
  • +
  • -

#7 DuoShin85   User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 135
  • Joined: 18-December 09

Re: Array of Objects

Posted 16 March 2010 - 05:53 AM

I don't believe it'll throw an error, because he's setting the private string studNo in the constructor rather than using the StudNo property.

Edit: Since you're using a method in your Student class to calculate the Student's finalMark, I don't think you need the setter on the FinalMark property either.

This post has been edited by DuoShin85: 16 March 2010 - 05:57 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1