I'm trying to convert an amount from numbers to string. While converting 3070 to three thousand seventy only I noticed a flaw in the code, the output is supposed to be three thousand seventy only but instead of this the output is Three Thousand Rupees only.
I got the code from the internet,
When i debug the code, I see the following lines
if ((rupees / 1000) > 0)
{
res = rupees / 1000;
rupees = rupees % 1000;
result = result + ' ' + rupeestowords(res) + " Thousand";
}
The problem arises in this code because 1010,1020,.....,3070,3080,3090,4010,4020.etc all the numbers are % to 1000, that means if I enter these number the output will be wrong,
I am unable to get the proper logic here. I think i need to validate the rupees again inside another if condition.
Code below X Thousands
if ((rupees / 100) > 0)
{
res = rupees / 100;
rupees = rupees % 100;
result = result + ' ' + rupeestowords(res) + " Hundred";
}
if ((rupees % 10) > 0)
{
res = rupees % 100;
result = result + " " + rupeestowords(res);
}
result = result + ' ' + " Rupees only";
return result;
}
In this code:
if ((rupees % 10) > 0)
{
res = rupees % 100;
result = result + " " + rupeestowords(res);
}
This line is wrong:
res = rupees % 100;
Should be
res = rupees / 10;
Also the following line is wrong:
if ((rupees % 10) > 0)
Should be:
if ((rupees / 10) > 0)
Leaving:
if ((rupees / 10) > 0)
{
res = rupees % 10;
result = result + " " + rupeestowords(res);
}
Related
so i'm having an issue with the decimal part where the number that you input in the console is for example 0,02 and it gives in words only 2 out of the 3 characters (skips the second 0) i tried many solutions including just doing an if() where i have a double mn = 0.9 but i found out that the if works only to 0 it accepts the variable that i made but its seems like it doesnt understands it. If you have any idea how i can fix this glad to hear
P.S I've attached the whole code on the other paragraph. Thank you!
what i want to happen Input 0,02 ---> output zero point zero two
what is happening Input - 0,02 ----> output zero point 2
static void Main(string[] args)
{
Console.OutputEncoding = System.Text.Encoding.Unicode;
// OT VIKTOR BOTKOV 304sr
try
{
Console.WriteLine("Въведете число.....:");
string number = Console.ReadLine();
number = ConvertAmount(double.Parse(number));
Console.WriteLine("Числото изписано в думи е : \n{0}", number);
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static String[] units = { "нула", "едно", "две", "три",
"четири", "пет", "шест", "седем", "осем", "девет", "десет", "единайсет",
"дванайсет", "тринайсет", "четиринайсет", "петнайсет", "шеснайсет",
"седемнайсет", "осемнайсет", "деветнайсет" };
private static String[] tens = { "", "", "двайсет", "трийсет", "четиресет",
"педесет", "шейсет", "седемдесет", "осемдесет", "деведесет" };
private static String[] hundreds = {"","сто", "двеста", "триста","четиристотин",
"петстотин", "шестстотин", "седемстотин", "осемстотин", "деветстотин" };
private static String[] thousands = {"","хиляда","две хиялди", "три хиляди","четири хиляди",
"пет хиляди", "шест хиляди", "седем хиляди", "осем хиляди", "девет хиляди" };
private static String[] unitsс = { "нула ", "един", "два", };
public static String ConvertAmount(double amount)
{
try
{
double mn = 0.09;
Int64 amount_int = (Int64)amount;
Int64 amount_dec = (Int64)Math.Round((Decimal)(amount - (double)(amount_int)) * 100);
if (amount_dec == 0)
{
return Convert(amount_int) + " лв.";
}
else if (amount_int <= mn)
{
return Convert(amount_int) + " Цяло и " + " Нула " + Convert(amount_dec) + " ст.";
}
else
{
return Convert(amount_int) + " лв. и " + Convert(amount_dec) + " ст.";
}
}
catch (Exception e)
{
}
return "";
}
public static String Convert(Int64 i)
{
Console.OutputEncoding = System.Text.Encoding.Unicode;
if (i<=2)
{
return unitsс[i];
}
if (i < 20)
{
return units[i];
}
if (i < 100)
{
return tens[i / 10] + ((i % 10 > 0) ? " и " + Convert(i % 10) : "");
}
if (i < 1000)
{
return hundreds[i / 100] + ((i % 100 > 0) ? " и " + Convert(i % 100) : "");
}
if (i < 100000)
{
return Convert(i / 1000) + " Хиляди "
+ ((i % 1000 > 0) ? " " + Convert(i % 1000) : "");
}
else if (i < 1000000)
{
return Convert(i / 10000) + " Хиляди "
+ ((i % 10000 > 0) ? " и " + Convert(i % 10000) : "");
}
if (i < 10000000)
{
return Convert(i / 1000000) + " Милиона "
+ ((i % 1000000 > 0) ? " " + Convert(i % 1000000) : "");
}
if (i < 100000000)
{
return Convert(i / 1000000) + " Милиона "
+ ((i % 1000000 > 0) ? " и " + Convert(i % 1000000) : "");
}
if (i < 1000000000)
{
return Convert(i / 1000000) + " Милиона "
+ ((i % 1000000 > 0) ? " " + Convert(i % 1000000) : "");
}
if (i < 10000000000)
{
return Convert(i / 1000000000) + " Милиaрда "
+ ((i % 1000000000 > 0) ? " " + Convert(i % 1000000000) : "");
}
return Convert(i / 1000000000000) + " "
+ ((i % 1000000000000 > 0) ? " " + Convert(i % 10000000000000) : "");
}
}
I am creating a multiplication table, when you input a number and click the calculate button it should display. I have tried watching a few tutorials on YouTube and have checked out some coding forums however I can only find people using the Console Application however I am using the Windows Form Application
1 * 1 = 1
2 * 1 = 2
3 * 1 = 3
4 * 1 = 4
5 * 1 = 5
6 * 1 = 6
7 * 1 = 7
8 * 1 = 8
9 * 1 = 9
10 * 1 = 10
However, when I run the program it only displays
1 * 10 = 10
Here is my code;
private void btnCalc_Click(object sender, EventArgs e)
{
int n, i;
n = Int32.Parse(txtNum.Text);
for (i = 1; i <= 10; ++i)
txtCalc.Text = Convert.ToString(n + " * " + i + " = " + n * i);
}
This loop keeps setting the control's text to a different value over and over, leaving you to see only the final value.
for (i = 1; i <= 10; ++i)
{
txtCalc.Text = Convert.ToString(n + " * " + i + " = " + n * i);
}
A straightforward solution is:
string text = "";
for (i = 1; i <= 10; ++i)
{
text += Convert.ToString(n + " * " + i + " = " + n * i);
}
txtCalc.Text = text;
You will still run into some formatting issues you'll need to solve, but you'll get the fundamental info in there.
You're overwriting the text over and over again. What you want to do is append new text every time through the loop. Try something like:
txtCalc.Text = "";
for (i = 1; i <= 10; ++i)
{
txtCalc.Text += Convert.ToString(n + " * " + i + " = " + n * i) + Environment.NewLine;
}
your txtCalc.Text... overwrites the field in every iteration. You probably want something like this:
txtCalc.Text = "";
for (i = 1; i <= 10; ++i)
{
txtCalc.Text += Convert.ToString(n + " * " + i + " = " + n * i);
}
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 3 years ago.
Improve this question
I have tried to really understand this question I am going to ask now for a year. Trying to google it, read about multicore computing, parallel.foreach approach, ask questions about this. But never got a clear answer for what is going on. I always suspect no one truly knows and just guessing.
The Question:
My computer has: 24 cores.
I have a function: calculationFunction()
It is possible to test this code
Now to the benchmark which is my BIG question:
If I run calculationFunction() with 1 core. It takes 42 seconds
If I run calculationFunction() with 18 cores. It takes 11 seconds
Now is my question why doesn't it go faster than 11 seconds. If 1 core takes 42 seconds. Shouldn't 18 cores take: 42/18 = Around or close to 2.33 seconds?
What is truly the problem here. Is there a bottleneck somewhere or doesn't actually 18 cores get involved in this process. Something is not right?
IMPORTANT TO MENTION:
I know from other exact same tests. I have opened 18 instances of the same application and split a work into 18 pieces and this did go exactly 18 times faster.
So is there something that restricts ONE instance of an application to NOT use all 18 cores that I assign in this code?
It is possible to test this code:
public void runThreads()
{
//Change this variable to make tests
int nrCores = 18;
List<List<String>> minusLIST2D = new List<List<String>>();
List<List<String>> plusLIST2D = new List<List<String>>();
int nrloops = 3000000;
nrloops = nrloops / nrCores;
/*---------------------------------------------------------------*/
var stopwath = new Stopwatch();
stopwath.Start();
Task[] tasks = new Task[nrCores];
for (int i = 0; i < nrCores; i++)
{
//Add lists
minusLIST2D.Add(new List<String>());
plusLIST2D.Add(new List<String>());
//Start Task
int index = i;
tasks[index] = Task.Factory.StartNew(() => calculationFunction(nrloops, minusLIST2D[index], plusLIST2D[index]));
}
Task.WaitAll(tasks); //Wait for all Tasks to complete
stopwath.Stop();
MessageBox.Show("Elapsec secs: " + stopwath.Elapsed.TotalSeconds.ToString());
}
public void calculationFunction(int nrloops, List<String> minusLIST, List<String> plusLIST)
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
double num1 = 0; double num2 = 0; double num3 = 0; double number1 = 0; double number2 = 0; double number3 = 0; double thenum1 = 0; double thenum2 = 0; double thenum3 = 0;
double NUM1 = 0; double NUM2 = 0; double NUM3 = 0; double NUMBER1 = 0; double NUMBER2 = 0; double NUMBER3 = 0; String str = ""; String num11 = ""; String num22 = ""; String num33 = "";
String number11 = ""; String number22 = ""; String number33 = ""; double calc1 = 0;
for (int i = 0; i < nrloops; i++)
{
//Calculate both calc scenarios!
num1 = 12.3; number1 = 13.3; thenum1 = 14.3;
num2 = 12.3; number2 = 13.3; thenum2 = 14.3;
num3 = 12.3; number3 = 13.3; thenum3 = 14.3;
NUM1 = num1; NUM2 = num2; NUM3 = num3; NUMBER1 = number1; NUMBER2 = number2; NUMBER3 = number3;
if (num1 <= 0 || number1 <= 0) { NUM1 = thenum1; NUMBER1 = thenum1; }
if (num2 <= 0 || number2 <= 0) { NUM2 = thenum2; NUMBER2 = thenum2; }
if (num3 <= 0 || number3 <= 0) { NUM3 = thenum3; NUMBER3 = thenum3; }
if (NUM1 > 0 && NUM2 > 0 && NUM3 > 0 && NUMBER1 > 0 && NUMBER2 > 0 && NUMBER3 > 0)
{
str = ""; num11 = ""; num22 = ""; num33 = ""; number11 = ""; number22 = ""; number33 = "";
if (num1 > 0 && num2 > 0 && num3 > 0 && number1 > 0 && number2 > 0 && number3 > 0) { }
else { str = string.Format("{0:F10}", thenum1) + " / " + string.Format("{0:F10}", thenum2) + " / " + string.Format("{0:F10}", thenum3); }
if (num1 <= 0) { num11 = "0"; num1 = thenum1; } else { num11 = string.Format("{0:F10}", num1); }
if (num2 <= 0) { num22 = "0"; num2 = thenum2; } else { num22 = string.Format("{0:F10}", num2); }
if (num3 <= 0) { num33 = "0"; num3 = thenum3; } else { num33 = string.Format("{0:F10}", num3); }
if (number1 <= 0) { number11 = "0"; number1 = thenum1; } else { number11 = string.Format("{0:F10}", number1); }
if (number2 <= 0) { number22 = "0"; number2 = thenum2; } else { number22 = string.Format("{0:F10}", number2); }
if (number3 <= 0) { number33 = "0"; number3 = thenum3; } else { number33 = string.Format("{0:F10}", number3); }
//Calculate
calc1 = ((num1 * number2 * number3) - 45) / 10;
//String
str = calc1 + "," + "ab" + " - " + "ab" + " - " + "ab" + "," +
"ab" + " - " + "ab" + " - " + "ab" + "," +
num11 + " / " + num22 + " / " + num33 + "," +
number11 + " / " + number22 + " / " + number33 + "," +
str + "," +
calc1 + "%";
if (calc1 > 0)
{
plusLIST.Add(str);
}
else
{
minusLIST.Add(str);
}
}
}
}
I always suspect no one truly knows and just guessing.
Well, there are engineers who dedicate their whole careers studying and designing parallel systems. I suggest you think again.
Even with basic academic knowledge in the domain I can tell you a few things.
If 1 core takes 60 seconds. Shouldn't 18 cores take: 60/18 = Around
or close to 3.33 seconds?
No, usually not. The (class of) problems that can be parallelized with this kind of efficiency are very rare. And even for those problems implementing this level efficiency is not a trivial task.
First there is the Amdahl's Law. Every problem has a serial portion (that cannot be parallelized) and a parallel portion. For example if only 90% of the problem can be parallelized Amdahl's Law says the maximum theoretical speedup you can achieve is 10x. Take a time to think about the implications. You can have 10, 100, 1'000'000 or an infinite amount of processors. You can never achieve more than 10x speedup for a problem that is 90% parallelizable.
And that is just a theoretical limit, assuming perfect parallelization with zero overhead.
In reality, threads and processes need time to initialize (which is time you don't have in the serial version). Threads need to be synchronized so they will spend some time just waiting for other threads. And any form of communication between them has an overhead. In reality the speedup is much worse then the theoretical limit given by Amdahl's Law.
I am baffled here. I have a function to return a double, sometimes it returns the correct value, but most of the times it return NaN. From my understanding NaN is returned when we have something like 0/0. Below is the function returning NaN, all it's variables are declared as doubles.
public double SuperiorValue(List<double> l)
{
valueKn[0] = 0;
valueKn[1] = 0;
valueKn[2] = 1.69;
valueKn[3] = 1.18;
valueKn[4] = 0.95;
valueKn[5] = 0.82;
valueKn[6] = 0.75;
valerKn[7] = 0.67;
valueKn[8] = 0.63;
valueKn[9] = 0.58;
valueKn[10] = 0.561;
valueKn[11] = 0.542;
valueKn[12] = 0.523;
valueKn[13] = 0.504;
valueKn[14] = 0.485;
valueKn[15] = 0.466;
valueKn[16] = 0.447;
valueKn[17] = 0.428;
valueKn[18] = 0.409;
valueKn[19] = 0.39;
valueKn[20] = 0.382;
Xm = (l.Sum()) / (l.Count);
for (int i = 0; i < l.Count; i++)
{
sumTemporary = l[i] - Xm;
sum = sum + sumTemporary;
}
kn = valueKn[l.Count];
sx = Math.Sqrt((1 / l.Count - 1) * (sum * sum));
Vx = sx / Xm;
Xksup = Xm * (1 + kn * Vx);
Xkinf = Xm * (1 - kn * Vx);
return Xksup;
}
What baffles me even more is that never is the list made of less than 3 elements and greater than 15, and it still often returns NaN but like I said sometimes it returns the correct value. Any thoughts on this ?
Look at
Math.Sqrt((1 / l.Count - 1) * (sum * sum))
The 1 / l.Count is an integer division, it will be 0 for all l.Count > 1 and then you are computing Sqrt(-1 * (sum * sum)) .
But (1.0 / l.Count - 1) would still be negative, you probably want (1.0 / (l.Count - 1))
The fix is then:
Math.Sqrt((1.0 / (l.Count - 1)) * (sum * sum))
I saw this function on Percentile calculation, so I copied it and pasted it into the compiler, and it gives me an OutOfRange exception at
else
{
int k = (int)n;
double d = n - k;
return sequence[k - 1] + d * (sequence[k] - sequence[k - 1]);//EXCEPTION
}
What could be the source of the problem, and how do I solve it?
Function:
public double Percentile(double[] sequence, double excelPercentile)
{
Array.Sort(sequence);
int N = sequence.Length;
double n = (N - 1) * excelPercentile + 1;
// Another method: double n = (N + 1) * excelPercentile;
if (n == 1d) return sequence[0];
else if (n == N) return sequence[N - 1];
else
{
int k = (int)n;
double d = n - k;
return sequence[k - 1] + d * (sequence[k] - sequence[k - 1]);
}
}
The issue is that k is a number larger than the number of items in the array.
As was mentioned, the function is designed to work with values between 0 and 1. Restricting the input should correct the problem.
public double Percentile(double[] sequence, double excelPercentile)
{
//if(excelPercentile > 1)
//excelPercentile = 1;
//else if(excelPercentile < 0)
//excelPercentile = 0;
//Depending on how you validate the input you can assume that it's a whole number percentage. Then you only need to check for the number to be between 0 and 100
if(excelPercentile > 100)
excelPercentile = 100;
else if(excelPercentile < 0)
excelPercentile = 0;
excelPercentile /= 100;
Array.Sort(sequence);
int N = sequence.Length;
double n = (N - 1) * excelPercentile + 1;
// Another method: double n = (N + 1) * excelPercentile;
if (n == 1d) return sequence[0];
else if (n == N) return sequence[N - 1];
else
{
int k = (int)n;
double d = n - k;
return sequence[k - 1] + d * (sequence[k] - sequence[k - 1]);
}
}