0

Hello i've got a easy programming task in C# but im stuck on the last part. I need to make 2 arrays that have random numbers (3x3) and on the end i need to sum them up in one number. So its: array1 with 3 numbers + array2 with 3 numbers = array3 with all the array numbers addition. Can somebody please help me? My code is:

        static void Main(string[] args)
        {
            int[,] pole1 = new int[3, 3];
            int[,] pole2 = new int[3, 3];
            int[,] pole3 = new int[3, 3];
            Random random1 = new Random();
            Console.WriteLine("Pole 1 je: ");
            for (int a = 0; a <= 2; a++)
            {
                for (int b = 0; b <= 2; b++)
                {
                    pole1[a, b] = random1.Next(1, 9);
                    Console.Write(pole1[a, b]);
                }
                Console.WriteLine();
            }
            Console.WriteLine("Pole 2 je: ");
            for (int a = 0; a <= 2; a++)
            {
                for (int b = 0; b <= 2; b++)
                {
                    pole2[a, b] = random1.Next(1, 9);
                    Console.Write(pole2[a, b]);
                }
                Console.WriteLine();
            }
            Console.WriteLine("Součet polí je: ");
            for (int a = 0; a <= 2; a++)
            {
                for (int b = 0; b <= 2; b++)
                {
                    pole3[a, b] = (pole1[a, b] + pole2[a, b]);
                    Console.Write(pole3[a, b]);
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }

I've got the two arrays but idk how to sum them up.

4
  • It looks right. Are you getting wrong results? Commented Oct 13, 2020 at 8:47
  • When i do the addition it sums the numbers one by one but i need to sum them up whole like array1 = 586 array2 = 650 array3 = 1 236 but what it does it sums up some numbers and some not. Like i get bad results Commented Oct 13, 2020 at 8:52
  • Wait, to reach 1236, you're adding each array as a single number. As in, if the first array is [9,9,9] and the second is [0,0,1], you want the output to be 1,0,0,0? Commented Oct 13, 2020 at 8:56
  • Does every number in pole1 and pole2 contain only 1 digit? Commented Oct 13, 2020 at 9:00

2 Answers 2

1

I think your code is working properly

enter image description here

Console.Write(pole1[a, b] + "\t");    // Add "\t"
Console.Write(pole2[a, b] + "\t");    // Add "\t"
Console.Write(pole3[a, b] + "\t");    // Add "\t"

UPDATE : if you want like bellow

Array 1 : 373
Array 2 : 176
Result  : 549

Last loop will be

for (int a = 0; a <= 2; a++)
{
    int sum = (pole1[a, 0]  + pole2[a, 0]) * 100 + (pole1[a, 1] + pole2[a, 1]) * 10 + (pole1[a, 2] + pole2[a, 2]) ;
    pole3[a, 0] = sum / 100;
    pole3[a, 1] = (sum - pole3[a, 0] * 100) / 10;
    pole3[a, 2] = sum - (pole3[a, 0] * 100) - (pole3[a, 1] * 10);
    Console.Write($"{pole3[a, 0]}" + "\t" + $"{pole3[a, 1]}" + "\t" + $"{pole3[a, 2]}");

    Console.WriteLine();
}

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

I don't think so, see the comment above.
Yes this is exactly the problem its adding up the numbers one by one but i need to add up the whole number like this picture i made: https://i.imgur.com/BNFhZUv.png
The answer VietDD gave in the updated loop worked for me. Thanks
0

The Sum() method in Linq is a good goto if you want to sum up an array, however multi-dimensional arrays do not implement IEnumerable<T> by default, which can cause a few bumps in the road. Luckily Linq provides us a way to specify the type using the Cast<T> method like so:

int total = pole1.Cast<int>().Sum() + pole2.Cast<int>().Sum();

Note that I've replaced <T> with <int> as we are using generics (read more on MSDN).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.