12
CSCI 3328 Object CSCI 3328 Object Oriented Programming Oriented Programming in C# in C# Chapter 7: Arrays – Chapter 7: Arrays – Exercises Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 [email protected]

CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 [email protected]

Embed Size (px)

Citation preview

Page 1: CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa.edu

CSCI 3328 Object Oriented CSCI 3328 Object Oriented Programming in C# Programming in C#

Chapter 7: Arrays – ExercisesChapter 7: Arrays – Exercises

1

Xiang Lian

The University of Texas – Pan American

Edinburg, TX 78539

[email protected]

Page 2: CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa.edu

Objectives

• In this chapter, you will do some exercises about:– 1-dimenionsal or multidimensional arrays – Data manipulations in arrays

2

Page 3: CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa.edu

Multiple Choices• Lists and tables of values with the same data type can be stored in _____.

– A. variables B. arrays C. indexes D. keywords• The _________ statement allows you to iterate through the elements in an

array without using a counter.– A. for B. each C. for each D. foreach

• The number that refers to a particular array element is called the element's _______.– A. number B. data type C. index D. class

• An array that uses two indices is referred to as a(n) _____ array.– A. 1-dimensional B. 2-dimensional C. matrix D. list

• Which of the following statements is true to iterate all elements in the array?– A. for (int i = 0; i < array.Length; i++) – B. for (int i = 1; i < array.Length; i++) – C. for (int i = 1; i <= array.Length; i++) – D. for (int i = 0; i <= array.Length; i++)

3

Page 4: CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa.edu

Multiple Choices (cont'd)• Use the foreach header _____ to iterate through double array

numbers.– A. for each (double d=1; d<numbers.Length; d++)– B. foreach (double d = 1 in numbers)– C. foreach (double d in numbers)– D. for each (double d = numbers[])

• Command-line arguments are stored in _____.– A. string [] args B. string args C. char args D. char [] args

• Use the expression _____ to receive the total number of arguments in a command line.– A. args.length B. args.Length() C. args.GetLength() D. args.Length

• The indexed array name of one-dimensional array units's element 2 is _______.– A. units{1} B. units(2) C. units[0, 2] D. units [1]

4

Page 5: CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa.edu

Multiple Choices (cont'd)

• Which of the following sorts array averageRainfall?– A. Array(averageRainfall).Sort() – B. Sort.Array(averageRainfall)– C. Sort(averageRainfall)– D. Array.Sort(averageRainfall)

5

Page 6: CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa.edu

True/False Statements

• A single array can store values of different types.

• An array index should normally be of type float.

• An individual array element that is passed to a method and modified in that method will contain the modified value when the called method completes execution.

• Command-line arguments are separated by commas.

6

Page 7: CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa.edu

True/False Statements (cont'd)

• The declaration of an array of size 11 is as follows:– int arr = new int [10];

• The modification of an array element is as follows:– 10 = arr[0];

• The declaration of a 2-dimensional array is as follows:– int [ ] arr = new int [20][10];

• The declaration of a 10*10 array is as follows:– int [,] arr = new int [11, 11];

• The linear search in arrays requires elements in a sorted order.

• The binary search works well for unsorted arrays

7

Page 8: CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa.edu

Debugging Errors

const int array_size = 5;array_size = 10;int [] b = new int [5];For (int i =0; i<=b.Length; i++)

b[i]=1;int [] a = {1, 2, , 4, 5};b=a;int [,] c={{1,2}, {3, 4}};c[1][1] = 5;

8

Page 9: CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa.edu

Debugging Errors (cont'd)

int [, ] array = new int [3, 3];

for (int i = 0; i<=3; i++)

for (int j=0; j<=3; j++)

array [i, j] = i+j;

9

Page 10: CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa.edu

Write a Program

• Write a complete program to generate random number between 1 and 99, and output the resulting number to a resultTextBox.

10

Page 11: CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa.edu

Write a Program (cont'd)

• Declare an array, numArray, to hold 100 integer numbers

• Assign random numbers within [1, 100] into this array

• Sort this array• Check whether a number 50 exists in this array

using the linear search and foreach statement• Output the multiplication of all even integers in

this array; if no even numbers exist, then output 1

11

Page 12: CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa.edu

12