# Learn C programming in 11 days

## Day 2 Simple programs

Cell growth. Kwon N0=100, k=0.01, t=10.

Nt=N0*ekt

#include <iostream.h>

void main()

{int n0,nt,t;float k;

n0=100,k=0.01,t=10;

nt = n0e^k*t);

cout<<nt<<endl;

}

Program by a good student.

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

/*  cell_growth.cpp - VC5.0                              */

/*  Cell growth                                          */

/*  Hao Xuesheng, 21-Feb-2001                            */

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

#include <iostream.h>

#include <math.h>

void main()

{

int n0;       // Initial cell number

int nt;       // final cell number

float k; // growth rate

int t;        // time

cout << "Initail cell number: ";

cin >> n0;

cout << "Growth rate: ";

cin >> k;

cout << "Time: ";

cin >> t;

nt = n0*exp(k*t);

cout << "Final cell number: " << nt << endl;

}

Output

Initial ell number: 1000

Growth rate: 0.01

Time: 10

Final cell number: 1105

Problem when k is big enough.

Output

Initial ell number: 1000

Growth rate: 0.5

Time: 10

Final cell number: 1105

 N0 K t Nt 10 0.01 1 100 0.01 10 1000 0.02 10 1000 0.05 100

Area of a triangle

#include <stdio.lib>;

void main()

{float a,b,c;s;area;

scanf('%d,%d,%d',a,b,c)

s=1/2(a+b+c);

area=sqr(s(s-a)(s-b)(s-c);

print("%d)",arae)

}

Only 1 error and 1 warning when compiling

d:\jc\01\jx\c\triangle_err\triangle_err.cpp(1) : warning C4067: unexpected tokens following preprocessor directive - expected a newline

d:\jc\01\jx\c\triangle_err\triangle_err.cpp(1) : fatal error C1083: Cannot open include file: 'stdio.lib': No such file or directory

Change: stdio.lib à stdio.h

triangle_err.obj - 10 error(s), 1 warning(s)

Even worse with warning!

#include <stdio.h>;

#include <math.h>;

void main()

{float a,b,c,s,area;

scanf("%d,%d,%d",a,b,c);

s=1/2*(a+b+c);

area=sqrt(s*(s-a)*(s-b)*(s-c));

printf("%d)",area);

}

Getting closer

#include <stdio.h>;

#include <math.h>;

void main()

{float a,b,c,s,area;

scanf("%f,%f,%f",&a,&b,&c);

s=1.0/2*(a+b+c);

area=sqrt(s*(s-a)*(s-b)*(s-c));

printf("%f)",area);

}

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

/*  area.cpp - VC5.0                                     */

/*  Arae of a triangle                                   */

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

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

#include <iostream.h>

#include <math.h>

void main()

{

float a, b, c;         // sides of the triangle

float s;

float area;

cout << "The sides of a triangle: ";

cin >> a >> b >> c;

s = 0.5F*(a+b+c);

area = (float) sqrt(s*(s-a)*(s-b)*(s-c));

cout << "Area = " << area << endl;

}

Output

The sides of a triangle: 3 4 5

Area = 6

With double

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

/*  area.cpp - VC5.0                                     */

/*  Arae of a triangle                                   */

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

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

#include <iostream.h>

#include <math.h>

void main()

{

double a, b, c;        // sides of the triangle

double s;

double area;

cout << "The sides of a triangle: ";

cin >> a >> b >> c;

s = 0.5*(a+b+c);

area = sqrt(s*(s-a)*(s-b)*(s-c));

cout << "Area = " << area << endl;

}

Output

The sides of a triangle: 3 4 5

Area = 6

The sides of a triangle: 3 4 7

Area = 0

The sides of a triangle: 3 4 5

Area = -1.#IND

### 计算月、周、日、时、分、秒。

· 将程序编写成与计算机会话的形式，以增加趣味性。

· 程序应该有通用性，不仅适用于2001年元旦，也适用于其它年份的元旦。

· 我们知道，逢四的年份是闰年。但是，100, 1900年等逢百的年份不是闰年，而逢百的年份中，16002000等逢四百的年份却又是闰年。根据以上规则，可以用下面的公式计算公元元年元月元旦到2001年元旦前一天(20001231)为止的天数：

2000×365 + 2000/4 - 2000/100 + 2000/400

· 根据年历的有关规定，公元元年元月元旦为星期一，以上求得的天数再加1，就是到2001年元旦的天数，用它除以7得到的余数，就可以用来表示星期几。

(1) 考虑到程序的通用性，程序一开始读入年份Year，这就不仅适用于2001年，也适用于其它年份。

(2) 由于C语言中整数的取值范围远比实数的取值范围小，在计算从公元元年元月元旦一直到今天经历了多少秒钟时，其结果会超出整数的范围，因此我们用实数存放秒数。此外，虽然小时数和分钟数虽然不超出整数范围，但它们也都是很大的数字，输出结果中用数量级表示就可以了，因此也都用实数表示。

(3) 在计算天数Days时，用了四个连续的赋值语句，而不是直接用上面的计算公式。把一个比较复杂的表达式拆分成几个简单的表达式，可以减少书写表达式时的错误，这也是程序设计中常用的方法。

(4) 程序中使用了一个getchar()函数，其作用是在运行过程中起到“暂停”的作用，以便看清屏幕上的输出信息。按任意一个键，就可以继续程序的运行。

(5) 程序中用了一个求余算法来计算某一天是星期几，即计算出天数Days后，将其除以7，所的余数就是星期几。在还没有进一步学习有关语句和控制结构前，我们还不能直接打印输出Sunday, Monday等星期几的信息，只能用数字来表示，如0表示星期日，1表示星期一等。

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

/*  what_day.cpp - VC6.0                                 */

/*  What day today?                                      */

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

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

#include <iostream.h>

#include <stdio.h>

void main()

{

int year, months, weeks, days;

double hours, minutes, seconds;

int number;

cout << "                ***** Happy New Millennium 2001! ***** \n\n";

year = 2001;

cout << "\n OK! Now I can tell you the number of: \n";

cout << " Months, weeks, days, hours, minutes and seconds, \n";

cout << " since the 1st January 1, till 1 Jan 2001 \n\n";

cout << " Believe so? \n";

year = year - 1;

months = year * 12;

days = year * 365;

days = days + year/4;

days = days - year/100;

days = days + year/400;

weeks = days/7;

hours = days * 24;

minutes = hours * 60;

seconds = minutes * 60;

cout << " Here we are: \n\n";

cout << " Months:  " << months  << endl;

cout << " Weeks:   " << weeks   << endl;

cout << " Days:    " << days    << endl;

cout << " Hours:   " << hours   << endl;

cout << " Minutes: " << minutes << endl;

cout << " Seconds: " << seconds << endl;

cout << " Am I clever? :-) \n";

getchar();

cout << " Yes, I want to know what day is today? \n\n";

cout << " Let me try ... \n";

days = days + 1;

number = days%7;

getchar();

cout << " OK! I can tell you the number of the day. \n";

cout << " It is : " << number << endl;

cout << " And you can look through the table below: \n\n";

cout << " 0 - Sunday    1 - Monday   2 - Tuesday   3 - Wednesday \n";

cout << " 4 - Thursday  5 - Friday   6 - Saturday                \n\n";

cout << " Thanks, but I want to know: 'What Day is today!' \n";

cout << " I don't like the silly numbers! \n\n";

cout << " Let me try again ... \n";

getchar();

cout << " Sad! I can't do it now. :-( \n";

cout << "                ***** Good-Bye *****     \n\n";

}

Output

***** Happy New Millennium 2001! *****

OK! Now I can tell you the number of:

Months, weeks, days, hours, minutes and seconds,

since the 1st January 1, till 1 Jan 2001

Believe so?

Here we are:

Months:  24000

Weeks:   104355

Days:    730485

Hours:   1.75316e+007

Minutes: 1.0519e+009

Seconds: 6.31139e+010

Am I clever? :-)

Yes, I want to know what day is today?

Let me try ...

OK! I can tell you the number of the day.

It is : 1

And you can look through the table below:

0 - Sunday    1 - Monday   2 - Tuesday   3 - Wednesday

4 - Thursday  5 - Friday   6 - Saturday

Thanks, but I want to know: 'What Day is today!'

I don't like the silly numbers!

Let me try again ...

Sad! I can't do it now. :-(

***** Good-Bye *****

Not only 2001!

cout << "                ***** Happy New Year! ***** \n\n";

cout << " But tell me which year is it? ";

cin >> year;