Learn C programming in 11 days

Day 10 Samples

21 match sticks

 

/*********************************************************/

/*  matches.cpp - VC5 program                            */

/*  Match picking - simple game                          */

/*  J LUO, 29-Jun-2000                                   */

/*********************************************************/

 

#include<iostream.h>

 

const char NL='\n';

 

void main() {

  cout << "I have got 21 mathces. Let's pick them up in turn. \n\n";

  cout << "You may pick up 1, 2, 3, or 4 \n";

  cout << "If you pick up the last one, you loss. \n\n";

 

  int n = 21;          // the number to matches

  int i;               // the number you take

 

  while ( n>1) {

    cout << "You take ... ";

    cin >> i;

    if ( (i<1) || (i>4) ) {

      cout << "Don't try to cheat me, take 1, 2, 3, or 4, please! \n";

     } else {

      cout << "I take " << 5-i << endl;

      n = n -5;         // I'm smart!

     }

  }

  cout << " Now take the last one, please. \n";

  cout << " And you loss :-) \n\n";

}

 

Guess a number

 

/*********************************************************/

/*  guess.cpp - VC5.0                                    */

/*  Guess a number - simple game                         */

/*  J LUO, 29-Jun-2000                                   */

/*  J Luo, 22-Feb-2001                                   */

/*  Modified, change to printf/scanf                     */

/*  J Luo, 22-Feb-2001                                   */

/*********************************************************/

 

#include<iostream.h>

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

 

int getNumber();

 

void main()

{

 

   int n = getNumber();

 

   char yesNo = 'y';

   int m;

   int k;

 

 

   printf("I have got a number betweeen 1 and 100. \n");

   printf("Would you like to guess? [Y/N]");

   scanf("%c", &yesNo);

  

   if ( yesNo == 'y' ) yesNo = 'Y';

 

   k = 0;

   while ( yesNo == 'Y' )

   {

       printf("It is: ");

       scanf("%d", &m);

       k = k + 1;

       if ( m < n )

       {

         printf("%d is too small! \n", m);

      } else if ( m>n ) {

         printf("%d is too large! \n", m);

      } else {

         printf("Good, you did it in %d times! \n", k);

         printf("Yes, the number is: %d \n", n);

         break;             

      }

       if ( k >= 8 )

       {

         printf("Sorry, you did it in %d times! \n", k);

          printf("The number is: %d \n", n);

          break;

      }

   }

 

   if ( k == 0 )

   {

      printf(" Hope you like this game! \n");

   } else if ( k <= 7 ) {

      printf(" You are smart! :-) \n");

   } else {

      printf(" You need some practice. :-( \n");

   }

}

 

int getNumber()

{

   srand( (unsigned) time( NULL ) );

   for (int i=0; i<1000; i++) rand();

 

   int n;

   n =  100L * rand()/(RAND_MAX+1) + 1;

   return n;

}

 

 

 

/*********************************************************/

/*  calander.cpp - VC6.0                                 */

/*  Print out a calendar.                                */

/*  J Luo, 02-Jun-2001                                   */

/*********************************************************/

 

#include<stdio.h>

 

int InputYear(int *, int []);

void PutTable(int, int [], char [12][42][3]);

void PrintTable(int *, char [12][42][3]);

void PrintHeader(int);

void PrintCheck(char table[12][42][3]);

                                            

void main()

