# Learn C programming in 11 days

## Day 6 Functions

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

/*  maximum.cpp - VC6.0                                  */

/*  Maximum function                                     */

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

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

#include<stdio.h>

int max(int x, int y);

void main()

{

int a, b, c;

printf("Enter two integers: ");

scanf("%d,%d", &a, &b);        // seperate with "," when input

c = max(a,b);

printf("Maximum of %d and %d is: %d\n", a, b, c);

}

int max(int x, int y)

{

int z;

if ( x>y )

{

z=x;

}

else

{

z=y;

}

return(z);

}

1,2

Maximum of 1 and 2 is: 2

Other approachs:

int max(int x, int y)

{

int z;

if ( x>y ) z=x;

else z=y;

return(z);

}

Or:

int max(int x, int y)

{

int z;

if ( x>y ) z=x; else z=y;

return(z);

}

Or:

int max(int x, int y)

{

int z;

z = (x>y)?x:y;

return(z);

}

Or:

int max(int x, int y)

{

return  (x>y)?x:y;

}

Or:

int max(int x, int y)

{

return x>y?x:y;

}

A useful tool:

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

/*  anykey.cpp - VC5.0                                   */

/*  Simple function                                      */

/*  J Luo, 14-Mar-2001                                   */

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

#include <stdio.h>

void HangOn();

void main()

{

for(int i=1; i<=100; i++)

{

printf("%-3d x %3d = %-5d\n", i, i, i*i); // Good format?

if ( (i%10) == 0 ) HangOn();

}

}

void HangOn()

{

printf("Press ANY key to contine ...\n\n");

getchar();

}

Greatest Common Factor

/*  cfg.cpp - VC5.0                                      */

/*  Greatest Common Factor                               */

/*  J Luo, 16-Mar-2001                                   */

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

#include <stdio.h>

void swap(int &, int &);

int Cfg(int, int);

void main()

{

int m, n;

int cfg;

printf("Enter two numbers: ");

scanf("%d %d", &m, &n);

if ( n>m ) swap(m, n);

cfg = Cfg(m, n);

printf("Greatest Common Factor of %d and %d is: %d\n", m, n, cfg);

}

void swap(int &a, int &b)

{

int temp;

temp = a;

a = b;

b= temp;

}

int Cfg(int a, int b)

{

int r;

r = a%b;

while ( r != 0 )

{

a = b;

b = r;

r = a%b;

}

return b;

}

Gedebahe guess

/*  gedebahe.cpp - VC5.0                                 */

/*  Verify Ge De Ba He guess                             */

/*  J Luo, 17-Mar-2001                                   */

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

#include <stdio.h>

int Prime(int);

void main()

{

int i, j;

int flag;

int m;

do

{

printf (" Please input an even number bigger than 5: ");

scanf ("%d", &m);

} while ( (m%2 != 0) || (m<6) );

for (i=3; i<=m/2; i=i+2)

{

flag = Prime(i);

if (flag ==1)

{

j = m - i;

flag = Prime(j);

if (flag ==1)

{

printf ("%d = %d + %d \n", m, i, j);

break;

}

}

}

}

int Prime(int i)

{

int j, flag;

flag=1;

for (j=2; j<i-1; j++)

{

if (i%j == 0)

{

flag=0;

break;

}

}

return flag;

}

Bisection for a root

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

/*  bisection.cpp - VC5.0                                */

/*  Find a root by bisection                             */

/*  J Luo, 21-Mar-2001                                   */

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

#include <stdio.h>

#include <math.h>

inline float Fx(float);

void main()

{

float x1, x2, x;

float f1, f2, f;

float delta=0.000001F;

int got;

got = 0;

do

{

printf("Initial x1 and x2: ");

scanf("%f %f", &x1, &x2);

f1 = Fx(x1);

if ( fabs(f1) <= delta )

{

got = 1;

x = x1;

break;

}

f2 = Fx(x2);

if ( fabs(f2) <= delta )

{

got = 1;

x = x2;

break;

}

if ( f1*f2 < 0 ) break;

} while ( got == 0 );

while ( got == 0 )

{

x = 0.5F * (x1+x2);

f = Fx(x);

if ( fabs(f) <= delta )

{

x = x;

break;

} else if ( f*f1 < 0 )

{

x2 = x;

f2 = f;

} else

{

x1 = x;

f1 = f;

}

}

printf("  X = %f\n", x);

printf("f(x)= %f\n", f);

}

inline float Fx(float x)

{

return x*x*x - 1;

}

Recursive functions – big challenge!

Fibonachi series.

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

/*  fibonachi.cpp - VC6.0                                */

/*  Using a loop                                         */

/*  sum of fbonachi series: 1, 2, 3, 5, 8, 13, ...       */

/*  J Luo, 23-Mar-2001                                   */

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

#include <stdio.h>

void main()

{

unsigned n;

unsigned f1, f2, f;

unsigned sum;

f1 = 1;

f2 = 1;

n = 3;

sum = f1 + f2;

do

{

f = f1 + f2;

sum = sum + f;

n = n + 1;

f1 = f2;

f2 = f;

} while ( sum <= 1000000000 );

printf("n = %d\n", n-1);

printf("sum = %d\n", sum);

}

Using an array:

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

/*  fibonachi.cpp - VC6.0                                */

/*  Using a loop                                         */

