C# "can't convert double to float" [closed] - c#

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 6 days ago.
Improve this question
Why can't I just cast a double to a float?
public static Random random = new Random();
public static void Main(string[] args)
{
//Market myMarket = new Market();
int iter = 30;
//List<string> tags = new List<string>();
//List<float> mins = new List<float>();
//List<float> maxes = new List<float>();
for(int i = 0; i < iter; i++)
{
float res = (float)RandomDouble(0.01, 1.0);
//tags.Add(""+i);
//mins.Add((float)(RandomDouble(0.01, 1.0)));
//maxes.Add((float)(RandomDouble(mins[i], 10)));
}
}
public static double RandomDouble(float min, float max)
{
return random.NextDouble() * (max - min) + min;
}
The above code generates a compile error Argument 2: cannot convert from 'double' to 'float'
Note: Checked the following links, didn't notice any useful answers:
C# converting type double to float
How to Convert double to Float Class into Java

Related

No output for loop program [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 4 months ago.
Improve this question
The scope is to output the numbers from 1 to (input number) with the multiples of 3 being a * each. E.g. for 8 as input: 1,2,*,4,5,*,7,8. I tried to do this with a for loop but it has no output for some reason.
using System;
using System.Collections.Generic;
namespace HelpPleaseStackOverFlow
{
class Program
{
static void Main(string[] args)
{
int number = Convert.ToInt32(Console.ReadLine());
int x;
for (x = 1; x <= number; )
if (x % 3 == 0)
{
Console.WriteLine("*");
}
else;
{
Console.WriteLine(x);
x = x++;
}
}
}
}
I tried converting the x variable (integer) to a string and then making it the * that should be printed out, in the first half of the IF statement and the last part with the ELSE basically the same.
I also tried to make the for statement like this:
for (x = 1; x <= number; x++)
with the x++ at the end but this just game me the result of ,,8. With an input of 7; 8 should not be part of this and I have no idea why it put 8 as an output.
most of the erroros are that you are lacking some {} and ()
String input = Console.ReadLine();
int number;
int.TryParse(input, out number);
for (int x = 1; x <= number; ++x)
{
if ((x % 3) == 0)
{
Console.WriteLine("*");
}
else
{
Console.WriteLine(x);
}
}
Console.ReadLine();
Hope it helps.

How to take 'snapshot' of variable value before a for loop? C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
static void Main(string[] args)
{
Random rnd = new Random();
float myfloat = rnd.Next(1, 50);
for (int i = 0; i <= 2; i++)
{
myfloat = 3;
}
Console.WriteLine(myfloat);
Console.ReadLine();
}
Say I want to change the value of a variable for the duration of a for loop, but then I want it to go back to whatever it was just befoe the loop (if i don't know what that value was). How would I do something like that?
With this code 'myfloat' is stuck at 3 after the loop ends.
I recommend using a "temporary" variable.
static void Main(string[] args)
{
Random rnd = new Random();
float myfloat = rnd.Next(1, 50);
float tempFloat = myfloat;
for (int i = 0; i <= 2; i++)
{
myfloat = 3;
}
myfloat = tempFloat;
Console.WriteLine(myfloat);
Console.ReadLine();
}
It could look something like this.

C# Creating an Index in a For Loop for an Array [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 2 years ago.
Improve this question
I am attempting to do an assignment and I have attempted to try to record the results of two dice rolls by pulling the possibilities I could get from an array. Basically if I rolled a 2, 4, 5, 5, and 2, I'd record I got two 2s, one 4, and two 5s. However, I am trying to figure out the best way to record it without having to resort to list every single variable 2-12. Might someone be able to assist me in learning how to make the shortcut for this from the code I provide? The code is as follows:
using System;
namespace Assignment
{
class Program
{
static void Main(string[] args)
{
//Initialize variable(s).
int diceRollNum = 0;
//Create the array.
int[] DiceResultArray = new int[11];
//Create the random number.
Random diceRoll = new Random();
//Write out Headers.
Console.WriteLine($"Roll\tCount");
//
for (diceRollNum = 0; diceRollNum < 36000; diceRollNum++)
{
//Roll the dice.
int firstDice = diceRoll.Next(1, 6);
int secondDice = diceRoll.Next(1, 6);
//Add the dice sums.
diceRollNum = firstDice + secondDice;
//Record results.
DiceResultArray[diceRollNum] =
}
//
for (int i = 0; i < DiceResultArray.Length; i++)
{
Console.WriteLine($"{i+2}\t{DiceResultArray[i]}");
}
}
}
}
We are looking for specifically what happens under the Record Results comment. If anyone could help explain this to me, that would be wonderful!
Your code has few issues
diceRollNum is updated in the loop and it runs infinitely.
Random.Next(minValue, maxValue) generates a random value excluding the maxValue. Therefore to get a random number between 1 and 6 we should invoke Next() passing 1 and 7 min and max as parameters respectively
Should reduce 1 from diceRollSum (a new variable which stores the sum of dice values) when accessing the array, as the array is indexed from 0-11, not 1-12
using System;
namespace Assignment
{
class Program
{
static void Main( string[] args )
{
//Initialize variable(s).
int diceRollNum = 0;
//Create the array.
int[] DiceResultArray = new int[12];
//As 1 is not a possible value for the sum of dice values, we can instantiate an array with 11 items and reduce 2 from diceRollSum
//A slightly optimized Approach noted by Andrew
//int[] DiceResultArray = new int[11];
//Creates random instance.
Random diceRoll = new Random();
//Write out Headers.
Console.WriteLine( $"Roll\tCount" );
//
for (diceRollNum = 0; diceRollNum < 36000; diceRollNum++)
{
//Roll the dice.
int firstDice = diceRoll.Next( 1, 7 );
int secondDice = diceRoll.Next( 1, 7 );
//Add the dice sums.
int diceRollSum = firstDice + secondDice;
//Record results.
DiceResultArray[diceRollSum - 1]++;
//Slightly Optimized
//DiceResultArray[diceRollSum - 2]++;
}
//
for (int i = 0; i < DiceResultArray.Length; i++)
{
Console.WriteLine( $"{i+1}\t{DiceResultArray[i]}" );
//Slightly Optimized
//Console.WriteLine( $"{i+2}\t{DiceResultArray[i]}" );
}
}
}
}

Permutation and Combination in C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
Improve this question
Please tell me how can I apply permutation and combination in C# console application and take values of N and r and calculate permutation and combination.
I just had a go doing this for fun, it's actually a little challenging as a naive implementation overflows long very quickly. I've included those in comments.
Equations
nPr = n! / (n - r)!
nCr = n! / r! (n - r)!
Implementaion
public static class PermutationsAndCombinations
{
public static long nCr(int n, int r)
{
// naive: return Factorial(n) / (Factorial(r) * Factorial(n - r));
return nPr(n, r) / Factorial(r);
}
public static long nPr(int n, int r)
{
// naive: return Factorial(n) / Factorial(n - r);
return FactorialDivision(n, n - r);
}
private static long FactorialDivision(int topFactorial, int divisorFactorial)
{
long result = 1;
for (int i = topFactorial; i > divisorFactorial; i--)
result *= i;
return result;
}
private static long Factorial(int i)
{
if (i <= 1)
return 1;
return i * Factorial(i - 1);
}
}
Usage
Console.WriteLine(PermutationsAndCombinations.nPr(10, 3));
Console.WriteLine(PermutationsAndCombinations.nCr(10, 3));
Prints:
720
120

how to calculate average from array in wcf service [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
why it doesn't give me average 3?
public double total = 0;
public double avg = 0;
public double[] Yahoo = { 1, 2, 3, 4,5 };
for (int i = 0; i < Yahoo.Length; i++)
{
total += Yahoo[i];
}
avg = total / Yahoo.Length;
I ran this console application:
class Program
{
static void Main(string[] args)
{
double total = 0;
double avg = 0;
double[] Yahoo = { 1, 2, 3, 4,5 };
for (int i = 0; i < Yahoo.Length; i++)
{
total += Yahoo[i];
}
avg = total / Yahoo.Length;
Console.WriteLine(avg);
Console.ReadKey();
}
}
and I got 3.
However you could use LINQ for that you want:
Yahoo.Average();
Ah, in order to use this method, you have to set this on the top of your source code file the following using statement:
using System.Linq;
Note One thing that seems odd to me, is the way you have declared your variables
public double total = 0;
public double avg = 0;
public double[] Yahoo = { 1, 2, 3, 4,5 };
since those should be declared in a method, they shouldn't have an access modifier. That's wrong.

Categories