Загрузил tadiva4452

Лабораторная работа по ОАиП "Многомерные массивы, классы и методы" С#

6
C++
;
0
.
,
,
.
#
C++
.
-
#
-
.
"
0
".
,
-
,
-
,
.
C++ "
"
,
,
.
-
,
-
-
.
#,
,
.
-
.
.
.
140
-
,
,
integer
int[,]
–
.
10x15:
table = new int[10, 15];
.
,
-
.
new,
:
int[10, 15]
10×15.
-
,
.
myArray
(3,5)
, . .
-
10:
myArray [3, 5] = 10;
#
.
.
[,...,]
...
= new
N];
141
[
1,
2,
,
4x10x3.
int [,,]
multiArray = new int[4, 10, 3];
multiArray,
-
(2,4,1)
100:
multidim[2, 4, 1] = 100;
,
3x3x3,
,
.
.
//
//
-
3x3x3.
using System;
class Example
{
static void Main()
{
int [,,] m = new int[3,-3,3];
int sum = 0;
int n = 1;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
142
for (int k = 0; k < 3; k++)
m[i, j, k] = n++;
sum = m[0, 0, 0] +
m[l, 1, 1] + m[2, 2, 2];
Console.WriteLine("
: "
+ sum);
}
}
:
:
42
.
:
[,]
=
{
{val, val, val, ..., val},
{val, val, val, ..., val},
{val, val, val, ..., val}
};
val
,
–
.
-
143
,
–
. .
,
.
.
1.
//
//
using System;
namespace Example6
{
class Example6_1
{
static void Main()
{
int i, j;
string strValue;
int[,] iArray = new int[3, 4];
int[,] jArray = new int[3, 4];
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 4; ++j)
{
//
144
strValue = Console.ReadLine();
iArray[i, j] =
Convert.ToInt32(strValue);
}
}
//
iArray
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 4; ++j)
{
Console.Write(" iArray[" + i + ",
" + j + "] =
" + iArray[i, j]);
}
Console.WriteLine();
}
Console.WriteLine();
//
jArray
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 4; ++j)
{
jArray[i, j] = iArray[i, j] * 10;
Console.Write(" jArray[" + i + ",
" + j + "] = " + jArray[i, j]);
}
Console.WriteLine();
}
145
Console.WriteLine();
}
}
}
,
-
ReadLine.
,
,
,
.
-
ReadLine.
-
ToInt32
int
iArray[i, j].
jArray
jArray[i, j] = iArray[i, j] * 10;
.
146
-
StreamReader.
StreamReader
-
.
StreamReader(string
),
–
,
.
,
\,
-
.
,
\(
).
2.
//
//
//
using System;
using System.IO;
namespace Example6
{
class Example6_2
{
static void Main()
{
int i, j;
string strValue;
int[,] iArray1 = new int[3, 4];
int[,] iArray2 = new int[3, 4];
147
StreamReader sRead = new
StreamReader("C:\\C#\\dat.txt");
StreamWriter sWrite = new
StreamWriter("C:\\C#\\res.txt");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 4; j++)
{
strValue = sRead.ReadLine();
iArray1[i, j] =
Convert.ToInt32(strValue);
iArray2[i, j] = iArray1[i, j] *
100;
strValue = string.Format("\n {0,
4:D} {1, 4:D} {2, 6:D} {3, 6:D}",
i, j, iArray1[i, j], iArray2[i,
j]);
Console.WriteLine(strValue);
Console.WriteLine();
}
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 4; j++)
{
sWrite.WriteLine(iArray2[i, j]);
}
148
}
sWrite.Close();
}
}
}
Console (Example6_1)
,
StreamReader
StreamWriter (Example6_2).
-
.
iArray1
,
int,
–
-
double
Sin(x).
strValue,
-
iArray1
.
iArray2[i, j] = iArray1[i, j] * 100;
iArray2
-
res3.txt.
Array –
,
-
System.
.
Length
,
.
149
int
Copy (Array source, Array dest, count)
count,
,
source
dest,
iArray1
.
.
3.
//
//
.
//
//
150
-
using System;
using System.IO;
namespace Example6
{
class Example6_3
{
static void Main()
{
int i, j, num;
double x = 0;
string strValue;
double[,] iArray1 = new double[10, 3];
double[,] iArray2 = new double[10, 3];
Random rnd = new Random();
StreamWriter sWrite = new
StreamWriter("C:\\C#\\res3.txt");
Console.WriteLine("\n ---------------------------------------");
for (i = 0; i < 10; i++)
{
num = rnd.Next(1, 51);
iArray1[i, 0] =
Convert.ToDouble(num);
num = (rnd.Next(1, 101) - 50) / 10;
iArray1[i, 1] =
Convert.ToDouble(num);
iArray1[i, 2] = Math.Sin(x);
151
x = x + 0.314159;
strValue = string.Format("\n {0, 4:D}
{1, 10:F0} {2, 10:F2} {3, 10:F2}", i,
iArray1[i, 0], iArray1[i, 1],
iArray1[i, 2]);
Console.WriteLine(strValue);
}
Console.WriteLine("\n ---------------------------------------");
for (i = 0; i < 10; i++)
{
for (j = 0; j < 3; j++)
{
iArray2[i, j] = iArray1[i, j] *
100;
sWrite.WriteLine(iArray2[i, j]);
}
}
num = iArray2.Length;
Array.Copy(iArray2, iArray1, num);
for (i = 0; i < 10; i++)
{
strValue = string.Format("\n {0, 4:D}
{1, 10:F0} {2, 10:F2} {3, 10:F2}", i,
iArray2[i, 0], iArray2[i, 1],
iArray2[i, 2]);
Console.WriteLine(strValue);
152
}
Console.WriteLine("\n ---------------------------------------");
sWrite.Close();
}
}
}
153
Example6_4
,
-
.
,
.
,
,
–
.
4.
//
.
//
//
using System;
using System.IO;
namespace Example6
{
class Example6_4
{
static void Main()
{
int i, j;
int num, sum = 0;
int[,] iArray = new int[5, 6];
int[ ] iA = new int[6];
Random rnd = new Random();
string strValue = "\n ------------------------------";
Console.Write("\n
//
154
");
for (j = 0; j < 6; j++)
Console.Write("{0, 5}", j);
Console.WriteLine(strValue);
//
;
//
for (i = 0; i < 5; i++)
{
for (j = 0; j < 6; j++)
{
num = rnd.Next(1, 101);
iArray[i, j] =
Convert.ToInt32(num);
iA[j] = iArray[i, j];
sum = sum + iArray[i, j];
}
}
//
for (i = 0; i < 5; i++)
{
//
Console.Write("\n " + i);
for (j = 0; j < 6; j++)
{
//
Console.Write("{0, 5}",
iArray[i, j]);
}
155
}
Console.WriteLine(strValue);
Console.WriteLine();
Console.WriteLine("
:
" + sum);
Console.WriteLine("
:
" + sum / 30);
Console.WriteLine();
}
}
}
A,
.
.
,
,
,
,
–
156
.
-
5.
//
A(M, N)
//
.
//
2
- 2
using System;
namespace Example6
{
class Example6_5
{
static void Main()
{
int M = 6, N = 8;
int i, j, temp;
int[,] iArray1 = new int[M, N];
int[,] iArray2 = new int[M, N];
int[ ] iArray3 = new int[N];
Random rnd = new Random();
string strValue = "\n ----------------------------------------";
Console.Write("\n
");
Console.WriteLine("\n
");
Console.WriteLine(strValue);
//
for (i = 0; i < M; i++)
{
for (j = 0; j < N; j++)
157
:
{
temp = rnd.Next(1, 101);
iArray1[i, j] =
Convert.ToInt32(temp);
iArray2[i, j] =
Convert.ToInt32(temp);
iArray3[j] = iArray1[i, j];
}
foreach (int jj in iArray3)
{
Console.Write("{0, 5}", jj);
}
Console.Write("\n");
}
Console.WriteLine(strValue);
Console.WriteLine("\n
: ");
Console.WriteLine(strValue);
for (j = 0; j < N; j++)
{
temp = iArray2[2, j];
iArray2[2, j] = iArray2[M - 2, j];
iArray2[M - 1, j] = temp;
}
for (i = 0; i < M; i++)
{
for (j = 0; j < N; j++)
158
{
iArray3[j] = iArray2[i, j];
}
foreach (int jj in iArray3)
{
Console.Write("{0, 5}", jj);
}
Console.Write("\n");
}
Console.WriteLine(strValue);
Console.WriteLine();
Console.WriteLine();
}
}
}
159
6.
//
A(5, 6)
//
.
//
,
//
//
,
//
using System;
namespace Example6
{
class Example6_6
{
static void Main()
{
int im = 0, jm = 0, i, j;
int num, sum = 0;
int max = -100, min = 100;
int[,] iArray = new int[5, 6];
int[ ] iA = new int[6];
Random rnd = new Random();
string strValue = "\n ------------------------------";
Console.Write("\n
");
//
for (j = 0; j < 6; j++)
Console.Write("{0, 5}", j);
Console.WriteLine(strValue);
160
//
;
for (i = 0; i < 5; i++)
{
sum = 0;
for (j = 0; j < 6; j++)
{
num = rnd.Next(1, 101);
iArray[i, j] =
Convert.ToInt32(num);
iA[j] = iArray[i, j];
sum = sum + iArray[i, j];
}
if (sum > max)
{
max = sum;
im = i;
}
}
for (j = 0; j < 5; j++)
{
if (iArray[im, j] < min)
{
min = iArray[im, j];
jm = j;
}
}
//
161
for (i = 0; i < 5; i++)
{
//
Console.Write("\n " + i);
for (j = 0; j < 6; j++)
{
//
Console.Write("{0, 5}",
iArray[i, j]);
}
}
Console.WriteLine(strValue);
Console.WriteLine();
Console.WriteLine("\n i = " + im);
Console.WriteLine("\n j = " + jm);
Console.WriteLine();
}
}
}
j.
i
.
.
if (sum > max)
162
sum
-
.
-
{
max = sum;
im = i;
}
im.
-
if (iArray[im, j] < min)
{
min = iArray[im, j];
jm = j;
}
jm
.
.
163
k = i * j.
.
if (k < 10) Console.Write("
else
");
Console.Write(" ");
k < 10
–
,
.
,
.
7.
//
//
.
//
using System;
namespace Example6
{
class Example6_7
{
static void Main()
{
int i, j, k, m = 0;
for (i = 1; i < 10; i++)
{
Console.WriteLine("\n");
for (j = 1; j < 10; j++)
{
k = i * j;
164
m++;
if (k < 10) Console.Write("
else
");
Console.Write(" ");
Console.Write(k);
}
}
Console.WriteLine("\n\n
: "
+ m);
Console.WriteLine("\n");
}
}
}
.
break
-
continue.
165
break
.
continue
,
.
8.
//
(
)
.
//
//
.
//
(
)
.
using System;
namespace Example6
{
class Example6_8
{
static void Main()
{
//
int i, j, k, m = 0;
for (i = 1; i < 10; i++)
{
Console.WriteLine("\n");
for (j = 1; j < 10; j++)
{
k = i * j;
if (j > 10 - i) break;
m++;
if (k < 10) Console.Write("
166
");
else Console.Write(" ");
Console.Write(k);
}
}
Console.WriteLine("\n\n
: "
+ m);
Console.WriteLine("\n");
m = 0;
for (i = 1; i < 10; i++)
{
Console.WriteLine("\n");
for (j = 1; j < 10; j++)
{
k = i * j;
if (k < i * (10 - i))
Console.Write("
");
else
{
if (k < 10) Console.Write
("
" + k);
else Console.Write(" " + k);
m++;
}
}
}
Console.WriteLine("\n\n
+ m);
167
: "
Console.WriteLine("\n");
}
}
}
,
-
.
,
168
-
.
#
-
,
.
,
-
.
,
.
-
,
.
,
:
[][]
= new
[
][];
.
,
.
,
.
,
.
,
jagged,
(2,1),
10:
jagged[2][1]
= 10;
.
169
-
.
,
C#
.
9.
//
//
.
using System;
namespace Example6
{
class Example6_9
{
static void Main()
{
int j, max = 0;
int[]
= new int[5];
for (j = 0; j < 5; j++)
{
[j] = j + 2;
if (
[j] > max) max =
[j];
}
//
,
//
//
int
0 = 5;
int
1 = max;
int
2 = Convert.ToInt32((4 * max
170
- 1) / 3);
int[]
= {
0,
1,
2 };
int[ ][ ]
= new int[3][ ];
[0] = new int[
[0]];
[1] = new int[
[1]];
[2] = new int[
[2]];
Console.WriteLine();
Console.Write("
for (j = 0; j <
");
2; j++)
Console.Write("{0,5}", j);
Console.WriteLine("\n\t\t ---------------------------------- ");
Console.Write("
for (j = 0; j <
[0][5]");
0; j++)
[0][j] = j + 1;
foreach (int jj in
[0])
Console.Write("{0,5}", jj);
Console.WriteLine();
Console.Write("
for (j = 0; j <
[1][6]");
1; j++)
[1][j] = j + 11;
foreach (int jj in
[1])
Console.Write("{0,5}", jj);
Console.WriteLine();
Console.Write("
for (j = 0; j <
171
[2][7]");
2; j++)
[2][j] = j + 111;
foreach (int jj in
[2])
Console.Write("{0,5}", jj);
Console.WriteLine("\n\t\t ---------------------------------- ");
Console.WriteLine();
}
}
}
,
.
,
,
,
,
.
,
-
,
.
,
,
.
.
int[ ][,] jArray = new int[5][,];
172
2.
.
-
,
.
1.
5-
6-
.
2.
A(5,6)
.
,
(4, 6)
3.
.
(4).
,
.
(20, 30)
4.
-
.
(30).
(4, 5),
5.
.
D(5).
,
.
173
6.
(6, 4),
.
2-
7.
4-
-
.
(4, 5),
.
-
.
(5,5)
8.
.
9.
,
(4, 4).
10.
(4,4),
-
.
(5, 6)
11.
(30)
,
-
.
12.
100.
13.
.
A(5, 10)
14.
-
(10, 4).
N×N.
15.
.
174
N×N.
16.
.
N×N.
17.
.
18.
N×N,
,
,
,
-
.
N×N
19.
,
-
,
.
.
20.
N×M
-
,
-
. .
16.
21.
4×4
.
N×M
22.
.
N×N.
23.
-
.
24.
,
N×N.
.
N×N
25.
90°,
.
175
-
N×N
26.
-
,
,
,
.
N×N
27.
.
,
28.
.
,
.
-
,
29.
,
30.
176
7
1.
,
,
-
,
,
.
-
.
,
.
,
,
,
,
.
»
,
,
,
.
–
,
.
,
,
,
,
Windows.
New
(new)
.
177
:
1.
new –
.
.
.
)
. 7.1.
178
-
2.
3.
public –
protected
4.
5.
internal –
protected internal –
–
.
(
(
6.
)
)
private –
.
,
7.
abstract
–
.
.
8.
sealed
–
.
.
9.
static –
.
2.0.
2 – 6
.
,
.
,
,
.
,
:
,
–
-
,
,
,
,
,–
,
,
179
.
,
:
,
-
,
.
:
//
(int)
public int Add (int first, int second)
{
int Result;
Result = first + second;
Return result;
}
public void myVoidMethod ()
// void –
{
MessageBox.Show ("
");
}
,
,
(
)
.
.
,
.
,
.
//
Rotate
Rotate (45, "Degrees");
Main –
,
-
.
180
,
.
–
.
;
.
–
,
.
,
.
public void DisplayName (string name, byte age)
{
Console.WriteLine ("Hello " + name + ". You are "
+ age.ToString () +"years old.");
}
.
String,
:
name,
Byte.
, age, –
,
.
:
2
C
4,5 e x
8,5 sin( x)
ex
fn
D
8 ex
4
1
sin( x 4 )
x3
double
public double fn (a as double, x as double, z double)
{
return (a exp(x) + z
sin(x));
}
181
...
c = fnc ( a, x x, 0. ) / fnc ( 1., x, 8.5 )
b = -1.
d = 1/( fnc (8., x^4, b) + x ^ 3)
...
fnc
,
, z
d.
D(5).
1.
vect
-
,
,
k.
public
)
using System;
namespace ConsoleApplication1
{
class Program
{
class vect
//
{
double[] a; //
int n;
//
public vect(int n)
//
{
this.a = new double[n];
this.n = n;
}
182
vect
public int vv()
//
,
//
{
for (int i = 0; i < n; i++)
{
string buf;
//
buf = Console.ReadLine();
//
a[i] = Convert.ToDouble(buf);
//
}
return (0);
}
public int vyv()
//
//
{
for (int i = 0; i < n; i++)
Console.WriteLine("a["+i+"]="
a[i]);
return (0);
}
public int perest(int k)
//
1
183
+
// k-
.
{
//
; 0
//
-1,
-
int kl = 0;
//
if ( k >= n ) kl = -1;
else
{
//
int cnt = 0;
//
int pos = -1;
double tmp;
//
while ( cnt < k )
{
pos++;
if (a[pos] < 0) cnt++;
}
//
tmp = a[pos];
//
a[pos] = a[0];
a[0] = tmp;
kl = 0;
}
return kl;
}
184
//
static void Main()
{
//
vect d = new vect(5);
d.vv();
//
d.vyv();
//
//
d.perest(2);
//
d.vyv();
//
//
}
}
}
}
2.
D(3,3).
.
myprog.
.
.
using System;
//
namespace ConsoleApplication3
{
class myprog
185
.
a
{
static void Main()
{
//
double[,] d = new double[3,3];
double[,] dt = new double[3, 3];
double[,] ddt = new double[3, 3];
//
d c
int i; int j; //
,
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
d[i, j] =
Convert.ToDouble(Console.ReadLine());
}
//
transp(d, 3, dt);
//
//
dt
d
mt_q_mult(dt, d,ddt,3);
}
//
static void transp(double[,] mtr,
int p, double[,] mtrt)
// double[,] mtr //
186
{
int i; int j;
for (i = 0; i < p; i++)
{
for (j = 0; j < p; j++)
mtrt[j, i] = mtr[i, j];
}
}
//
static void mt_q_mult(double[,] m_a,
double[,] m_b, double[,] m_r, int p)
// m_r=m_a * m_b , int p
{
int i; int j; int k; double mel;
for (i = 0; i < p; i++) //
m_a
{
//
m_b
for (j = 0; j < p; j++)
{
mel=0;
//
m_a
m_b
for (k = 0; k < p; k++)
mel=mel+m_a[i,k]*m_b[k,j];
m_r[i,j]=mel;
}
}
}
187
}
}
2.
,
.
.
(4, 4)
1.
(4, 4).
Y,
–
,
,Y–
(4, 4)
2.
Z =
.
(4, 4).
+
,
–
,
,Y –
.
(4, 4)
3.
B(3, 3)
C(4)
D(3).
.
.
(4,4)
4.
D(3,3).
-
.
188
(3,
5.
3)
(3,
3).
–
,
=
.
(4,
6.
4)
D(3,
.
3).
.
,
.
,
(6)
7.
.
D(10).
.
2
8.
DX2 + FX + E = 0
+ B
+
= 0;
ZX2 + YX + S = 0.
.
(0.052; 0.9; 0.15; 0.84; 0.67)
9.
(0.948; 0.1; 0.33; 0.16; 0.85).
,
–
,
.
.
A(4, 4)
10.
B(3, 3).
.
A, B, C, D
11.
.
ABC,
ABD, ACD.
189
12.
.
.
(4, 4)
13.
(3, 3).
.
(4,
14.
4)
(4,
Z =
– Y,
4).
–
;Y–
,
.
X, Y, Z.
15.
n
S
Ci
Ci Di ,
D,
Di –
i 1
=
+ Y;
(4, 4)
16.
D =
- Z
D(3, 3).
,
.
(5),
17.
(10), D(15).
.
18.
.
R
.
19.
.
,
,
–
.
.
190
Y(4, 4)
20.
Z(8, 8).
,
.
21.
.
.
2×2, 3×3, 4×4.
22.
.
23.
.
.
24.
.
,
.
-
25.
)2 + (
-
)2 + (
- b)2 = r2
- d)2 = R2
.
,
.
(5),
26.
(10),
2
+ d
(15).
+ r = 0,
, d –
–
,r–
.
(5)
27.
Y(7).
,
.
(5)
.
28.
.
191
(10)
(4,4),
29.
(6,6).
,
.
F(2,2)
30.
(2,2)
.
.
192