Практикум
Замечание. Задачи из данного пункта решить двумя способами, используя одномерный массив, а затем двумерный. Размерность массива вводится с клавиатуры.
1. Подсчитать сумму квадратов четных элементов.
2. Вывести на экран номера всех элементов больших заданного числа.
3. Вывести на экран номера всех нечетных элементов.
4. using System;
5. using System.Collections.Generic;
6. using System.Linq;
7. using System.Text;
8.
9. namespace ConsoleApplication10
10. {
11. class Program
12. {
13. static int[,] Input()
14. {
15. Console.WriteLine("Введите размер массива");
16. int n = int.Parse(Console.ReadLine());
17.
18. int[,] mas = new int[n, n];
19.
20. for (int i = 0; i < mas.GetLength(0); i++)
21. for (int j = 0; j < mas.GetLength(1); j++)
22. mas[i, j] = i;
23. return mas;
24. }
25.
26.
27. static void Print(int[,] mas)
28. {
29. Console.WriteLine(" ");
30.
31. for (int i = 0; i < mas.GetLength(0); i++)
32. {
33. for (int j = 0; j < mas.GetLength(1); j++)
34. Console.Write(mas[i, j] + " ");
35.
36. Console.WriteLine(); }
37. }
38. static void nomer(int[,] mas)
39. {
40. for (int i = 0; i < mas.GetLength(0); i++)
41. for (int j = 0; j < mas.GetLength(1); j++)
42. if (mas[i, j] % 2 != 0)
43. Console.Write("[{0},{1}] ", i, j);
44.
45. }
46.
47. static void Main(string[] args)
48. {
49.
50. int[,] mas = Input();
51. Console.WriteLine("Обычный массив");
52. Print(mas);
53. Console.WriteLine("Измененный массив");
54. nomer(mas);
55. Console.WriteLine();
56.
57. Console.ReadKey();
58.
59. }
60.
61. }
62. }
63. Вывести на экран номера всех элементов, которые не делятся на 7.
64. Вывести на экран номера всех элементов, не попадающих в заданный интервал.
65. Определить, является ли произведение элементов трехзначным числом.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication10
{
class Program
{
static int[,] Input()
{
Console.WriteLine("Введите размер массива");
int n = int.Parse(Console.ReadLine());
int[,] mas = new int[n, n];
Random b = new Random();
for (int i = 0; i < mas.GetLength(0); i++)
for (int j = 0; j < mas.GetLength(1); j++)
mas[i, j] = b.Next(1, 10);
return mas;
}
static void Print(int[,] mas)
{
Console.WriteLine(" ");
for (int i = 0; i < mas.GetLength(0); i++)
{
for (int j = 0; j < mas.GetLength(1); j++)
Console.Write(mas[i, j] + " ");
Console.WriteLine();
}
}
static void sum(int[,] mas)
{
int P=1;
for (int i = 0; i < mas.GetLength(0); i++)
for (int j = 0; j < mas.GetLength(1); j++)
P=mas [i,j]*P;
{
if (P / 100 != 0 && P / 1000 == 0)
Console.WriteLine("Число трехзначное");
else Console.WriteLine("Число не трехзначное");
}
}
static void Main(string[] args)
{
int[,] mas = Input();
Console.WriteLine("Обычный массив");
Print(mas);
Console.WriteLine("Измененный массив");
sum(mas);
Console.WriteLine();
Console.ReadKey();
}
}
}
66. Вывести на экран элементы с четными индексами (для двумерного массива – сумма индексов должна быть четной).