ex5-1.c
/* code: ex5-1.c (v1.20.00) */
#include <stdio.h>
int main () {
int i;
for (i = 0; i < 10; i++)
printf ("%d ", i);
printf ("\n");
for (i = 0; i < 10; i++)
printf ("%d ", i);
printf ("\n");
for (i = 0; i < 10; i++)
printf ("%d ", i);
printf ("\n");
return 0;
}
ex5-2.c
/* code: ex5-2.c (v1.20.00) */
#include <stdio.h>
void print_numbers (void) {
int i;
for (i = 0; i < 10; i++)
printf ("%d ", i);
printf ("\n");
}
int main () {
print_numbers ();
print_numbers ();
print_numbers ();
return 0;
}
ex5-3.c
/* code: ex5-3.c (v1.20.00) */
#include <stdio.h>
void print_numbers (void);
int main () {
print_numbers ();
print_numbers ();
print_numbers ();
return 0;
}
void print_numbers (void) {
int i;
for (i = 0; i < 10; i++)
printf ("%d ", i);
printf ("\n");
}
ex5-4.c
/* code: ex5-4.c (v1.20.00) */
#include <stdio.h>
void g (void) {
int i;
for (i = 0; i < 3; i++) {
printf ("a");
}
}
void f (void) {
int i;
for (i = 0; i < 5; i++) {
g ();
}
}
int main (int argc, char **argv) {
f ();
return 0;
}
ex5-5.c
/* code: ex5-5.c (v1.20.00) */
#include <stdio.h>
float triangle (float base, float height) {
float c;
c = (base * height) / 2.000F;
return c;
}
int main () {
float t;
t = triangle (3.00, 4.00);
printf ("triangle = %f\n", t);
t = triangle (5.00, 6.00);
printf ("triangle = %f\n", t);
return 0;
}
ex5-6.c
/* code: ex5-6.c (v1.20.00) */
#include <stdio.h>
void add_pass_by_value (int i) {
i = i + 1;
}
void add_pass_by_reference (int *i) {
*i = *i + 1;
}
int main () {
int a;
a = 10;
add_pass_by_value (a);
printf ("%d\n", a);
a = 10;
add_pass_by_reference (&a);
printf ("%d\n", a);
return 0;
}
ex5-7.c
/* code: ex5-7.c (v1.20.00) */
#include <stdio.h>
int factorial (int n) {
if (n == 0) {
return 1;
}
else {
return n * factorial (n - 1);
}
}
int main () {
int i;
i = 5;
printf ("%d! = %d\n", i, factorial (i));
return 0;
}
q5-1.c
/* code: q5-1.c (v1.20.00) */
#include <stdio.h>
float trapezoid (float a, float b, float h) {
float c;
c = ((a + b) / 2.000F) * h;
return c;
}
int main () {
float t;
t = trapezoid (3.00, 4.00, 5.00);
printf ("trapezoid = %f\n", t);
t = trapezoid (5.00, 6.00, 7.00);
printf ("trapezoid = %f\n", t);
return 0;
}
q5-2.c
/* code: q5-2.c (v1.20.00) */
#include <stdio.h>
struct student {
int id;
char grade;
float average;
};
typedef struct student STUDENT_TYPE;
STUDENT_TYPE initialize_student_record (STUDENT_TYPE s) {
s.id++;
s.grade = 'x';
s.average = 0.0;
return s;
}
int main () {
STUDENT_TYPE student;
student.id = 20;
student.grade = 'a';
student.average = 300.000;
printf ("%d %c %f\n", student.id, student.grade, student.average);
student = initialize_student_record (student);
printf ("%d %c %f\n", student.id, student.grade, student.average);
return 0;
}
q5-3.c
/* code: q5-3.c (v1.20.00) */
#include <stdio.h>
int fibonacci (int n) {
if (n == 0) {
return 0;
}
else if (n == 1) {
return 1;
}
else {
return (fibonacci (n - 1) + fibonacci (n - 2));
}
}
int main () {
int i;
i = 10;
printf ("fibonacci(%d) = %d\n", i, fibonacci (i));
return 0;
}
q5-4.c
/* code: q5-4.c (v1.20.00) */
#include <stdio.h>
void foo (int n) {
if (n < 15) {
foo (n + 1);
printf ("%d ", n);
}
}
int main () {
foo (0);
return 0;
}
q5-5.c
/* code: q5-5.c (v1.20.00) */
#include <stdio.h>
int factorial (int n) {
if (n == 0) {
return 1;
}
else {
return n * factorial (n - 1);
}
}
int main () {
int i;
/* i = -1; */
i = 1;
printf ("%d! = %d\n", i, factorial (i));
return 0;
}
EOF