; Arrays 1
Documents
Resources
Learning Center
Upload
Plans & pricing Sign in
Sign Out
Your Federal Quarterly Tax Payments are due April 15th Get Help Now >>

Arrays 1

VIEWS: 4 PAGES: 17

c sharp programming language lecture

More Info
  • pg 1
									                       1




Advanced Programming

      Lecture 4
       Array
                                                                        2

         Random Number Generation

• Class Random
  – Within namespace System
  – Pseudo-random
     • The numbers are generated using an equation with a seed
         – The seed is usually the exact time of day
  – Random randomObject = new Random();
  – randomObject.Next()
     • Returns a number from 0 to Int32.MaxValue
         – Int32.MaxValue = 2,147,483,647
  – randomObject.Next( x )
     • Returns a value from 0 up to but not including x (scaling)
  – randomObject.Next( x, y )
     • Returns a number between x (shift) and up to but not including
       y (scaling)
  using System;
  using System.Windows.Forms;
class RandomInt {
 static void Main( string[] args )      {
      int value;
     string output = "";
      Random randomInteger = new Random();
        for ( int i = 1; i <= 20; i++ )   {
  // pick random integer between 1 and 6
        value = randomInteger.Next( 1, 7 );
    output += value + " ";
        if ( i % 5 == 0 )
           output += "\n";
      } // end for structure
MessageBox.Show( output, "20 Random Numbers from 1
                       to 6",

MessageBoxButtons.OK, MessageBoxIcon.Information );

                     } // end Main

              } // end class RandomInt
                                                         5

                             Arrays

• A group of contiguous memory locations
   – Same name
   – Same type
• Refer to particular element in the array by position
  number
• Can refer to any element by giving the name of the
  array followed by the position number (subscript)
  of the element in square brackets ([])
• First element is the zeroth element
   – First element of array c is c[ 0 ]
                                                   6

                             Arrays
                                  c[ 0 ]    -45
                                  c[ 1 ]     6
Name of array (Note that
all elements of this array        c[ 2 ]     0
have the same name, c)
                                  c[ 3 ]     72
                                  c[ 4 ]    1543
                                  c[ 5 ]    -89
                                  c[ 6 ]     0
                                  c[ 7 ]     62
                                  c[ 8]      -3

   Position number (index         c[ 9 ]     1
   or subscript) of the           c[ 10 ]   6453
   element within array c
                                  c[ 11 ]   -78
                                             7

      Declaring and Allocating Arrays


• Programmer specifies the type of the
  elements of the array

• new operator to allocate dynamically the
  number of elements in the array

• In arrays of value types, each element
  contains one value of the declared type
                                                                8

     Allocating an Array and Initializing It

• Arrays can be allocated using the word new to
  specify how many elements the array should hold
• Arrays can be initialized with initializer lists
   – Allocate space for the array – number of elements in
     initializer list determines the size of array
   – Elements in array are initialized with the values in the
     initializer list

                       int[] c = new int[12];

                               int[]c;
                          c = new int[12];
  using System;
  using System.Windows.Forms;
  class InitArray {
       static void Main( string[] args )      {
     string output = "";
     int[] x;                InitArray.cs
     x = new int[ 10 ];
int[] y = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
     const int ARRAY_SIZE = 10;               int[] z;
        z = new int[ ARRAY_SIZE ];
       for ( int i = 0; i < z.Length; i++ )
       z[ i ] = 2 + 2 * i;
output += "Subscript Array x\tArray y\tArray z\n";
for ( int i = 0; i < ARRAY_SIZE; i++ )

   output += i + "\t" + x[ i ] + "\t" + y[ i ] +

         "\t" + z[ i ] + "\n";

     MessageBox.Show( output,

      "Initializing an array of int values",

       MessageBoxButtons.OK,
       MessageBoxIcon.Information );           } } // end class
Write application to output the following message?
                                                 12

                   Example

• Write a console application that generate 10
  random numbers and stores them in an array
  and then finds out how many times each of
  these numbers occurs in this array
using System;
  using System.Windows.Forms;
  class Histogram {
          static void Main( string[] args )       {
    int[] n = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 };
     string output = "";
                                  Histogram.cs
     output += "Element\tvalue\tHistogram\n";
          for ( int i = 0; i < n.Length; i++ )         {
    output += "\n" + i + "\t" + n[ i ] + "\t";
  for ( int j = 1; j <= n[ i ]; j++ ) // print a bar
          output += "*";
     }
MessageBox.Show( output, "Histogram Printing Program",
         MessageBoxButtons.OK, MessageBoxIcon.Information );
   } } // end class Histogram
Write application to output the following message?
  using System;
  using System.Windows.Forms;
  class StudentPoll {
     // main entry point for application
    static void Main( string[] args ) {
       int[] responses = { 1, 2, 6, 4, 8, 5, 9, 7, 8, 10, 1,
         6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 };
                                  StudentPoll.cs
       int[] frequency = new int[ 11 ];
       string output = "";
           for ( int answer = 0; answer < responses.Length; answer++ )
        ++frequency[ responses[ answer ] ];
       output += "Rating\tFrequency\n";
for ( int rating = 1; rating < frequency.Length; rating++ )
         output += rating + "\t" + frequency[ rating ] + "\n";
       MessageBox.Show( output, "Student poll program",
         MessageBoxButtons.OK, MessageBoxIcon.Information );
    } } // end class StudentPoll
                                                        16

                      Example

Consider the following set of numbers:
31    7      14     29     5     2      10
Suppose that the above set is provided as an array.
  Perform insertion sort on this array. After each
  pass of insertion sort, show the state of the array
  and give the number of comparisons between
  elements of the array that had to be performed on
  that pass.
                                                          17

                      Example

• Construct C# console application to solve the
  following problem. A football team plays n games
  per year in its league. Given n and the scores of all
  of the games the team played this year (both the
  team’s score and its opponent’s score for each
  game), compute the team’s margin of victory in
  the games that it played (win = 3, tied = 1 and
  ignore lost).

								
To top