Architect Arithmetic (Bonus Challenge, 1st Task)

Hi guys,

I am stuck in this first bonus challenge/task. I ve done and reviewed the previous tasks but somehow the ideas I have doesnt seem to work.

The task is 10:
“Make the entire program a method so that you can execute it in your Main() method with one line:
CalculateTotalCost();”

May you paste in the code to help me out?

Thanks in advance, Adis

Hello @adis_1102. Unfortunately we can’t give answers on the forum, but if you post your code, or ask specific question, we can help.

Ok…thanks for the answer. I ll try again and come back with a more specific answer.

1 Like

Hello,
So I went through the information I have so far from this class and tried something, which doesnt work BUT, I have as requested some more specific questions.

Here we go:

using System;

namespace ArchitectArithmetic
{
  class Program
  {
    public static void Main(string[] args)
    {
     totalArea(2,3,3,4,5);
    }


    static double rect2(double length, double width)
    {
      return length*width;
    }

    static double circle2(double radius)
    {
      return 0.5*Math.PI*Math.Pow(radius,2);
    }

    static double triangle2(double bottom, double height)
    {
      return 0.5*bottom*height;
    }

   static double totalArea(double area1,double area2,double area3)
    {
      double area1=rect2();
      double area2=circle2();
      double area3=triangle2();
      return area1+area2+area3;
    }
 
  }
}

