Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Lesson 5.docx
Скачиваний:
0
Добавлен:
01.03.2025
Размер:
127.75 Кб
Скачать

Using foreach with Arrays

C# also provides the foreach statement. This statement provides a simple, clean way to iterate through the elements of an array. For example, the following code creates an array called numbers and iterates through it with the foreach statement:

int[] numbers = { 4, 5, 6, 1, 2, 3, -2, -1, 0 };

foreach (int i in numbers)

{

System.Console.Write("{0} ", i);

}

// Output: 4 5 6 1 2 3 -2 -1 0

With multidimensional arrays, you can use the same method to iterate through the elements, for example:

int[,] numbers2D = new int[3, 2] { { 9, 99 }, { 3, 33 }, { 5, 55 } };

// Or use the short form:

// int[,] numbers2D = { { 9, 99 }, { 3, 33 }, { 5, 55 } };

foreach (int i in numbers2D)

{

System.Console.Write("{0} ", i);

}

// Output: 9 99 3 33 5 55

Variants

Task 1. One-dimensional arrays

1) Write a program that fills in an array of 8 elements with numbers in the decreasing order.

2) Write a program that a program that fills in an array of 8 elements with numbers in the ascending order.

3) Write a program that a asks user to type in elements of an array and deletes all numbers that are equal to 0 from the array.

4) Write a program which will ask a user to enter numbers for A array and which will output all negative numbers and numbers that are less than 50.

5) Write a program which will ask a user to enter numbers for B array and which will output all even numbers and numbers ending with 0.

6) Write a program that asks a user to type 10 integers and writes how many times the biggest value occurred.

7) Write a program that asks a user to type n integers and prints out a number of negative numbers and all of the negative numbers in the next line.

8) Write a program that shows the output array of 10 numbers. It must ask which numbers you want to delete and then print out a new array.

9) Write a program that asks a user to type n integers and prints out a number of positive numbers and all of the positive numbers in the same line in brackets.

10) Write a program that asks a user to enter two arrays A and B and outputs A∩B.

11) Write a program that asks a user to enter two arrays A and B and outputs AUB.

12) Write a program that searches an array of ten integers for duplicate values. Make the program display each duplicate found.

13) Aerosvit Airlines provides flights from Kyiv to six other cities in Ukraine. The cities are referred to by number, 1 to 6. The price of a round-trip ticket to each city is shown here.

City

1

2

3

4

5

6

Price

56.79

105.69

93.49

155.99

87.49

73.99

Write a program that computes the total price of tickets ordered by a customer. The program should prompt the user for the number of the destination city and the number of tickets desired. If the user enters an invalid city number, the program should display an error message and terminate. The program should display the total price of the ticket order. Use an array to store the ticket price table.

14) Write a program that prompts the user to enter a ten-element array and displays the array. Then, the program must use the bubble sort to sort the array in the increasing order and display the sorted array.

15) Write a program that finds roots of the equation Ax + Bsin = 0, where  = 1.3, A = {1.1, 2.5, 3.9, 8.1, 4.5, 12.1, 11.9, 15.5}.

(i = 1..8).

16) Write a program that calculates the following array: , where is positive root of the equation Ax + Bcos = 0, with А = 0.75, B = 4.5,  = -1.5 .. 2.5, .

17) Write a program that sums of all positive elements of an array A = {-1.2, 3.5, 4.1, 8.5, 5.3, -6.1, 3.4, 2.7}.

18) Write a program that finds product of elements of an array A = {1, 3.5, 4, -0.8, 1.9, 5, 13} which satisfies the following condition , if С = 2, D = 10.

19) . Write a program that calculates

with Y = -5.5, X = {-1.8, -1.6, ... , 1.2}. The result should be presented in the form of an array.

20) Write a program that finds numbers of the first odd and the last even numbers of an array N = {10, 8, 4, 3, 6, 15, 2}.

21) Write a program that prompts a user to enter a ten-element array and displays the array. Then, the program must use the bubble sort to sort the array in the decreasing order and display the sorted array.

22) Write a program that finds the difference between the maximum and K-th element of an array А = {-1.1, 2.5, -2.9, 8.8, 14.5, 2.2, -1.3, 5.9} if К = 4.

23) Write a program that calculates the element of an array

where x – the roots of a series of equations Аx + sin(i) = 0, ,

А = 5.5.

24) Write a program that sums of the first К positive elements of an array А = (2.8, -3.5, -2.1, 4, 6, 8.1, 6.2, 9.5, 1.1). К = 5.

