Invalid return, unknown reason [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Trying to solve this kata (actually Codewars task) , but unfortunately it returns invalid output
Rgb(255, 255, 255) # returns FFFFFF
Rgb(255, 255, 300) # returns FFFFFF
Rgb(0,0,0) # returns 000000
Rgb(148, 0, 211) # returns 9400D3
What I wrote is
using System;
public class Kata
{
public static string Rgb(int r, int g, int b)
{
return String.Format("{0:X2}{1:X2}{2:X2}", r, g, b);
}
}
Output ( Codewars test output )
Expected string length 6 but was 7. Strings differ at index 4.
Expected: "FFFFFF"
But was: "FFFF12C"
Any advices ?

The sample input you provide is wrong. If you run the following line:
Rgb(255, 255, 300) # returns FFFFFF
You'll see your function really returns FFFF12C
What the examples are hinting at is the largest value you should accept is 255, any value above that should be treated as if it's 255.
If you change your function to do just that:
public static string Rgb(int r, int g, int b)
{
return String.Format("{0:X2}{1:X2}{2:X2}", Math.Min(r, 255), Math.Min(g, 255), Math.Min(b, 255));
}
It will now return FFFFFF for the sample data, and treat other values correctly as well.

The data type should be a byte, and 300 is not valid.
using System;
public class Kata
{
public static string Rgb(byte r, byte g, byte b)
{
return String.Format("{0:X2}{1:X2}{2:X2}", r, g, b);
}
}

Related

Math.Round with Mid point Rounding [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
This sample code is producing unexpected result
decimal s = 463.815M;
decimal a = Math.Round(s, 2, MidpointRounding.AwayFromZero);
decimal b = Math.Round(s, 2, MidpointRounding.ToEven);
decimal t = 4.685M;
decimal c = Math.Round(t, 2, MidpointRounding.AwayFromZero);
decimal d = Math.Round(t, 2, MidpointRounding.ToEven);
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
Console.WriteLine(d);
Console.Read();
It produces
463.82
463.82
4.69
4.68
I was expecting a and c to have incremented by 1 which c did but to my surprise a didn't. Can anyone explain the reason for this please?
[update]
a and c are expected to have same results as:
a has .815 and c also has .685 i.e. 5 at the end.
a and c both are using MidpointRounding.AwayFromZero
This is the expected result, because 0.815 fraction is rounded up to 0.82. The same exact thing happens when you round to even, because 2 is even.
The result would be different if you used 0.825 as a fraction:
decimal s = 463.825M;
decimal a = Math.Round(s, 2, MidpointRounding.AwayFromZero);
decimal b = Math.Round(s, 2, MidpointRounding.ToEven);
Now the code prints
463.83
463.82
to illustrate the difference between MidpointRounding.AwayFromZero and MidpointRounding.ToEven.

c#, getting pixel information from Bitmap image not giving required result [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I want to get any image, convert it to a bitmap, resize it to (1024,16) then
get the RGB value of each pixel.
This is my function to resize
public Bitmap Resize(bitmap image1)
{
Bitmap image2 = new Bitmap(16, 1024);
Graphics gr = Graphics.FromImage(image2);
gr.DrawImage(image1,0,0,image2.Height,image2.Width);
return image2;
}
Then this is my function to get the RGB value of each pixel and write it to a text file
for (int y = 0; y < image2.Height; y++)
{
for (int x = 0; x < image2.Width; x++)
{
Color pixelcolor = image2.GetPixel(x,y);
byte weR = pixelcolor.R;
byte weG = pixelcolor.G;
byte weB = pixelcolor.B;
sR.WriteLine(weR.ToString());
sR.WriteLine(weG.ToString());
sR.WriteLine(weB.ToString());
sR.WriteLine();
}
}
But the problem is that after looping 256 times the RGB value turn 0 and does not change again for the rest if the pixel.
That is, it gives the correct RGB value for the image 256 times then everything turns 0.
This happen for any image of any color.
please what is wrong with my code?
According to https://msdn.microsoft.com/en-us/library/dbsak4dc(v=vs.110).aspx you should go with:
gr.DrawImage(image1,0,0,image2.Width,image2.Height);
not
gr.DrawImage(image1,0,0,image2.Height,image2.Width);

ArgumentOutOfRange Error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm trying to code a file copy program with progressbar but It crashes at percentage computing.Here is my code:
private void operation()
{
byte[] buffer = new byte[1024 * 1024];
using (streamSFD)
{
using (Stream streamOFD = new FileStream(textBox1.Text, FileMode.Open))
{
while (streamOFD.Read(buffer, 0, buffer.Length) > 0)
{
streamSFD.Write(buffer, 0, buffer.Length);
progressBar1.Value = percentage((int)streamOFD.Length, (int)streamSFD.Length);
label2.Text = streamSFD.Length.ToString() + "/" + streamOFD.Length.ToString();
label1.Text = progressBar1.Value.ToString() + "%";
}
}
}
}
private int percentage(int x, int y)
{
return (y * 100) / x;
}
From the comments to the question it became clear, that the percentage method returned negative values. How can this happen?
Note that the property Stream.Length is a long, and so is Stream.Position (which is perhaps also a property you want to use for calculating the progress percentage).
Casting a long (width of 64 bit) to an int (width of 32 bit) will truncate the long by just taking the lower 32 bits into account.
Imagine you have the following positive long value:
0x1FFFFFF00 (hex) = 8589934336 (decimal)
Casting this to int will discard the upper 32 bits, resulting in:
0xFFFFFF00 (hex) = -256 (decimal)
(In most CPU/ALU architectures, negative integer numbers are commonly represented by the two's complement of their absolute value.)
The key to fix the problem is to not truncate the long values. Since the resulting percentage value should be an int value in the range of 0...100 (it is a percentage for a progress bar), Math.Min will be utilized to cap any calculation results greater than 100 (although, for the cod given in the question the calculation result should not exceed 100).
private int percentage(long x, long y)
{
return (int) Math.Min( (y * 100) / x, 100L );
}
Now, casting Stream.Length to an int is not necessary anymore:
progressBar1.Value = percentage(streamOFD.Length, streamSFD.Length);

C# Method Calculate [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am trying to calculate the perimeter of a circle using method
for some reason I get an error at:
//double p = 2 * Math.PI * r;
I am new to using method please help and show me what I did wrong.
static void Main(string[] args)
{
double perimeter;
Console.Write("Enter Perimeter: ");
double.TryParse(Console.ReadLine(), out perimeter);
double per = PerimeterOfCircle(perimeter);
Console.WriteLine("\nPerimeter of Circle = {0}",
per.ToString("F3"));
Console.ReadKey();
}
static double PerimeterOfCircle(double p)
{
double p = 2 * Math.PI * r;
return p;
}
Looks like you have the parameter named incorrectly. Change it to r:
static double PerimeterOfCircle(double r) // <-- changed from p to r here
{
double p = 2 * Math.PI * r;
return p;
}
Also you can embed the format string within WriteLine:
Console.WriteLine("\nPerimeter of Circle = {0:F3}", per);
Simple: You did not provide a value for r. The code does not even compile.

What is the color white returned from the GetPixel() method? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What is the RBG value of the color white returned from the GetPixel() method?
Is it (255, 255, 255) or (0, 0, 0)?
White = 255 255 255
Black = 0 0 0
There's an easy code to check it
Byte r = Color.White.R; // r = 255
Byte g = Color.White.G; // g = 255
Byte b = Color.White.B; // b = 255

Categories