0% found this document useful (0 votes)
90 views

Sum The Values On A Diagonal of A 3x3x3 Matrix

The document contains code examples demonstrating the use of arrays in C#, including initializing arrays, calculating averages, using multidimensional arrays, accessing values using indexes and loops, assigning array references, using the Length property, reversing arrays, jagged arrays, and using the foreach loop to iterate over arrays.

Uploaded by

sanyam9999
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

Sum The Values On A Diagonal of A 3x3x3 Matrix

The document contains code examples demonstrating the use of arrays in C#, including initializing arrays, calculating averages, using multidimensional arrays, accessing values using indexes and loops, assigning array references, using the Length property, reversing arrays, jagged arrays, and using the foreach loop to iterate over arrays.

Uploaded by

sanyam9999
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

// Compute the average of a set of values.

using System;
class Average {
static void Main() {
int[] nums = new int[10];
int avg = 0;
nums[0] = 99;
nums[1] = 10;
nums[2] = 100;
nums[3] = 18;
nums[4] = 78;
nums[5] = 23;
nums[6] = 63;
nums[7] = 9;
nums[8] = 87;
nums[9] = 49;
for(int i=0; i < 10; i++)
avg = avg + nums[i];
avg = avg / 10;
Console.WriteLine("Average: " + avg);
}
}

int[] nums = { 99, 10, 100, 18, 78, 23,63, 9, 87, 49 };

int[] nums;
nums = new int[] { 99, 10, 100, 18, 78, 23,63, 9, 87, 49 };

// Demonstrate a two-dimensional array.


using System;
class TwoD {
static void Main() {
int t, i;
int[,] table = new int[3, 4];
for(t=0; t < 3; ++t) {
for(i=0; i < 4; ++i) {
table[t,i] = (t*4)+i+1;
Console.Write(table[t,i] + " ");
}
Console.WriteLine();
}
}
}
// Sum the values on a diagonal of a 3x3x3 matrix.
using System;
class ThreeDMatrix {
static void Main() {
int[,,] m = new int[3, 3, 3];
int sum = 0;
int n = 1;
for(int x=0; x < 3; x++)
for(int y=0; y < 3; y++)
for(int z=0; z < 3; z++)
m[x, y, z] = n++;
sum = m[0, 0, 0] + m[1, 1, 1] + m[2, 2, 2];
Console.WriteLine("Sum of first diagonal: " + sum);
}
}

// Assigning array reference variables.


using System;
class AssignARef {
static void Main() {
int i;
int[] nums1 = new int[10];
int[] nums2 = new int[10];
for(i=0; i < 10; i++) nums1[i] = i;
for(i=0; i < 10; i++) nums2[i] = -i;
Console.Write("Here is nums1: ");
for(i=0; i < 10; i++)
Console.Write(nums1[i] + " ");
Console.WriteLine();
Console.Write("Here is nums2: ");
for(i=0; i < 10; i++)
Console.Write(nums2[i] + " ");
Console.WriteLine();
nums2 = nums1; // now nums2 refers to nums1
Console.Write("Here is nums2 after assignment: ");
for(i=0; i < 10; i++)
Console.Write(nums2[i] + " ");
Console.WriteLine();
}
}

// Use the Length array property.


using System;
class LengthDemo
{
static void Main()
{
int[] nums = new int[10];
Console.WriteLine("Length of nums is " + nums.Length);
// Use Length to initialize nums.
for(int i=0; i < nums.Length; i++)
nums[i] = i * i;
// Now use Length to display nums.
Console.Write("Here is nums: ");
for(int i=0; i < nums.Length; i++)
Console.Write(nums[i] + " ");
Console.WriteLine();
}
}

// Reverse an array.
using System;
class RevCopy {
static void Main() {
int i,j;
int[] nums1 = new int[10];
int[] nums2 = new int[10];

for(i=0; i < nums1.Length; i++)


nums1[i] = i;

Console.Write("Original contents: ");


for(i=0; i < nums2.Length; i++)
Console.Write(nums1[i] + " ");

Console.WriteLine();

// Reverse copy nums1 to nums2.


if(nums2.Length >= nums1.Length)
for(i=0, j=nums1.Length-1; i < nums1.Length; i++, j--)
nums2[j] = nums1[i];
Console.Write("Reversed contents: ");
for(i=0; i < nums2.Length; i++)
Console.Write(nums2[i] + " ");
Console.WriteLine();
}
}

// Demonstrate Length with jagged arrays.


using System;
class Jagged
{
static void Main()
{
int[][] network_nodes = new int[4][];
network_nodes[0] = new int[3];
network_nodes[1] = new int[7];
network_nodes[2] = new int[2];
network_nodes[3] = new int[5];
int i, j;

// Fabricate some fake CPU usage data.


for(i=0; i < network_nodes.Length; i++)
for(j=0; j < network_nodes[i].Length; j++)
network_nodes[i][j] = i * j + 70;

Console.WriteLine("Total number of network nodes: " +


network_nodes.Length + "\n");

for(i=0; i < network_nodes.Length; i++) {


for(j=0; j < network_nodes[i].Length; j++) {
Console.Write("CPU usage at node " + i +" CPU " + j + ": ");
Console.Write(network_nodes[i][j] + "% ");
Console.WriteLine();
}
Console.WriteLine();
}
}
}

using System;
class Jagged {
static void Main() {
var jagged = new[] {
new[] { 1, 2, 3, 4 },
new[] { 9, 8, 7 },
new[] { 11, 12, 13, 14, 15 }
};
for(int j = 0; j < jagged.Length; j++) {
for(int i=0; i < jagged[j].Length; i++)
Console.Write(jagged[j][i] + " ");
Console.WriteLine();
}
}
}
The program produces the following output:
1 2 3 4
9 8 7
11 12 13 14 15

// Use the foreach loop.


using System;
class ForeachDemo
{
static void Main()
{
int sum = 0;
int[] nums = new int[10];
// Give nums some values.
for(int i = 0; i < 10; i++)
nums[i] = i;

// Use foreach to display and sum the values.


foreach(int x in nums) {
Console.WriteLine("Value is: " + x);
sum += x;
}
Console.WriteLine("Summation: " + sum);
}
}

// Use foreach on a two-dimensional array.


using System;
class ForeachDemo2 {
static void Main() {
int sum = 0;
int[,] nums = new int[3,5];

// Give nums some values.


for(int i = 0; i < 3; i++)
for(int j=0; j < 5; j++)
nums[i,j] = (i+1)*(j+1);
// Use foreach to display and sum the values.
foreach(int x in nums) {
Console.WriteLine("Value is: " + x);
sum += x;
}
Console.WriteLine("Summation: " + sum);
}
}

You might also like