Chapter 5,6&7 combo


#1

Here is a cool play off of what was learned in Chapter 7 and implementing material learned in Chapters 5 and 6. I have no idea what I am doing but thought this was pretty neat. Essentially it just tells you what the largest radius is that a circle could have to fill up a space of 450sq somethings. Could be modified into something more practical perhaps.

Plus its fun to watch the computer calculate that fast when it is ran.

[code]float areaOfCircle(float radius)
{
return (3.14 * (radius * radius));
}

int main(int argc, const char * argv[])
{
float circleArea;
float r;
for (r = 0; r < 100; r+=0.0001) {
printf(“Checking r = %.4f”, r);
circleArea = areaOfCircle®;
printf(" and the area of the circle is %.4f\n", circleArea); {
if (circleArea >= 450)
break;
}
}
printf(“The best radius is %.4f\n”, r);
return 0;

}[/code]


#2

If I could give out grades, I’d give you an A+. Nice work.


#3

I’ll take it! Thanks for the words of encouragement. I really appreciate it. This is a really fun book.


#4

Good job bthomps.

Based on yours I made a similar program that gives you both the radius and diameter of a given circle’s area based on user input.

//Type the are of the circle and hit return.

#include <stdio.h>

float formula (float radius)
{
    return (3.14 * (radius * radius));
}

int main ()
{
    float Area;
    
    printf("What is the area of the circle?");
    
    float userinput;
    scanf("%f", &userinput);
    
    float answer;
    
    for (answer = 0; answer < 100; answer += 0.0001)
    {
        printf ("Checking for answer: %f\n", answer);
        
        Area = formula (answer);
        
        if (Area >= userinput)
        {
            printf ("This is the radius: %f\n", answer);
            
            float diameter = (answer * 2);
            
            printf("And the diameter is: %f ", diameter);
            
            break;
        }
    }
    return 0;
}

Thanks for sharing. This forum is a great addition to the book.