/**************************************************************************
|* *|
|* LEARNING C BY EXAMPLES *|
|* HOSSAIN ALAMGIR *|
|* +8801924579411 *|
|* *|
|* *|
|* C is a general-purpose programming language that was originally *|
|* designed by Dennis Ritchie of Bell Laboratories in 1972. After gaining *|
|* tremendous popularity, C became an ANSI standard in 1989. *|
|* *|
**************************************************************************/
================================EXAMPLE 1.1================================
#include <stdio.h>
int main(){
printf("Hello World!");
return 0;
}
================================EXAMPLE 1.2================================
#include <stdio.h>
int main(){
printf("Programming is fun.\nAnd Programming in C is even more fun!");
return 0;
}
================================EXAMPLE 1.3================================
#include <stdio.h>
int main()
{
printf("************\n Welcome\n************\n");
return 0;
}
================================EXAMPLE 1.4================================
#include <stdio.h>
int main(){
printf("I'm gonna WIN!\nI'm gonna WIN!\nI'm gonna WIN!\n");
return 0;
}
================================EXAMPLE 1.5================================
#include <stdio.h>
int main()
{
printf("[1] Select crisps\n");
printf("[2] Select popcorn\n");
printf("[3] Select chocolate\n");
printf("[4] Select cola\n");
printf("[0] Exit");
return 0;
}
================================EXAMPLE 2.1================================
#include <stdio.h>
int main(){
int f,c;
f = 100;
c= 5* (f-32)/9;
printf("fahr = %d, celsius = %d", f,c);
return 0;
}
================================EXAMPLE 2.2================================
#include <stdio.h>
int main(){
int math,eng,comp,average;
math = 87,
eng = 72,
comp = 93,
average = (math+eng+comp)/3;
printf("math = %d, eng = %d, comp = %d, average = %d",math,eng,comp,average);
return 0;
}
================================EXAMPLE 2.3================================
#include <stdio.h>
#include <math.h>
int main()
{
double interest,money,rate,year;
scanf("%lf %lf %lf",&money,&year,&rate);
interest=money*pow(1+rate,year)-money;
printf("interest = %.2f",interest);
return 0;
}
================================EXAMPLE 2.4================================
#include <stdio.h>
#include <math.h>
int main()
{
double V,r,pi;
pi = 3.1415926535897932384626;
scanf("%lf",&r);
V=4*pi*pow(r,3)/3;
printf("%.2f",V);
return 0;
}
================================EXAMPLE 2.5================================
#include <stdio.h>
#include <math.h>
int main()
{
int a1,a2,a3,a4,sum;
float avg;
scanf("%d %d %d %d", &a1,&a2,&a3,&a4);
sum = a1+a2+a3+a4;
avg = sum/4.0;
printf("Sum = %d; Average = %.2f",sum,avg);
return 0;
}
================================EXAMPLE 3.1================================
#include <stdio.h>
int main()
{
int n;
scanf("%d",&n);
if(n>38)
printf("Too big!");
else if(n==38)
printf("Good Guess!");
else if(n<38)
printf("Too small!");
return 0;
}
================================EXAMPLE 3.2================================
#include <stdio.h>
#include <math.h>
int main()
{
float x1,x2,x3,y1,y2,y3;
scanf("%f%f%f%f%f%f",&x1,&y1,&x2,&y2,&x3,&y3);
float a,b,c,m,n;
a=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
b=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
c=sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
if(a+b>c&&a+c>b&&b+c>a)
{
n=a+b+c;
float p=n/2.0;
m=sqrt(p*(p-a)*(p-b)*(p-c));
printf("L = %.2f, A = %.2f\n",n,m);
}
else
printf("Impossible");
return 0;
}
================================EXAMPLE 3.3================================
#include <stdio.h>
int main()
{
float x,y;
scanf("%f",&x);
if(x<=15)
{
y=(4*x)/3;
}
else
{
y=2.5*x-10.5;
}
printf("f(%.2f) = %.2f",x,y);
return 0;
}
================================EXAMPLE 3.4================================
#include <stdio.h>
#include <math.h>
int main()
{
int a, b, sum;
scanf("%d %d", &a, &b);
sum = a + b;
if(a<0 && b>0){
printf("(%d)+%d=%d", a,b,sum);
}
else if(b<0 && a>0){
printf("%d+(%d)=%d", a,b,sum);
}
else if(a<0 && b<0){
printf("(%d)+(%d)=%d", a,b,sum);
}
else if(a>0 && b>0){
printf("%d+%d=%d", a,b,sum);
}
return 0;
}
================================EXAMPLE 3.5================================
#include <stdio.h>
#include <math.h>
int main()
{
int month;
scanf("%d",&month);
switch(month)
{
case 12:
case 1:
case 2:
printf("winter");
break;
case 3:
case 4:
case 5:
printf("spring");
break;
case 6:
case 7:
case 8:
printf("summer");
break;
case 9:
case 10:
case 11:
printf ("autumn");
break;
default:
printf("data error");
break;
}
return 0;
}
================================EXAMPLE 4.1================================
#include <stdio.h>
#include <math.h>
int main()
{
double score, sum, avg;
int num, nFailure;
sum = 0.0;
avg = 0.0;
num = 0;
nFailure = 0;
scanf("%lf", &score);
while(score >=0){
sum = sum + score;
num = num +1;
if(score < 60) nFailure = nFailure + 1;
scanf("%lf", &score);
}
if(num == 0){
printf("Average = 0.00\n");
}
else{
avg = sum / num;
printf("Average = %.2f\n",avg);
printf("Count = %d\n", nFailure);
}
}
================================EXAMPLE 4.2================================
#include <stdio.h>
#include <math.h>
double funpi(double e);
int main()
{
double pi,e;
scanf("%lf",&e);
pi=funpi(e);
printf("pi = %lf",pi);
return 0;
}
double funpi(double e)
{
double x,sum=0;
int n=1,flag=1;
do{
x=1.0*flag/n;
sum=sum+x;
flag=-flag;
n=n+2;
}
while(fabs(x)>=e);
return sum*4;
}
================================EXAMPLE 4.3================================
#include <stdio.h>
#include <math.h>
int main()
{
int v,c=0,t=0;
scanf("%d",&v);
while(v !=9999)
{
t +=v;
++c;
scanf("%d",&v);
}
if(c !=0)
{
printf( "average=%.2f",(double)t/c);
}
return 0;
}
================================EXAMPLE 4.4================================
#include <stdio.h>
int main()
{
int data, digit;
scanf("%d", &data);
do {
digit = data % 10;
printf("%d ", digit);
data = data / 10;
} while(data > 0);
return 0;
}
================================EXAMPLE 4.5================================
#include <stdio.h>
int main()
{
int data, digit;
int pv = 1;
scanf("%d", &data);
printf("%d =", data);
do {
digit = data % 10;
if(data < 10)
printf(" %d * %d", digit, pv);
else
printf(" %d * %d +", digit, pv);
data = data / 10;
pv = pv * 10;
} while(data > 0);
return 0;
}
================================EXAMPLE 5.1================================
#include <stdio.h>
int main() {
int i, j;
for (i=1; i<=10; ++i) {
for (j=1; j<=i; ++j)
{ printf("*"); }
printf("\n");
}
return 0;
}
================================EXAMPLE 5.2================================
#include <stdio.h>
int p(int a,int b);
int main()
{
int n,t=0;
scanf("%d",&n);
int m=p(10,n-1);
int v=p(10,n);
for(int i=m;i<v;i++){
int k=i;
int sum=0;
while(k>0){
t=k%10;
k=k/10;
sum+=p(t,n);
}
if(sum==i)
printf("%d\n",i);
}
return 0;
}
int p(int a,int b)
{
int t=a;
for(int i=1;i < b;i++)
a=a*t;
return a;
}
================================EXAMPLE 5.3================================
#include <stdio.h>
int main()
{
for(int i=1;;i++){
if(i%5==1 && i%6==5 && i%7==4 && i%11==10){
printf("%d",i);
break;
}
}
return 0;
}
================================EXAMPLE 5.4================================
#include <stdio.h>
int main()
{
int n,sum,sum1=0,i,j;
scanf("%d",&n);
for(i=1;i<=n;i++){
sum=0;
for(j=1;j<=i;j++)
sum=sum+j;
sum1=sum1+sum;
}
printf("s=%d",sum1);
return 0;
}
================================EXAMPLE 5.5================================
#include <stdio.h>
int main()
{
int a[10];
int i;
int n=0;
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
if(a[i]%2==0)
{
n++;
}
}
printf("n=%d\n",n);
return 0;
}
================================EXAMPLE 6.1================================
#include <stdio.h>
int main()
{
int n,m,s=0,h=1;
scanf("%d",&n);
for(m=1;m<=n;m++)
{
h=h*m;
s=s+h;
}
printf("%d",s);
return 0;
}
================================EXAMPLE 6.2================================
#include <stdio.h>
int main()
{
int n, sum = 0, c, value;
scanf("%d", &n);
for (c = 1; c <= n; c++)
{
scanf("%d", &value);
sum = sum + value;
}
printf("%d", sum);
return 0;
}
================================EXAMPLE 6.3================================
#include <stdio.h>
int main() {
int e, i, s;
scanf("%d", &e);
int v[e];
for(i = 0; i < e; i++) {
scanf("%d", &v[i]);
}
s = v[0];
for(i = 0; i < e; i++) {
if(s > v[i]) {
s = v[i];
}
}
printf("%d", s);
return 0;
}
================================EXAMPLE 6.4================================
#include <stdio.h>
int main()
{
int a,b,n,sum;
int i;
scanf("%d", &n);
a=b=1;
printf("%d %d", a,b);
for(i=3; i<=n; i++){
sum= a+b;
a=b;
b=sum;
printf(" %d",sum);
}
return 0;
}
================================EXAMPLE 6.5================================
#include <stdio.h>
#include <math.h>
int main()
{
int m,n=2,k=1;
float limit;
scanf("%d",&m);
limit=sqrt(m);
if(m<=0||m==1){
k=0;
printf("NO");
}
else
{while(n<=limit)
{
if(m%n==0)
{
k=0;
printf("NO");
break;
}
n++;
}
}
if(k==1){
printf("YES");
}
return 0;
}
================================EXAMPLE 7.1================================
#include <stdio.h>
{
int n, program[9]={0};
int i, index;
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%d", &index);
if(index >=1 && index <=8) program[index]++;
}
for(i=1;i<=8;i++){
printf("%4d%4d\n", i, program[i]);
}
return 0;
}
================================EXAMPLE 7.2================================
#include <stdio.h>
int main()
{
int arr[10], score;
int n = 0;
scanf("%d", &score);
if(score > 100) return 0;
while(score >= 0) {
arr[n++] = score;
if(n<10){
while (1) {
scanf("%d", &score);
if(score <= 100) break;
}
}
else break;
}
// sort
int i, j, maxIndex, tmp;
for (i = 0; i < n-1; i++){
maxIndex = i;
for(j=i+1; j<n; j++)
if(arr[j] > arr[maxIndex])
maxIndex = j;
if (maxIndex !=1){
tmp = arr[i];
arr[i] = arr[maxIndex];
arr[maxIndex] = tmp;
}
}
for (i=0; i < n; i++)
if (i < n-1)printf("%d ", arr[i]);
else printf("%d\n", arr[i]);
int fnum = 0;
for (i = 0; i < n; i++){
if (arr[i] < 60){
printf("%d ", arr[i]);
fnum++;
}
}
printf("%d\n", fnum);
printf("%d %d\n", arr[0], arr[n-1]);
return 0;
}
================================EXAMPLE 7.3================================
#include <stdio.h>
#define N 20
int main(void)
{
int i, j, x, seq[N];
int num = 0;
for(i=0;i<N;i++) {
scanf("%d", &x);
for(j=0;j<num;j++) {
if(x == seq[j]) break;
}
if(j<num)
continue; // means appear, read next data
else {
seq[num] = x;
if(num==0)
printf("%d", seq[num]);
else
printf(" %d");
num++;
}
}
return 0;
}
================================EXAMPLE 7.4================================
#include <stdio.h>
int main()
{
int arr[100];int n;
scanf("%d",&n);
for(int p=0;p<n;p++)
{
scanf("%d",&arr[p]);
}
if(n==1)
printf("%d",arr[0]);
for(int i=0;i<n-1;)
{
int j=3;
while(j)
{
if(i+1==n)
break;
if(i%3==0)
printf("%d",arr[i+1]-arr[i]);
else
printf(" %d",arr[i+1]-arr[i]);
j--;i++;
}
if(i==n-1)
break;
printf("\n");
}
}
================================EXAMPLE 7.5================================
#include <stdio.h>
#include <math.h>
int main()
{
int x,a,b,c,d;
scanf("%d",&x);
a=x/1000;
b=(x/100)%10;
c=(x%100)/10;
d=x%10;
a=(a+9)%10;
b=(b+9)%10;
c=(c+9)%10;
d=(d+9)%10;
printf("The encrypted number is %d%d%d%d",c,d,a,b);
return 0;
}
================================EXAMPLE 8.1================================
#include <stdio.h>
int main(){
int n;
int m[11][11];
int value=1;
int i,j;
scanf("%d",&n);
for(i=0;i<(n+1)/2;i++){
for(j=i;j < n-i;j++){
m[i][j]=value++;
}
for(j=i+1;j < n-i;j++){
m[j][n-i-1]=value++;
}
for(j=n-i-2;j>=i;j--){
m[n-i-1][j]=value++;
}
for(j=n-i-2;j > i;j--){
m[j][i]=value++;
}
}
for(int i=0;i < n;i++){
for(int j=0;j < n;j++){
printf("%3d",m[i][j]);
}
printf("\n");
}
return 0;
}
================================EXAMPLE 8.2================================
#include <stdio.h>
int main()
{
int n, m[10][10]={0};
int i, j, sum=0;
scanf("%d", &n);
// read matrix
for(i=0;i < n;i++)
for(j=0;j < n;j++)
scanf("%d", &m[i][j]);
// sum matrix
for(i=0;i < n;i++)
for(j=0;j < n;j++) {
if((i+j!=n-1)&&(j!=n-1)&&(i!=n-1))
sum+=m[i][j];
}
printf("%d\n", sum);
return 0;
}
================================EXAMPLE 8.3================================
#include <stdio.h>
int main()
{
int n, m[10][10]={0};
int i, j, sum=0;
scanf("%d", &n);
for(i=0;i < n;i++)
for(j=0;j < n;j++)
scanf("%d", &m[i][j]);
for(i=0;i < n;i++) {
for(j=0;j < n;j++) {
// Transposed matrix
printf("%4d", m[j][i]);
}
printf("\n");
}
return 0;
}
================================EXAMPLE 8.4================================
#include <stdio.h>
int main()
{
int m[10][10]={0};
int i, j, sum;
for(i=0;i < 4;i++)
for(j=0;j < 3;j++)
scanf("%d", &m[i][j]);
for(i=0;i < 4;i++) {
sum = 0;
for(j=0;j < 3;j++) {
sum += m[i][j];
}
printf("%d\n", sum);
}
return 0;
}
================================EXAMPLE 8.5================================
#include <stdio.h>
int main()
{
int n, m[10][10]={0};
int i, j;
int row, col, max;
scanf("%d", &n);
for(i=0;i < n;i++)
for(j=0;j < n;j++)
scanf("%d", &m[i][j]);
max = m[0][0];
row = col = 0;
for(i=0;i < n;i++) {
for(j=0;j < n;j++) {
if (max < m[i][j]) {
max = m[i][j];
row = i;
col = j;
}
}
}
printf("max=%d,i=%d,j=%d\n", max, row, col);
return 0;
}
================================EXAMPLE 9.1================================
#include <stdio.h>
int main ()
{
int n, sign;
scanf("%d", &n);
if(n < 0){
sign=-1;
}
else if(n > 0){
sign=1;
}
else sign=0;
printf("sign(%d) = %d", n, sign);
return 0;
}
================================EXAMPLE 9.2================================
#include <stdio.h>
int func(int m)
{
int n;
int counter = 0;
for ( n = 1; n <= m; n++)
{
if ( 0 == m % n )
{
counter++;
}
}
if ( 2 == counter )
{
return 1;
}
return 0;
}
int main()
{
int m;
int row = 0;
int count = 0;
for ( m = 1; m <= 100; m++)
{
if (func(m))
{
row++;
count++;
printf("%6d",m);
}
if (10 == row)
{
printf("\n");
row = 0;
}
}
return 0;
}
================================EXAMPLE 9.3================================
#include <stdio.h>
void diamond(int num, char image);
int main()
{
int num;
char image;
scanf("%d %c", &num, &image);
diamond(num, image);
return 0;
}
void diamond(int num, char image)
{
int i, n;
if (num<=0) {
puts("None");
return;
}
if (num%2 == 0) {
puts("Error");
return;
}
for (i=0;i<=num/2;i++) {
n = num/2 - i;
while (n--) printf(" ");
n = 2 * i + 1;
while (n--) printf("%c", image);
printf("\n");
}
for (i=1;i<=num/2;i++) {
n = i;
while (n--) printf(" ");
n = num - 2*i;
while (n--) printf("%c", image);
printf("\n");
}
return;
}
================================EXAMPLE 9.4================================
#include <stdio.h>
#define N 10
double avg(int score[N]);
int min(int score[N]);
double avg(int score[N])
{
int i;
double sum=0;
for(i=0;i < N;i++)
sum += score[i];
return (sum / N);
}
int min(int score[N])
{
int i;
int m;
m = score[0];
for(i=1;i < N;i++)
// find minimal score
if(m > score[i]) m = score[i];
return m;
}
int main()
{
int i;
int score[N];
for(i=0;i < N;i++)
scanf("%d", &score[i]);
printf("average=%.0f\n", avg(score));
printf("min=%d\n", min(score));
return 0;
}
================================EXAMPLE 9.5================================
#include <stdio.h>
#define N 22
int fun_number(long long x);
int fun_number2(int x[], int y[]);
void data_mult(int x[], int multiple, int y[]);
int getlength(int x[]);
int fun_number(long long x)
{
int digit[N] = {-1};
int i = 0, j, y;
int flag;
long long oldx;
oldx = x;
do {
y = x % 10;
digit[i++] = y;
x = x / 10;
} while (x>0);
x = 2*oldx;
do {
y = x % 10;
j = 0;
flag = 0;
while (j < i) {
if(digit[j]==y && digit[j]!= -1) {
digit[j] = -1;
flag = 1;
break;
}
else j++;
}
if(flag == 0)
return 0;
x = x / 10;
} while (x > 0);
return 1;
}
int fun_number2(int x[], int y[])
{
int digit[N] = {'n'};
int flag;
int i, j, k;
i = 0;
while (x[i]!='\0') {
digit[i] = x[i];
i++;
}
digit[i] = '\0';
k = 0;
do {
j = 0; flag = 0;
while (j < i) { // i is the length of array digit
if(digit[j]==y[k] && digit[j]!='n') {
digit[j] = 'n';
flag = 1;
break;
}
else j++;
}
if(flag == 0)
return 0;
k++;
} while (y[k]!='\0');
return 1;
}
void data_mult(int x[], int multiple, int y[])
{
int len, i, j;
int carry, t;
int temp[N];
len = getlength(x);
carry = 0; j = 0;
for(i=len-1;i>=0;i--) {
t = multiple * (x[i] - '0');
temp[j] = (t%10 + carry)%10 + '0';
carry = (t >= 10 ? t/10 : 0) + (t%10 + carry)/10;
j++;
}
if (carry >= 1) temp[j++] = '0' + carry;
temp[j] = '\0';
j = 0;
len = getlength(temp);
for(i=len-1;i>=0;i--) {
y[j] = temp[i];
j++;
}
y[j] = '\0';
}
int getlength(int x[])
{
int len=0;
while (x[len++]!='\0');
return (len - 1);
}
int main_old()
{
long long x;
scanf("%lld", &x);
if(fun_number(x) == 1)
printf("Yes\n");
else
printf("No\n");
printf("%lld", x * 2);
return 0;
}
int main()
{
int x[N], y[N], ch;
int i = 0, j;
while( (ch=getchar()) != '\n' ) {
x[i] = ch;
i++;
if(i>=20) break; // Up to 20 digits
}
x[i] = '\0';
data_mult(x, 2, y);
if(fun_number2(x, y) == 1)
printf("Yes\n");
else
printf("No\n");
j = 0;
while (y[j]!='\0') {
printf("%c", y[j]);
j++;
}
return 0;
}
================================EXAMPLE 10.1===============================
#include <stdio.h>
int addNumbers(int n);
int main() {
int num;
scanf("%d", &num);
printf("%d", addNumbers(num));
return 0;
}
int addNumbers(int n) {
if (n != 0)
return n + addNumbers(n - 1);
else
return n;
}
================================EXAMPLE 10.2===============================
#include <stdio.h>
int main ()
{
int n;
scanf("%d",&n);
while(n>0)
{
printf("%d",n%10);
n=n/10;
}
return 0;
}
================================EXAMPLE 10.3===============================
#include <stdio.h>
#include <math.h>
int main()
{
int num;
int count = 0;
scanf("%d", &num);
count = (num == 0) ? 1 : (log10(num) + 1);
printf("The number of digits in %d is: %d", num,count);
return 0;
}
================================EXAMPLE 10.4===============================
#include <stdio.h>
int main()
{
int n,sum=0,m;
scanf("%d",&n);
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
printf("%d",sum);
return 0;
}
================================EXAMPLE 10.5===============================
#include <stdio.h>
void hano(int n,char x,char y,char z);
int main ()
{
int n;
char x,y,z;
scanf("%d\n",&n);
scanf("%c %c %c ",&x,&y,&z);
hano(n,x,y,z);
}
void hano(int n,char x,char y,char z)
{
if(n==1){
printf("%d: %c -> %c\n",n,x,y);
}
else{
hano(n-1,x,z,y);
printf("%d: %c -> %c\n",n,x,y);
hano(n-1,z,y,x);
}
}
================================EXAMPLE 11.1===============================
#include <stdio.h>
int max_min(int n, int *a);
int main()
{
int n;
scanf("%d", &n);
int a[n];
for(int i = 0;i < n;i++)
{
scanf("%d", (a + i));
}
max_min(n, a);
return 0;
}
int max_min(int n, int *a)
{
int t;
for(int i = 0;i < n - 1;i++)
{
for(int j = 0;j < n - i - 1;j++)
{
if(*(a + j) < *(a + j + 1))
{
t = *(a + j + 1);
*(a + j + 1) = *(a + j);
*(a + j)= t;
}
}
}
printf("max = %d\n", *(a));
printf("min = %d\n", *(a + n - 1));
}
================================EXAMPLE 11.2===============================
#include <stdio.h>
int sub(int *a, int n)
{
int t,i;
int max = a[0], min = a[0];
for(i=0;i<n;i++)
{
if(a[i]>max)
max = a[i];
if(a[i] < min)
min = a[i];
}
printf("difference value = %d", (max-min));
}
int main()
{
int a[10];
int i;
for(i=0;i<10;i++)
scanf("%d", &a[i]);
sub(a,10);
return 0;
}
================================EXAMPLE 11.3===============================
#include <stdio.h>
int main()
{
int i, j, m, n;
int a[6][6];
int b[6] = {0};
scanf("%d%d",&m,&n);
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", &a[i][j]);
b[i] += a[i][j];
}
}
for (i = 0; i < m; i++)
{
printf("%d\n",b[i]);
}
return 0;
}
================================EXAMPLE 11.4===============================
#include <stdio.h>
#define N 10
void sort(double *b, int n);
int main()
{
int n, i, j;
double a[N];
scanf("%d", &n);
for(i=0;i < n;i++)
scanf("%lf", &a[i]);
sort(a, n);
return 0;
}
void sort(double *b, int n)
{
int i, j, index;
for(i=0;i < n;i++) {
index = i;
for(j=i+1;j < n;j++)
if(*(b+index) < *(b+j)) {
index = j;
}
if(index != i) {
double temp;
temp = *(b+index);
*(b+index) = *(b+i);
*(b+i) = temp;
}
if(i > 0) printf(" ");
printf("%.2f", *(b+i));
}
}
================================EXAMPLE 12.1===============================
#include <stdio.h>
#define N 50
int main()
{
char s1[N],s2[N],s[2*N];
char *p1, *p2;
gets(s1);
gets(s2);
p1 = s2;
p2 = s1;
int len;
while(*p2) { s[len++] = *p2; p2++; }
while(*p1) { s[len++] = *p1; p1++; }
s[len] = '\0';
printf("%s\n", s);
return 0;
}
================================EXAMPLE 12.2===============================
#include <stdio.h>
#define N 10
void mov(int *x, int n, int m);
int main()
{
int n, m;
int x[N] = {'\0'};
scanf("%d %d", &n, &m);
for(int i=0;i<n;i++)
scanf("%d", &x[i]);
mov(x, n, m);
return 0;
}
void mov(int *x, int n, int m)
{
int y[N] = {'\0'};
int loc = 0;
while(*(x+loc)) {
y[(loc+m)%n] = *(x+loc);
loc++;
}
printf("After move:");
loc = 0;
while(*(y+loc)) {
printf(" %d", *(y+loc));
loc++;
}
}
================================EXAMPLE 12.3===============================
#include <stdio.h>
#include <string.h>
#define N 5
#define N1 20
int main()
{
char str[N1], max[N1];
int i;
printf("");
gets(max);
strcpy(max,max);
for (i = 2; i <= N; i++)
{
gets(str);
if (strcmp(str, max) > 0)
strcpy(max, str);
}
printf("Max is: %s\n", max);
return 0;
}
================================EXAMPLE 12.4===============================
#include <stdio.h>
int search(int list[],int n,int x);
int main(){
int rep;
int i,j,n,x;
int a[100];
int *p;
int b[100];
p=a;
scanf("%d",&rep);
for(i=0;i < rep;i++){
scanf("%d",&n);
for(j=0;j < n;j++)
{
scanf("%d",p++);
}
p=p-n;
scanf("%d",&x);
b[i]=search(p,n,x);
p=p+n;
}
for(i=0;i < rep;i++){
if(b[i]==-1){
printf("Not found\n");
}else{
printf("index = %d\n",b[i]);
}
}
}
int search(int list[],int n,int x){
int i,j;
for(i=0;i < n;i++){
if(list[i]==x){
return i;
}
}
return -1;
}
================================EXAMPLE 13.1===============================
#include <stdio.h>
int main()
{
int repeat, x;
char *m[12] = {"January","February","March","April",
"May","June","July","August","September",
"October","November","December"};
scanf("%d", &repeat);
int i;
for(i=0; i < repeat; i++){
scanf("%d", &x);
if(x>=1 && x<=12)
printf("%s\n", m[x-1]);
else
printf("Wrong input!\n");
}
return 0;
}
================================EXAMPLE 13.2===============================
#include <stdio.h>
int main()
{
char c;
char str[80];
int repeat, ri;
void delchar(char *str, char c);
scanf("%d", &repeat);
getchar();
for(ri = 1; ri <= repeat; ri++){
gets(str);
scanf("%c", &c);
getchar();
delchar(str,c);
printf("result: ");
puts(str);
}
}
void delchar(char *str, char c)
{
int i,j;
for(i=j=0;str[i]!='\0';i++){
if(str[i]!=c)
str[j++]=str[i];}
str[j]='\0';
}
================================EXAMPLE 13.3===============================
#include <stdio.h>
void bubbleSort(int*, int);
int main(void)
{
int a[10], i, * p, n;
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
p = a;
bubbleSort(p, n);
for (i = 0; i < n; i++) {
printf("%d ", a[i]);
}
}
void bubbleSort(int* b, int n)
{
int i, temp, * p;
for (i = 0; i < n - 1; i++)
{
for (p = b; p < b + n - i - 1; p++)
if (*p > * (p + 1))
{
temp = *p;
*p = *(p + 1);
*(p + 1) = temp;
}
}
}
================================EXAMPLE 13.4===============================
#include <stdio.h>
#define N 100
void delchar(char *str, char c);
int main()
{
char str[N] = {'\0'};
gets(str);
delchar(str, 'a');
puts(str);
return 0;
}
void delchar(char *str, char c)
{
int i=0, k=0;
while(*(str+i)) {
if(*(str+i) != c) {
*(str+k) = *(str+i);
k++;
}
i++;
}
*(str+k) = '\0';
}
================================EXAMPLE 14.1===============================
#include <stdio.h>
#include <stdlib.h>
int main(){
int n;
scanf("%d",&n);
int* a=(int *)malloc(sizeof(int)*n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
double sum=0;
double min=99999,max=0;
for(int i=0;i<n;i++){
sum+=a[i];
if(a[i]>max){
max=a[i];
}
if(a[i] < min){
min=a[i];
}
}
double average=sum/n;
printf("average = %.2f\n",average);
printf("max = %.2f\n",max);
printf("min = %.2f",min);
return 0;
}
================================EXAMPLE 14.2===============================
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int main()
{
int size;
int num = 0;
int *MM;
scanf("%d", &size);
if((MM = (int *)calloc(size*size, sizeof(int))) == NULL) {
exit(1);
}
int row,col,loc;
row = 0;
col = size >> 1;
loc = row * size + col;
*(MM + loc) = ++num;
while(num < size * size) {
if(num % size == 0) {
row = (row + 1) % size;
col = col;
}
else {
row = (row - 1 + size) % size;
col = (col + 1) % size;
}
loc = row * size + col;
*(MM + loc) = ++num;
}
for (row = 0; row < size; row++) {
for (col = 0; col < size; col++) {
loc = row * size + col;
printf("%5d", *(MM + loc));
}
printf("\n");
}
printf("\n");
free(MM);
return 0;
}
================================EXAMPLE 15.1===============================
#include <stdio.h>
struct node{
char name[35];
double price;
}a[10];
int main(){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
getchar();
gets(a[i].name);
scanf("%lf",&a[i].price);
}
double max=0;
int s=0;
for(int i=0;i<10;i++){
if(a[i].price>max){
max=a[i].price;
s=i;
}
}
double min=999.0;
int k=0;
for(int i=0;i<n;i++){
if(a[i].price < min){
min=a[i].price;
k=i;
}
}
printf("%.2f, %s\n",a[s].price,a[s].name);
printf("%.2f, %s\n",a[k].price,a[k].name);
return 0;
}
================================EXAMPLE 15.2===============================
#include <stdio.h>
#define N 1000
struct yuan
{
char name[11];
double z1;
double z2;
double z3;
double sum;
}ren[N];
int main()
{
int n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s %lf %lf %lf",ren[i].name,&ren[i].z1,&ren[i].z2,&ren[i].z3);
ren[i].sum=ren[i].z1+ren[i].z2-ren[i].z3;
}
for(i=0;i<n;i++)
{
printf("%s %.2lf\n",ren[i].name,ren[i].sum);
}
return 0;
}
================================EXAMPLE 15.3===============================
#include <stdio.h>
struct str
{
int num;
char name[20];
char p;
union
{
char bumenhao[10];
char zhicheng[50];
}a;
}
t[100];
int main()
{
int n,i,tcount=0,wcount=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{ scanf("%d %s %c",&t[i].num,t[i].name,&t[i].p);
if(t[i].p=='w')
gets(t[i].a.bumenhao);
else
gets(t[i].a.zhicheng);
}
for(i=0;i<n;i++)
{
if(t[i].p=='w')
{
printf("%d %s %c%s\n",t[i].num,t[i].name,t[i].p,t[i].a.bumenhao);
wcount++;
}
if(t[i].p=='t')
{printf("%d %s %c%s\n",t[i].num,t[i].name,t[i].p,t[i].a.zhicheng);
tcount++;
}
}
printf("tcount = %d, wcount = %d\n",tcount,wcount);
return 0;
}