Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

2 D Arrays Exercises

Exercise 1

Exercise 2

Exercise 3

Exercise 4

Exercise 5

Exercise 6

 

Exercise 1

1. In the main function, declare and populate a 2 D array of integers with the following numbers in the rows and columns as indicated

     1 2 3 4

     4 3 2 1

     5 6 7 8

     8 7 6 5

 

2. Call a function named PrintArray and pass the above array. The function will print the array by line, appropriately labeled

 

3. Call a function named FindSum and pass the above array. The function will find the sum of the numbers in the array and print it, appropriately

      labeled.

 

Exercise 2

Same as exercise 1 except

1. Populate the array with arbitrary integers between 0 and 9 including the end points (make them up) using an initializer list.

2. Call a function named printArray and pass the array to it. The method will print the elements as above.

 

Exercise 3

1. In the main function, create a 10 x 8 two dimensional array consisting of random integers in the range 50 to 99, including the end points

2. Call a function named printArray that will print the array, appropriately labeled, 8 numbers per line, with a space between each

3. Call a function named findSmallest that will find and print, appropriately labeled, the smallest number in the array

4. Call a function named findAverage that will find and print, appropriately labeled, the average of the numbers in the array

5. Call a function named findMode that will find and print, appropriately labeled, the number that occurs most often. It there are ties, print only 1

6. Call a function named removeEvens, that will modify the array such that all even numbers are replaced by the number 11

7. Call the function named printArray discussed in part 2 above and print the array as modified in part 6 above.

 

Exercise 4

1. Create a two dimensional array consisting of 100 random integers in the range 100 to 800, including the end points

2. Call a function named printArray that will print the array, 5 numbers per line, with a space between each

3. Call a function named findMode that will find and print, appropriately labeled, the number that occurs the most often (if there are ties, print only 1)

Exercise 5

 

1.  Assume that you have a square such that its 4 sides are tangent to a circle drawn inside the square.

 

2.  Throw randomly generated darts such that they all fall within the square

 

3.  Use this approach to approximate the value of pi

 

4.  Your output should contain the dimensions of the square and circle, the total number of throws, and the value of pi obtained

 

Exercise 6

Appropriately label all output

1.  In the main function

      a.  Declare and initialize a two-D array of integers containing 10 rows and 10 columns. The diagonal cells that run from upper left to lower right

           contain the integer 1. The other cells contain 0. 

 

      b.  Prompt the user to enter a row number and a column number - these will be used below.

 

      c.  Populate the array as described above using either a for loop or a while loop.

 

2.  Call a function named printArray that will print the contents of the array, 10 numbers per line

 

3.  Call a function named printCol that will print the contents of the col number referenced above, one item per line. Do this using a for or a while loop-

      and any other structures needed.

 

4.  Call a function named printRow that will print the contents of the row number referenced above, all on one line with a space between each. Do this

      using a for or a while loop - and any other structures needed.

 

5.  Call a function nemed printDiagonal that will print the numbers on the diagonal, all on the same line, with a space between. Do this using a for or a

      while loop - and any other structures needed.