Возникли сложности при написании программы - C#

Узнай цену своей работы

Формулировка задачи:

Задача: Дана двумерная квадратная матрица. Получить массив b1 …bn , где bi - это сумма элементов, расположенных за первым отрицательным элементом в i-той строке (если все элементы строки неотрицательны, то принять bi=100).
class Program
    {
        static void Matrix(string a, int[,] mas)
        {
            Console.WriteLine(a);
            for (int i = 0; i < mas.GetLength(0); i++)
            {
                for (int j = 0; j < mas.GetLength(1); j++)
                    Console.Write("{0} ", mas[i, j]);
                Console.WriteLine();
            }
        }
        static void Mas_B(string a, int[,] mas)
        {
            int bi = 0;
            for (int i = 0; i < mas.GetLength(0); i++)
            {
                for (int j = 0; j < mas.GetLength(1); j++)
                {
                    if (mas[i, j] < 0) 
                        bi += mas[i, j];
                    Console.WriteLine("bi" + bi);
                    int[,] Bi = new int[i, j];
                    Console.WriteLine(Bi[i,j]);
                }
 
            }
        }
 
        static void Main(string[] args)
        {
            int[,] MyArray ={ {0, -1, 2}, {4, -5, 6}, {7, -8, 9}};
            Matrix("Матрица квадратная: ", MyArray);
            
            Mas_B(" ", MyArray);
        }
    }
}
Программа находит отрицательные элементы массива и выводит их сумму. А как получить массив b1 …bn? Я так думаю, что в него нужно записать отрицательные элементы или я не прав?

Решение задачи: «Возникли сложности при написании программы»

textual
Листинг программы
using System;
 
namespace MatrixTask {
    class Program {    
   
        static int[,] CreateMatrix(int dimension) {
            Random rand = new Random();
            int[,] temp = new int[dimension,dimension];
            for (int i = 0; i < dimension; i++) {
                for (int j = 0; j < dimension; j++) {
                    temp[i, j] = rand.Next(-100, 100); 
                }
            }
            return temp;
        }
 
        static void PrintMatrix(int[,] matrix) {
            for (int i = 0; i < matrix.GetLength(0); i++) {
                for (int j = 0; j < matrix.GetLength(1); j++) {
                    Console.Write(matrix[i,j] + " ");
                }
                Console.WriteLine();
            }
        }
 
        static int[] GetResultArray(int[,] matrix) {
            int[] temp = new int[matrix.GetLength(0)];                       
            for (int i = 0; i < matrix.GetLength(0); i++) {
                int sum = 0;
                bool negElement = false;
                for (int j = 0; j < matrix.GetLength(1); j++) {
                    if (negElement) {
                        sum += matrix[i, j];
                    }
 
                    if (matrix[i,j] < 0) {
                        negElement = true;                        
                    }                    
                }
                if (!negElement) {
                    temp[i] = 100;
                }
                else {
                    temp[i] = sum;
                }
            }
            return temp;
        }
 
 
        static void Main(string[] args) {
            int dim;
            do {
                Console.Write("Input matrix dimension: ");
                int.TryParse(Console.ReadLine(), out dim);
                Console.Clear();
            } while (dim < 2);
 
            int[,] matrix = CreateMatrix(dim);
            PrintMatrix(matrix);
            Console.WriteLine();
            int[] resultArray = GetResultArray(matrix);
            for (int i = 0; i < resultArray.Length; i++) {
                Console.Write(resultArray[i] + " ");
            }
 
            Console.ReadKey(true);
        }
    }    
}

Оцени полезность:

12   голосов , оценка 3.75 из 5
Похожие ответы