{

     int days, year;

     int monthN[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

     char table[12][42][3] = {' '};

 

     days = InputYear(&year, monthN);

     PutTable(days, monthN, table);

    PrintTable(&year, table);

}

 

int InputYear(int *y, int monthN[])

{

  int days, year;

 

  printf("Which year? ");

  scanf("%d", &year);

  *y = year;

 

  if ( year%4   == 0 ) monthN[1] = 29;

  if ( year%100 == 0 ) monthN[1] = 28;

  if ( year%400 == 0 ) monthN[1] = 29;

 

  year = year - 1;

  days = 365*year + year/4 - year/100 + year/400 + 1;

    

  return days;

}

 

void PutTable(int days, int monthN[], char table[12][42][3])

{

     int firstDay, lastDay;

     int i, j;

     int n1, n2, m;

 

     for (i=0; i<12; i++) {

          firstDay = days%7-1;

          lastDay = firstDay + monthN[i];

         for (j=firstDay+1; j<lastDay+1; j++) {

              m = j - firstDay;

              n1 = m/10;

              n2 = m - n1*10;

              if ( n1 != 0 ) table[i][j][1] = n1 + 48;

              table[i][j][2] = n2 + 48;

         }

         days = lastDay + 1;

     }

//     PrintCheck(table);

}

 

void PrintTable(int *year, char table[12][42][3])

{

     int i, j, k, l, m;

     int n1, n2;

    

     printf("%33s%4d%s\n", " ***** ", *year, " ***** ");

 

     for (m=0; m<12; m=m+3) {

          PrintHeader(m);

         n1=0;

         for (l=0; l<6; l++) {

              n2 = n1 + 7;

              for (i=m; i<m+3; i++) {

                   printf("|");

                   for (j=n1; j<n2; j++) {

                       for (k=0; k<3; k++) {

                           printf("%c", table[i][j][k]);

                       }

                   }

                   printf(" ");

              }

              printf("|\n");

              n1 = n2;

         }

     }

     for (j=0; j<70; j++) printf("%c", '-');

     printf("\n");

 

}

 

void PrintHeader(int i)

{

     char *day = {" S  M  T  W  T  F  S "};

     char monthC[12][4] =

     {

          "Jan","Feb","Mar","Apr","May","Jun",

          "Jul","Aug","Sep","Oct","Nov","Dec"

     };

     int j;

 

     for (j=0; j<70; j++) printf("%c", '-');

     printf("\n");

     for (j=i; j<i+3; j++) printf("| %11s          ", monthC[j]);

     printf("|\n");

     for (j=0; j<70; j++) printf("%c", '-');

     printf("\n");

     for (j=i; j<i+3; j++) printf("| %s", day);

     printf("|\n");

 

}

 

void PrintCheck(char table[12][42][3])

{

     int i, j, k;

 

     for (i=0; i<12; i++) {

         for (j=0; j<42; j++) {

              if (j%7==0 ) printf("\n");

              for (k=0; k<3; k++) {

                  printf("%c", table[i][j][k]);

              }

         }

          printf("\n");

     }

}

 

 

 

Which year? 2001

                            ***** 2001 *****

----------------------------------------------------------------------

|         Jan          |         Feb          |         Mar          |

----------------------------------------------------------------------

|  S  M  T  W  T  F  S |  S  M  T  W  T  F  S |  S  M  T  W  T  F  S |

|     1  2  3  4  5  6 |              1  2  3 |              1  2  3 |

|  7  8  9 10 11 12 13 |  4  5  6  7  8  9 10 |  4  5  6  7  8  9 10 |

| 14 15 16 17 18 19 20 | 11 12 13 14 15 16 17 | 11 12 13 14 15 16 17 |

| 21 22 23 24 25 26 27 | 18 19 20 21 22 23 24 | 18 19 20 21 22 23 24 |

| 28 29 30 31          | 25 26 27 28          | 25 26 27 28 29 30 31 |

|                      |                      |                      |

----------------------------------------------------------------------

|         Apr          |         May          |         Jun          |

----------------------------------------------------------------------

|  S  M  T  W  T  F  S |  S  M  T  W  T  F  S |  S  M  T  W  T  F  S |

|  1  2  3  4  5  6  7 |        1  2  3  4  5 |                 1  2 |

|  8  9 10 11 12 13 14 |  6  7  8  9 10 11 12 |  3  4  5  6  7  8  9 |

| 15 16 17 18 19 20 21 | 13 14 15 16 17 18 19 | 10 11 12 13 14 15 16 |

| 22 23 24 25 26 27 28 | 20 21 22 23 24 25 26 | 17 18 19 20 21 22 23 |

| 29 30                | 27 28 29 30 31       | 24 25 26 27 28 29 30 |

|                      |                      |                      |

----------------------------------------------------------------------

|         Jul          |         Aug          |         Sep          |

----------------------------------------------------------------------

|  S  M  T  W  T  F  S |  S  M  T  W  T  F  S |  S  M  T  W  T  F  S |

|  1  2  3  4  5  6  7 |           1  2  3  4 |                    1 |

|  8  9 10 11 12 13 14 |  5  6  7  8  9 10 11 |  2  3  4  5  6  7  8 |

| 15 16 17 18 19 20 21 | 12 13 14 15 16 17 18 |  9 10 11 12 13 14 15 |

| 22 23 24 25 26 27 28 | 19 20 21 22 23 24 25 | 16 17 18 19 20 21 22 |

| 29 30 31             | 26 27 28 29 30 31    | 23 24 25 26 27 28 29 |

|                      |                      | 30                   |

----------------------------------------------------------------------

|         Oct          |         Nov          |         Dec          |

----------------------------------------------------------------------

|  S  M  T  W  T  F  S |  S  M  T  W  T  F  S |  S  M  T  W  T  F  S |

|     1  2  3  4  5  6 |              1  2  3 |                    1 |

|  7  8  9 10 11 12 13 |  4  5  6  7  8  9 10 |  2  3  4  5  6  7  8 |

| 14 15 16 17 18 19 20 | 11 12 13 14 15 16 17 |  9 10 11 12 13 14 15 |

| 21 22 23 24 25 26 27 | 18 19 20 21 22 23 24 | 16 17 18 19 20 21 22 |

| 28 29 30 31          | 25 26 27 28 29 30    | 23 24 25 26 27 28 29 |

|                      |                      | 30 31                |

----------------------------------------------------------------------

 

Common Greatest Factor  - simple approach

 

// cgd.cpp - C++ program, VC5.0

// Find the common greatest divider

// P. 95, Chapter 3, No. 4-3

// Simple approach

// J Luo, 14-Sep-2000

 

#include<iostream.h>

 

void main()

{

     int a, b, t;

 

     cout << "Enter two integers: ";

     cin >> a >> b;

    

     if ( a > b ) {

         t = a;

         a = b;

         b = t;

     }

 

     for ( int i=a; i>=2; i--) {

         if ( a%i == 0 ) {

              if (b%i == 0 ) {

                   cout << i << endl;

                   break;

              }

         }

     }

}

 

Simple recursive function

 

/*********************************************************/

/*  digits1.cpp - C++ program                            */

/*  Digits counting                                      */

/*  Simple recursive function                            */

/*  J LUO, 12/10/1999, Suzhou                            */

/*  Modified                                             */

/*  J Luo, 03-Jun-2001                                   */

/*********************************************************/

 

#include <iostream.h>

 

int digit(int);

 

void main()

{

  int n = 12345;

 

  cout << n << endl;

  digit(n);

}

 

int digit(int n)

{

  int m;

 

  m = n % 10;

  n = n / 10;

 

  if ( n ) digit(n);

  cout << " M = " << m << "\t";

  cout << " N = " << n << "\n";

  return m ;

}  

 

DotPlot

 

 

// dotplot1b.cpp - VC 5.0 program

// dot matrix plot, different length of the two sequence

// J Luo, 07-Nov-2000

 

#include<iostream.h>

#include<string.h>

 

void main()

{

     const int n1= 17;  // error if n1=16

     const int n2= 13;  // error if n2=12

 

     char seq1[n1];

     char seq2[n2];

 

     strcpy(seq1,"ACGTACGTACGTACGT");

     strcpy(seq2,"ACGTACGTACGT");

    

     cout << ' ';

     for (int i1=0; i1<n1-1; i1++) cout << seq1[i1];     // X axis for seq1

     cout << endl;

 

     for (int i2=0; i2<n2-1; i2++)         // Y axis for seq2

     {

         cout << seq2[i2];

         for (i1=0; i1<n1-1; i1++)

         {

              if ( seq2[i2] == seq1[i1] )

              {

                   cout << 'x';

              } else {

                   cout << ' ';

              }

 

         }

         cout << endl;

     }

}

 

 

// dotplot2.cpp - VC 5 program

// dot matrix plot

// J Luo, Shan Guan, 01-Nov-2000

// J Luo, Modified for different sequence size, 7-Nov-2000

 

#include<iostream.h>

#include<string.h>

 

void plot(char *, int, char *, int);

 

void main()

{

     int n1= 16;

     int n2= 12;

 

     char *seq1;

     char *seq2;

 

     seq1 = "ACGTACGTACGTACGT";

     seq2 = "ACGtACGtACGt";

 

     plot(seq1, n1, seq2, n2);

}

 

void plot(char *seq1, int n1, char *seq2, int n2)

{

     cout << ' ';

    for (int x=0; x<n1; x++) cout << seq1[x];

     cout << endl;

 

    for (int y=0; y<n2; y++)

     {

         cout << seq2[y];

         for (x=0; x<n1; x++)

         {

              if ( seq2[y] == seq1[x] )

              {

                   cout << 'x';

              } else {

                   cout << ' ';

              }

 

         }

         cout << endl;

     }

}

 

 

// dotplot3.cpp - VC 5.0 program

// dot matrix plot

// Dynamic array

// J Luo, Shan Guan, 01-Nov-2000

// Bug fixed by Fang Gang, 08-Nov-2000

 

#include<iostream.h>

#include<string.h>

 

void PutDot(char *, int, char *, int, char *&);

void Plot(char *, int, char *, int, char *&);

 

void main()

{

     int n1= 16;

     int n2= 12;

 

     char *seq1;

     char *seq2;

     char *dot;

 

     dot = new char[n1*n2];

 

     seq1 = "ACGTACGTACGTACGT";

     seq2 = "ACGTACGTACGT";

 

    PutDot(seq1, n1, seq2, n2, dot);

     Plot(seq1, n1, seq2, n2, dot);

}

 

void PutDot(char *seq1, int n1, char *seq2, int n2, char * &dot)

{

 

    for (int y=0; y<n2; y++)

     {

         for (int x=0; x<n1; x++)

         {

              if ( seq2[y] == seq1[x] )

              {

                   dot[x*n2+y] = 'x';        // Error: dot[x*n1+y] = 'x'

              } else {

                   dot[x*n2+y] = ' ';        // Error: dot[x*n1+y] = ' '

              }

         }

     }

}

 

void Plot(char * seq1, int n1, char * seq2, int n2, char *&dot)

{

     cout << ' ';

    for (int x=0; x<n1; x++) cout << seq1[x];

     cout << endl;

 

    for (int y=0; y<n2; y++)

     {

         cout << seq2[y];

         for (int x=0; x<n1; x++)

         {

              cout << dot[x*n2+y];

         }

         cout << endl;

     }

}

 

 

// dotplot5.cpp - VC 5 program

// dot matrix plot

// using files

// J Luo, Shan Guan, 01-Nov-2000

 

#include<iostream.h>

#include<string.h>

#include<fstream.h>

 

void PutDot(char *, int, char *, int, char *&);

void Plot(char *, int, char *, int, char *&);

void GetData(char *&, int, char *&, int);

 

void main()

{

     int n1= 16;

     int n2= 16;

 

     char *seq1;

     char *seq2;

     char *dot;

 

     dot = new char[n1*n2];

 

     GetData(seq1, n1, seq2, n2);

    PutDot(seq1, n1, seq2, n2, dot);

     Plot(seq1, n1, seq2, n2, dot);

}

 

void GetData(char * &seq1, int n1, char * &seq2, int n2)

{

     seq1 = "ACGTACGTACGT";

     seq2 = "ACGTACGTACGT";

 

     ifstream fin;

//     fin.open("test.txt");

 

     char *t1 = new char[n1];

     char *t2 = new char[n2];

 

     fin >> t1;

     fin >> t2;

 

//   seq1 = t1;

//   seq2 = t2;

 

    for (int i=0; i<n1; i++) cout << seq1[i];

     cout << endl;

 

    for (int j=0; j<n2; j++) cout << seq2[j];

     cout << endl;

}

 

void PutDot(char *seq1, int n1, char *seq2, int n2, char * &dot)

{

    for (int i=0; i<n1; i++)

     {

          for (int j=0; j<n2; j++)

         {

              if ( seq1[i] == seq2[j] )

              {

                   dot[i*n2+j] = 'x';

              } else {

                   dot[i*n2+j] = ' ';

              }

 

         }

     }

}

 

void Plot(char * seq1, int n1, char * seq2, int n2,char *&dot)

{

    for (int i=0; i<n1; i++) cout << seq1[i];

     cout << endl;

 

    for (int j=0; j<n2; j++) cout << seq2[j];

     cout << endl;

 

     for (i=0; i<n1; i++)

     {

         for (j=0; j<n2; j++)

         {

              cout << dot[i*n2+j];

         }

         cout << endl;

     }

}

 

 

// birthday.cpp - C++ program, VC5.0

// J Luo, 07-Sep-2000, ICE 599, Gottingen->Berlin

 

#include<iostream.h>

#include<string.h>

 

class Tdate

{

     public:

         void getDate();

         void printDate();

         void isLeap();

 

     private:

         int y, m, d;   

         char mon[3];

};      

 

void Tdate::getDate()

{

     cout << "Your birhday: ";

     cin >> y >> m >> d;

}

 

void Tdate::printDate()

{

     switch ( m )

     {

         case  1: strcpy(mon,"Jan"); break;

         case  2: strcpy(mon,"Feb"); break;

         case  3: strcpy(mon,"Mar"); break;

         case  4: strcpy(mon,"Apr"); break;

         case  5: strcpy(mon,"May"); break;

         case  6: strcpy(mon,"Jun"); break;

         case  7: strcpy(mon,"Jul"); break;

         case  8: strcpy(mon,"Aug"); break;

         case  9: strcpy(mon,"Sep"); break;

        case 10: strcpy(mon,"Oct"); break;

        case 11: strcpy(mon,"Nov"); break;

        case 12: strcpy(mon,"Dec"); break;

          default: strcpy(mon,"???");

     }

     cout << d << "-" << mon << "-" << y << endl;

}

 

void Tdate::isLeap()

{

    int leap = ( (y%4 == 0) && (y%100 != 0) ) || (y%400==0);

     if ( leap ) cout << y << " is a leap year.";

     else cout << y << " is not a leap year. ";

     cout << endl;

}

 

void main()

{

 

     Tdate birthday;

        

     birthday.getDate();

     birthday.printDate();

     birthday.isLeap();

}

 

 

 

// swap.cpp - Turbo C++, V3.0

// J LUO, 4-Jun-2000

// swap two float numbers

 

#include<iostream.h>

 

void swap(float*, float*);

 

void main()

{

   float a, b;

 

   cout << "Enter 2 numbers ... " << endl;

   cin >> a >> b;

 

   cout << a << "\t" << b << "\t" << endl;

 

   if ( b < a ) swap(&a, &b);

 

   cout << a << "\t" << b << "\t" << endl;

 

}

 

void swap(float* x, float* y)

{

   float temp;

 

   temp = *x;

   *x = *y;

   *y = temp;

}

 

 

// jjcc.cpp - C++ program, VC5.0

// Simple arithmetic

// J Luo, 01-Sep-2000

 

#include<iostream.h>

 

void main()

{

     int a, b,c;

     char o;

 

     cout << "a = "; cin >> a;

     cout << "b = "; cin >> b;

 

     cout << "Operan: "; cin >> o;

 

     switch ( o )

     {

       case '+': c = a + b; break;

       case '-': c = a - b; break;

       case '*': c = a * b; break;

       case '/': c = a / b; break;

        default: cout << "Error operan: " << o << endl;

    }

     cout << a << o << b << " = " << c << endl;

}

 

 

// leapYear1.cpp - C++ program, VC5.0

// Is it a leap year?

// J Luo, 04-Sep-2000, Heinrich-Heine Hotel, Berlin

 

#include<iostream.h>

 

void main()

{

     int y;

     int leap;

 

     cout << "Which year? ";

     cin >> y;

 

     leap = 0;

//    if ( (y%4==0 && y%100!=0) || (y%400 == 0) ) leap = 1;

 

     if ( y%4   == 0 ) leap = 1;

     if ( y%100 == 0 ) leap = 0;

     if ( y%400 == 0 ) leap = 1;

 

     if ( leap == 1 ) cout << y << " is a leap year. ";

     else cout << y << " is not a leap year. ";

     cout << endl;

}

 

 

// leapYear2.cpp - C++ program, VC5.0

// Is it a leap year?

// Using function

// J Luo, 04-Sep-2000, Heinrich-Heine Hotel, Berlin

 

#include<iostream.h>

 

int isLeap(int);

 

void main()

{

     int year;

     int leap;

 

     cout << "Which year? ";

     cin >> year;

 

     leap = isLeap(year);

 

     if ( leap == 1 ) cout << year << " is a leap year. ";

     else cout << year << " is not a leap year. ";

     cout << endl;

}

 

int isLeap(int y)

{

    if ( (y%4==0 && y%100!=0) || (y%400 == 0) ) return 1;

     else return 0;

}

 

 

// leapYear3.cpp - C++ program, VC5.0

// Is it a leap year?

// Using classes

// 29 errors in 1st run: Class -> class

// 10 errors in 2ns run

// J Luo, 05-Sep-2000, Heinrich-Heine Hotel, Berlin

 

#include<iostream.h>

 

class Tdate       

{

     public:

         void setDate(int y, int m, int d);

         int isLeap();

         void print();

 

     private:

         int year, month, day;    

};      

 

void Tdate::setDate(int y, int m, int d)

{

     year  = y;

     month = m;

     day   = d;

}

 

int Tdate::isLeap()

{

    if ( (year%4==0 && year%100!=0) || (year%400 == 0) ) return 1;

     else return 0;

}

 

void Tdate::print()

{

     cout << year << "/" << month << "/" << day << endl;

}

 

void main()

{

 

    Tdate date1, date2;

 

     date1.setDate(2000,9,5);

     date1.print();

 

     date2.setDate(1961,5,24);

     date2.print();               

 

     int leap = date2.isLeap();    

     cout << leap << endl;

}

 

Practical programs made by Chen Yunjia

 

//C++ Program, Y.J. Chen, 2001-2-21

//Completed matrix in the Needleman-Wunsch(Global) algorithm.

 

#include <iostream.h>

#include <fstream.h>

#include <stdio.h>

 

const char *global1="global1.txt",*global2="global2.txt";

void main()

{

     ofstream output_file;

     int i,j,max,m,align[15][16];

     char seq1[20]="*ADLGAVFALCDRYFQ",seq2[20]="*ADLGRTQNCDRYYQ";

 

//根据匹配为一,不匹配为零原则,为距阵赋初值

 

     for(i=1;i<=14;i++)

     {

        for(j=1;j<=15;j++)

        {

           if (seq2[i]==seq1[j])

              align[i][j]=1;

           else

             align[i][j]=0;

        }

     }

 

//为距阵第一行、第一列赋初值

 

     for(i=0;i<=15;i++)

        align[0][i]=seq1[i];

     for(i=1;i<=14;i++)

        align[i][0]=seq2[i];

    

//标准输出Needleman-Wunsch初值距阵

 

     cout<<"初值距阵"<<endl;

     for(i=0;i<=15;i++)

        cout<<seq1[i]<<",";

 

     cout<<endl;

 

    for(i=1;i<=14;i++)

     {

          cout<<seq2[i]<<",";

         for(j=1;j<=15;j++)

             cout<<align[i][j]<<",";

         cout<<endl;

     }

 

//将Needleman-Wunsch初始距阵输出到文件global_align1.txt中

 

     output_file.open(global1);

 

     for(i=0;i<=15;i++)

        output_file<<seq1[i]<<",";

 

    output_file<<endl;

 

    for(i=1;i<=14;i++)

     {

          output_file<<seq2[i]<<",";

         for(j=1;j<=15;j++)

            output_file<<align[i][j]<<",";

         output_file<<endl;

     }

     output_file.close();

 

     cout<<endl;

 

//计算Smith-Waterman算法矩阵中每单元值

 

    for(j=14;j>=1;j--)

        for(i=13;i>=1;i--)

        { 

          {max=0;

          for(m=i+1;m<=14;m++)

             if(align[m][j+1]>max)

               max=align[m][j+1];

 

          for(m=j+1;m<=15;m++)

             if(align[i+1][m]>max)

               max=align[i+1][m];

         }

        align[i][j]+=max;

        }

 

     getchar();

//标准输出Needleman-Wunsch算法最终矩阵

     cout<<"最终距阵"<<endl; 

    

     for(i=0;i<=15;i++)

        cout<<seq1[i]<<",";

 

     cout<<endl;

    for(i=1;i<=14;i++)

     { cout<<seq2[i]<<",";

       for(j=1;j<=15;j++)

     { 

          cout<<align[i][j]<<",";}

         cout<<endl;

     }

 

//将Needleman-Wunsch最终距阵输出到文件global_align2.txt中

 

     output_file.open(global2);

 

     for(i=0;i<=15;i++)

        output_file<<seq1[i]<<",";

 

    output_file<<endl;

 

    for(i=1;i<=14;i++)

     {

          output_file<<seq2[i]<<",";

         for(j=1;j<=15;j++)

             output_file<<align[i][j]<<",";

 

         output_file<<endl;

     }

     output_file.close();

}

 

 

// series.cpp - C++ program, VC5.0

// 1 + x + x^2/2! + x^3/3! + ...

// J Luo, 17-Nov-2000

 

#include<iostream.h>

#include<math.h>

 

void main()

{

     int n;

     double e, x, t, sum;

 

     cout << "x = ";

     cin >> x;

 

     cout << "Epsilon = ";

     cin >> e;

 

     n = 1;

     t = x / n;

     sum = 1 + t;

     while ( fabs(t) > e )

     {

         n = n + 1;

         t = t * (x/n);

         sum = sum + t;

         cout << n << '\t' << t << '\t' << sum << endl;

     }

}

 

 

// overload.cpp - C++ program

// Function overload

// J Luo, 20-Oct-2000

 

#include<iostream.h>

 

void swap(int &, int &);

void swap(float &, float &);

 

void main()

{

     int x1, x2;

 

     cout << " Enter two integers ";

     cin >> x1 >> x2;

 

     cout << x1 << '\t' << x2 << endl;

 

     if ( x2<x1 ) swap(x1, x2);

 

     cout << x1 << '\t' << x2 << endl;

 

     float y1, y2;

     cout << " Enter two real numbers: ";

     cin >> y1 >> y2;

 

     cout << y1 << '\t' << y2 << endl;

 

     if ( y2<y1 ) swap(y1, y2);

 

     cout << y1 << '\t' << y2 << endl;

 

}

 

void swap(int &a, int &b)

{

     int temp;

 

     temp = a;

     a = b;

     b = temp;

}

 

void swap(float &a, float &b)

{

     float temp;

 

     temp = a;

     a = b;

     b = temp;

}

 

 

// struct1.cpp - Turbo C++, V3.0

// J LUO, 3-Jun-2000

// Simple structure demo

 

#include<iostream.h>

#include<string.h>

 

void getData();

void putData();

 

struct Student

{

   char name[20];

   long int id;

   float score;

};

 

void main()

{

   getData();

   putData();

}

 

const int n = 3;

 

Student bio[n];

 

void getData()

{

 

   int k = 0;

   while ( k < n)

   {

        cout << " Enter name, id, score [0 for stop]... " << endl;

        cin >> bio[k].name >> bio[k].id >> bio[k].score;

        k++;

   }

}

 

void putData()

{

   for (int i=0; i<n; i++)

   {

      cout << bio[i].name << "\t"

            << bio[i].id   << "\t"

            << bio[i].score<< endl;

   }

}

 

 

// struct1.cpp - Turbo C++, V3.0

// J LUO, 3-Jun-2000

// Simple structure demo

 

#include<iostream.h>

#include<string.h>

 

struct Student

{

   char name[20];

   long int id;

   float score;

};

 

void getData(int, Student[]);

void putData(int, Student[]);

void sortData(int, Student[]);

 

 

void main()

{

   const int n=3;

   Student bio[n];

 

   getData(n, bio);

   putData(n, bio);

   sortData(n, bio);

   putData(n, bio);

}

 

void getData(int n, Student bio[])

{

 

   cout << " Enter name, id, score ... " << endl;

 

   for (int i=0; i<n; i++)

   {

      cin >> bio[i].name >> bio[i].id >> bio[i].score;

   }

}

 

void sortData(int n, Student bio[])

{

   Student temp;

 

   for (int i=0; i<n-1; i++)

   {

      for (int j=i+1; j<n; j++)

      {

      if ( bio[j].score > bio[i].score )

      {

         temp = bio[i];

         bio[i] = bio[j];

         bio[j] = temp;

      }

      }

   }

}

 

void putData(int n, Student bio[])

{

   for (int i=0; i<n; i++)

   {

      cout << bio[i].name << "\t"

        << bio[i].id   << "\t"

        << bio[i].score<< endl;

   }

}

 

Dynamic array

 

// mean4.cpp - C++ program, VC5.0

// Mean of three numbers

// J Luo, 17-Oct-2000

// Using dynamic array

// Guan S and J Luo, 17-Oct-2000

 

void Input(float *, int);

float Mean(float [], int);

void Output(float [], int, float);

 

#include<iostream.h>

 

void main()

{

     int m;

     cout << "No of data: ";

     cin >> m;

 

     float *x;

     x = new float[m];

 

     float average;

     Input(x,m);

     average = Mean(x,m);

     Output(x,m,average);

}

 

void Input(float *x, int n)

{   

     cout << "Enter data: ";

    for(int i=0; i<n; i++)

     {

         cin >> x[i];

     }

}

 

float Mean(float x[], int n)

{   

     float sum, mean;

 

     sum = 0;

    for (int i=0; i<n; i++){

         sum = sum + x[i];

     }

     mean = sum / n;

 

     return mean;

}

 

void Output(float x[], int n, float mean)

{   

 

    for (int i=0; i<n; i++) {

         cout << x[i] << '\t';

     }

    cout << endl;

 

     cout << "Mean: " << mean << endl;

}

 

 

// sort_swap.cpp - C++ program, VC 5.0

// Sawp sorting

// Parameter transfer by pointer and reference

// J Luo, 17-Oct-2000

// Modified, 19-Oct-2000

 

#include<iostream.h>

 

void Input(float *, int);

void SwapSort(int *, int);

void SwapSort(float *, int);

inline void Swap(int &, int &);

inline void Swap(float &, float &); // Error if no &

 

void main()

{

     int n;

     cout << "No of data: ";

     cin >> n;

 

     float *x;

     x = new float[n];

 

     Input(x, n);

     SwapSort(x, n);

}

 

void Input(float *x, int n)

{

 

     cout << "Enter data: " << endl;

    for (int i=0; i<n; i++) cin >> *(x+i);

 

}

 

void SwapSort(int *x, int n)

{

     int i, j;

 

     for (i=0; i<n-1; i++)

         for (j=i+1; j<n; j++)

              if (x[j] < x[i] ) Swap(x[j], x[i]);

 

     for (i=0; i<n; i++) cout << *(x+i) << "\t";

     cout << endl;

}

 

void SwapSort(float *x, int n)

{

     int i, j;

 

     for (i=0; i<n-1; i++)

         for (j=i+1; j<n; j++)

              if (x[j] < x[i] ) Swap(x[j], x[i]); // Reference

 

     for (i=0; i<n; i++) cout << *(x+i) << "\t";

     cout << endl;

}

 

void Swap(int &a, int &b)  // Error if no &

{

     int temp;

 

     temp = a;

     a = b;

     b = temp;

}

 

void Swap(float &a, float &b)

{

     float temp;

 

     temp = a;

     a = b;

     b = temp;

}

 

 

// q_sort1 - Turbo C++, Ver. 3.0

// S Guan & J LUO, 24-Apr-2000                                                                           *

// Quick sort - method 1

 

#include<iostream.h>

#include<iomanip.h>

#include<stdio.h>

#include <stdlib.h>

#include <time.h>

 

void quickSort(int [], int, int);

 

const long n = 10;

 

void main()

{

     int a[n] = {5, 3, 7, 2, 8, 4, 9, 6, 1, 0};

     int left, right;

     int i;

 

 

     cout << " Before sorting ... " << endl;

     for (i=0; i<n; i++) cout << setw(8) << a[i];

     cout << endl;

 

     left = 0;

     right = n-1;

 

    quickSort(a, left, right);

 

     cout << " After sorting ... " << endl;

     for (i=0; i<n; i++) cout << setw(8) << a[i];

     cout << endl;

}

 

void quickSort(int a[], int left, int right)

{

     int l = left;

     int r = right;

     int bound = a[l];

     int i;

 

     while( l<r )

     {

         while ( (a[r] >= bound) && (r>l) ) r--;

         if ( r>=l )

         {

            a[l] = a[r];     // Error if (r>l)

            cout << "Left, right, l, r, bound: = "

              << left << "\t" << right << "\t"

              << l    << "\t" << r     << "\t"

              << bound << endl;

            for (i=left; i<=right; i++) cout << setw(8) << a[i];

            cout << endl;

            getchar();

         }

 

         while ( (a[l] <= bound) && (l<r) ) l++;

         if ( l<=r )

         {

            a[r] = a[l];     // Error if (l<r)

            cout << "Left, right, l, r, bound: = "

              << left << "\t" << right << "\t"

              << l    << "\t" << r     << "\t"

              << bound << endl;

            for (i=left; i<=right; i++) cout << setw(8) << a[i];

            cout << endl;

            getchar();

         }

     }

     a[l] = bound;

 

     if (l<=right) quickSort(a, l+1, right);

    if (r>=left)  quickSort(a, left, r-1);

}