Friday, May 30, 2014

Quiz 1


namespace Zau_CSI_154_QUIZ_1_1
{
    public partial class Form1 : Form
    {
        //Q.1
        int[,] arrA = { { -5, 6, 19, 18, -13 }, { -27, 53, -45, 19, 33 }, { -12, 33, -57, 29, -16 }, { 21, 42, -26, - 23, 58} };

        //Q.3
        List<string> travelers = new List<string>();
        List<string> fromcities = new List<string>();
        List<string> tocities = new List<string>();
        
        //Creating list for Question No. 6 (Judge Scores)
        List<double> judgescores = new List<double>(6);

        public void PreloadLists()
        {
            //Preloads both lists with some data
            string[] traveler = { "Zau", "David", "Grace", "Mary", "Joe", "Sam", "Anna", "Micheal", "Jack", "Antony", "Rooney", "Katy" };
            string[] fromcity = { "Seattle", "Portland", "Los Angeles", "Seattle", "Los Angeles", "Houston", "New York", "Denver", "Dallas", "Miami", "Boston", "Houston" };
            string[] tocity = { "New York", "Los Angeles", "New York", "Seattle", "Dallas", "Denver", "Los Angeles", "Portland", "Denver", "Dallas", "Boston", "New Orlean" };

            travelers.AddRange(traveler);
            fromcities.AddRange(fromcity);
            tocities.AddRange(tocity);
        }

        public Form1()
        {
            InitializeComponent();
        }

        //Method to display array
        private void DisplayArray(int[,] array)
        {
            //clear richtextbox
            richTextBox1.Clear();
            //get number of rows and column array has.
            int rows = array.GetLength(0);
            int cols = array.GetLength(1);

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    richTextBox1.AppendText(array[i, j] + "  ");
                }
                //next line
                richTextBox1.AppendText("\n");
            }
        }

        //Q.2. Method
        private double GetAvgOfOddNegatives(int[,] array)
        {
            richTextBox1.Clear();
            int rows = array.GetLength(0);
            int cols = array.GetLength(1);
            double total = 0;
            int counter = 0;
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    if (array[i, j] < 0 && array[i, j] % 2 != 0)
                    {
                        total += array[i, j];
                        counter++;
                    }
                }
            }
            double average = 0;
            if (counter != 0)
            {
                average = total / counter;
            }

            return average;
        }
        
        //Q. 1
        private void btnDisplayArray_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
            DisplayArray(arrA);
        }

        //Q. 2 Display
        private void btnAvgOddNeg_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
            double result = GetAvgOfOddNegatives(arrA);
            if (result != 0)
                richTextBox1.Text = result.ToString("f3");
            else
                MessageBox.Show("Sorry, No neg odd num");
        }

        //Question Number 3
        private void Form1_Load(object sender, EventArgs e)
        {
            PreloadLists();
            listBox1.Items.AddRange(travelers.ToArray());

            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
        }

        //Question Number 4
        private void btnGetItinery_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                int sindex = listBox1.SelectedIndex;
                //use the index to extract the grade
                //from the grades list
                string Name = travelers[sindex];
                string frCity = fromcities[sindex];
                //display it
                string toCity = tocities[sindex];
                richTextBox2.AppendText(Name + "\nFrom: " + frCity + "\nTo: " + toCity);
            }
            else
                MessageBox.Show("You must first select a name");
        }

        //Question Number 5
        private void btnRollDice_Click(object sender, EventArgs e)
        {
            Random rand = new Random();
            int d1 = rand.Next(1, 7);
            int d2 = rand.Next(1, 7);

            pictureBox1.Image = Image.FromFile(@"dice/die" + d1 + ".gif");
            pictureBox2.Image = Image.FromFile(@"dice/die" + d2 + ".gif");
        }

        //Question Number 6;
        private void btnGetSum_Click(object sender, EventArgs e)
        {
            double judge1 = double.Parse(txtJudge1.Text);
            judgescores.Add(judge1);
            double judge2 = double.Parse(txtJudge2.Text);
            judgescores.Add(judge2);
            double judge3 = double.Parse(txtJudge3.Text);
            judgescores.Add(judge3);
            double judge4 = double.Parse(txtJudge4.Text);
            judgescores.Add(judge4);
            double judge5 = double.Parse(txtJudge5.Text);
            judgescores.Add(judge5);
            double judge6 = double.Parse(txtJudge6.Text);
            judgescores.Add(judge6);

            double Max = judgescores.Max();
            double Min = judgescores.Min();
            double sum = judgescores.Sum();
            double ActualResult = (sum - (Max + Min));

            txtResult.Text = ActualResult.ToString("f2");
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
//Note:  Put dice folder inside Debug folder

No comments:

Post a Comment