void functions

How do I code this void function and then return to the main function?

When I tried doing

void DisplayLargestAvgArea(...)
{
double largestAvgArea = 0.0
if avgAreaOfCircle > avgAreaOfSquare...
largestAvgArea = avgAreaOfCircle
else if...

}

^did the same for min. Then, I returned it to the main function.
^Both didn't work.



Thank you in advance
Last edited on
> ^Both didn't work.
Perhaps post the code you actually compiled.

There's nothing inherently wrong (apparently) with the pseudo-code you posted.

> Then, I returned it to the main function.
Well you're not returning anything with void functions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void displayLargestAvgArea( double circle, double square, double rect, double triangle )
{
    // An array of four 'double', initialized with the variables whose were passed into the function
    double vArea[4] = { circle, square, rect, triangle };
    // An array of four std::string objects
    std::string vName[4] = { "circle", "square", "rectangle", "triangle" }; 

    double max = 0; 
    int pos = 0; 
    // idx is just a (arbitrary choiced) name of an int variable.
    // It's meant to be an abbreviation of 'index'. 
    for (int idx = 0 ; idx < 4; ++idx)
    {
        if (vArea[idx] > max)
        {
            max = vArea[idx];
            pos = idx;
        }
    }
    std::cout << "The largest area has the " << vName[pos] 
              << ". It has an area of " << max << " units.\n";
}
 
Last edited on
Whilst it suffices for this example, it does assume that areas can't be negative.

It might be better to say for example
double max = vArea[0];
here is the code

1
2
3
4
5
6
7
8
9
10
}
void LargestAvgArea(double avgCircle, double avgSquare, double avgRectangle)
{
	//i need to compare each of the randomized average areas above and then display
}

void SmallestAvgArea(double avgCircle, double avgSquare, double avgRectangle)
{
	//i need to compare each of the randomized average areas above and then display
}
Last edited on
hi salem c,

I meant to display the largest average area when comparing each shape... if that makes sense.
I just posted the codes above. Hope that helps out.

thanks for your reply!
hi nuderobmonkey,

I am still quite new with c++, so I don't get what idx is.
If you would please write comments on what things are in the codes you provided, that would help me understand the language more!

Thanks for your reply!
oh also, I don't know how to use arrays just yet.
Last edited on
Hello gongong, sorry to hear that it is not allowed to you, using arrays. Because without them you are forced to use a bunch of if-else brunches.

Btw: The function I wrote, consists of very basic c(++) stuff. I added some comments in the hope that it would be clearer to you. I guess you stumbled over the definitions and usings of the two arrays. For understanding what's going on there, I recommend you for seaching a good tutorial which handles pointers and arrays. Sorry for my insufficient knowledge of the englisch language. That's the reason why I rather provide with code than with good explanations.
1
2
3
4
5
6
7
8
double max = 0;
for ( int idx = 0 ; idx < 3; ++idx )
{
  if (vArea[idx] > max)
  {
    max = vArea[idx];
  }
}

A loop like that has fixed, small number of iterations. It can be "unrolled":
1
2
3
4
5
6
7
8
9
double max = vArea[0];
if (vArea[1] > max)
{
  max = vArea[1];
}
if (vArea[2] > max)
{
  max = vArea[2];
}

You have not had arrays yet, but hopefully you can see that:
double vArea[3] = { circle, square, rect };
Is almost same as:
1
2
3
4
double vArea[3];
vArea[0] = circle;
vArea[1] = square;
vArea[2] = rect;

And therefore the unrolled loop (after substitution) looks like:
1
2
3
4
5
6
7
8
9
double max = circle;
if (square > max)
{
  max = square;
}
if (rect > max)
{
  max = rect;
}

I figured it out.

Thanks for your help, people!
Topic archived. No new replies allowed.