Error when trying decompress byte array ( C#) - c#

I am trying to decompress byte array into a bitmap. but getting error.
This my coding....when execute it produce the error that "Index was outside the bounds of the array."
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
private static byte[] minbuffer;
static void Main(string[] args)
{
byte[] buffer = new byte[] { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x83,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x1B,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0x7B,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC,0xF9,0xFF,0xFF,0xFF,0xFF,0xFF,0xBF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF9,0xF9,0xFF,0xFF,0xFF,0xFF,0xFF,0x87,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF9,0xF9,0xFF,0xFF,0xFF,0xFF,0xFF,0xC3,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFB,0xF9,0xFF,0xFF,0xFF,0xFF,0xFF,0xF0,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC3,0xF9,0xFF,0xFF,0xFF,0xFF,0xFF,0xF8,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF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0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC6,0x1F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x90,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0x03,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF0,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xE3,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF };
buffer = decompress(buffer, 168, 44);
Console.ReadLine();
}
private static byte[] decompress(byte[] raw, int width, int height)
{
byte[] signdecompress = new byte[width * height];
for (int h = 0; h < height; h++)
{
for (int w = 0; w < (width / 8); w++)
{
byte num = raw[h * (width / 8) + w];
int ByteIndex = (h * width + w * 8);
string str = Convert.ToString(num);
for (byte s = 0; s < 8; s++)
{
if (str[s] == '1')
{
signdecompress[ByteIndex + s] = (byte)(255);
}
else
signdecompress[ByteIndex + s] = (byte)(0);
}
}
}
return signdecompress;
}
}
}
please help. i need the result to become
0xFF,0xD8,0xFF,0xE0,0x00,0x10,0x4A,0x46,0x49,0x46,0x00,0x01,0x01,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0xFF,0xDB,0x00,0x43,0x00,0x02,0x02,0x02,0x02,0x02,0x01,0x02,0x02,0x02,0x02,0x03,0x02,0x02,0x03,0x03,0x06,0x04,0x03,0x03,0x03,0x03,0x07,0x05,0x05,0x04,0x06,0x08,0x07,0x09,0x08,0x08,0x07,0x08,0x08,0x09,0x0A,0x0D,0x0B,0x09,0x0A,0x0C,0x0A,0x08,0x08,0x0B,0x0F,0x0B,0x0C,0x0D,0x0E,0x0E,0x0F,0x0E,0x09,0x0B,0x10,0x11,0x10,0x0E,0x11,0x0D,0x0E,0x0E,0x0E,0xFF,0xC0,0x00,0x0B,0x08,0x00,0x2C,0x00,0xA8,0x01,0x01,0x11,0x00,0xFF,0xC4,0x00,0x1F,0x00,0x00,0x01,0x05,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0xFF,0xC4,0x00,0xB5,0x10,0x00,0x02,0x01,0x03,0x03,0x02,0x04,0x03,0x05,0x05,0x04,0x04,0x00,0x00,0x01,0x7D,0x01,0x02,0x03,0x00,0x04,0x11,0x05,0x12,0x21,0x31,0x41,0x06,0x13,0x51,0x61,0x07,0x22,0x71,0x14,0x32,0x81,0x91,0xA1,0x08,0x23,0x42,0xB1,0xC1,0x15,0x52,0xD1,0xF0,0x24,0x33,0x62,0x72,0x82,0x09,0x0A,0x16,0x17,0x18,0x19,0x1A,0x25,0x26,0x27,0x28,0x29,0x2A,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x92,0x93,0x94,0x95,0x96,0x97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0xC5,0xE6,0x9B,0xA8,0xEE,0x35,0x07,0x96,0x6D,0x42,0xEA,0x19,0x3C,0xD1,0x17,0xEE,0xE5,0xB4,0x1B,0x62,0x0F,0xE5,0xAC,0x98,0x64,0xF6,0x0F,0x83,0xBF,0x00,0xBE,0x0D,0xFE,0xCF,0xFE,0x05,0x97,0xC3,0xBF,0x07,0xBE,0x1E,0xE9,0x5E,0x07,0xB0,0x9F,0x1F,0x6C,0x96,0xD1,0x1A,0x4B,0xBB,0xDC,0x3C,0x8E,0x9F,0x68,0xBA,0x90,0xB4,0xD3,0xEC,0x33,0x49,0xB3,0xCC,0x76,0xD8,0x1B,0x6A,0xE0,0x71,0x5E,0xBF,0x45,0x14,0x51,0x45,0x14,0x51,0x45,0x14,0x51,0x45,0x14,0x57,0xE5,0x0F,0xED,0x6F,0xE3,0x2F,0x1D,0x7C,0x52,0xFF,0x00,0x82,0xC9,0x7C,0x1E,0xFD,0x86,0x1F,0xC6,0xFA,0xAF,0x81,0x3E,0x09,0xFC,0x43,0xF0,0x05,0xE5,0xFF,0x00,0x8C,0x25,0xF0,0xAF,0x93,0x06,0xAF,0xA8,0xFE,0xEF,0x51,0x7F,0x23,0xED,0x53,0x47,0x2E,0xC8,0x7F,0xE2,0x5B,0x1A,0x94,0x45,0x5D,0xE9,0x3C,0xE9,0x26,0xF5,0x60,0x17,0xED,0xFF,0x00,0x85,0xDF,0xB2,0xAF,0xEC,0xE3,0xF0,0x5F,0xFB,0x0E,0x6F,0x86,0x9F,0x06,0x3C,0x2B,0xE1,0xBD,0x5B,0x47,0xF3,0xBF,0xB3,0xB5,0xCF,0xEC,0xB5,0xB9,0xD5,0xE1,0xF3,0xB7,0xF9,0x9F,0xE9,0xF3,0x6F,0xB9,0x39,0x12,0xBA,0x73,0x21,0xF9,0x0E,0xCF,0xBB,0xC5,0x7B,0xFD,0x14,0x51,0x45,0x14,0x51,0x45,0x14,0x51,0x5F,0xFF,0xD9