25) Write a program that finds a product of the last N negative elements of an array А = (-5, 6.1, -9.2, 4, 5, -2, 7, -1, 5, 4, 1.9, -3, 5); N = 3.

26) Create an array , even elements of the array are equal to the elements of an array А = (-5.1, 2.3, 4.6, 5.8, -2.9), odd ones to the elements of an array В = (2.8, 3, 5.4, -1.9, -4.1).

27) Find the amount of positive, negative elements and elements equal to 0 of an array М = (-5, 0.1, 2.8, 0.64, 3, -5.1, 0, -7.5, 4.6, 10).

28) Calculate where х = , an array, its elements are the remainders of the division of the entire array N = (156, 18, 72, 10, 95, 100) into an integer number К = 9.

29) Calculate , where and are maximum and minimum elements of an array А = (5.5, -6, 8, 9.1, -3.5, 4.1, 10, -1, 2.5) respectively; – arithmetic mean of and .

30) A survey organization telephones 20 homes and records the household income of each family surveyed. Write a program that inputs the 20 incomes into an array and then sorts the array in the decreasing order. The program should display the following statistics: the maximum income, the minimum income, the average income, and the median income. The median of a set of sorted numbers is the middle number, if there is an odd number of numbers. If there is an even number of numbers, the median is the average of the two middle numbers.

Task 2. Multidimensional array

1) Write a program that reads in a real matrix 10x10 and finds the smallest element in the main diagonal and the smallest element in the secondary diagonal.

2) Write your own C program that transposes matrix. Program stores given matrix dimensions. Every single matrix element must be entered. Transposed matrix is the one with rows and columns switched.

3) Write your own C program that stores a real matrix 10x10, and finds the sum of elements in every column and product of elements in every row. The program prints the smallest sum (including parent column’s index), and the biggest product (including parent row’s index). Sums and products should be stored in one-dimensional arrays.

4) Maximal numbers of rows and columns of a matrix is predefined. Write your own main program which reads in a given number of rows and columns and, additionally, given elements. The main program prints: sum on the elements of the elements of the matrix (calls upon a function that calculates sum of elements)

5) Maximal numbers of rows and columns of a matrix is predefined. Write your own main program which reads in a given number of rows and columns and, additionally, given elements. Main program prints the maximal value of every row of a matrix (calls upon a function that finds the biggest element in a row)

6) Write a program that adds two matrices those dimensions and elements are input by a user, and then prints out the result.

7) An array A(m,n) is given. Write a program that creates an array В of the smallest elements of each row of the array.

8) An array В(m,n) is given. Write a program that creates an array C(n) of the sums of each column of the array B.

9) An array E(m,n) is given. Write a program that changes places of elements in the i-th and k-th rows:

10) Write a program that divides all elements of an array Р(m,n) into the maximum number of k-th column:

11) An array A(m,n) is given. Create an array С of elements of even columns and an array В of elements of odd rows of the array А:

12) An array A(m,n) is given. Create one-dimentional arrays В and С containing odd and even elements of the array A respectively:

13) Create an array В of element products of the columns of an array Z:

14) Create an array Т of the sum of elements situated in rows with negative elements on the main diagonal of the original array Z:

15) Create an array NS of rows’ numbers of an array R, where duplicated values are:

16) An array Q(m,n) is given. Replace negative elements of the array by the number of the column where they are situated:

17) Create a one-dimensional array B of elements of an array A that are smaller than Е:

18) Create a one-dimensional array B of elements that are situated below the main diagonal of an original array К:

19) An array V(n,n) is given. Replace the greatest elements of each row by s:

20) An array H(n,n) is given. Convert the array by dividing of all elements by the maximum element of K-th row:

21) Find the difference R between the maximum and minimum elements of an array W:

22) An array A(m,n) is given. Create an array B of positive elements of an array A:

, m = 2, n = 3.

23) An array G(m,n) is given. Convert the array by replacing elements of K-th and (К+1)-th columns:

24) An array D(m,n) is given. Add the value S to negative elements of the array and subtract value X out of positive elements:

25) Replace all positive elements in an array F(n,m) by their squares and all negative – by their cubes.

26) Create an array R of numbers of rows of an array Y(m,n) (those rows which have maximum element):

27) Create an array K of positive proportions of elements of n-th and s-th columns of the array:

28) Create a one-dimensional array B of the elements of А(n,n) array that are greater then C:

29) Create a one-dimensional array T of the elements of W(n,n) array that are smaller then D:

30) Create a one-dimensional array T of elements of an array A that are smaller than G:

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]