|
-Laboratoriya. Ko‘p o‘lchamli massivlar bilan ishlash
|
bet | 137/154 | Sana | 08.01.2024 | Hajmi | 5,29 Mb. | | #131939 |
Bog'liq Majmua14-Laboratoriya. Ko‘p o‘lchamli massivlar bilan ishlash.
Ikki o’lchamli va ko’p o’lchamli massivlar
int[,] numbers = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
string[,] names = new string[2, 2] { { "Rosy", "Amy" }, { "Peter", "Albert" } };
int[,] numbers = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
string[,] names = new string[,] { { "Rosy", "Amy" }, { "Peter", "Albert" } };
int[,] numbers = new int[3, 2];
numbers[0, 0] = 1;
numbers[1, 0] = 2;
numbers[2, 0] = 3;
numbers[0, 1] = 4;
numbers[1, 1] = 5;
numbers[2, 1] = 6;
Console.WriteLine(numbers[0, 0]);
Console.WriteLine(numbers[0, 1]);
Console.WriteLine(numbers[1, 0]);
Console.WriteLine(numbers[1, 1]);
Console.WriteLine(numbers[2, 0]);
Console.WriteLine(numbers[2, 2]);
C# 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();
}
}
}
1. Berilgan vektorning eng katta va eng kichik elementlarini toping
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[] arr1 = new int[100];
int i, mx, mn, n;
Console.Write("\n\n Vektorning eng katta va eng kichik elementlarini topish dasturi :\n");
Console.Write("--------------------------------------------------\n");
Console.Write("Vektor uzunligini kiriting :");
n = Convert.ToInt32(Console.ReadLine());
Console.Write("Vektorning {0} ta elementlarini kiriting :\n", n);
for (i = 0; i < n; i++)
{
Console.Write("element - {0} : ", i);
arr1[i] = Convert.ToInt32(Console.ReadLine());
}
mx = arr1[0];
mn = arr1[0];
for (i = 1; i < n; i++)
{
if (arr1[i] > mx)
{
mx = arr1[i];
}
if (arr1[i] < mn)
{
mn = arr1[i];
}
}
Console.Write("Maksimum element : {0}\n", mx);
Console.Write("Minimum element : {0}\n\n", mn);
Console.ReadLine();
}
}
}
2. Berilgan matritsaning max va min elementlarini toping
const int x = 3, y = 5;
int min, max;
int[,] arr = new int[x, y] { { 10, 50, 13, 80, 40 }, { 1, 250, 65, 28, 15 }, { 12, 17, 45, 20, 6 } };
min = arr[0, 0];
max = arr[0, 0];
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
if (arr[i, j] > max)
{
max = arr[i, j];
}
if (arr[i, j] < min)
{
min = arr[i, j];
}
}
}
Console.Write("Massiv Elementlari\n");
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
Console.Write(arr[i, j] + " ,");
}
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine("Maximum element:" + max);
Console.WriteLine("Minimum element:" + min);
Console.ReadLine();
3. Vektorning max va min elementlarini C# funksiyalari yordamida toping.
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[] array1 = { 4, 5, 8, 10, 3, 4, 5 };
Console.Write("Vektorning max elementi:__");
Console.WriteLine(array1.Max());
Console.Write("Vektorning min elementi:__");
Console.WriteLine(array1.Min());
Console.ReadLine();
}
}
}
4. Matritsaning max va min elementlari o’rni almashtirilsin.
const int x = 3, y = 5;
int maxindexi = 0, maxindexj = 0,
minindexi = 0, minindexj = 0;
int temp, max, min;
int[,] arr = new int[x, y] { { 10, 50, 13, 80, 40 }, { 1, 250, 65, 28, 15 }, { 12, 17, 45, 20, 6 } };
min = arr[0, 0];
max = arr[0, 0];
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
if (arr[i, j] > arr[maxindexi,maxindexj])
{
maxindexi = i;
maxindexj = j;
max = arr[i, j];
}
if (arr[i, j] < arr[minindexi,minindexj])
{
minindexi = i;
minindexj = j;
min = arr[i, j];
}
}
}
temp = arr[maxindexi, maxindexj];
arr[maxindexi, maxindexj] = arr[minindexi, minindexj];
arr[minindexi, minindexj] = temp;
Console.Write("Massiv Elementlari\n");
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
Console.Write(arr[i, j] + " ,");
}
Console.WriteLine();
}
Console.WriteLine("Maximum element:" + max);
Console.WriteLine("Minimum element:" + min);
Console.ReadLine();
1 . Quyidagi matritsani hosil qiling:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Spiral_Matrix
{
class Spiral
{
static void Main(string[] args)
{
Console.Write("Matritsa o'lchamini kiriting:");
int n = Convert.ToInt32(Console.ReadLine());
int[,] a = new int[n, n];
int printValue = 1;
int c1 = 0, c2 = n - 1;
while (printValue <= n * n)
{
//o'ng yo'nalishdagi harakat
for (int i = c1; i <= c2; i++)
a[c1, i] = printValue++;
//pastga yo'nalatrilgan harakat
for (int j = c1 + 1; j <= c2; j++)
a[j, c2] = printValue++;
//Chap yo'nalishdagi harakat
for (int i = c2 - 1; i >= c1; i--)
a[c2, i] = printValue++;
//Tepaga harakatlanish
for (int j = c2 - 1; j >= c1 + 1; j--)
a[j, c1] = printValue++;
c1++;
c2--;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(a[i, j] + "\t");
}
Console.WriteLine();
}
Console.Read();
}
}
}
2. Teskari spiral matritsa hosil qiling
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Reverse_Spiral_Matrix
{
class Program
{
static void Main(string[] args)
{
int m = 3, n = 6;
int[,] mat = new int[3, 6]
{
{34, 5, 6, 98, 12, 23},
{9, 12, 56, 87, 99, 1},
{13, 91, 50, 8, 21, 2}
};
int[] b = new int[100];
int i, k = 0, l = 0;
int z = 0;
int size = m * n;
while (k < m && l < n)
{
int val;
for (i = l; i < n; ++i)
{
val = mat[k, i];
b[z] = val;
++z;
}
k++;
for (i = k; i < m; ++i)
{
val = mat[i, n - 1];
b[z] = val;
++z;
}
n--;
if (k < m)
{
for (i = n - 1; i >= l; --i)
{
val = mat[m - 1, i];
b[z] = val;
++z;
}
m--;
}
if (l < n)
{
for (i = m - 1; i >= k; --i)
{
val = mat[i, l];
b[z] = val;
++z;
}
l++;
}
}
for (int t = size - 1; t >= 0; --t)
{
Console.WriteLine(b[t]);
}
}
}
}
|
| |