Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Circle Calculator Job
#1
What do you guys think of this little program, I've started to learn C++ and this is my first attempt at a useful program. I haven't learnt much so its not great and I'm not sure if I got the forulae right.

Code:
#include <iostream>

float CircleArea(long int Radius, const float Pi);
float CircleCircumference(long int Radius, const float Pi);

/*Program created by ComDriver
13th Febuary 2005
This program can find a circle's area and circumference*/

int main()
{
    long int MenuChoice, Radius, Diameter, Circumference;
    const float Pi = 3.141592653589793238462633832795;
    
    //Menu starts here
    std::cout << "Menu- Find a cicle's-\n\n";
    std::cout << "1. Area\n";
    std::cout << "2. Circumference\n";
    //Menu ends here
    
    std::cout << "What would you like to do? ";
    std::cin >> MenuChoice;
    std::cin.ignore(80,'\n');
    
    //The users Menu choice on what he wants to find is evaluated here  
    if (MenuChoice == 1)
    {
                  
       std::cout << "\nWhat is the radius of the circle (in centimetres): ";
       std::cin >> Radius;
       std::cin.ignore(80,'\n');
      
       std::cout << "\nThe circle's area is: " << CircleArea(Radius, Pi) << " square cm.";
      
    }  
      
    if (MenuChoice == 2)
    {
    
       std::cout << "\nWhat is he radius of the circle: ";
       std::cin >> Radius;
       std::cin.ignore(80,'\n');
      
       std::cout << "\nThe circumference of the circle is: " << CircleCircumference(Radius, Pi) << "cm";
    
    }
    
    std::cin.get();
    return 0;
    
}

//This function finds a circles area
float CircleArea(long int Radius, const float Pi)
{
    
     return (Pi*Radius*Pi*Radius);
    
}

//This function finds a circles circumference
float CircleCircumference(long int Radius, const float Pi)
{
      
      return (2*Pi*Radius);
      
}
Reply
#2
Quote:
Code:
float CircleArea(long int Radius, const float Pi)
{
    
     return (Pi*Radius*Pi*Radius);
    
}

Too many Pis there Sad. Should be Pi*Radius*Radius.

And why, out of interest, are you restricting the radius to integer values ?

Richard.
url=http://www.bbcbasic.org/]Visit www.bbcbasic.org for BBC BASIC[/url]
Reply
#3
I made some changes-

Code:
#include <iostream>
double CircleArea(double Radius, const double Pi);
double CircleCircumference(double Radius, const double Pi);

/*Program created by ComDriver
13th Febuary 2005
This program can find a circle's area and circumference*/

int main()
{
    long int MenuChoice, Radius, Diameter, Circumference;
    const float Pi = 3.141592653589793238462633832795;
    
    //Menu starts here
    std::cout << "Menu- Find a cicle's-\n\n";
    std::cout << "1. Area\n";
    std::cout << "2. Circumference\n";
    //Menu ends here
    
    std::cout << "What would you like to do? ";
    std::cin >> MenuChoice;
    std::cin.ignore(80,'\n');
    
    //The users Menu choice on what he wants to find is evaluated here  
    if (MenuChoice == 1)
    {
                  
       std::cout << "\nWhat is the radius of the circle (in centimetres): ";
       std::cin >> Radius;
       std::cin.ignore(80,'\n');
      
       std::cout << "\nThe circle's area is: " << CircleArea(Radius, Pi) << " square cm.";
      
    }  
      
    if (MenuChoice == 2)
    {
    
       std::cout << "\nWhat is he radius of the circle: ";
       std::cin >> Radius;
       std::cin.ignore(80,'\n');
      
       std::cout << "\nThe circumference of the circle is: " << CircleCircumference(Radius, Pi) << "cm";
    
    }
    
    std::cin.get();
    return 0;
    
}

//This function finds a circles area
double CircleArea(double Radius, const double Pi)
{
    
     return (Pi*Radius*Radius);
    
}

//This function finds a circles circumference
double\ CircleCircumference(double Radius, double Pi)
{
      
      return (2*Pi*Radius);
      
}
Reply
#4
Quote:I made some changes-
Code:
#include <iostream>
double CircleArea(double Radius, const double Pi);
double CircleCircumference(double Radius, const double Pi);