/*  sum of fbonachi series: 1, 2, 3, 5, 8, 13, ...       */

/*  J Luo, 23-Mar-2001                                   */

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

#include <stdio.h>

void main()

{

const int N=45;

unsigned int f[N];

unsigned int i, n;

unsigned int sum;

printf("n=");

scanf("%d", &n);

f[0] = 1;

f[1] = 1;

sum = f[0] + f[1];

for (i=2; i<n; i++)

{

f[i] = f[i-2] + f[i-1];

sum = sum + f[i];

}

printf("n = %d\n", n);

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

{

if ( i%5 == 0 ) printf("\n");

printf("%10d", f[i]);

}

printf("\n");

printf("sum = %d\n", sum);

}

Using recursive function

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

/*  fibonachi.cpp - VC6.0                                */

/*  Using a recursive function                           */

/*  sum of fibonachi series: 1, 2, 3, 5, 8, 13, ...      */

/*  J Luo, 23-Mar-2001                                   */

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

#include <stdio.h>

int f(int);

void main()

{

int n=9;

int sum;

sum = 0;

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

{

printf("i = %d\t", i);

printf("f = %d\n", f(i));

sum = sum + f(i);

}

printf("sum = %d\n", sum);

}

int f(int n)

{

if ( n == 1 || n==2 ) return 1;

else return f(n-1) + f(n-2);

}

More recursive

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

/*  factorial2.cpp - VC6.0                                  */

/*  Using a recursive function                           */

/*  J Luo, 23-Mar-2001                                   */

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

#include <stdio.h>

int f(int);

void main()

{

int n=5;

printf("%d! = %d\n", n, f(n));

}

int f(int n)

{

if ( n == 1 ) return 1;

else return n*f(n-1);

}}

Quick Sort

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

/*  sort_quick.cpp - VC6.0                               */

/*  Quick Sort - Method 1                                */

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

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

#include <stdio.h>

int qsort(int, int, int []);

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

void main()

{

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

int i, n;

n = sizeof(a) / sizeof(int);

for (i=0; i<n; i++) printf("%4d", a[i]);

printf("\n");

sortPart(0, n-1, a);

for (i=0; i<n; i++) printf("%4d", a[i]);

printf("\n");

}

void sortPart(int l, int r, int a[])

{

int i;

i = qsort(l, r, a);

if ( l < i-1 ) sortPart(l,i-1,a);

if ( i+1 < r ) sortPart(i+1,r,a);

}

int qsort(int l, int r, int a[])

{

int i, j, n;

int temp;

i=l;

j=r;

n = r-l+1;

temp = a[i];

do

{

while ( (a[j] >= temp) && (j>i) ) j=j-1;

if ( j>i ) a[i] = a[j];

while ( (a[i] <= temp) && (i<j) ) i=i+1;

if ( i<j ) a[j] = a[i];

} while ( i < j );

a[i] = temp;

return i;

}

Swap two integers

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

/*  sawp.cpp - VC5.0                                     */

/*  Simple function                                      */

/*  J Luo, 14-Mar-2001                                   */

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

#include <stdio.h>

void swap(int &, int &);

void main()

{

int m, n;

printf("Enter two numbers: ");

scanf("%d %d", &m, &n);

printf("%d\t %d\n", m, n);

if ( n>m ) swap(m, n);

printf("%d\t %d\n", m, n);

}

void swap(int &a, int &b)

{

int temp;

temp = a;

a = b;

b= temp;

}

Bubble sort

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

/*  sort_bubble.cpp - VC5.0                              */

/*  Bubble sort                                          */

/*  J Luo, 16-Mar-2001                                   */

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

#include <stdio.h>

void Bubble(int, int []);

void Swap(int &, int &);

void main()

{

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

int i;

for (i=0; i<10; i++) printf("%d\t", a[i] );

printf("\n");

Bubble(10, a);

for (i=0; i<10; i++) printf("%d\t", a[i] );

printf("\n");

}

void Bubble(int n, int a[])

{

int flag;

int j;

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

{

flag=0;

for (j=n; j>i; j--)

{

if ( a[j] < a[j-1] )

{

flag=1;

Swap(a[j], a[j-1]);

}

}

if (flag==0) break;

}

}

void Swap(int &a, int &b)

{

int temp;

temp=a;

a = b;

b =temp;

}

Sort strings

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

/*  sort_string.cpp - VC5.0                              */

/*  Swap sorting of string                               */

/*  J Luo, 28-Mar-2001                                   */

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

#include <stdio.h>

#include <string.h>

void main()

{

char a[][4] =

{

"fat", "kat", "sat", "mat", "eat", "bat", "oat",

"cat", "hat", "tat", "pat", "rat", "vat", "gat"

};

int n;

int i, j, k;

char temp[4];

n = sizeof(a) / 4;

printf("Before sorting ... \n");

for (i=0; i<n; i++) printf("%s\t", a[i] );

printf("\n");

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

{

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

{

if ( strcmp(a[j],a[i]) < 0 )

{

strcpy(temp, a[i]);      // temp = a[i]

strcpy(a[i], a[j]);      // a[i] = a[j]

strcpy(a[j], temp);      // a[j] = temp;

}

}

}

printf("After sorting ... \n");

for (i=0; i<n; i++) printf("%s\t", a[i] );

printf("\n");

}