3 Replies - 618 Views - Last Post: 08 January 2010 - 03:42 PM Rate Topic: -----

#1 kdogg727  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-December 08

2D Array Problem

Posted 08 January 2010 - 08:18 AM

I have the sum of the rows displaying, but I'm trying to figure out how t display the sum of each column now.

namespace _DArrayProject
{
	class Program
	{
		static void Main(string[] args)
		{
			int[,] numbers = { { 13, 25, 64, 100, 12, 7 }, { 5, 28, 20, 34, 16, 46 }, { 7, 13, 6, 12, 42, 35 }, { 21, 32, 18, 17, 76, 42 }, { 100, 6, 7, 8, 17, 12 } };
			

			Console.WriteLine("Sum of Array");
			Console.WriteLine();

			for (int row = 0; row < 5; row++)
			{
				int sum = 0;
				for (int column = 0; column < 6; column++)
				{
					
					Console.Write(numbers[row, column] + "\t" );
					sum += numbers[row,column];
					
				}
				Console.Write("\t" + sum);
				Console.WriteLine();
			}
			Console.Read();

This post has been edited by kdogg727: 08 January 2010 - 08:34 AM


Is This A Good Question/Topic? 0
  • +

Replies To: 2D Array Problem

#2 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: 2D Array Problem

Posted 08 January 2010 - 08:31 AM

Would love to try and help out, but could you please put your code like so :code: just to give me a fighting chance at understanding it properly. Also you are supposed to put your question in the body of the post not in the title or header. Just re-iterate it with slight clarification as to what the actual problem is, or the error, or whatever the real problem is.
Was This Post Helpful? 0
  • +
  • -

#3 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: 2D Array Problem

Posted 08 January 2010 - 11:34 AM


			for (int i = 0; i < 6; i++) {
				int colSum = 0;
				for (int j = 0; j < 5; j++) {
					colSum += numbers[j, i];
				}
				Console.Write( colSum + "\t" );
				
			}
			Console.WriteLine();



just reverse the rows and cols essentially

This post has been edited by SwiftStriker00: 08 January 2010 - 11:36 AM

Was This Post Helpful? 0
  • +
  • -

#4 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: 2D Array Problem

Posted 08 January 2010 - 03:42 PM

namespace _DArrayProject
{

    class Program
    {

        static void Main(string[] args)
        {

            int[,] numbers = { { 13, 25, 64, 100, 12, 7 }, { 5, 28, 20, 34, 16, 46 }, { 7, 13, 6, 12, 42, 35 }, { 21, 32, 18, 17, 76, 42 }, { 100, 6, 7, 8, 17, 12 } };
           
            Console.WriteLine("Sum of Array");
            Console.WriteLine();

            for (int column = 0; column < 5; column++)
            {

                int sum = 0;
                for (int row = 0; row < 6; row++)
                {
                   
                    Console.Write(numbers[row, column] + "\t" );
                    sum += numbers[row,column];
                   
                } // for (int row = 0; row < 6; row++)

                Console.Write("\t" + sum);
                Console.WriteLine();

            } // for (int column = 0; column < 5; column++) 

            Console.Read();

        } // static void Main(string[] args)
    } // class Program
} // namespace _DArrayProject



Like SwiftStiker00 said you just had the columns and rows reversed. Hope this helps out. Just posted with your variables for clarification purposes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1