Get up to 80 % extra points for free! More info:

Discussion: Coloring a textbox with contain the maximum value of a two-dimensional array

In the previous quiz, Online C#.NET Quiz, we tested our experience gained from the course.

Activities
Avatar
Maria Rymar
Member
Avatar
Maria Rymar:11/20/2019 5:21

I have a two-dimensional array of random integers that are in an array of textboxes. I found the maximum and brought it to Label1. Everything works. But I still need to colour the textbox in which this maximum is located. I had versions, but unsuccessful. Please help, who knows where the error is?
int max = Convert.ToInt32(t[0, 0].Text);
for (i = 0; i < x; i++)
{

for (j = 0; j < y; j++)
{
if (Convert.ToIn­t32(t[i, j].Text) > max)
{
max = Convert.ToInt32(t[i, j].Text);
t[i, j].BackColor = Color.Red;
label1.Text = "" + max;
}
}
}

 
Reply
11/20/2019 5:21
Avatar
Replies to Maria Rymar
David Capka Hartinger:11/20/2019 8:20

Hi Maria, your code makes sense to me. Which line is causing the error and what's the error message?

Up Reply
11/20/2019 8:20
You can walk through a storm and feel the wind but you know you are not the wind.
Avatar
Maria Rymar
Member
Avatar
Replies to David Capka Hartinger
Maria Rymar:11/20/2019 8:27

Hi! Look, there is no error, but the program works incorrectly. I want to find the Textbox with the maximum value and colour it. But the program colours 3 and more textboxes and i dont now why. I need only one coloured textbox with the max value.

 
Up Reply
11/20/2019 8:27
Avatar
Replies to Maria Rymar
David Capka Hartinger:11/22/2019 11:42

I see :) The problem is you color the TextBox every time you find a new maximal value. It takes multiple guesses to find the maximal value and you can be sure there is no larger value only when both loops finish.

You need to use 2 loops to find the maximal value and then 2 different loops to color every TextBox having this value (coz there can be more of them). Your code should look something like this:

// find the maximum
int max = Convert.ToInt32(t[0, 0].Text);
for (i = 0; i < x; i++)
    for (j = 0; j < y; j++)
        if (Convert.ToInt32(t[i, j].Text) > max)
            max = Convert.ToInt32(t[i, j].Text);

// write the maximum
label1.Text = max.ToString();

// color all cells with the maximum
for (i = 0; i < x; i++)
    for (j = 0; j < y; j++)
        if (Convert.ToInt32(t[i, j].Text) == max)
            t[i, j].BackColor = Color.Red;
Edited 11/22/2019 11:43
Up Reply
11/22/2019 11:42
You can walk through a storm and feel the wind but you know you are not the wind.
Avatar
Jon Cortez
Member
Avatar
Jon Cortez:6/9/2022 7:49

Good morning guys! Our portal careerbooster.com reviews follow all the needed stages to create annotated bibliography and make it complete and credible. Rely on our writing masters and get everything on time without delays, errors, or plagiarism!

 
Up Reply
6/9/2022 7:49
Avatar
sam low
Member
Avatar
sam low:7/17/2022 6:35

In fact, many students experience difficulties in writing a term paper rather not because it is too complicated, but because it is very voluminous. But term papers are written, as a rule, in senior courses. At this time, many students already have a permanent job and do not have a lot of free time. So the service of writing a course company https://essaywriter.org/…ing-services work can help them a lot

 
Up Reply
7/17/2022 6:35
Avatar
Mason
Member
Avatar
Mason:7/29/2022 17:18

Your desire, excitement, and aspiration are heard, go https://www.1depositcasinocanada.com/ . Try your luck and share your emotions. Don't forget who risks wins.

 
Up Reply
7/29/2022 17:18
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

7 messages from 7 displayed.