I'm not entirely clear on what you are trying to do here, but the reason for the error is down to this line here:
for (byte s = 0; s < 8; s++)
It assumes that the string str is always eight characters long. This will prevent the error.
for (byte s = 0; s < str.Length; s++)
But it may not give you the output you are after..?

Your program breaks at the line:
if (str[s] == '1')
At this line, str == 255. The loop tries to iterate through 8 places in a string containing 3. Therefore, when it hit index 3, it becomes out of bounds (we always start at 0).
Your 3rd and most inner for loop should be:
for (byte s = 0; s < str.Length; s++;)
{
if (str[s] == '1')
signdecompress[ByteIndex + s] = (byte)255;
else
signdecompress[ByteIndex + s] = (byte)0;
}
Iterating based on the Length of the string or Count of something is always best practice, unless you know definitively that you will always only have a given number of indices.

Related

FFT on an image, using butterfly notation with decimation in spatial domain

I'm trying to implement FFT, using butterfly notation with decimation in spatial domain. I have two separated functions, one to calculate the FFT on 1D array, and the other that applies the first function over columns of the image, puts these columns in a temporary array, and then computes the FFT of rows in the temporary array.
Here is the code
public static Complex[] FFT(Complex[] input)
{
// Get the length of the input array
int n = input.Length;
Complex I = new Complex(0, 1);
// Check if the input has a length of 1
if (n == 1)
{
return input;
}
// Split the input into even and odd elements
Complex[] even = new Complex[n / 2];
Complex[] odd = new Complex[n / 2];
for (int i = 0; i < n; i++)
{
if (i % 2 == 0)
{
even[i / 2] = input[i];
}
else
{
odd[(i - 1) / 2] = input[i];
}
}
// Compute the FFT of the even and odd elements
Complex[] evenFFT = FFT(even);
Complex[] oddFFT = FFT(odd);
// Combine the FFT of the even and odd elements using the butterfly notation
Complex[] output = new Complex[n];
for (int i = 0; i < n / 2; i++)
{
Complex w = Complex.Exp(-2 * I * Math.PI * i / n);
output[i] = evenFFT[i] + w * oddFFT[i];
output[i + n / 2] = evenFFT[i] - w * oddFFT[i];
}
return output;
}
public static Complex[,] FFT2D(Complex[,] input)
{
int N = input.GetLength(0);
int M = input.GetLength(1);
Complex[,] output = new Complex[128, 128];
Complex[,] columnsFFT = new Complex[128, 128];
//Perform FFT over the columns of the input
for(int i = 0; i < M; i++)
{
//Put all the values from i'th column in the tempColumn variable
var tempColumn = new Complex[128];
for(int j = 0; j < N; j++)
{
tempColumn[j] = input[j, i];
}
//Calculate the FFT of i'th column
tempColumn = FFT(tempColumn);
//Assign the column to columnsFFT, after calculating its FFT
for(int z = 0; z < 128; z++)
{
columnsFFT[z, i] = tempColumn[z];
}
}
//Perform FFT over the rows of the columnsFFT
for(int i = 0; i < N; i++)
{
//Put the values from i'th row in tempRow, so we can perform the FFT on its entirety
var tempRow = new Complex[128];
for(int j = 0; j < M; j++)
{
tempRow[j] = columnsFFT[i, j];
}
//Calculate the FFT on tempRow
tempRow = FFT(tempRow);
//Assign the tempRow to the output
for(int z = 0; z < M; z++)
{
if (i == 0)
Console.WriteLine(tempRow[z].Real);
output[i, z] = tempRow[z];
}
}
return output;
}
The problem that i have is that it simply doesn't work, or i think it doesn't, based on the image output i get.
Input image
Output
What is it that i'm doing wrong here?

