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
{
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();
}
}
}
14 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Azm AmnPosted Aug 6, 2014, 7:07 AM
VulpesPosted Aug 6, 2014, 6:02 AM
However, a slightly easier way which doesn't involve as much disruption to the program would be to cache the first two values of x[0, 0, 0] to individual variables (v and w say) before they're overwritten.
At the end of the program, you can then check whether these cached values are smaller or larger than the other values and, if they are, adjust the output accordingly. Here's the code to do that:
Azm AmnPosted Aug 5, 2014, 11:13 PM
Azm AmnPosted Aug 5, 2014, 11:08 PM
VulpesPosted Aug 5, 2014, 8:04 PM
Anyway, as I said in my previous post, the bug is that x[0, 0, 0] is being set three times and therefore has the final value it's set to. The other two values are just overwritten and so don't figure in the minimum and maximum calculations.
To fix it, you need to avoid the overwriting which means that 'c' and 'b' both need to start iterating from 1, not 0. This also means that you'll need to ask the user for 3, 3 and 5 numbers rather than 3, 4 and 6.
I've made the highlighted changes to the program which now seems to be running OK:
Azm AmnPosted Aug 5, 2014, 8:19 AM
Azm AmnPosted Aug 4, 2014, 11:22 PM
VulpesPosted Aug 4, 2014, 10:19 AM
Azm AmnPosted Aug 4, 2014, 7:59 AM
Azm AmnPosted Aug 4, 2014, 7:57 AM
Actually the program which i put is what my lecturer gave me.
what he needs is that he wants me to find the error regardless of the question. He is saying to find the bug in the program and not to worry about the solution. When you run the program it would give second lowest number. It gives the second largest number when you enter the lowest value after it says enter 3 values and enter 4 values. But if you enter lowest number inside the enter 6 values it shows the correct answer. When i asked my lecturer he told there is a problem with the logical operators in the last part. Please help me
VulpesPosted Aug 3, 2014, 5:37 AM
Azm AmnPosted Aug 3, 2014, 12:54 AM
Azm AmnPosted Aug 3, 2014, 12:51 AM
VulpesPosted Aug 2, 2014, 10:02 AM
A 3 by 4 by 6 array contains 72 elements whereas you're asking the user to input 13.
Also the question asks you to randomly fill the array rather than ask the user to input them which wouldn't be very practical for an array of this size.
So I'd generate 72 random numbers between 0 and 99 (say), print them out and then print the largest and smallest ones. The code to do this is quite compact: