Wednesday, April 8, 2015

Natural Numbers. in c pgm

#include

int mult (int x, int y)
{
  int result;
  result = 0;
  while (y != 0)
{
    result = result + x;
   y = y - 1;
  }
  return(result);
}

int main ()
{
  int x, y;
  cout << "Enter two natural numbers: ";
  cin >> x >> y;
  cout << x << " * " << y << " = " << mult(x,y) << endl;
  return(0);
}



Leap in Year in c Program

#include
#include
#include
typedef int Bool;
char whatDay (int);

Bool isLeapYear (int);

int numOfDaysInMonth (int,Bool);

void printHeader (int);

void printMonth (int, int&);

void skip (int);

void skipToDay (int);

void disaster ();

void main ()
 {

  int year, firstDayInCurrentMonth;
  Bool leap;
  int currentMonth = 1;

  cout << "What year do you want a calendar for? ";
  cin >> year;
  cout << "What day of the week does January 1 fall on?" << endl;
  cout << "(Enter 0 for Sunday, 1 for Monday, etc.) ";
  cin >> firstDayInCurrentMonth;

  leap = isLeapYear(year);
  skip(9); cout << year << endl;

  while (currentMonth <= 12)
 {
    numDays = numOfDaysInMonth(currentMonth,leap);
    printHeader(currentMonth);
    printMonth(numDays, firstDayInCurrentMonth);
    cout << endl << endl << endl;
    currentMonth = currentMonth + 1;
  }
  cout << endl;
}

void disaster ()
{
  cout << "Disaster! Exiting ..." << endl;
  exit (-1);
}

void skip (int i)
{
  while (i > 0)
{
    cout << " ";
    i = i - 1;
  }
}

char whatDay (int d)
{
  if (d == 0) return('S');
  else if (d == 1) return('M');
  else if (d == 2) return('T');
  else if (d == 3) return('W');
  else if (d == 4) return('T');
  else if (d == 5) return('F');
  else if (d == 6) return('S');
  else disaster();
}

Bool isLeapYear (int y)
{
  return ((y % 4) == 0);
}

void printHeader (int m)
{

  if (m == 1) { skip(7); cout << "January" << endl;
}
  else if (m == 2) { skip(7); cout << "February" << endl;
}
  else if (m == 3) { skip(7); cout << "March" << endl;
}
  else if (m == 4) { skip(7); cout << "April" << endl;
}
  else if (m == 5) { skip(7); cout << "May" << endl;
}
  else if (m == 6) { skip(7); cout << "June" << endl;
}
  else if (m == 7) { skip(7); cout << "July" << endl;
}
  else if (m == 8) { skip(7); cout << "August" << endl;
}
  else if (m == 9) { skip(7); cout << "September" << endl;
}
  else if (m == 10) { skip(7); cout << "October" << endl;
}
  else if (m == 11) { skip(7); cout << "November" << endl;
}
  else if (m == 12) { skip(7); cout << "December" << endl;
}
  else disaster();

  cout << " S  M  T  W  T  F  S" << endl;
  cout << "____________________" << endl;
}

int numOfDaysInMonth (int m, Bool leap)
{
  if (m == 1) return(31);
  else if (m == 2) if (leap) return(29); else return(28);
  else if (m == 3) return(31);
  else if (m == 4) return(30);
  else if (m == 5) return(31);
  else if (m == 6) return(30);
  else if (m == 7) return(31);
  else if (m == 8) return(31);
  else if (m == 9) return(30);
  else if (m == 10) return(31);
  else if (m == 11) return(30);
  else if (m == 12) return(31);
  else disaster();
}

void skipToDay (int d)
{
  skip(3*d);
}

void printMonth (int numDays, int& weekDay)
{
  int day = 1;

  skipToDay(weekDay);
  while (day <= numDays)
{
    cout << setw(2) << day << " ";
    if (weekDay == 6)
 {
      cout << endl;
      weekDay = 0;
    }
    else weekDay = weekDay + 1;
    day = day + 1;
  }
}

Boolean in c Program

#include
#include
#include

typedef int Bool;
const Bool TRUE = 1;
const Bool FALSE = 0;

void readLetter(char&);
int computeCorrespondingDigit(char);
Bool doOneLetter();

void main ()
{
  Bool flag = FALSE;

  while (!flag)
{
    flag = doOneLetter();
  }
}

int exitLetter (char c)
{
  return ((c == 'Q') || (c == 'Z'));
}

Bool doOneLetter()
{
  char l;
  int i;

  readLetter(l);
  if (exitLetter(l))
{
    cout << "Quit." << endl;
    return(TRUE);
  }
  Else
 {
    i = computeCorrespondingDigit(l);
    cout << "The letter " << l << " corresponds to " << i << " on the telephone"<< endl;
    return(FALSE);
  }
}

int computeCorrespondingDigit (char l)
{
  switch (l)
{
  case 'A' :
  case 'B' :
  case 'C' :
    return(2);
  case 'D' :
  case 'E' :
  case 'F' :
    return(3);
  case 'G' :
  case 'H' :
  case 'I' :
    return(4);
  case 'J' :
  case 'K' :
  case 'L' :
    return(5);
  case 'M' :
  case 'N' :
  case 'O' :
    return(6);
  case 'P' :
  case 'R' :
  case 'S' :
    return(7);
  case 'T' :
  case 'U' :
  case 'V' :
    return(8);
  case 'W' :
  case 'X' :
  case 'Y' :
    return(9);
  }
}

void readLetter(char& c)
{
  cout << "Enter a letter: ";
  cin >> c;
  while (cin && !isalpha(c))
{
    cout << "You have not entered a letter. Try again: ";
    cin >> c;
  }
  if (!cin)
{
    cout << "Unrecoverable error... Exiting" << endl;
    exit(-1);
  }
  c = toupper(c);
}

Tuesday, April 7, 2015

RGB Color in C Program

 #include
#include
#include

enum Color
{Red, Blue, White, Green, Yellow};

const double DUMMY = 0.0;
const Color DUMMYC = White;
void show (Color);
void point (char, double);
void colorPoint (char, double, Color);

void main ()
{
  point('O', DUMMY);
  point('P', DUMMY);
  point('X', 8.0);
  point('P', DUMMY);
  point('Y', 7.0);
  point('P', DUMMY);
  point('X', 2.0);
  point('P', DUMMY);
  point('X', 3.0);
  point('P', DUMMY);
  point('O', DUMMY);
  point('P', DUMMY);
  colorPoint('O',DUMMY,DUMMYC);
  colorPoint('P',DUMMY,DUMMYC);
  colorPoint('C',DUMMY,Blue);
  colorPoint('P',DUMMY,DUMMYC);
  colorPoint('X',2.0,DUMMYC);
  colorPoint('P',DUMMY,DUMMYC);
}

void point (char message, double data)
{
  static double x_coord = 0.0;
  static double y_coord = 0.0;
  static double distance = 0.0;

  switch (message)
 {
  case 'X':
          x_coord = x_coord + data;
    break;
  case 'Y':  
          y_coord = y_coord + data;
    break;
    case 'O':
    x_coord = 0.0;
    y_coord = 0.0;
    break;
  case 'P':
    cout << "I am currently at x = " << x_coord << ", y = " << y_coord << endl;
    cout << "My distance from the origin is " << distance << endl;
    break;
  default:
    cout << "Unknown message to point" << endl;
    exit(-1);
  }
  distance = sqrt (x_coord * x_coord + y_coord * y_coord);
  return;
}

void colorPoint (char message, double data, Color color)
 {
  static Color col = White;

  switch (message)
{
  case 'X':
  case 'Y':
  case 'O':
    point(message,data);
    break;
  case 'C':
    col = color;
    break;
  case 'P':
    point('P',DUMMY);
    cout << "Also my color is ";
    show(col);
    cout << endl;
    break;
  default:
    cout << "Unknown message to colorPoint" << endl;
    exit(-1);
  }
  return;
}

void show (Color col)
{
  switch (col)
{
  case Red:
    cout << "Red";
    break;
  case Blue:
    cout << "Blue";
    break;
  case White:
    cout << "White";
    break;
  case Green:
    cout << "Green";
    break;
  case Yellow:
    cout << "Yellow";
    break;
  }
}

Simple Program of C Program

#include
void f ();
int i = 0;
void main ()
 {
  cout << i << endl;
  int i = 1;
  cout << i << endl;
  {
    int i = 2;
    cout << i << endl;
    {
      int i = 3;
      cout << i << endl;
      {
int i = 4;
cout << i << endl;
      }
      cout << i << endl;
    }
    cout << i << endl;
  }
  cout << i << endl;
  f(); f(); f();
}

void f ()
{
  cout << i << endl;
  int i = 5;
  cout << i++ << endl;
}

C Program of Simple Sum

#include
void inc (int, int&);
void main ()
 {
  int i = 3;
  int j;

  inc(i,j);
  cout << j << endl;
}

void inc (int a, int& b)
{
  b = a+1;
}


Sum of the Digit in c program

#include
void main () 
{
  int num, count = 1;
  float sum = 0.0;

  cout << endl;
  while (count <= 3) 
{
    cout << "Enter a number: ";
    cin >> num;
    sum = sum + num;
    count = count + 1;
  }
  cout << "Average is " << sum / count << endl;
}

There are several ways of fixing the problem. Here is one of them:
*/

void main () 
{
  int num, count = 1;
  float sum = 0.0;

  cout << endl;
  while (count <= 3)
 {
    cout << "Enter a number: ";
    cin >> num;
    sum = sum + num;
    count = count + 1;
  }
  cout << "Average is " << sum / (count - 1) << endl;
}


#include
int inc (int);
void main () 
{
  int i = 3;
  int j;

  j = inc (i);
  cout << j << endl;
}

int inc (int a) 
{
  return(a+1);
}


NEWLINE in a c pgm

#include
#include

typedef int Bool;
const Bool TRUE = 1;
const Bool FALSE = 0;

const char SPACE = ' ';
const char NEWLINE = '\n';

void main ()
{
  ifstream f;
  char c;
  int numGaps;

  f.open("test.words");

  numGaps = 0;
  f.get(c);
  while (f)
 {
    if (c == NEWLINE)
 {
      numGaps = numGaps + 1;
      f.get(c);
    }
    else if (c == SPACE)
 {
      while (f && c == SPACE)
 {
f.get(c);
      }
      numGaps = numGaps + 1;
    }
    else f.get(c);
  }
  cout << "There are " << numGaps << " words" << endl;
}

True or False in c program

#include
#include
typedef int Bool;
const Bool TRUE = 1;
const Bool FALSE = 0;

int readInt (Bool&);
int sumSquares (int i);

void main ()
{
  int i;
  Bool endOfData;

  i = readInt(endOfData);
  while (!endOfData)
{
    cout << "Sum of squares from 1 to " << i << " = " <<
      sumSquares (i) << endl;
    i = readInt(endOfData);
  }
}

int readInt (Bool& b)
{
  int i;

  cout << "Enter an integer greater than 1 (negative number to exit): ";
  cin >> i;
  while (i == 0)
{
    cout << "Re-enter integer: ";
    cin >> i;
  }
  if (i < 1) b = TRUE; else b = FALSE;
  return(i);
}

int sumSquares (int i)
{
  int count, result;

  count = 1;
  result = 0;
  while (count <= i)
{
    result = result + count * count;
    count = count + 1;
  }
  return(result);
}

Horizantal in a c pgm

#include
void main ()
{
  int width, horizPos, vertPos, beginHoriz, endHoriz;
  char c;
 
  cout << "Enter a character: ";
  cin >> c;
  cout << "Enter an integer: ";
  cin >> width;

  if ((width % 2) == 0) width = width+1;

  horizPos = 0;
  beginHoriz = width / 2;
  endHoriz = beginHoriz;
  while (beginHoriz >= 0)
 {
    while (horizPos < width)
{
      if (horizPos < beginHoriz) cout << " ";
      else if (horizPos <= endHoriz) cout << c;
      else cout << " ";
      horizPos = horizPos + 1;
    }
    cout << endl;
    horizPos = 0;
    beginHoriz = beginHoriz - 1;
    endHoriz = endHoriz + 1;
  }

  horizPos = 0;
  beginHoriz = 1;
  endHoriz = width - 2;
  while (beginHoriz <= width / 2)
 {
    while (horizPos < width)
{
      if (horizPos < beginHoriz) cout << " ";
      else if (horizPos <= endHoriz) cout << c;
      else cout << " ";
      horizPos = horizPos + 1;
    }
    cout << endl;
    horizPos = 0;
    beginHoriz = beginHoriz + 1;
    endHoriz = endHoriz - 1;
  }

}

Monday, March 23, 2015

Boolean of C Program



 
#include 
 
typedef int Bool;
const Bool TRUE = 1;
const Bool FALSE = 0;
 
Bool even (int);
Bool odd (int);
int readPosNum();
void testOneNum();
void panic();
 
void main () 
{
  int i;
  char c;
  Bool more = TRUE;
 
  while (cin && more)
 {
    testOneNum();
    cout << "More? [y = Yes, anything else No]: ";
    cin >> c;
    if (cin) more = (c == 'y');
  }
}
 
void testOneNum () 
{
  int i;
 
  i = readPosNum();
  if (even(i)) cout << "The number " << i << " is even." << endl;
  else cout << "The number " << i << " is odd." << endl;
}
 
int readPosNum () 
{
  int j;
 
  cout << "Enter a number >= 0: ";
  cin >> j;
  while (cin && j < 0) 
{
    cout << "Unacceptable, reenter: ";cin >> j;
  }
  if (cin) return(j);
  else panic();
}
 
Bool even (int i)
 {
  if (i == 0) return(TRUE);
  else return(odd(i-1));
}
 
Bool odd (int i) 
{
  if (i == 0) return(FALSE);
  else return(even(i-1));
}
 
void panic() 
{
  cout << "Disaster! Exiting ..." << endl;
  exit(-1);
}
 

While in C Program



#include 
#include 
 
int exp (int,int);
void readnums (int&, int&);
 
void main ()
 {
  int b, e;
  readnums(b,e);
  cout << b << " to the " << e << " = " << exp(b,e) << endl;
}
 
void readnums (int& b, int& e) 
{
  int correctInput;
 
  cout << "Enter the base and the exponent: ";
  cin >> b >> e;
 
  if (!cin) 
{ 
    cout << "Disaster! Terminating program." << endl;
    exit(-1);
  }
 
  correctInput = (b >= 0) && (e >= 0);
  while (!correctInput)
 {
    cout << "Something wrong! Try again ..." << endl;
    cout << "Re-enter base and exponent: ";
    cin >> b >> e;
    
    if (!cin) 
{ 
      cout << "Disaster! Terminating program." << endl;
      exit(-1);
    }
    correctInput = (b >= 0) && (e >= 0);
  }
  
}
 
int exp (int b, int e) 
{
  int result;
 
  result = 1;
  while (e != 0) 
{
    result = result * b;
    e = e - 1;
  }
  return(result);
}

Initial of C Programs



#include 
 
void main () 
{
  int i;
 
  i=1;
  cout << "Initial value of i = " << i << ". The value of ++i is " << ++i<<". Now i = " << i << endl;
 
  i=1;
  cout << "Initial value of i = " << i 
    << ". The value of i++ is " << i++ 
      << ". Now i = " << i << endl;
}
 

Sum of the Degits on c pgm




#include 
 
int f1 (int, int, int);
int f2 (int&, int&, int&);
int f3 (int, int&, int);
 
void main ()
 {
  int i, j, k;
 
  i=1;
  j=2;
  k=3;
 
  cout << endl;
  cout << "Initial values of i, j, and k are: " 
    << i << ", " << j << ", and " << k << endl << endl;
  cout << "f1(i,j,k) = " << f1(i,j,k) << endl;
  cout << "Values of i, j, and k after the call to f1 are: " 
    << i << ", " << j << ", and " << k << endl << endl;
  cout << "f2(i,j,k) = " << f2(i,j,k) << endl;
  cout << "Values of i, j, and k after the call to f2 are: " 
    << i << ", " << j << ", and " << k << endl << endl;
  cout << "f3(i,j,k) = " << f3(i,j,k) << endl;
  cout << "Values of i, j, and k after the call to f3 are: " 
    << i << ", " << j << ", and " << k << endl;
}
 
int f1 (int x, int y, int z)
{
  x=x+5;
  y=y+5;
  z=z+5;
  return(x+y+z);
}
 
int f2 (int& x, int& y, int& z)
 {
  x=x+5;
  y=y+5;
  z=z+5;
  return(x+y+z);
}
 
int f3 (int x, int& y, int z) 
{
  x=x+5;
  y=y+5;
  z=z+5;
  return(x+y+z);
}