You can only display 60 characters per line

When running this program, my first line has 62 digits while the other lines only have 60. How can I place 2 spaces in front of the first line so each line of the array is no more then 60 characters? So basically it would look like ( with two spaces in the beginning)
9999999999999999999999999999999999999999999999999999999999
999999999999999999999999999999999999999999999999999999999999
999999999999999999999999999999999999999999999999999999999999
using System;
namespace BigFactorial
{
class Program
{
static int MAX = 5000;
//This is telling the program what to multiply.
private static void mult(int[] x, int n)
{
int y = 0, i, m;
for (i = 0; i < MAX; i++)
{
m = y + x[i] * n;
x[i] = m % 10000;
y = m / 10000;
}
}
//This is telling the program how to calculate factorial of n.
public static void Factorial(int[] a, int n)
{
a[0] = 1;
for (int i = n; i > 1; i--)
{
mult(a, i);
}
}
//This is displaing the factorial after the calculation.
public static void Display(int[] a)
{
int i;
for (i = MAX - 1; a[i] == 0; i--) ;
Console.Write(a[i]);
for (int j = i - 1, c = 1; j >= 0; j--, c++)
{
string s = "" + a[j];
Console.Write(s.PadLeft(4, '0'));
if (c % 15 == 0)
Console.WriteLine();
}
Console.WriteLine();
}
public static void Main(string[] args)
{
while (true)
{
//Telling user to enter a number.
Console.Write("Enter a number to factor or a negative number to quit: ");
int n = int.Parse(Console.ReadLine());
//Quit function
if (n < 0) break;
int[] arr = new int[MAX];
Factorial(arr, n);
Display(arr);
}
//100000 is the max number it can calculate
}
}
}
You have already demonstrated that you know how to use String.PadLeft(), so cache the strings that you are writing in memory so you can inspect them rather than writing directly out to the console.
using System.Linq;
using System.Text;
public static void Display(int[] a)
{
int i = 0;
// build the string in memory, so we can inspect the length of the lines
StringBuilder output = new StringBuilder();
output.Append(a[i]);
for (int j = i - 1, c = 1; j >= 0; j--, c++)
{
output.Append($"{a[j]}".PadLeft(4, '0'));
if (c % 15 == 0)
output.AppendLine();
}
// get the lines into an array so we can normalise the length,
// although we know the length today, calculating it expresses the intent better
var lines = output.ToString().Split(new string [] { Environment.NewLine }, StringSplitOptions.None);
var maxLength = lines.Max(x => x.Length);
// now write the output to the console
foreach(var line in lines)
Console.WriteLine(line.PadLeft(maxLength, ' '));
}
It is not necessary to use StringBuilder like this, you could have used a List<string> and that would simplify the code further. It is however useful to identify that code written targeting the Console can easily be refactored to write to memory via StringBuilder as the syntax is very similar.
You could also have used a StringWriter instance called Console and the syntax would be identical... But that was a step too far for a logic block as simple as this.

