Home > Mobile >  Creating a new one-dimensional array from all positive elements of each row of a two-dimensional dyn
Creating a new one-dimensional array from all positive elements of each row of a two-dimensional dyn

Time:11-27

I am a newbie programmer, so there are some problems. The program displays only 3 positive elements in a one-dimensional array, although there may be many more in a two-dimensional array.

here is my code

using System;

namespace task_2
{
    class arrays
    {
        public int[,] A = new int[3, 3];
        public int[] B = new int[9];
        public void two_dimensional_array()
        {
            Random rand = new Random();
            for (int i = 0; i < 3; i  )
            {
                for (int j = 0; j < 3; j  )
                {
                    A[i, j] = rand.Next(-100, 100);
                }
            }
            Console.WriteLine("Two-dimensional array: ");
            for (int i = 0; i < 3; i  )
            {
                for (int j = 0; j < 3; j  )
                {
                    Console.Write("{0}\t", A[i, j]);
                }
                Console.WriteLine();
            }
        }
        public void one_dimensional_array()
        {
            Console.WriteLine("\nA one-dimensional array with only positive elements: ");
            for (int i = 0; i < 3; i  )
            {
                for (int j = 0; j < 3; j  )
                {
                        if (A[i, j] > 0)
                            B[i] = A[i, j];
                }

            }
            for (int i = 0; i < 9; i  )
            {
                Console.WriteLine(B[i]);
            }
        }

    }
    class Program
    {
        static void Main()
        {
            Console.OutputEncoding = System.Text.Encoding.Default;
            arrays a;
            a = new arrays();
            a.two_dimensional_array();
            a.one_dimensional_array();
        }
    }
}

I have attached a photo of the result below, where only three positive elements are displayed: enter image description here

CodePudding user response:

In your method one_dimensional_array(), you use a nested for loop to check all items in A and conditionally update items in B:

for (int i = 0; i < 3; i  )
{
    for (int j = 0; j < 3; j  )
    {
        if (A[i, j] > 0)
            B[i] = A[i, j];
    }
}

Using i and j to iterate over the items of A work as you expect: You loop through three rows and three columns.

The way you are using i to iterate over the indices of B prevents you from iterating over all of B's indices. You are actually only looping over index 0, 1 and 2 (as per your instructions of i's possible range in the outer for loop).

In your nested loop, you will hit the if (A[i, j] > 0) line nine times. The values of i, j, A[i, j] and B[i] will change as follows:

|  i  |  j  |  A[i, j]  |  B[i]  |
|-----|-----|-----------|--------|
|  0  |  0  |  A[0, 0]  |  B[0]  |
|  0  |  1  |  A[0, 1]  |  B[0]  |
|  0  |  2  |  A[0, 2]  |  B[0]  |
|-----|-----|-----------|--------|
|  1  |  0  |  A[1, 0]  |  B[1]  |
|  1  |  1  |  A[1, 1]  |  B[1]  |
|  1  |  2  |  A[1, 2]  |  B[1]  |
|-----|-----|-----------|--------|
|  2  |  0  |  A[2, 0]  |  B[2]  |
|  2  |  1  |  A[2, 1]  |  B[2]  |
|  2  |  2  |  A[2, 2]  |  B[2]  |

As a result,

  • B[0] is assigned the last positive value in the first row in A (if any value in that row is positive)
  • B[1] is assigned the last positive value in the second row in A (if any value in that row is positive)
  • B[2] is assigned the last positive value in the third row in A (if any value in that row is positive)

So, you need a way to iterate over all of B's indices (i.e. 0--8).

It is possible to calculate such a parameter based on the already existing i and j values:

var k = f(i, j)

which can be used as follows:

B[k] = A[i, j];

Can you figure out the expression for k?

CodePudding user response:

edit this line to: (If you want zero to be displayed instead of negative numbers)

        //B[i] = A[i, j];
        B[i*3 j] = A[i, j];

If you want negative numbers not to be displayed:

        int index=0;
        for (int i = 0; i < 3; i  )
        {
            for (int j = 0; j < 3; j  )
            {
                    if (A[i, j] > 0)
                        B[index  ] = A[i, j];
            }

        }

        for (int i = 0; i < index; i  )
        {
            Console.WriteLine(B[i]);
        }
  •  Tags:  
  • c#
  • Related