Listing 8.3. Bir o‘lchamli Massivning 2 yoki undan ortiq akrorlanuvchi elementlarini sonini aniqlang va ularni boshqa songa almashtirish.
Listing 8.4. Misol: Ikki o'lchovli massivda bir hil bolgan nechta elementlari bolsa sonini toping va ularni massiv tarkibida yoq bolgan sonlar bilan almashtiring
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Zadacha_massiv2 { class Program { /*2 o‘lchamli massivlar uchun IndexOf() funksiyasi ishlamaganligi sababli, ushbu funksiyani o‘zimiz yaratib olamiz.*/ //******************************************************************************** public static int IndexOF(Array A, int b) { int s= -1; for (int i = 0; i < A.GetLength(0); i++) { for (int j = 0; j < A.GetLength(1); j++) {/*Massivdan funksiya parametri sifatida foydalanilganda uning elementiga to‘g‘ridan to‘g‘ri murojaat qilib bo‘lmaydi, unga murojaat qilish uchun GetValue va SetValue lardan foydalaniladi */ if (b == Convert.ToInt32(A.GetValue(i, j))) { s=1; break; } } } return s; } //******************************************************************************** static void Main(string[] args) { Console.WriteLine("Massiv o'lchamini kiriting:"); Console.Write("m="); int m = int.Parse(Console.ReadLine()); Console.Write("n="); int n = int.Parse(Console.ReadLine()); int[,] AA = new int[m,n]; int[,] T = new int[m,n]; Array.Clear(T,0,T.Length); Random rnd=new Random(); int rnn=m*n+1; Console.WriteLine("Massivning boshlang'ich holati:"); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { AA[i,j]=rnd.Next(rnn); Console.Write("AA[{0},{1}]={2}\t",i,j,AA[i,j]); } Console.Write("\n"); } Array.Copy(AA, T, m * n); int s = 0; int tmp = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (tmp == T[i, j]) s++; if (s > 1) { l1: int a = rnd.Next(rnn); if (IndexOF(AA, a) != -1) goto l1; else AA[i, j] = a; } } } if (s!=0) Console.WriteLine("AA massivda {0} ga teng elementdan {1} ta", tmp, s); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { s = 0; tmp = T[i, j]; if (tmp == 0) continue; for(int k=0; k { for(int t=0;t { if (tmp == T[k, t]) { s++; if (s > 1) { T[k, t] = 0; l2: int a = rnd.Next(rnn); if (IndexOF(AA, a)!=-1) goto l2; else AA[k, t] = a; } } } } Console.WriteLine("AA massivda {0} ga teng elementdan {1} ta", tmp, s); } } Console.WriteLine("\nMassiv ozgartirilgandan keyingi holat"); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { Console.Write("AA[{0},{1}]={2}\t", i, j, AA[i, j]); } Console.Write("\n"); } Console.ReadKey(); } } }