How to extract quantization matrices from a JPEG in C#

Is it possible to extract the quantization matrices of a JPG file in C#? I have found the libjpeg.NET but I cannot figure out how to retrieve the QTs matrices. Please find below my code.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BitMiracle.LibJpeg;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
string file = #"PATH_TO_FILE";
JpegImage img = new JpegImage(file);
Console.Read();
// Ideally, there should be some img.GetQuantizationMatrix() method
}
}
}
If you just want the quantization tables (and nothing else), it is a simple matter to scan the JPEG stream for the DQT marker (FFDB-followed by a 2-byte length) and extract the values.
You do not need to decode the image to get this information.
Thanks to the suggestion by #user3344003, I managed to read the JPEG header to extract quantization matrix information. I followed the file layout spec presented in this web page.
void Main()
{
string folder = #"PATH_TO_FILE";
var files = Directory.GetFiles(folder, "*jpg", SearchOption.AllDirectories);
byte dqt = 0xDB;
foreach (string file in files)
{
file.Dump();
byte[] s = File.ReadAllBytes(file);
for (int i = 0; i < s.Length-1; i++) {
byte b1 = s[i];
byte b2 = s[i+1];
if (b1 == 0xff && b2 == 0xdb) {
int field_length = s[i + 2] + s[i + 3];
int matrix_length = field_length - 3;
int qt_info = s[i + 4];
("QT info" + qt_info).Dump();
("QT Matrix").Dump();
byte[] mat = new byte[matrix_length];
for (int k = 0; k < matrix_length; k++) {
mat[k] = s[i+5+k];
}
Print8x8Matrix(mat);
}
}
}
}
public static void Print8x8Matrix(byte[] bytes)
{
string s = "";
for (int i= 0; i < bytes.Length; i++) {
s += bytes[i] + " ";
if (i % 8 == 0)
{
s.Dump();
s="";
}
}
}

C# Function to translate Binary-Code

