Azm Amn

Azm Amn

  • NA
  • 108
  • 17.8k

C# program answer needed immediately pls help

Aug 2 2014 8:17 AM
Question 

1. Write a program that randomly fills a 3 by 4 by 6 array, then prints the largest and smallest values in the array.

I have my code below please help me solve. The problem is when i run it shows me the 2nd smallest number. There is a bug in it. please help me solve it.
 
namespace _3_dimensional_array
{
class Program
{
static void Main(string[] args)
{
int[, ,] x = new int[3, 4, 6]; // declaration of multi-dimensional array
int i, c, b;
int k = 0, l = 0, m = 0, n = 0, o = 0, s = 0; // to use in input for loop
int t, y, u; // to find a maximum loop
Console.WriteLine("Enter 3 values :");
for (i = 0; i < 3; i++)
{
x[i, k, l] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Enter 4 values :");
for (c = 0; c < 4; c++)
{
x[m, c, n] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Enter 6 values :");
for (b = 0; b < 6; b++)
{
x[s, o, b] = Convert.ToInt32(Console.ReadLine());
}
// loops for input
t = x[0, k, l];
y = x[m, 0, n];
u = x[s, o, 0];
for (int q = 1; q < 3; q++)
{
if (t < x[q, k, l])
{
t = x[q, k, l];
}
}
for (int p = 1; p < 4; p++)
{
if (y < x[m, p, n])
{
y = x[m, p, n];
}
}
for (int h = 1; h < 6; h++)
{
if (u < x[s, o, h])
{
u = x[s, o, h];
}
}
// loops for maximum value
int yu; // to assing maximum value
if ((t > y) && (t > u))
{
yu = t;
}
else if ((y > t) && (y > u))
{
yu = y;
}
else
{
yu = u;
}
// to find maximum value
Console.WriteLine("The maximum value is {0}.", yu);
int sd, af, qw; // use to find minimum value
sd = x[0, k, l];
af = x[m, 0, n];
qw = x[s, o, 0];
for (int a = 0; a < 3; a++)
{
if (sd > x[a, k, l])
{
sd = x[a, k, l];
}
}
for (int f = 0; f < 4; f++)
{
if (af > x[m, f, n])
{
af = x[m, f, n];
}
}
for (int j = 0; j < 6; j++)
{
if (qw > x[s, o, j])
{
qw = x[s, o, j];
}
}
// for loop for minimum value
int mu; // it is assigned to minimum value
if ((sd<qw) && (sd<af))
{
mu = sd;
}
else if (sd > af && qw > af)
{
mu = af;
}
else
{
mu = qw;
}
// used to find minimum value
Console.WriteLine("The minimum value {0}.", mu);
// holding output screen
Console.ReadLine();
}
}
}
 

Answers (14)