Zebra0.com

csharp arithmetic

To calculate the grade from the midterm and final exams we must divide (midterm+final) by 2. If we use midterm+final/2, it will find the midterm plus half of the final.explanation

Build the form as shown below:

Horizonal Scroll Bars

  Name:  hsbMidterm
  LargeChange = 1
  
  Name:  hsbFinal
  LargeChange = 1

Labels

  Name:  lblInstructions
  Text = "Select midterm and final grade:"
  
  Name:  lblMidterm
  Text = "Midterm: 0"
  
  Name:  lblFinal
  Text = "Final: 0"

  Name:  lblAverage
  Text = "Average: 0"

Write the code as shown below:

// Programmer: Janet Joy
// Calculate average from midterm and final grade.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void hsbMidterm_Scroll(object sender, ScrollEventArgs e)
        {
            //calculate grade
            int midterm = hsbMidterm.Value;
            int final = hsbFinal.Value;
            double average = (midterm + final) / 2;
            lblMidterm.Text = "Midterm: " + midterm;
            lblFinal.Text = "Final: " + final;
            lblAverage.Text = "Average: " + average;
        }

        private void hsbFinal_Scroll(object sender, ScrollEventArgs e)
        {
            //calculate grade
            int midterm = hsbMidterm.Value;
            int final = hsbFinal.Value;
            double average = (midterm + final) / 2;
            lblMidterm.Text = "Midterm: " + midterm;
            lblFinal.Text = "Final: " + final;
            lblAverage.Text = "Average: " + average;
        }
        
    }
}

Remember, you should try doing each of these programs on your own. Get it to work, break it, modify it, and then try to write it on your own!

End of lesson, Next lesson: