Reks

HI,

Could any one please help me to find slope for given value of x and y using functions.

Given value in the sense list of x and list of y values.

Thanks & Regards,

Reks

### Re: Visual C# General Formula to calculate Slope using C# function

Wole Ogunremi

Is this a school project Reks ### Re: Visual C# General Formula to calculate Slope using C# function

Peter Ritchie

A line is a two-dimensional entity, you need only two x,y values to calculate slope.

If you're got a list of x,y values, then you're probably doing smoothing or trend lines; each of which suite specific needs. Arbitrarily picking one of those isn't going to help you, please provide more detail for what you're trying to accomplish.

### Re: Visual C# General Formula to calculate Slope using C# function

Ernst Kuschke

So, what is the formula for calculating a slope
If it's cartesian co-ordinates we're talking about, as well as a straight line, you can say that

y = mx + c

where m represents the slope. Since c does not affect the slope, we can discard it for the purpose of calculating m here. So:

m = y/x

You thus will have a function that returns one value (m) and accepts two parameters (x & y):

public int GetSlope(int x, int y)
{
return y / x;
}

### Re: Visual C# General Formula to calculate Slope using C# function

Thomas Danecker

I think what he wanted to do is the following:

Code Snippet

public int GetSlope(int[] y, int x)

{

return (y[x + 1] - y[x]) / sampleRate;

}

### Re: Visual C# General Formula to calculate Slope using C# function

dgunaseelan

The formula as far I know to calculate slope of a line is (y2-y1)/(x2-x1). One has to program this

This is what i tried. Try this if its useful

int x1 = 5;
int x2 = 6;
int y1 = 10;
int y2 = 20;

float slope = (y2 - y1) / (x2 - x1);

Console.Write(slope);

### Re: Visual C# General Formula to calculate Slope using C# function

Ernst Kuschke

That's exactly how you'd do it, so just refactor that to move it into a function. 