using System; namespace ConsoleApp3 { class Program { static void Main(string[] args) { Random r = new Random(); int[] array0 = new int[1]; int[] array1 = new int[1]; int[] array2 = new int[1]; int[] array3 = new int[1]; int[] array4 = new int[1]; int[] array5 = new int[1]; int[] array6 = new int[1]; int[] array7 = new int[1]; int[] array8 = new int[1]; int[] array9 = new int[1]; int index0 = 0; int index1 = 0; int index2 = 0; int index3 = 0; int index4 = 0; int index5 = 0; int index6 = 0; int index7 = 0; int index8 = 0; int index9 = 0; while (index0 < 10 && index1 < 10 && index2 < 10 && index3 < 10 && index4 < 10 && index5 < 10 && index6 < 10 && index7 < 10 && index8 < 10 && index9 < 10) { array0[index0] = r.Next(100); array1[index1] = r.Next(100); array2[index2] = r.Next(100); array3[index3] = r.Next(100); array4[index4] = r.Next(100); array5[index5] = r.Next(100); array6[index6] = r.Next(100); array7[index7] = r.Next(100); array8[index8] = r.Next(100); array9[index9] = r.Next(100); Console.WriteLine(array0[index0]); Console.WriteLine(array1[index1]); Console.WriteLine(array2[index2]); Console.WriteLine(array3[index3]); Console.WriteLine(array4[index4]); Console.WriteLine(array5[index5]); Console.WriteLine(array6[index6]); Console.WriteLine(array7[index7]); Console.WriteLine(array8[index8]); Console.WriteLine(array9[index9] + "\n"); Console.WriteLine("Sum:\n"); Console.WriteLine(array0[index0] + array1[index1] + array2[index2] + array3[index3] + array4[index4] + array5[index5] + array6[index6] + array7[index7] + array8[index8] + array9[index9]); index0++; index1++; index2++; index3++; index4++; index5++; index6++; index7++; index8++; index9++; } Console.ReadKey(); } } } Код using System; namespace ConsoleApp3 { class Program { static void Main(string[] args) { Random r = new Random(); int[] array0 = new int[1]; int[] array1 = new int[1]; int[] array2 = new int[1]; int[] array3 = new int[1]; int[] array4 = new int[1]; int[] array5 = new int[1]; int[] array6 = new int[1]; int[] array7 = new int[1]; int[] array8 = new int[1]; int[] array9 = new int[1]; int index0 = 0; int index1 = 0; int index2 = 0; int index3 = 0; int index4 = 0; int index5 = 0; int index6 = 0; int index7 = 0; int index8 = 0; int index9 = 0; while (index0 < 10 && index1 < 10 && index2 < 10 && index3 < 10 && index4 < 10 && index5 < 10 && index6 < 10 && index7 < 10 && index8 < 10 && index9 < 10) { array0[index0] = r.Next(100); array1[index1] = r.Next(100); array2[index2] = r.Next(100); array3[index3] = r.Next(100); array4[index4] = r.Next(100); array5[index5] = r.Next(100); array6[index6] = r.Next(100); array7[index7] = r.Next(100); array8[index8] = r.Next(100); array9[index9] = r.Next(100); Console.WriteLine(array0[index0]); Console.WriteLine(array1[index1]); Console.WriteLine(array2[index2]); Console.WriteLine(array3[index3]); Console.WriteLine(array4[index4]); Console.WriteLine(array5[index5]); Console.WriteLine(array6[index6]); Console.WriteLine(array7[index7]); Console.WriteLine(array8[index8]); Console.WriteLine(array9[index9] + "\n"); Console.WriteLine("Sum:\n"); Console.WriteLine(array0[index0] + array1[index1] + array2[index2] + array3[index3] + array4[index4] + array5[index5] + array6[index6] + array7[index7] + array8[index8] + array9[index9]); index0++; index1++; index2++; index3++; index4++; index5++; index6++; index7++; index8++; index9++; } Console.ReadKey(); } } } Нужно узнать сумму рандомных чисел из 10 массивов. Помогите сделать так же, но только что бы код был поменьше. Ещё есть вопрос: нужно сделать например на 100 массивов, мне придётся это всё руками писать?) нужно сделать только через While. Прошу помогите. нельзя делать так: int sum = array.Sum(); Console.WriteLine("\nSum = " + sum); Код int sum = array.Sum(); Console.WriteLine("\nSum = " + sum);
"Ещё есть вопрос: нужно сделать например на 100 массивов, мне придётся это всё руками писать?)", а что мешает их через цикл создать? ;D
Сделал уже ) sing System; namespace ConsoleApp6 { class Program { static void Main(string[] args) { Random r = new Random(); int[] array = new int[10]; int index = 0; double sum = 0; Console.WriteLine("Ricxvebi: "); while (index < 10) { array[index] = r.Next(100); sum = sum + array[index]; Console.WriteLine(array[index]); index++; } Console.WriteLine("Jami: " + sum); while (index < 10) { array[index] = r.Next(100); Console.WriteLine(array[index]); index++; } Console.WriteLine("Sashualo: " + (sum / 10)); Console.WriteLine("Maximum da minimum ver gavakete:("); Console.ReadKey(); } } } Код sing System; namespace ConsoleApp6 { class Program { static void Main(string[] args) { Random r = new Random(); int[] array = new int[10]; int index = 0; double sum = 0; Console.WriteLine("Ricxvebi: "); while (index < 10) { array[index] = r.Next(100); sum = sum + array[index]; Console.WriteLine(array[index]); index++; } Console.WriteLine("Jami: " + sum); while (index < 10) { array[index] = r.Next(100); Console.WriteLine(array[index]); index++; } Console.WriteLine("Sashualo: " + (sum / 10)); Console.WriteLine("Maximum da minimum ver gavakete:("); Console.ReadKey(); } } }
mikef0x, Молодец, с циклами покуда вы справились, похвально. Если вы хотите складывать значения то это можно делать i += b; то есть b будет каждый раз суммироваться ( sum = sum + array[index]; ). Не совсем понимаю, в 1 цикле вы выводите 10 рандомных чисел в диапазоне 1 -100, потом выводите их сумму, но так как индекс у вас уже отработал 10 циклов следующий цикл работать не будет и вашу сумму 10 чисел он поделит на 10, то есть выведет среднее. Навряд ли так было задумано, или обнулите Index перед вторым циклом или задайте другую переменную. Ricxvebi: 31 20 49 42 21 59 92 72 93 32 Jami: 511 Sashualo: 51,1 Maximum da minimum ver gavakete:( Код Ricxvebi: 31 20 49 42 21 59 92 72 93 32 Jami: 511 Sashualo: 51,1 Maximum da minimum ver gavakete:( Ps. что за язык ? турецкий ? арабский ? китайский ? переводчик именно такие языки определил ))