My FIRST C program
*********************************************************/
/* 1st.cpp - VC5.0 */
/* My FIRST C program */
/* J Luo, 14-Feb-2001 */
/*********************************************************/
#include <stdio.h> // 文件包含语句
void main()
{
printf("Hello, world!"); // 格式输出
}
Output
Hello, world!Press any key to continue!
如何让输出结果漂亮些?
简例1.1a Better output. 我的第一个漂亮的C语言程序
void main()
{
printf("\n\t\t\t Hello, world! \n\n");
}
Output
Hello, world!
Press any key to continue!
A real program.
/*********************************************************/
/* 1st.cpp - VC5.0 */
/* My FIRST C program */
/* J Luo, 14-Feb-2001 */
/*********************************************************/
#include <stdio.h>
void main()
{
printf("\n\t\t\t Hello, world! \n\n");
printf("/*********************************************************/\n");
printf("/* 1st.cpp - VC5.0 */\n");
printf("/* My FIRST C program */\n");
printf("/* J Luo, 14-Feb-2001 */\n");
printf("/*********************************************************/\n");
printf("\n");
}
Output
Hello, world!
/*********************************************************/
/* 1st.cpp - VC5.0 */
/* My FIRST C program */
/* J Luo, 14-Feb-2001 */
/*********************************************************/
(Press any key to continue!)
Summary
* 说明
1. 注释行: /* */
2. 包含语句: include
3. 主函数: main()
4. 分隔符: {}
5. 格式输出函数: printf(参数1,参数2,参数3,…)
C语言不仅可用来输出文字信息,而且还能进行计算。
Add two numbers
/*********************************************************/
/* 2nd.cpp - VC5.0 */
/* Sum of two integers */
/* J Luo, 14-Feb-2001 */
/*********************************************************/
#include <stdio.h>
void main()
{
int a, b; // variables of two integers
int c; // variable of sum
a = 123;
b = 987;
c = a+b;
printf("\n %d \n\n",c);
}
Output
1110
printf("\n Sum = %d \n\n",c);
Output
Sum = 1110
printf("\n %d + %d = %d \n\n", a,b,c);
Output
123 + 987 = 1110
Sum and mean
/*********************************************************/
/* 3rd.cpp - VC5.0 */
/* Sum and mean */
/* J Luo, 14-Feb-2001 */
/*********************************************************/
#include <stdio.h>
void main()
{
int x1, x2, x3;
int sum; // variable of sum
double mean; // average
x1 = 123;
x2 = 456;
x3 = 789;
sum = x1+x2+x3;
mean = sum/3;
printf("Sum = %d \n",sum);
printf("Mean = %f \n\n",mean);
}
Output
Sum = 1368
Mean = 456.000000
Better output format (P. 76-78)
printf("Mean = %f \n\n",mean);
Output
Sum = 1368
Mean = 456.0
Problem!
Mean = ? if x2 = 457.0?
Output
Sum = 1369
Mean = 456.0
mean = sum/3.0;
Sum = 1369
Mean = 456.3
/*********************************************************/
/* 3rd.cpp - VC5.0 */
/* Sum and mean */
/* J Luo, 14-Feb-2001 */
/*********************************************************/
#include <stdio.h>
void main()
{
double x1, x2, x3; // double precision
double sum; // variable of sum
double mean; // average
x1 = 123;
x2 = 457;
x3 = 789;
sum = x1+x2+x3;
mean = sum/3.0;
printf("Sum = %6.1f \n",sum); // format control (P.
printf("Mean = %6.1f \n\n",mean);
}
Output
Sum = 1369.0
Mean = 456.3
Output with C++
/*********************************************************/
/* 3rd.cpp - VC5.0 */
/* Sum and mean */
/* Using cout */
/* J Luo, 14-Feb-2001 */
/*********************************************************/
#include <iostream.h> // C++ Input/Output
void main()
{
double x1, x2, x3;
double sum;
double mean;
x1 = 123;
x2 = 457;
x3 = 789;
sum = x1+x2+x3;
mean = sum/3.0;
cout << " Sum = " << sum << '\n';
cout << "Mean = " << mean << '\n';
}
Output
Sum = 1368
Mean = 456
Input from keyboard
/*********************************************************/
/* 3rd.cpp - VC5.0 */
/* Sum and mean */
/* Using cout and cin */
/* J Luo, 14-Feb-2001 */
/*********************************************************/
#include <iostream.h> // C++ Input/Output
void main()
{
double x1, x2, x3;
double sum;
double mean;
cout << "Input data: ";
cin >> x1 >> x2 >> x3;
sum = x1+x2+x3;
mean = sum/3;
cout << " Sum = " << sum << '\n';
cout << "Mean = " << mean << '\n';
}
Output
Input data: 123 456 789
Sum = 1368
Mean = 456
/*********************************************************/
/* 4th.cpp - VC5.0 */
/* Sum and mean */
/* Using a function */
/* J Luo, 14-Feb-2001 */
/*********************************************************/
#include <iostream.h>
double Sum(double x1, double x2, double x3);
// double Sum(double, double, double);
void main()
{
double x1, x2, x3;
double sum;
double mean;
cout << "Input data: ";
cin >> x1 >> x2 >> x3;
sum = Sum(x1,x2,x3); // case sensitive
mean = sum/3;
cout << " Sum = " << sum << '\n';
cout << "Mean = " << mean << '\n';
}
double Sum(double x1, double x2, double x3)
{
double sum;
sum = x1+x2+x3;
return sum;
}
Output
Input data: 123 456 789
Sum = 1368
Mean = 456
Output(Decimal)
Input data: 12.3 45.6 78.9
Sum = 136.8
Mean = 45.6
Now an array.
/*********************************************************/
/* 5th.cpp - VC5.0 */
/* Uing an array */
/* J Luo, 15-Feb-2001 */
/*********************************************************/
#include <iostream.h>
void main()
{
double x[3];
double sum;
double mean;
cout << "Input data: ";
cin >> x[0] >> x[1] >> x[2]; // array starts from 0, not 1
sum = x[0]+x[1]+x[2];
mean = sum/3;
cout << " Sum = " << sum << '\n';
cout << "Mean = " << mean << '\n';
}
Output(same as above)
Input data: 123 456 789
Sum = 1368
Mean = 456
Now a loop!
/*********************************************************/
/* 6th.cpp - VC5.0 */
/* Uing an array and a loop */
/* J Luo, 15-Feb-2001 */
/*********************************************************/
#include <iostream.h>
void main()
{
double x[3];
double sum;
double mean;
cout << "Input data: ";
for (int i=0; i<3; i++)
{
cin >> x[i];
sum = sum + x[i];
}
mean = sum/3.0;
cout << " Sum = " << sum << '\n';
cout << "Mean = " << mean << '\n';
}
Output(same as above)
Input data: 123 456 789
Sum = 1368
Mean = 456
More user friendly
cout << "Input your data, please ... " << endl;
for (int i=0; i<3; i++)
{
cout << "x(" << i << ") = ";
cin >> x[i];
sum = sum + x[i];
}
Output
Input your data please:
X(0) = 123
X(1) .
0000000= 456
X(2) = 789
Sum = 1368
Mean = 456
/*********************************************************/
/* 7th.cpp - VC5.0 */
/* Uing a constant */
/* J Luo, 15-Feb-2001 */
/*********************************************************/
#include <iostream.h>
void main()
{
const n=5;
double x[n];
double sum;
double mean;
cout << "Input your data, please ... " << endl;
for (int i=0; i<n; i++)
{
cout << "x(" << i << ") = ";
cin >> x[i];
sum = sum + x[i];
}
mean = sum/n;
cout << " Sum = " << sum << '\n';
cout << "Mean = " << mean << '\n';
}
Output
Input your data please:
X(0) = 123
X(1) = 456
X(2) = 789
Sum = 1368
Mean = 456
Now a useful C program!
/*********************************************************/
/* mean_sd.cpp - VC5.0 */
/* Mean and SD */
/* My FIRST useful C program */
/* J Luo, 14-Feb-2001 */
/*********************************************************/
#include <iostream.h> // C++ Input/Output
#include <math.h> // Mathematical library
void main()
{
const n=5;
double x[n];
double sumX, sumXX;
double mean;
double SD;
cout << "Input your data, please ... " << endl;
for (int i=0; i<n; i++)
{
cout << "x(" << i << ") = ";
cin >> x[i];
}
sumX = 0;
sumXX = 0;
for (i=0; i<n; i++)
{
sumX = sumX + x[i];
sumXX = sumXX + x[i]*x[i];
}
mean = sumX/n;
SD = sqrt(sumXX - sumX*sumX/n);
cout << " Sum = " << sumX << '\n';
cout << "Mean = " << mean << '\n';
cout << " SD = " << SD << '\n';
}
可否用中文?
printf("/*********************************************************/\n");
printf("/* 1st.cpp - VC5.0 */\n");
printf("/* 我的第一个C语言程序 */\n");
printf("/* 罗静初, 2000年2月14日 */\n");
printf("/*********************************************************/\n");
Summary
* 说明
6. 注释行: /* */
7. 包含语句: include
8. 主函数: main()
9. 分隔符: {}
10. 格式输出函数: printf(参数1,参数2,参数3,…)
/*********************************************************/
/* 7th.cpp - VC5.0 */
/* Uing a constant */
/* J Luo, 15-Feb-2001 */
/*********************************************************/
#include <iostream.h>
void main()
{
const n=5;
double x[n];
double sum;
double mean;
cout << "Input your data, please ... " << endl;
for (int i=0; i<n; i++)
{
cout << "x(" << i << ") = ";
cin >> x[i];
sum = sum + x[i];
}
mean = sum/n;
cout << " Sum = " << sum << '\n';
cout << "Mean = " << mean << '\n';
}
Output
Input your data please:
X(0) = 123
X(1) = 456
X(2) = 789
Sum = 1368
Mean = 456
Now a useful C program!
/*********************************************************/
/* mean_sd.cpp - VC5.0 */
/* Mean and SD */
/* My FIRST useful C program */
/* J Luo, 14-Feb-2001 */
/*********************************************************/
#include <iostream.h> // C++ Input/Output
#include <math.h> // Mathematical library
void main()
{
const n=5;
double x[n];
double sumX, sumXX;
double mean;
double SD;
cout << "Input your data, please ... " << endl;
for (int i=0; i<n; i++)
{
cout << "x(" << i << ") = ";
cin >> x[i];
}
sumX = 0;
sumXX = 0;
for (i=0; i<n; i++)
{
sumX = sumX + x[i];
sumXX = sumXX + x[i]*x[i];
}
mean = sumX/n;
SD = sqrt( (sumXX - sumX*sumX/n) / (n-1) );
cout << " Sum = " << sumX << '\n';
cout << "Mean = " << mean << '\n';
cout << " SD = " << SD << '\n';
}
Output
Input your data, please ...
x(0) = 1
x(1) = 2
x(2) = 3
x(3) = 4
x(4) = 5
Sum = 15
Mean = 3
SD = 1.58114