Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
class Program
{
    static void Main(string[] args)
    {
        double tribase = 9;
        double triwidth = 6;
        double trihypotenuse = Math.Sqrt((tribase * tribase) + (triwidth * triwidth));
        double triarea = 0.5 * (tribase * triwidth);
        double triperimeter = tribase + triwidth + trihypotenuse;
        Console.WriteLine("Area of right triangle = {0}\nPerimeter of rigth triangle = {1}\n", triarea, triperimeter);

        double squareside = 8;
        double squarearea = squareside * squareside;
        double squareperimeter = 4 * squareside;
        Console.WriteLine("Area of square = {0}\nPerimeter of square = {1}\n",  squarearea, squareperimeter);
    }
}
Posted
Comments
CHill60 23-Feb-14 13:05pm    
This sounds like homework. Which bits do *you* think could go into a separate function?
RalvarezHose 23-Feb-14 13:10pm    
Yes, it was one of my homework question, it is now 3 weeks past due, got only half a point. Professor told me to use any resource and get it right and she will give me couple of extra points. So far I have got following:

static double GetSquareArea(double squareside)
{
double squarearea = squareside * squareside;
return squarearea;
}
static double GetSquarePerimeter(double squareside)
{
double squareperimeter = 4 * squareside;
return squareperimeter;

}
No idea how to do perimeter and area of right triangle in different functions.
george4986 23-Feb-14 13:22pm    
are u using visual studio for coding?
RalvarezHose 23-Feb-14 13:25pm    
Yes, I am using visual studio 2013 express for desktop.
george4986 23-Feb-14 13:34pm    
for perimeter select these lines and follow steps

double tribase = 9;
double triwidth = 6;
double trihypotenuse = Math.Sqrt((tribase * tribase) + (triwidth * triwidth));
double triperimeter = tribase + triwidth + trihypotenuse;

1 solution

You are so close already...
This is the code you have already put into functions yourself...
C#
static double GetSquareArea(double squareside)
    {
        double squarearea = squareside * squareside;
        return squarearea;
    }
        static double GetSquarePerimeter(double squareside)
    {
        double squareperimeter = 4 * squareside;
        return squareperimeter;

        }


So let's revisit the code you have to refactor and see what we have left...the bits in bold are the bits that change because we now have your two functions ...
C#
class Program
{
    static void Main(string[] args)
    {
        double tribase = 9;
        double triwidth = 6;
        double trihypotenuse = Math.Sqrt((tribase * tribase) + (triwidth * triwidth));
        double triarea = 0.5 * (tribase * triwidth);
        double triperimeter = tribase + triwidth + trihypotenuse;
        Console.WriteLine("Area of right triangle = {0}\nPerimeter of rigth triangle = {1}\n", triarea, triperimeter);

        double squareside = 8;
        double squarearea = GetSquareArea(squareside);
        double squareperimeter =  GetSquarePerimeter(squareside);
        Console.WriteLine("Area of square = {0}\nPerimeter of square = {1}\n",  squarearea, squareperimeter);
    }
}

So do the same with the perimeter of a triangle ... the bits in bold need to go out into a separate function into which you will pass the values of tribase and triwidth
C#
class Program
{
    static void Main(string[] args)
    {
        double tribase = 9;
        double triwidth = 6;
        double trihypotenuse = Math.Sqrt((tribase * tribase) + (triwidth * triwidth));
        double triarea = 0.5 * (tribase * triwidth);
        double triperimeter = tribase + triwidth + trihypotenuse;
        Console.WriteLine("Area of right triangle = {0}\nPerimeter of rigth triangle = {1}\n", triarea, triperimeter);

        double squareside = 8;
        double squarearea = GetSquareArea(squareside);
        double squareperimeter =  GetSquarePerimeter(squareside);
        Console.WriteLine("Area of square = {0}\nPerimeter of square = {1}\n",  squarearea, squareperimeter);
    }
}

Notice that trihypotenuse is an interim calculation to get to the perimeter ... you don't need it anywhere else in Main so that needs to go into the function as well.
Have a go at it and post your attempt as a comment to this solution and I'll tell you if you've got it right
 
Share this answer
 
Comments
RalvarezHose 23-Feb-14 13:57pm    
{ class Program
{
static void Main(string[] args)
{
double tribase = 6;
double triwidth = 9;
double triarea = triArea(tribase, triwidth);
double triperimeter = triPerimeter(tribase, triwidth, trihypotenuse);
Console.WriteLine("Area of right triangle = {0}\nPerimeter of rigth triangle = {1}\n", triarea, triperimeter);

double squareside = 8;
double squarearea = GetSquareArea(squareside);
double squareperimeter = GetSquarePerimeter (4 * squareside);
Console.WriteLine("Area of square = {0}\nPerimeter of square = {1}\n", squarearea, squareperimeter);
}

private static double triPerimeter(double tribase, double triwidth, double trihypotenuse)
{
double trihypotenuse = Math.Sqrt((tribase * tribase) + (triwidth * triwidth));
double triperimeter = tribase + triwidth + trihypotenuse;
return triperimeter;
}

private static double triArea(double tribase, double triwidth)
{
double triarea = 0.5 * (tribase * triwidth);
return triarea;
}

static double GetSquareArea(double squareside)
{
double squarearea = squareside * squareside;
return squarearea;
}
static double GetSquarePerimeter(double squareside)
{
double squareperimeter = 4 * squareside;
return squareperimeter;

}


}
CHill60 23-Feb-14 14:04pm    
That's really close now ... your only problem is with trihypotenuse ... remember I said you don't actually need it anywhere else in Main? You have (correctly) calculated it in your triPerimeter function so you don't need to pass it as a parameter to the function. One other thing ... be consistent on the names you call the functions ... do something like private static double GettriPerimeter rather than using the same name as one of your variables - it can get confusing otherwise. Well done by the way
RalvarezHose 23-Feb-14 13:59pm    
I got these two error messages when I move trihypo to function.
Error 1 The name 'trihypotenuse' does not exist in the current context C:\Users\Bibek\Desktop\Winter classes 2013\Introduction to Programing C#\Assignments\ProjectFive\ProjectFiveOne\ProjectFiveOne\Program.cs 16 63 ProjectFiveOne

Error 2 A local variable named 'trihypotenuse' cannot be declared in this scope because it would give a different meaning to 'trihypotenuse', which is already used in a 'parent or current' scope to denote something else C:\Users\Bibek\Desktop\Winter classes 2013\Introduction to Programing C#\Assignments\ProjectFive\ProjectFiveOne\ProjectFiveOne\Program.cs 27 20 ProjectFiveOne
CHill60 23-Feb-14 14:12pm    
Hadn't noticed you were replying at the same time as me! If you follow the advice in my comment about not passing in the parameter trihypotenuse then the errors will be fixed
RalvarezHose 23-Feb-14 14:13pm    
Got it. Thank you so much for your help. It helped me clear lot of confusion I was having.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900