About Me

BSc 3D Computer Animation student, currently at SMU in Wales. Love 3D obviously, as well as digital painting, video/film, photography, special effects... the list goes on. Need to know anything else, just e-mail me!

Wednesday 20 October 2010

First Proper C++ Program

This is it, my first real C++ program. All it does is print pythagorean triples between 0 and 100, but I'm proud of it :D

// PythagoreanTriples.cpp : Defines the entry point for the console application.
//



#include "stdafx.h"
#include "cmath"

float calculateHypot(int a, int b) // calculates square root of a^2 + b^2
{
float c;
c = ((a*a)+(b*b));
return sqrt(c);
}

bool checkInteger(float a) // subtracts int-cast version of c from c to determine if integer
{
if(a-(int)a != 0) return true; // if floating value, return true
return false; // if integer value, return false
}

int _tmain(int argc, _TCHAR* argv[])
{
printf("***********************\n");
printf("* PYTHAGOREAN TRIPLES *\n");
printf("***********************\n");
int a;
int b;
//printf("The result is: %f", c);
for (a=1; a <= 100; a++)
{
for(b=1; b <= 100; b++)
{
float c = calculateHypot(a,b);
bool isInt = checkInteger(c);
if(isInt != true) // kinda reverse of what you'd think, but it only seems to work this way
{
int intC = int(c); // purely for display purposes - changes C into an integer value to cull trailing 0s
printf ("a = %d b = %d c= %d\n",a,b,intC);
}
}
}
}

No comments:

Post a Comment