Middle and high schools math programming in Ch
  1. Arithmetic Operations

  2. Commonly used math functions

  3. Calculating the average

  4. Using functions with one variable: f(x)=3x+5

  5. Using functions with two variables: f(x,y)=3x+y+5

  6. Calculating quadratic polynomials: 3x2 + 2x- 4

  7. Solving Quadratic Equation: ax2 + bx + c = 0

  8. Calculating factors

  9. Calculating prime factors

  10. Calculating the greatest prime factor

  11. Calculating Sigma: x =010 x2

  12. Calculating Sigma: i =0n xi

Arithmetic Operations

Ch can be used as a calculator to perform arithmtics.

> 1+1 2 >9-5 4 > 3*4 12 > 10/2 5 > 10/4 2 > 10/4.0 2.5000 > 1+2+5*6+6/2 36 > 3+2*9-((6+8)+8/2) 3

Commonly used math functions

The powerful math functions such as sin(), cos(), pow() and div() are demonstrated below.

> sin(1) 0.8415 > cos(1) 0.5403 > pow(2,3) 8.0000 > div(14,3) .quot = 4 .rem = 2 > div(14,4) .quot = 3 .rem = 2 Don't know what is pow(), div(), sin()? It is easy. Just run the command man. It works in Windows, Mac OS X, and Linux. For example: > man pow POW(3) C/C++ Programmer's Manual POW(3) NAME pow, powf, powl - power functions SYNOPSIS #include <math.h> double pow(double x, double y); float powf(float x, float y); long double powl(long double x, long double y); DESCRIPTION The pow() function returns the value of x raised to the power of y.

Calculating the average

Assuming you have numbers 3, 9 and 10. How do you get the average value of these numbers? Here is a quick example to do it in Ch.

> (3+9+10)/3.0 7.3333

Using functions with one variable: f(x)=3x+5

You can define the function f(x) = 3x+5 in a Ch command window call it right away. We use x with the data type double.

> double f(double x) {return 3*x+5;} > f(12) 41.0000 > f(2.5) 12.5000

Using functions with two variables: f(x,y)=3x+y+5

You can define the function f(x,y) = 3x+y+5 in a Ch command window and call it right away. We assume that x and y are the data type double.

> double f(double x, double y) {return 3*x+y+5;} > f(12,2) 43.0000 > f(2.5,2.5) 15.0000

Calculating quadratic polynomials: 3x2 + 2x- 4

A quadratic polynomial can be calculated as a function f(x)= 3x2 + 2x- 4

> double f(double x) {return 3*pow(x,2)+2*x-4;} > f(3) 29.0000 > f(4.5) 65.7500 Calculating factors

Numbers that can be multiplied together to get another number are its factors. For example, 2*3 = 6, 1*6=6, so 2 and 3, 1 and 6 are factors of 6. It is not recommended to type the code from command line if it is more than 2 lines, since it is not easy to go back to make the modifications if you typed something wrongly. Thus, you can use ChIDE or your favorite editor to create a file called factor.ch, then use cat to display the content of factor.ch.

> cat factor.ch void factors(int n) { int i; for (i=1; i<=n; i++) { if (n%i==0) printf("%d ",i); } } > . factor.ch > factors(6) 1 2 3 6 ". factor.ch " means to load the functions so that the functions factors() inside the file factor.ch can be called from command line. The dot (in ch) is the same as dot in bash to load the scripts or functions.

Solving Quadratic Equation ax2 + bx + c = 0

> double a=1, b=1, c = -6, x1, x2 > quadratic(a, b, c, x1, x2) 2 > x1 2.0000 > x2 -3.0000

Calculating prime factors

A prime number can only be divided by 1 or itself and it cannot be factored any further. You can use ChIDE to create a file called primeFactor.ch, then use cat to display the content of primeFactor.ch.

> cat primeFactor.ch int primeFactor(int n) { int i, total=0; for (i=1; i<=n; i++) { if (n%i==0) total=total+1; } return total; } void is_prime(int n) { if (primeFactor(n) == 2) printf("True\n"); else printf("Flase\n"); } > . primeFactor.ch > is_prime (9) Flase > is_prime (13) True ". primeFactor.ch " means to load the functions so that the functions primeFactor() and is_prime() inside the file primeFactor.ch can be called from command line.

Calculating the greatest prime factor

You can create a function called gratestPrime() and save it in the file prime.ch, then load it from the file using the command ". prime.ch" . You can see what is in the file prime.ch using the command cat.

> cat prime.ch void greatestPrime (int n) { int greatestPrimeNumber = 0; int i,j; printf("The list of prime factors for %d:", n); for ( i = 2 ; i <=n ; i++ ) { j = i - 1; /* the loop below is to find out if i is the prime */ while ( j > 1 ) { if ( i % j == 0 ) // stop if i is not the prime break; else j=j-1; } /* * "if (j ==1)" it means that i is the prime * then we need to know if i is the prime of n * if yes, we print it out and * save the biggest prime factor in "greatestPrimeNumber" */ if ( j == 1 ) { if ( n % i == 0 ) { printf(" %d", i); if ( i > greatestPrimeNumber) greatestPrimeNumber = i; } } } printf("\nThe greatest prime factor for %d: %d\n", n, greatestPrimeNumber); } > . prime.ch > greatestPrime(26) The list of prime factors for 26: 2 13 The greatest prime factor for 26: 13 > greatestPrime(1005) The list of prime factors for 1005: 3 5 67 The greatest prime factor for 1005: 67

Calculating Sigma: x =010 x2

> int x, total=0 > for (x=0; x<10; x++) { total = total+ pow(x,2); } > total 285

Calculating Sigma: ∑i=0n xi

You can calculate Sigma i=0n xi using different methods in Ch.

The sum in an array can be calculated using the function sum().

> int x[10]={1,2,3,4,5,6,7,8,9,10} > sum(x) 55.0000

The sum in an array can be calculated using a for-loop and saved in the variable total.

> int i, total=0 > int x[10]={1,2,3,4,5,6,7,8,9,10} > for (i=0; i<10; i++) {total = total + x[i];} > total 55 The sum in an array can be calculated using your own function. > cat f.ch int f(int n, int x[]) { int total, i; for (i=0; i<n; i++){ total = total + x[i]; } return total; } > . f.ch > int x[10]={1,2,3,4,5,6,7,8,9,10} > f(10,x) 55 If you want to save this function and be able to call it whenever you start ch shell, click here.