Advertisement
novacore2483

max value noah V

Apr 25th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.41 KB | None | 0 0
  1. /*
  2.  * Created by: Noah Voroney
  3.  * Created on: thur Apr 25 2019
  4.  * Created for: ICS3U Programming
  5.  * Daily Assignment – Day #37 - Find Max Value
  6.  * This program crete an array of ten and displays the largest number
  7. */
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Data;
  12. using System.Drawing;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Windows.Forms;
  17.  
  18. namespace MaxValueNoahV
  19. {
  20.     public partial class frmMaxValueNoahV : Form
  21.     {
  22.         public frmMaxValueNoahV()
  23.         {
  24.  
  25.             InitializeComponent();
  26.         }
  27.  
  28.  
  29.         private int GetMaxValue(int[] tmpArrayOfIntegers)
  30.         {
  31.             //variables
  32.             int tmpMaxValue = 0;
  33.             int tmpCounter;
  34.  
  35.             for (tmpCounter = 0; tmpCounter < tmpArrayOfIntegers.Length; tmpCounter++)
  36.             {
  37.                 //if the max value is less than the number in the array, set the max value to that number
  38.                 if (tmpMaxValue < tmpArrayOfIntegers[tmpCounter])
  39.                 {
  40.                     tmpMaxValue = tmpArrayOfIntegers[tmpCounter];
  41.                 }
  42.             }
  43.  
  44.             //return the max value
  45.             return tmpMaxValue;
  46.         }
  47.  
  48.         private void btnStart_Click(object sender, EventArgs e)
  49.         {
  50.             //variables
  51.             const int MAX_ARRAY_SIZE = 10;
  52.             const int MAX_RANDOM_NUM = 500;
  53.             int[] arrayOfIntegers = new int[MAX_ARRAY_SIZE];
  54.             int counter, randomNum, maxValue;
  55.  
  56.             maxValue = 0;
  57.             Random randomNumGenerator = new Random();
  58.  
  59.             //clear the list box
  60.             this.lstNumbers.Items.Clear();
  61.  
  62.             for (counter = 0; counter < MAX_ARRAY_SIZE; counter++)
  63.             {
  64.                 //get the random number
  65.                 randomNum = randomNumGenerator.Next(1, MAX_RANDOM_NUM);
  66.  
  67.                 //assign the number to the array
  68.                 arrayOfIntegers[counter] = randomNum;
  69.  
  70.                 //add the number to the list box
  71.                 this.lstNumbers.Items.Add(randomNum);
  72.  
  73.                 //refresh the form
  74.                 this.Refresh();
  75.             }
  76.  
  77.             //get the max value from the array
  78.             maxValue = GetMaxValue(arrayOfIntegers);
  79.  
  80.             //display the max value
  81.             this.lblAnswer.Text = "The max value is " + maxValue;
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement