using System;
namespace area
{
class Program
{
static void Main(string[] args)
{
double basse;
double height;
Console.WriteLine("Enter your base length: ");
basse = Convert.ToDouble(Console.ReadLine());
Console.WriteLine( "Enter the height: ");
height = Convert.ToDouble(Console.ReadLine());
double area = Program.triangleArea(basse, height);
Console.WriteLine("Your area is {0:f3}", area);
Console.ReadLine();
double pryrmid = Program.pyramidVolume( triangleArea);
Console.WriteLine(" Pyramid Volume is {0:f3}" , pryrmid);
}
public static double triangleArea(double basse, double height)
{
return (0.5 * basse) * height;
}
public static double pyramidVolume (double triangleArea)
{
return (1/3) * triangleArea;
}
}
}
I'm trying the calculate the volume of a pryamid using the methods ive defined.
I keep getting the error
Argument '1': cannot convert from 'method group' to 'double' (CS1503)
- \vmware-host\Shared Folders\Documents\SharpDevelop Projects\WS_6_D\WS_6_D\Program.cs:28,57
and
The best overloaded method match for
'area.Program.pyramidVolume(double)' has some invalid arguments
(CS1502) - \vmware-host\Shared Folders\Documents\SharpDevelop
Projects\WS_6_D\WS_6_D\Program.cs:28,34
I was wondering if someone could help me get on the right track.
The problem is that triangleArea in
double pryrmid = Program.pyramidVolume( triangleArea); is not a variable, hence it points to the static method.
Try double pryrmid = Program.pyramidVolume( area); instead.
The compiler is expecting something it can evaluate to a double, but you supply the name of a function (triangleArea).
Instead, you probably want to pass the area you calculated previously.
I think you meant to say
double pryrmid = Program.pyramidVolume(area);
instead of
double pryrmid = Program.pyramidVolume( triangleArea);
triangleArea is your method, you used area as your result value.
Related
I am trying to get Max value from List<>, but it's returning rounded value to integer. Is there some special way how to proceed this?
private List<double> dataX = new List<double>();
double maxVal = dataX.Max<double>();
Debug.WriteLine("max: " + maxVal);
Edit:
As requested here is feeding data:
for (int i = 0; i < 10; i++)
{
data.Add(new ChartData(i, rand.NextDouble() * 10));
Debug.WriteLine(data.Last<ChartData>().Y);
}
My debug window shows this:
5,9358753151893
7,87125875608588
3,77212246589927
9,36056426230844
2,27154730924943
9,80201833872218
5,7350595275569
3,04650606729393
5,81677517658881
0,0514464220271662
max: 8
So I don't think the feeding side is wrong. And for whole picture, here you can see ChartData type:
public class ChartData
{
public double X { get; set; }
public double Y { get; set; }
public ChartData(double X, double Y)
{
this.X = X;
this.Y = Y;
}
}
And how I'm getting simple List from my ChartData class:
private List<ChartData> data = new List<ChartData>();
private List<double> dataX = new List<double>();
void updateMaxMin()
{
dataX.Clear();
dataY.Clear();
for (int i = 0; i < data.Count - 1; i++)
{
dataX.Add(data[i].X);
dataY.Add(data[i].Y);
}
}
There are two likely scenarios here.
You are rounding the values as you enter them into the list (as #sam mentioned in his comment).
You are expecting a double value ending in 0 to show these decimal places. A double will always drop off the insignificant digits. So for example, 1.500 will be truncated to 1.5. This is how doubles were intended to work. Another article that briefly talks about this is Double Skips last decimal if zero. If you are looking for a different Visual output, I would recommend converting the result to a string and then using string formatting. An example would be the following (using 2 decimal places):
Console.WriteLine(string.Format("max: {0:0.00}", maxVal));
Most likely the problem is in the way you insert into the list as some had suggested in here (you mentioned about rounded to an integer, so I'm assuming it is probably not visual display related).
Try debug your data in the list:
private List<double> dataX = new List<double>();
...
foreach(var data in dataX)
{
Debug.WriteLine("data: " + data);
}
double maxVal = dataX.Max<double>();
Debug.WriteLine("max: " + maxVal);
A possible issue with the way you populate the list could be something like:
var myNum = new[] { 1, 2, 3, 4, 5, 6 };
foreach (var num in myNum)
{
dataX.Add(num / 2);
}
The data that was added into the dataX is actually an integer (as the division by 2 returns an integer).
double doesn't keep insignificant digits. - there's no difference between 9 and 9.0 and 9.0000
If you want just display purpose use refer this link C# Double - ToString() formatting with two decimal places but no rounding for convert the double to string with decimal places. but,if you using calculation it's no need for keeping with decimal places.
Ok I found mistake. I have been calculating max value from different array. So i got correct max value anyway. There should be:
double maxVal = dataY.Max<double>();
instead of
double maxVal = dataX.Max<double>();
So I guess, this isn't much helping, so I will delete this question after you realized I did basic fault.
Thank you all anyway.
I've just started object oriented programming and I don't quite understand how to call another method. I wanted to calculate the percentage increase and then put out the resulting overall price. This would be done by calling the method to do the working out then returning the value to the other method. However I'm unsure about how to cross over between methods.
Could someone please explain how I'm meant to do this? Preferably not just giving the answer so I can fully understand what's happening.
p.s. this piece of code is in a class called RetailPricing. As I've copied and pasted it, it doesn't look like it's all formatted properly (I understand how to call this class to the main program)
namespace week7exercise2
{
class RetailPricing
{
public void CalculateRetailPrice()
{
double inputcost;
double inputpercent;
string inputitemcost;
string inputmarkup;
Console.Write("Please Input The Cost Of The Item: ");
inputitemcost = Console.ReadLine();
inputcost = double.Parse(inputitemcost);
Console.Write("Please Input The Markup Percentage: ");
inputmarkup = Console.ReadLine();
inputpercent = double.Parse(inputmarkup);
Console.Write("Your Retail Price Is: " + newprice);
}
public double sum(double MarkUpPercentage, double overallprice, double newprice)
{
MarkUpPercentage = inputpercent + 100;
overallprice = MarkUpPercentage / 100;
newprice = inputcost * overallprice;
return newprice;
}
}
}
What I understood is that you want to call a function to perform some calculations.
Before your last line:
Console.Write("Your Retail Price Is: " + newprice);
What you need to do is call your function for calculating price and it'll return the price after calculating it. So simply do:
double newprice = sum(inputpercent,inputcost)
And change your sum function to:
public double sum(double MarkUpPercentage, double overallprice)
{
MarkUpPercentage = inputpercent + 100;
overallprice = MarkUpPercentage / 100;
double newprice = inputcost * overallprice;
return newprice;
}
I'm trying to learn the basics to c# coding (or any code for that matter). I'm not understanding how to take user input and put it into a string that I can later use as an integer. I thought I could write it like
string baseOfTriange = int.Parse(Console.ReadLine));
but it's not working. I've also tried float thinking maybe that was it, however, I'm just lost on the concept. This is what I have so far, I just can't figure out how to convert a string to an int. Thanks for any help.
static void Main(string[] args)
{
// area of a triangle = base(height)/2
Console.WriteLine("Welcome, enter your triangle dimensions. ");
Console.WriteLine();
Console.Write("What is the base of the triangle? ");
string baseOfTriangle = int.Parse(Console.ReadLine());
Console.WriteLine("You answered, " + baseOfTriangle + ". ");
Console.Write("What is your height of the triangle? ");
string heightOfTriangle = int.Parse(Console.ReadLine());
Console.WriteLine("You answered, " + heightOfTriangle + ". ");
Console.WriteLine("The area of the triangle is " + (baseOfTriangle * heightOfTriangle / 2));
Console.ReadLine();
}
I'll assume you're getting a compile time error because of the following line:
string baseOfTriangle = int.Parse(Console.ReadLine());
The return type of int.Parse is int but your trying to assign it to a variable of type string. Change the type of baseOfTriangle and heightOfTriangle to int and that will solve your problem.
int baseOfTriangle = int.Parse(Console.ReadLine());
^^^
Also, you probably want a floating point answer. Otherwise 1*1/2 will give you an answer of 0. Change it to baseOfTriangle * heightOfTriangle / 2.0. Or better yet use double and double.Parse.
Take a look at the C# Programming Guide: How to: Convert a String to a Number:
http://msdn.microsoft.com/en-us/library/bb397679.aspx
Also look at the TryParse methods for most built-in number types for c#. For example, int32.TryParse():
http://msdn.microsoft.com/en-us/library/system.int32.tryparse(v=vs.110).aspx
static void Main(string[] args)
{
Console.Write("What is the base of the triangle? ");
int baseOfTriangle = int.Parse(Console.ReadLine());
Console.Write("What is your height of the triangle? ");
int heightOfTriangle = int.Parse(Console.ReadLine());
Console.WriteLine("The area of the triangle is {0}", (baseOfTriangle * heightOfTriangle) / 2);
Console.ReadLine();
}
The usual method to convert to int is with Convert.ToInt32()
I'm bigener in C# programming
So, I was just wondering about how to take user input in the same line?
this is my code and also I want to print the output in the same line
using System;
namespace Wa2
{
class BodyMassCalculation
{
public static void Main (string[] args)
{
Console.WriteLine ("BMI Calculator\n");
double weight;
Console.WriteLine ("Enter your weight in kilograms: ");
weight = Convert.ToInt16(Console.ReadLine());
double height;
Console.WriteLine ("Enter your height in centimetres: ");
height = Convert.ToInt16(Console.ReadLine());
double meter;
meter = height / 100;
Double BMI;
BMI = (weight) / (meter*meter);
Console.WriteLine ("Your BMI is " , BMI);
Console.WriteLine(BMI.ToString("00.00"));
}
}
}
Try this:
Console.Write("Enter your input here: ");
string userinput = Console.ReadLine();
Just change Console.WriteLine to Console.Write.
Use Console.Write() instead of Console.WriteLine().
I think that's what you mean anyway, the question isn't very clear.
I think you're asking if it's possible to read both height and weight at the same time:
// C equivalent
printf ("Enter height (cm) and weight (kg): ");
scanf ("%d %d\n", &h, &w);
Yes, there are several alternatives.
Arguably the easiest is use Console.ReadLine() (like you're doing) and parse the string.
You can also try multiple "Console.Read()" (one for each argument).
I hope somebody can be of assistance, thank you in advance.
I am using C# to generate some simulation models for evaluating the medium stationary time of a request in a system and the degree of usage of that serving station from the system.
I am using a function to generate the required numbers:
public double genTP(double miu)
{
Random random = new Random();
double u, x;
u = (double)random.NextDouble();
x = (-1 / miu) * Math.Log(1 - u);
return x;
}
This is the main:
Program p1 = new Program();
double NS = 1000000;
double lambda = 4;
double miu = 10;
double STP = 0;
double STS = 0;
double STL = 0;
double i = 1;
double Ta = 0;
double Tp = 0;
double Dis = 0;
do
{
Tp = p1.genTP(miu);
STP += Tp;
STS += Ta + Tp;
Dis = p1.genDIS(lambda);
if (Dis < Ta + Tp)
{
Ta = Ta + Tp - Dis;
}
else
{
STL += Dis - (Ta + Tp);
Ta = 0;
}
i++;
} while (i <= NS);
Console.WriteLine(STS / NS);
Console.WriteLine((STP/(STP+STL))*100);
1) The medium stationary time (which is r) returned is wrong, I get values like 0.09.. but I should get something like ~0.1665.. The algorithm is ok, I am 100% sure of that, I tried something like that in Matlab and it was good. Also the degree of usage (the last line) returned is ok (around ~39.89), only the r is wrong. Could it be a problem with the function, especially the random function that should generate a number?
2)Regarding my function genTP, if I change the parameter from double to int, then it returns 0 at the end. I used debugger to check why is that, and I saw that when the method calculates the value of x with (-1 / miu), this returns 0 automatically, I have tried to cast to double but with no result. I was thinking that this could be a source of the problem.
You're creating a new instance of Random each time you call genTP. If this is called multiple times in quick succession (as it is) then it will use the same seed each time. See my article on random numbers for more information.
Create a single instance of Random and pass it into the method:
private static double GenerateTP(Random random, double miu)
{
double u = random.NextDouble();
return (-1 / miu) * Math.Log(1 - u);
}
And...
Random random = new Random();
do
{
double tp = GenerateTP(random, miu);
...
}
A few more suggestions:
Declare your variables at the point of first use, with minimal scope
Follow .NET naming conventions
Don't make methods instance methods if they don't use any state
I prefer to create a static random field in the calculation class
static Random random = new Random();
Now I can use it without thinking of quick succession and I don't need to give it as a function parameter (I'm not trying to say it works faster but just similar to mathematical notation)
Regarding your second question, it is because the compiler does an integer division because they are two integers.
int / int = int (and the result is truncated).
If any of the args are floating point types, the operation is promoted to a floating point division. If you change your arg to an int, you should either use -1d (a double with value -1), or cast 'miu' to a double before usage:
x = (-1d / miu) ...
or
x = (-1 / (double)miu) ...
Or, of course, use them both as doubles. I prefer the first way.