In: Computer Science
Provide code samples for the following in C#
a. Declare a two-dimensional array of integers names intArray17.
b. Create a loop to calculate the sum of every element in the first column.
c. Create a loop to calculate the sum of every element in the array.
The code is commented as required. Variables are named accordingly.
I have made the code in online ide.
Change the name of namespace accordingly to make it run in your ide.
Code:
//Rextester.Program.Main is the entry point for your code. Don't
change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework
4.5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[]
args)
{
//Your code goes here
//(a)
int[,] intArray17 = new int[3,2]{{1,2},{3,4},{5,6}}; //Declaring 2d
array of size 3*2
//(b)
int sum_first_column=0;//Initializing sum of first column with
0
for(int i=0;i<3;i++){
sum_first_column+=intArray17[i,0];//Adding ith element of first column to required sum
}
Console.WriteLine("The sum of every elemnet in first column is:
"+sum_first_column);
//(c)
int sum=0;//Initializing sum of first column with 0
for(int i=0;i<3;i++){
for(int j=0;j<2;j++)
sum+=intArray17[i,j];//Adding (i,j) element to required sum
}
Console.WriteLine("The sum of every elemnet is: "+sum);
}
}
}
Code screenshot and output: