Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Problem with c code, trying to find area of a rectangle?
Here's the code I've got so far. I think it's something to do with trying to call with two variables, since my code worked fine finding the area of a circle. I'm just not sure how else to do it. Any help is appreciated!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float rectangle(float h, w)
{
float a;
float p;
a = h*w;
p = 2*(h+w);
printf("rectangle has an area of %f and a base perimeter of %f\n", a, p);
return (h, w);
}
int main(void)
{
float w;
float h;
printf("Enter height and width: ");
fflush(stdout);
scanf("%f%f", &h, &w);
rectangle(h, w);
return EXIT_SUCCESS;
}
Unfortunately it still isn't working with the return statement modified.
3 Answers
- Anas ImtiazLv 69 years agoFavorite Answer
1- Incorrect function definition i.e. float rectangle(float h, w)
2- You cannot return more than 1 variables from a function.
3- Logical error: your function does not need a return type.
Try this code:
#include <stdio.h>
void rectangle(double height, double width)
{
printf("\nArea:\t\t%lf", height*width);
printf("\nPerimeter:\t%lf", 2*(height+width));
}
int main()
{
double height, width;
printf("Enter height and width of rectangle:\t");
scanf("%lf%lf", &height, &width);
rectangle(height, width);
return 0;
}
For code + sample run: http://ideone.com/23AlB
Source(s): Programmer. - ʄaçadeLv 79 years ago
return (h, w);
You cannot return TWO values from a function. Try returning the area or just the perimeter.
Here is a tip: Do not name your functions as nouns. Use a verb or verb-phrase. Instead of "rectangle", call it "getRectangleArea ()" of "findRectPerimeter ()". That way, you will start to think naturally in terms of "What does this function give to me?"
Added: On the other hand, you are not using the return value anyway. The caller ignores it. So you may as well return nothing.
Have you tried inserting a delimiter (space?) between the %f and the second %f in the scanf?
- 4 years ago
this query get requested plenty even as they first use visible c++ ide alot. in case your software compiled outstanding, nonetheless this device may desire to run and disappear till now you get to be certain the outcomes. you will possibly ought to word device("pause") to pause this device at the tip of your software.