Here is what I thought how I can solve that:

  1. Define the shapes (this should be saved as “rect2”, “circle2” and “triangle2”

  2. sum them up ( I am stuck here…)
    My try was

    static double totalArea(double area1,double area2,double area3)
    {
      double area1=rect2();
      double area2=circle2();
      double area3=triangle2();
      return area1+area2+area3;

    }

So how do I sum up the results from method rect2, circle2 and triangl2?
Where do I need to put in the values (e.g. lenght, width, radius), already in the definition

double area1=rect2();

or in the output at the top Main()?

Thanks in advance,

Hi,

Thanks for sharing your code! There’s quite a few things going on so I’ll try to break it down. Before I do so, I highly recommend sketching on paper a “map” of your code, to better visualize how the information is going to travel.

Beginning
You call the function totalArea() with 5 parameters as input (2, 3, 3, 4, 5). This means that your function has to be able to take that many in (currently, it only takes 3). That’s what the first error message hints at

No overload for method ‘totalArea’ takes 5 arguments

public static void Main(string[] args)
    {
     totalArea(2,3,3,4,5);
    }

Middle
So now it’s important to think what information the totalArea() needs to calculate these areas, so I would name them things like double rectHeight, double rectLength (etc). But to be clear, you need all the values that would calculate the areas for you.

Then when you call rect2(), you can call it like so: double area1 = rect2(rectHeight, rectLength); and you will get your desired value.

End
Going back to the main function, totalArea(2,3,3,4,5); will return the value of the sum of the areas. It will not print them to the terminal/console. So if you want to see what that value is, you need to print it explicitly. In general, it is good practice when you’re starting out to add print statements in places where you are doing operations on variables to make sure the desired effects are happening. It’ll catch errors way before the program becomes too big, which is useful whether you’re a beginner or advanced!
Edit: the better question to ask if you already have functions that do this for you and can work in this line for you…

I hope this clarifies some of the ideas! Sharing issues is a great way to get better at coding!

Hi,
First of all, thanks for your advice. It didn’t help me directly, however, it brought up some ideas(I guess it was itented…otherwise you would have posted the code :slightly_smiling_face:

I rewrote the code and implemented it in a different way. It works, BUT I am not satisfied…

The definition of “CalculateTotalCosts” is now:

    static double CalculateTotalCost(double area1Lenght, double area1Heigt,double area2Radius,double area3Bottom,double area3Height)
    {
     double CalculateTotalCost=(area1Lenght*area1Heigt+0.5*Math.PI*Math.Pow(area2Radius,2)+0.5*area3Bottom*area3Height)*180;
     Console.WriteLine(Math.Round(CalculateTotalCost,2));
     return CalculateTotalCost;
    }

the output in Main() is:

CalculateTotalCost(4,5,4,10,9);

$ dotnet run
16223.89

The reason I changed the code was the following:
If I follow my first attempt and also what you suggested:

double area1 = rect2(rectHeight, rectLength)

then I need to put in a value for “area1”, however, I need to put in 2 values, one for the height and one for the length…how do I do that in Main()?

the same issues occurs in my new code…I would rather separate shapes, instead of putting em together in one line of code…but I encounter the same issue as above…

Any suggestions or hints?

Well, you already went to the trouble of writing methods for shapes, like you said!
You can probably do totalArea(rect2(*enter paramaters*),circle2(*enter paramaters*), triangle2(*enter paramaters*)); and have the totalArea() just sum these like you had originally. It really depends on what you want your functions to do and what would be the most logical way.

Then totalArea() would look like this:

static double totalArea(double area1,double area2,double area3)
    {
 
      return area1+area2+area3;

    }

Your intuition is right to avoid lines like: double CalculateTotalCost=(area1Lenght*area1Heigt+0.5*Math.PI*Math.Pow(area2Radius,2)+0.5*area3Bottom*area3Height)*180;. There’s usually an easier way for you to do it! The act of looking for it is an important muscle for programmers to develop.

2 Likes

Hi,
Thanks for your help again, it helped me a lot.
Though I used it, however, it didnt come to my mind to use methods inside a method…

I have an issue putting “Console.WriteLine()” into the method…so that you dont have to use it in the “Main()” area.
I found actually two solutions (more or less), one I prefer but dont understand and another I understand but doesnt seem to be clear as a code, so I dont wanna use it.

The first part is now:

using System;

namespace ArchitectArithmetic
{
  class Program
  {
    public static void Main(string[] args)
    {
    CalculateTotalCost(rect2(4,5),circle2(4),triangle2(10,9));
    }

    static double rect2(double length, double width)
    {
      return length*width;
    }

    static double circle2(double radius)
    {
      return 0.5*Math.PI*Math.Pow(radius,2);
    }

    static double triangle2(double bottom, double height)
    {
      return 0.5*bottom*height;
    }

The second part is:

Solution 1:

static void CalculateTotalCost(double areaRect, double areaCircle, double areaTriangle)

    {
    Console.WriteLine((areaRect+areaCircle+areaTriangle)*180);  
    }

This one uses “void” as a return parameter, however, I am not sure if there isnt a better way instead of calculating inside of "Console.WriteLine().

“Solution” 2:

    static double CalculateTotalCost(double areaRect, double areaCircle, double areaTriangle)

    { 

    Console.WriteLine((areaRect+areaCircle+areaTriangle)*180);  
    return areaRect+areaCircle+areaTriangle;
    }

So here the costs will be printed to the console, while the return is just the area without any costs

My questions are:

-May you suggest any solution where I can print the “return” to the console? I tried combining them but nothing worked.

-I dont see any difference here between using “static void” and using “static double…” with the "return parameter…In general I didnt really understood the difference…

Thanks in advance, Adis

Hello @adis_1102.

This is generally discouraged(as the data from the function is not useable in any other code); however, in your case it’s fine.

This is completely fine, but if you wanted, you could store the computation in a variable:

double x =5*5; //computation
Console.WriteLine(x);

Do you want to return anything? Are you going to use this data later, or are you just returning it to fill the function return type?

You could have two Console.WriteLine(), but you probably though of that. You could also create an array of the two computations, and access them by index:

int comp_1 = 5*5;
int comp_2 = 5*7;
int[] comps = {comp_1, comp2}
Console.WriteLine($"{comps[0]}, {comps[1]}");//formatted how you like

(Sorry if I mis-understood what you meant!)

The return parameter/type of a function is the data type it will return. They basically tell the program to expect a certain type to be returned, and to throw an error if the wrong type is returned. These are the same as the datatypes you use when declaring variables: int, double, string, etc-with one exception: void. void indicates that there will be nothing returned from the function.

I hope this helps!

1 Like

Hi everyone,

Attached I d like to share my solution to this case. I am open and thankful for improvements.
Thanks a lot to @toastedpitabread and @codeneutrino for supporting me in this case.

using System;

namespace ArchitectArithmetic
{
  class Program
  {
    public static void Main(string[] args)
    {
      CalculateTotalCosts(rect2(4,5),circle2(4),triangle2(10,9));
    }


    static double rect2(double length, double width)
    {
      return length*width;
    }

    static double circle2(double radius)
    {
      return 0.5*Math.PI*Math.Pow(radius,2);
    }

    static double triangle2(double bottom, double height)
    {
      return 0.5*bottom*height;
    }

    static void CalculateTotalCosts(double areaRect, double areaCircle, double areaTriangle)
    { 
       Console.WriteLine($"{Math.Round((areaRect+areaCircle+areaTriangle)*180,2)} Pesos");  
    }

  }
}

The output:

16223.89 Pesos

1 Like