Right now I try to write a C# Program to translate 8 Base Binary into Text.
But I guess I am not experienced enough with C# to truly make it Work.
I think the code I come up with, should, from a logical Point-of-View somewhat do what I want, but the Syntax isn't properly doing it, since don´t know it better.
This is what I have so far:
using System;
using System.Linq;
using System.Text;
class binaryTranslate
{
public int convertBin(string CodeInput)
{
int [] code = CodeInput.ToArray();
int CodeCount = code.ToString().Length;
int EightBaseSegAmount = CodeCount / 8;
int ByteCapacity = 8;
StringBuilder translated = new StringBuilder();
for (var i = 1; i < EightBaseSegAmount + 1; i++)
{
StringBuilder Byte = new StringBuilder(ByteCapacity);
int ByteStart = (i * 8) - 8;
int ByteEnd = (i * 8) - 1;
int ByteIncrement = 1;
for (var j = ByteStart ; j < ByteEnd + 1; j++)
{
Byte.Append(code[j]);
}
for (var k = 0; k > 7; k++)
{
int BitValue = 128;
if (Byte[k] == 1)
{
if (k > 0)
{
int Squared = Math.Pow(2, k);
ByteIncrement += BitValue / Squared;
}
else
{
ByteIncrement += BitValue;
}
}
}
char toSymbol = Convert.ToChar(ByteIncrement);
translated.Append(toSymbol);
}
return translated;
}
public static int Main()
{
convertBin("010010000110000101101100011011000110111100100001");
}
}
First of all, your code won't compile. Here are the errors/mistakes.
The first one is, at the first line of your function, you are converting the input string to an array using String.ToArray(), which returns a char[] but your try to assign it to a variable (code) typed int[]. You can solve this by replacing the int[] with either char[] or var.
The second one is, inside the second for loop (k = 0; k > 7), you use Math.Pow() and assign it's return value to an int variable (Squared). But Math.Pow returns double. You can solve this by casting the return value of Math.Pow to int. Like; int Squared = (int)Math.Pow(2, k);
The last thing is not easily solvable like the first two because, your code is not exactly correct. You are trying to return something called translated, which is a variable of type StringBuilder. But your function is defined to return an int.
Now these were compile errors. There are a bunch of logical and decision errors/mistakes. Your algorithm also isn't very correct.
Here is a sample code you can use/examine. I'd like to help you further, why your code was incorrect, what was your design mistakes etc. if you want to.
class binaryTranslate
{
public enum IncompleteSegmentBehavior
{
Skip = 0,
ZerosToStart = 1,
ZerosToEnd = 2
}
private byte ConvertBinstrToByte(string sequence)
{
if (string.IsNullOrEmpty(sequence))
return 0; // Throw?
if (sequence.Length != sizeof(byte) * 8)
return 0; // Throw?
const char zero = '0';
const char one = '1';
byte value = 0;
for (int i = 0; i < sequence.Length; i++)
{
if (sequence[i] != zero && sequence[i] != one)
return 0; // Throw
value |= (byte)((sequence[i] - zero) << (7 - i));
}
return value;
}
private string HandleIncompleteSegment(string segment, int segmentSize, IncompleteSegmentBehavior behavior)
{
string result = null;
var zeroAppender = new StringBuilder();
for (int i = 0; i < segmentSize - segment.Length; i++)
zeroAppender.Append('0');
var zeros = zeroAppender.ToString();
switch (behavior)
{
case IncompleteSegmentBehavior.Skip:
break;
case IncompleteSegmentBehavior.ZerosToStart:
result = zeros + result;
break;
case IncompleteSegmentBehavior.ZerosToEnd:
result = result + zeros;
break;
default:
break;
}
return result;
}
public byte[] ConvertBinstrToBytes(string binarySequence, IncompleteSegmentBehavior behavior = IncompleteSegmentBehavior.Skip)
{
var segmentSize = sizeof(byte) * 8;
var sequenceLength = binarySequence.Length;
var numberOfBytes = (int)Math.Ceiling((double)sequenceLength / segmentSize);
var bytes = new byte[numberOfBytes];
for (int i = 0; i < numberOfBytes; i++)
{
var charactersLeft = sequenceLength - i * segmentSize;
var segmentLength = (charactersLeft < segmentSize ? charactersLeft : segmentSize);
var segment = binarySequence.Substring(i * segmentSize, segmentLength);
if (charactersLeft < segmentSize)
{
segment = HandleIncompleteSegment(segment, segmentSize, behavior);
if (segment == null)
continue;
}
bytes[i] = ConvertBinstrToByte(segment);
}
return bytes;
}
}
This code passes these assertions.
var bytes = new binaryTranslate()
.ConvertBinstrToBytes("00000000");
Assert.Equal(bytes.Length, 1);
Assert.Equal(bytes[0], 0b00000000);
bytes = new binaryTranslate()
.ConvertBinstrToBytes("10000000");
Assert.Equal(bytes.Length, 1);
Assert.Equal(bytes[0], 0b10000000);
bytes = new binaryTranslate()
.ConvertBinstrToBytes("11111111");
Assert.Equal(bytes.Length, 1);
Assert.Equal(bytes[0], 0b11111111);
bytes = new binaryTranslate()
.ConvertBinstrToBytes("00000001");
Assert.Equal(bytes.Length, 1);
Assert.Equal(bytes[0], 0b00000001);
bytes = new binaryTranslate()
.ConvertBinstrToBytes("1100110000110011");
Assert.Equal(bytes.Length, 2);
Assert.Equal(bytes[0], 0b11001100);
Assert.Equal(bytes[1], 0b00110011);
If you are really converting to a string the code should look like this
namespace binaryTranslate
{
class Program
{
static void Main(string[] args)
{
//convertBin("01001000 01100001 01101100 01101100 01101111 00100001");
string results = BinaryTranslate.convertBin(new byte[] { 0x44, 0x61, 0x6c, 0x6c, 0x6f, 0x21 });
}
}
public class BinaryTranslate
{
public static string convertBin(byte[] CodeInput)
{
return string.Join("", CodeInput.Select(x => x.ToString("X2")));
}
}
}
this should do the trick.
public static string FromBinary(string binary)
{
int WordLength = 8;
binary = binary.Replace(' ', '');
while(binary.Length % WordLength != 0)
binary += "0";
string output = String.Empty;
string word = String.Empty;
int offset = 0;
while(offset < binary.Length)
{
int tmp = 0;
word = binary.Substring(offset, 8);
for(int i=0; i<(WordLength - 1); i++)
if(word[i] == '1')
tmp += (int) Math.Pow(2, i);
output += Convert.ToChar(tmp);
offset += WordLength;
}
return output;
}

