Sunday, September 3, 2017

4. Find the sum of all the elements present in a jagged array of 3 inner arrays.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace lab4
{
    public class prgm4
    {
        public static void Main()
        {
            int[][] jag = new int[3][];
            jag[0] = new int[2];
            jag[1] = new int[4];
            jag[2] = new int[3];
            int[] sum = new int[3];
            int result = 0;
            int i, j;
            for (i = 0; i < 3; i++)
                sum[i] = 0;
            Console.WriteLine("Enter the size of the array ");
            for (i = 0; i < jag.Length; i++)
            {
                Console.WriteLine("Enter the values off array " + (i + 1) + " jag:");
                for (j = 0; j < jag[i].Length; j++)
                {
                    jag[i][j] = int.Parse(Console.ReadLine());
                }
            }
            for (i = 0; i < jag.Length; i++)
            {
                for (j = 0; j < jag[i].Length; j++)
                {
                    sum[i] = sum[i] + jag[i][j];
                }
            }
            for (i = 0; i < 3; i++)
                result = result + sum[i];
            Console.WriteLine("The result of individual jags row-wise: ");
            for (i = 0; i < 3; i++)
                Console.Write(sum[i] + "\t");
            Console.WriteLine();
            Console.WriteLine("The final sum of all the elements in the jagged array:" + result);
            Console.Read();
        }
    }
}

No comments:

Post a Comment