|
Kompyuter tizimlari kafedrasi
|
bet | 135/154 | Sana | 08.01.2024 | Hajmi | 5,29 Mb. | | #131939 |
Bog'liq MajmuaC# da tishli massivlar
string[][] stringJaggedArray = new string[2][];
intJaggedArray[0] = new int[2];
intJaggedArray[1] = new int[4];
intJaggedArray[2] = new int[6];
intJaggedArray[0] = new int[2] {
2,
12
};
intJaggedArray[1] = new int[4] {
4,
14,
24,
34
};
intJaggedArray[2] = new int[6] {
6,
16,
26,
36,
46,
56
};
Console.Write(intJaggedArray[0][0]);
Console.WriteLine(intJaggedArray[2][5]);
for (int i = 0; i < intJaggedArray.Length; i++)
{
System.Console.Write("Element({0}): ", i);
for (int j = 0; j < intJaggedArray[i].Length; j++)
{
System.Console.Write("{0}{1}", intJaggedArray[i][j], j == (intJaggedArray[i].Length - 1) ? "" : " ");
}
Umumiy e’lon qilish
Console.WriteLine("Bir o'lchovli massiv e'loni");
string[] strArray = new string[] {
"Mahesh Chand",
"Mike Gold",
"Raj Beniwal",
"Praveen Kumar",
"Dinesh Beniwal"
};
foreach (string str in strArray)
{
Console.WriteLine(str);
}
Console.WriteLine("-----------------------------");
Console.WriteLine("Ko'p o'lchovli massiv e'loni");
string[,] string2DArray = new string[2, 2] {
{
"Rosy",
"Amy"
}, {
"Peter",
"Albert"
}
};
foreach (string str in string2DArray)
{
Console.WriteLine(str);
}
Console.WriteLine("-----------------------------");
Console.WriteLine("Tishli massiv e'loni");
int[][] intJaggedArray3 = {
new int[] {
2,
12
},
new int[] {
14,
14,
24,
34
},
new int[] {
6,
16,
26,
36,
46,
56
}
};
// Tishli massivni chiqarish
for (int i = 0; i < intJaggedArray3.Length; i++)
{
Console.Write("Element({0}): ",i);
for (int j = 0; j < intJaggedArray3[i].Length; j++)
{
Console.Write("{0} ", intJaggedArray3[i][j]);
}
Console.WriteLine();
}
Misollar. Massiv elementlariga tasodifiy qiymat berish
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
const int n = 100;
Random random = new Random();
double[] a = new double[n];
int[] aa = new int[n];
for (int i = 0; i < n; i++)
{
a[i] = random.Next(1,10);
//Console.WriteLine(i + "-indexga mos kelgan tasodifiy son:__" + a[i]);
}
for (int j = 0; j < n; j++)
{
aa[j] = random.Next(1, 10);
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (a[i] == aa[j])
{
Console.WriteLine(aa[j]);
}
else
{
Console.WriteLine("Sonlar bir xil emas");
}
}
}
Console.ReadKey();
}
}
}
1-misol. Matritsani to’ldirib 2 ta matritsadagi bir xil elementlarni izlash
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ons tint n = 100;
Random random = new Random();
double[] a = new double[n];
int[] aa = new int[n];
for (int i = 0; i < n; i++)
{
a[i] = random.Next(1,10);
//Console.WriteLine(i + “-indexga mos kelgan tasodifiy son:__” + a[i]);
}
for (int j = 0; j < n; j++)
{
aa[j] = random.Next(1, 10);
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (a[i] == aa[j])
{
Console.WriteLine(aa[j]);
}
else
{
Console.WriteLine(“Sonlar bir xil emas”);
}
}
}
Console.ReadKey();
}
}
}
2-m. Matritsa Kvadrati
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[,] a, c;
a = new int[3, 3];
c = new int[3, 3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3;j++)
{
a[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine(" Kiritilgan matritsa ");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(a[i, j] + "\t");
}
Console.WriteLine();
}
Console.WriteLine(" Kvadarati ");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
c[i, j] = 0;
for (int k = 0; k < 3; k++)
{
c[i, j] = c[i, j] + (a[i, k] * a[k, j]);
}
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(c[i, j] + "\t");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
3-m. Transponerlangan matritsa
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Trans_matritsa
{
class Program
{
static void Main(string[] args)
{
int[,] mat, trans;
int n, m;
Console.Write(" Matritsa qatorlar sonini kiriritng : ");
n = Convert.ToInt32(Console.ReadLine());
Console.Write(" Matritsa ustunlar sonini kiriritng : ");
m = Convert.ToInt32(Console.ReadLine());
mat = new int[n, m];
trans = new int[n, m];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
mat[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine(" Kiritilgan matritsa ");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
Console.Write(mat[i, j] + "\t");
}
Console.WriteLine();
}
Console.WriteLine(" Transponerlangani ");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
trans[j, i] = mat[i, j];
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
Console.Write(trans[i, j] + "\t");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
|
| |