C Math


Home

About the Author

General Programming

Pascal

Delphi

C&C++

Visual Basic

SQL

JAVA Script

Links

 

C Graphics ] [ C Math ] C Misc ] Common C&C++ Pitfalls ] Ponter Tutorial ]

  1. Calculating a quadratic equation
  2. Convert base 10 numbers into any base type that is in range 2 - 36.

Any questions mail them to cpp@teentwo.8m.com


1. Calculating a quadratic equation

#include <iostream.h>
#include <math.h>
void cal_root (float a, float b, float c, float & answer1, float & answer2);

void main()
{
  float num_a;
  float num_b;
  float num_c;
  float answer1;
  float answer2;
  char repeat = 'y';
  while ((repeat == 'y')||(repeat == 'Y'))
  {
    cout<< "Enter the value for A\n";
    cin>> num_a;
    cout<< "Enter the value for B\n";
    cin>> num_b;
    cout<< "Enter the value for C\n";
    cin>> num_c;
    cal_root(num_a, num_b, num_c, answer1, answer2);
    cout<< "Would you like to do another quadratic equation. (Y- yes N-
                  no)\n";
    cin>>repeat;
  }
}

void cal_root (float a, float b, float c, float & answer1, float & answer2)
{
  float check;
  check = (pow(b,2))- 4 * a * c;
  if (check < 0 )
  {
    cout<< "The roots are complex and cannot be solved by this program";
  }
  if (check == 0)
  {
    cout<< "The roots are both the same and are equal to -b/2a.\n";
    answer1 = -b/(2*a);
    answer2 = answer1;
    cout<< answer1 <<endl;
    cout<< answer2 <<endl;
  }
  if (a == 0)
  {
    cout<< "It is not a quadratic equation and use of this equation will"
           << " division by zero\n";
  }
  if (check > 0)
  {
    cout<< "Is positive there are two unequal, real roots.\n";
    answer1 = (-b + sqrt(pow(b,2)-4*a*c))/(2*a);
    answer2 = (-b - sqrt(pow(b,2)-4*a*c))/(2*a);
    cout<< answer1 <<endl;
    cout<< answer2 <<endl;
  }
  return;
}

Any questions mail them to cpp@teentwo.8m.com


2. Convert base 10 numbers into any base type that is in range 2 - 36.

#include <stdio.h>
char *tbase(int n, int base, char *s);

main(int argc, char *argv[])
{
  int n, n2, i;
  char
          s[50]="\x43\x6f\x64\x65\x64\x20\x62\x79\x20\x70\x61\x69\x6e\x74";
  printf("%s\n", s);
  if(argc < 3)
  {
    printf("Usage: %s <to base> <dec>, <dec>, dec, de....\n", argv[0]);
    return 0;
  }

  for(i=2;i<argc;i++)
  printf("%d\t==\t%s\n", atoi(argv[i]), tbase(atoi(argv[i]), atoi(argv[1]), s));

  return 0;
}

char *tbase(int n, int base, char *s)
{
  int c, i, b;
  if(base < 0)
  {
    printf("Base type cannot be under zero.\n");
    return NULL;
  }
  i=0;
  do
  {
    if(((n%base) >= 0) && ((n%base) <= 9))
   {
     *(s+i) = (n%base)+'0';
   }
  else
    *(s+i) = ((n%base)-10)+'a';
  ++i;
  } while((n /= base) > 0);
  *(s+i) = '\0';
  i=0;
  while(*(s+i)) i++;
  for(b=0, i--; b < i ; b++, i--)
  {
    c = *(s+b), *(s+b) = *(s+i), *(s+i) = c;
  }
  return s;
}

Any questions mail them to cpp@teentwo.8m.com

Link of the week 
http://www.cpp-
programming.
com/
Newsgroups
comp.lang.c
comp.lang.c++
comp.programming

This page is best viewed in 800x600x256. This page was last edited Monday, July 10, 2000

1