Minimum surface area for a given volume of prism with edges of integer length

If I have some int volume, how would I go about writing a method that returns length, width, and height (all ints) so that length*width*height>=volume and 2(width*length+height*length+height*width) is as small as possible?
Basically, the smallest surface area (and the values that comprise it) for a given volume of a rectangular prism.
Also, ideally it would be able to set length, width, and/or height and they would remain fixed while solving the problem. For instance, calculate width and height while volume=52 and length=3.
EDIT: I wrote this simple brute force to show me the answers I want.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace surface_area_volume_ratio
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Volume--\nSurace Area-Ratio : L, W, H");
Console.ReadLine();
Console.Clear();
for (int volume = 1; volume < 200; volume++)
{
double dv=volume;
List<double> ratios = new List<double>();
List<int> surface = new List<int>();
List<int> ls = new List<int>();
List<int> ws = new List<int>();
List<int> hs = new List<int>();
for (int l = 1; l <= volume; l++)
{
double dl = l;
for (int w = 1; w <= volume; w++)
{
double dw = w;
for (int h = 1; h <= volume; h++)
{
double dh = h;
if (l * w * h >= volume)
{
int s = (2 * (l * w + l * h + w * h));
surface.Add(s);
ratios.Add(s/dv);
ls.Add(l);
ws.Add(w);
hs.Add(h);
}
}
}
}
double smallest=0;
if (ratios.Count>0)
smallest = ratios.Min();
Console.WriteLine(volume+"--");
for (int i = 0; i < ratios.Count; i++)
{
if (smallest==ratios[i])
Console.WriteLine("{0}-{1} : {2}, {3}, {4}",surface[i],ratios[i],ls[i],ws[i],hs[i]);
}
Console.ReadLine();
Console.Clear();
}
}
}
}
Calculate volume as such:
double volume = length * width * height;
Reverse this process with a cube root to get back to length width and height:
var val = Math.Pow(1000, ((double)1 / 3));
double length = val,
width = val,
height = val;
Also, an example of how to hard-code one of the values. All you do is devide the hard-coded value from the volume, and then do a square root to get the last two values:
double volume = 1000; //example volume
double length = 50; //<- example hard coded length
double val = Math.Sqrt(volume / length);
double width = val,
height = val;
I originally posted this without realizing that integer type was a condition of the question. I am leaving this code here for reference purposes.
Here's a brute force solution:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int volume = 52;
int l=0, w=0, h=0, minval=int.MaxValue;
for (int length = 1; length <= volume; length++)
{
for (int width = 1; width <= volume; width++)
{
for (int height = 1; height <= volume; height++)
{
if(length * width * height >= volume){
int area = 2*(length*width + width*height + length*height);
if(area < minval){
l=length;
w=width;
h=height;
minval = area;
}
}
}
}
}
Console.WriteLine("length = " + l + ", width= " + w + ", height = " + h);
}
}
}
This is to find the rectangular prism with integer lengths satisfying the conditions. If integer lengths are not required then the problem is trivial, refer caesay's answer.
IMHO you should post your algorithm and your attempt at the solution then ask for corrections/ help. Stackoverflow shouldn't be used to get your entire work done for you.
Start with this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int volume = 0;
int surfaceArea = 0;
for (int length = 1; length < 100; length++)
{
for (int width = 1; width < 100; width++)
{
for (int height = 1; height < 100; height++)
{
}
}
}
}
}
}
​

Categories

Resources