URGENT HELP NEEDED!!! PLEASE!!!

Hello all,

I am in my first semester of CompSci courses and I am having a little trouble with a HW problem. My prof wants me to do the following:

Break up the program (areas) from last week into one main function and 3 user-defined functions:

// gets an integer from the user and returns it

// make 3 calls to this function:

// get the length of the rectangle from the user and return it to main

// get the width of the rectangle from the user and return it to main

// get the radius of the circle from the user and return it to main

int GetNum(void);


// takes two arguments, the length and width of the rectangle and returns the area

int CalculateAreaR(int length, int width);


// takes one argument, the radius of the circle and returns the area

double CalculateAreaC(int radius);


The main problem I am having while trying to understand this is that it says make three calls to the voided function. I was under the belief that a voided function cannot return values. How far off am I? How am I to get the numbers inputted to the length, width, and radius, return them to the GetNum(void) function, and retrieve them afterwards? I know this is probably a simple problem with a simple answer, but if you could provide me with any amount of information regarding this that you believe is beneficial, that would be greatly appreciated. Thanks!!!

Edit: I just realized that the assignment is asking for the numbers to returned to main. However, I am still very confused with the wording of this assignment and if anyone could help me out, I will forever be in your debt :)
Last edited on
Where exactly are you confused?
int GetNum(void); takes no arguments, but returns an int. If you would have void DoSomething(int), the new function will take an int as an argument, but will not return anything.
Should the int GetNum(void) be inside of the main function? I am unsure of how to ask for the length/width/radius and return it to main while using the int GetNum(void). That is where I am confused. Thanks again guys.
You can't place a function inside of another functions. Functions are declared and defined outside of main(), or any other function.

1
2
3
4
5
6
7
8
9
10
int getRadius()
{
   return radius;
}

int main()
{
   cout << getRadius();
   return 0;
}
Okay, please bear with me. I guess I am confused when the instructions ask me to make three calls to the function and return it to main. Could someone please give me an idea of what he is asking for? The assignment is due in a few hours and I did not think it would take nearly this long as all of the others took me 30 mins each. He asks for me to break up the functions from last week which was the following:

#include <stdio.h>

int main ()
{
int length, width, areaR, radius;
double areaC;

printf ("Please enter the length of the rectangle: ");
scanf ("%d", &length);
printf ("\nPlease enter the width of the rectangle: ");
scanf ("%d", &width);
printf ("\nPlease enter the radius of the circle: ");
scanf ("%d", &radius);

areaR = length * width;

areaC = 3.14 * radius * radius;

printf ("\nThe length of the rectangle has been measured at %d.\n", length);
printf ("\nThe width of the rectangle has been measured at %d.\n", width);
printf ("\nThe radius of the circle has been measured at %d.\n", radius);

printf ("\nThe area of the rectangle is %d.\n", areaR);
printf ("\nThe area of the circle is %f.\n\n", areaC);

return 0;
}

Where am I supposed to incorporate the int GetNum(void)?

I know I am asking a lot, I just really need the help. Thanks again all.
Last edited on
Here is what I have so far. Unfortunately, I do not think it is what he is asking for. Here goes:

#include <stdio.h>

int GetNum (void);
double AreaC;
int length; int width; int radius; int AreaR;

int main()
{

printf("Please enter the length of the rectangle: ");
scanf("%d", &length);
printf("Please enter the width of the rectangle: ");
scanf("%d", &width);
printf("Please enter the radius of the circle: ");
scanf("%d", &radius);

AreaC = radius * radius * 3.14;
AreaR = length * width;

printf("The area of the rectangle is %d.", AreaR);
printf("The area of the circle is %f.", AreaC);

return 0;
}


When the heck and how am I supposed to incorporate the int GetNum(void)?
Please don't take this the wrong way, but...
This is trivial at best, and honestly, would have been solved simply by paying a little more attention in class.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>


int GetNum(void) {

	int Value;
	scanf("%d", &Value);
	return Value;

}

int CalculateAreaR(int length, int width) {

	return length * width;

}

int CalculateAreaC(int radius) {

	return radius * radius * 3.14;

}

int main() {

	printf("Please enter the length of the rectangle: ");
	int Length = GetNum();

	printf("Please enter the width of the rectangle: ");
	int Width = GetNum();

	printf("Please enter the radius of the circle: ");
	int Radius = GetNum();

	int AreaR = CalculateAreaR(Length, Width);
	double AreaC = CalculateAreaC(Radius);

	printf("The area of the rectangle is %d.\n", AreaR);
	printf("The area of the circle is %f.\n", AreaC);

	return 0;

}
Last edited on
Topic archived. No new replies allowed.