2 Math programs

Today I took a test missed 3 out of 30. That means I scored 9 out of 10 right. It’s not bad (not as good as David Baird though). My radical program is pretty neat (it works too). Here are a couple of math programs.

Script started on Thu Feb 24 11:28:12 2000
[cbarnes@ais c++stuff]$ cat quadratic1.cpp
#include <iostream.h>
#include <math.h>
#include <stdlib.h>

double a, b, c, x1, x2, imag;
int getInput();
int calc();

int main()
{
 getInput();
 calc();
 return 0;
}

int getInput()
{
 system("clear");
 cout << "Ax^2 + Bx + C = 0" << endl;
 cout << "A?";
 cin >> a;
 cout << "B?";
 cin >> b;
 cout << "C?";
 cin >> c;
}

int calc()
{
 double root;
 if (4 * a * c > b * b) 
 {
  imag = 1;
  root = -1 * (b * b - 4 * a * c); 
 }
 else root = b * b - 4 * a * c;
 x1 = -b + sqrt(root) / (2 * a);
 x2 = -b - sqrt(root) / (2 * a);
 if (imag == 0) cout << endl << x1 << endl << x2 << endl;
 if (imag == 1) cout << endl << x1 << "i" << endl << x2 << "i" << endl;
}
[cbarnes@ais c++stuff]$ g++ quadratic1.cpp
[cbarnes@ais c++stuff]$ a.out
Ax^2 + Bx + C = 0
A?1
B?-2
C?1

2
2
[cbarnes@ais c++stuff]$ a.out
Ax^2 + Bx + C = 0
A?1
B?2
C?2

-1i
-3i
[cbarnes@ais c++stuff]$ cat radical.cpp
#include <iostream.h>
#include <math.h>

unsigned long int Orig;
unsigned long int Number;
unsigned long int Div = 3;
unsigned long int LastNum = 0;
unsigned long int Inside = 1;
unsigned long int Outside = 1;

getInput()
{
 unsigned long int Ipt;
 cout << "Enter a positive integer" << endl;
 cin >> Ipt;
 return Ipt;
}

int main()
{
 Orig = getInput();
 cout << "The Square root of  " << Orig << "  is :" << endl;
 Number = Orig;
 if (Div >= (sqrt(Number)+1))
 {
  cout << Orig;
  return 0;
 } 

 while (Number != 1)
 {
  while (!(Number -percent 2))
  {
   Inside *= 2;
   if (LastNum == 2)
   {
    Outside *= 2;
    LastNum = 1;
    Inside /= 4;
   }
   else LastNum = 2;
   Number /= 2;
  }
  while (!(Number -percent Div))
  {
   Inside *= Div;
   if (LastNum == Div)
   {
    Outside *= Div;
    LastNum = 1;
    Inside /= Div * Div;
   }
   else LastNum = Div;
   Number /= Div;
  }
  Div = Div + 2;
 }
 cout << Outside << " Square Roots of " << Inside;
 cout << endl;
}
[cbarnes@ais c++stuff]$ g++ radical.cpp
[cbarnes@ais c++stuff]$ a.out
Enter a positive integer
32
The Square root of  32  is :
4 Square Roots of 2
[cbarnes@ais c++stuff]$ exit

Script done on Thu Feb 24 11:29:55 2000
This entry was posted in Coding and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *