#include <stdio.h> int main() { int a[4][2]={{89, 76},{65, 91}, {56, 78}, {49, 56}}; return 0; } |
#include <stdio.h> struct student{ char *ID; int Midterm; int Final; char Grade; }; int main() { struct student smith={"1000123456", 89, 98, 'A'}, doe={"1000123457", 45, 53, 'F'}, jones={"1000123458", 67, 80, 'C'}; printf("%s\n", smith.ID); printf("%c\n", jones.Grade); printf("%d\n", doe.Midterm); doe.Midterm=79; doe.Grade='D'; printf("%c\n", doe.Grade); return 0; } |
#include <stdio.h> struct student{ char *Name; int Midterm; int Final; char Grade; }; int main() { struct student fall[15]; int i; FILE *data; data=fopen("junk.dat", "r"); for (i=0;i<15;i++) fscanf(data, "%s %d %d %c", fall[i].Name, &fall[i].Midterm, &fall[i].Final, &fall[i].Grade); fclose(data); printf("%d\n", fall[0].Midterm); printf("%s\n", fall[0].Name); return 0; } |
Doe 45 47 F Jones 89 65 B Smith 76 79 B ........ |
#include <stdio.h> struct student{ char *Name; int Midterm; int Final; char Grade; }; int main() { struct student fall[3]= { {"Jones", 45, 65, 'D'}, {"Doe", 98, 79, 'B'}, {"Smith", 67, 76, 'C'}}; /* .................. */ printf("%s %d %d %c\n", fall[0].Name, fall[0].Midterm, fall[0].Final, fall[0].Grade); return 0; } |
#include <stdio.h> struct student{ char *Name; int Midterm; int Final; char Grade; }; int main() { struct student Smith={"David Smith", 12, 45, 'F'}, *ptr; ptr = &Smith; /* .................. */ printf("%s %d %d %c\n", ptr->Name, ptr->Midterm, ptr->Final, ptr->Grade); return 0; } |
#include <stdio.h> typedef struct { char *Name; int Midterm; int Final; char Grade; } student; int main() { student Jones={"Jones", 12, 45, 'F'}, *ptr; ptr = &Jones; /* .................. */ printf("%s\n", ptr->Name); return 0; } |
#include <stdio.h> typedef struct {float Real; float Im;} Complex; Complex ComplexAdd(Complex z1, Complex z2) { Complex z; z.Real = z1.Real + z2.Real; z.Im = z1.Im + z2.Im; return z; } int main() { Complex z1, z2, z; printf("Enter real and imaginary parts of z1 separated by space = "); scanf("%f %f", &z1.Real, &z1.Im); printf("Enter real and imaginary parts of z2 separated by space = "); scanf("%f %f", &z2.Real, &z2.Im); z = ComplexAdd(z1, z2); printf("%f + %f I \n", z.Real, z.Im); return 0; } |
| (1) |
| (2) |
| (3) |
#include <stdio.h> #include <math.h> int main() { float a, b, c, disc,x1, x2; a=1.0;b=200000;c=-3; disc=b*b-4*a*c; x1=(-b-sqrt(disc))/(2*a); x2=(-b+sqrt(disc))/(2*a); printf("x1 = %f, x2 = %f\n",x1,x2); return 0; } |
|
|
|
| (11) |
| (12) |
|
| (16) |
| (17) |
| (18) |
/* Compute the square root of 2 */ #include <stdio.h> #include <math.h> #define EPS 1.0e-10 #define N 100 double f(double x) { return pow(x,2)-2; } /* start of main */ int main() { double x1, x2, x3; int count; printf("Enter xleft and xright separated by space ="); scanf("%lf %lf", &x1, &x2); /* sanity check */ if (f(x1)*f(x2)>0) {printf("Invalid x1 and x2 !\n"); return 0;} /* bisection start */ for (count=0;count< N; count++) { x3= (x1+x2)/2.0; if (f(x1)*f(x3)<0 ) x2=x3; else x1=x3; if ( f(x3)==0.0 || fabs(x1-x2)< EPS ) break; } printf("iteration = %d\n", count); printf("x= %lf\n", x1); return 0; } |
|
|
|
|
|
|
|
| (19) |
|
#include <stdio.h> #include <math.h> #define EPS 1.0e-10 double f(double x) { return x*x-2; } double fp(double x) { return 2*x; } double newton(double x) { return x - f(x)/fp(x); } int main() { double x1, x2; int i; printf("Enter initial guess ="); scanf("%lf", &x1); if (fp(x1)==0.0) { printf("No convergence.\n"); return 0; } for (i=0;i<100;i++) { x2=newton(x1); if (fabs(x1-x2)< EPS) break; x1=x2; } printf("iteration = %d\n", i); printf("x= %lf\n", x1); return 0; } |
|
|
|
|
|