Yes, but unfortunately your main declaration of Radius is still as an integer!

Code:
long int MenuChoice, Radius, Diameter, Circumference;
    const float Pi = 3.141592653589793238462633832795;

Richard.
url=http://www.bbcbasic.org/]Visit www.bbcbasic.org for BBC BASIC[/url]
Reply
#5
Nice going man. Big Grin I remember when I was learning C, I would practice by writing a load of similar progs to this... I used to write them on paper in boring lessons. :oops: But back then, lessons weren't very important.

By the way, it's not necessary to include pi as an argument to each of those functions. If you put this at the top of your program:

[syntax="C"]
#define PI 3.141592654
[/syntax]

Then you can just refer to PI anywhere in your program.

Example:

[syntax="C"]
double CircleArea(double Radius)
{

return (PI * Radius * Radius);

}
[/syntax]

Excellent coding style, by the way. Very neat and clear. Smile


-shiftLynx
img]http://www.cdsoft.co.uk/misc/shiftlynx.png[/img]
Reply
#6
Use define magic for uberfast programs Big Grin

Code:
#define PI 3.1415926
#define CircleArea(a) (PI * a * a)
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#7
Quote:Use define magic for uberfast programs Big Grin

Code:
#define PI 3.1415926
#define CircleArea(a) (PI * a * a)

I know of two students, friends of mine, who didnt knowed that, and they study information science o.o

If you ever happen to code with CC65 for the C64 i have two for you all:
Code:
// COLOR_RAM is defined in somewhere else
#define setcolor(x,y,f) COLOR_RAM[(y-1)*40+x] = f

// I know that there is a conio function that does the same, but this is
// much faster than the conio one ;)
#define CHAR_RAM    ((unsigned char*)0x0400)
#define setchar(x,y,f) CHAR_RAM[(y-1)*40+x] = f
color=red]Look at you, Hacker. A pathetic creature of meat and bone, panting and sweating as you run through my corridors. How can you challenge a perfect, immortal machine?" - Shodan, AI at Citadel Station orbiting Earth[/color]
Reply
#8
Yeah, I had something similar when I used Borland C 3.1 for MSDOS coding...

Code:
#include<dos.h>
#define (SCREEN MAKE_FP(0xa000, 0))
#define plot(x,y,c) SCREEN[(y<<6)+(y<<8)+x] = c
#define point(x,y) (SCREEN[(y<<6)+(y<<8)])

aaah, those were the days Big Grin

And the almighty stuff you always need:

Code:
#define FALSE 0
#define TRUE !FALSE
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#9
Quote:
Code:
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)

Call me stupid, but i dont get the idea behind these two.
color=red]Look at you, Hacker. A pathetic creature of meat and bone, panting and sweating as you run through my corridors. How can you challenge a perfect, immortal machine?" - Shodan, AI at Citadel Station orbiting Earth[/color]
Reply
#10
Quote:
na_th_an Wrote:
Code:
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)

Call me stupid, but i dont get the idea behind these two.

The ? : notation is shorthand for 'if'. The expression is put before the ?, the value if the expression is true is put after the ? and the value if the expression is false is put after the :.

Code:
value = <expression> ? <value-if-true> : <value-if-false>;


Take this as an example:

[syntax="C"]
if(opt)
someValue = 5;
else
someValue = 6;
[/syntax]

That can be written:

[syntax="C"]
someValue = opt ? 5 : 6;
[/syntax]


na_th_an's min/max macros effectively "choose" the least/greatest values of the parameters - really useful and it's used by a lot of people. Smile

Big, not-so-nice way for min:

[syntax="C"]
if(y < z)
x = y;
else
x = z;
[/syntax]

Nice way for min:

[syntax="C"]
x = (y < z) ? y : z;
[/syntax]

But because of na_th_an's macro, you can just do this:

[syntax="C"]
x = min(y, z);
[/syntax]


Funkey, eh? Big Grin I think I went overkill explaining it... but I'm sure some other people have wondered the same as you, but kept silent about it. Smile

-shiftLynx
img]http://www.cdsoft.co.uk/misc/shiftlynx.png[/img]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)