Chapter 5. Functions

Home

 

PROGRAM 51 : tbl_of_pow.c

#include <stdio.h>

#define N 7

long power(int, int);
void prn_heading(void);
void prn_tbl_of_powers(int);

int main(void)
{
  prn_heading();
  prn_tbl_of_powers(N);
  return 0;
}

void prn_heading(void)
{
  printf("\n::::: A TABLE OF POWERS :::::\n\n");
}

void prn_tbl_of_powers(int n)
{
  int i, j;

  for (i = 1; i <= n; ++i) {
    for (j = 1; j <= n; ++j)
      if (j == 1)
        printf("%ld", power(i, j));
      else
        printf("%9ld", power(i, j));
      putchar('\n');
  }
}

long power(int m, int n)
{
  int i;
  long product = 1;

  for (i = 1; i <= n; ++i)
    product *= m;
  return product;
}

 

PROGRAM 52 : compute_sum.c

#include <stdio.h>

int main(void)
{
  int n = 3, sum, compute_sum(int);

  printf("%d\n", n); /* 3 is printed */
  sum = compute_sum(n);
  printf("%d\n", n); /* 3 is printed */
  printf("%d\n", sum); /* 6 is printed */
  return 0;
}

int compute_sum(int n) /* sum the integers from 1 to n */
{
  int sum = 0;

  for ( ; n > 0; --n) /* stored value of n is changed */
    sum += n;
  return sum;
}

PROGRAM 53 : main.c

#include "pgm.h"

int main(void)
{
  char ans;
  int i, n = N;

  printf("%s",
    "This program does not do very much.\n"
    "Do you want more information? ");
  scanf(" %c", &ans);
  if (ans == 'y' || ans == 'Y')
    wrt_info("pgm");
  for (i = 0; i < n; ++i)
    fct1(i);
  printf("Bye!\n");
  return 0;
}

PROGRAM 531 : fct.c

#include "pgm.h"

void fct1(int n)
{
  int i;

  printf("Hello from fct1()\n");
  for (i = 0; i < n; ++i)
    fct2();
}

void fct2(void)
{
  printf("Hello from fct2()\n");
}

PROGRAM 532 : wrt.c

#include "pgm.h"

void wrt_info(char *pgm_name)
{
  printf("Usage: %s\n\n", pgm_name);
  printf("%s\n",
    "This program illustrates how one can write a program\n"
    "in more than one file. In this example, we have a\n"
    "single .h file that gets included at the top of our\n"
    "three .c files. Thus the .h file acts as the \"glue\"\n"
    "that binds the program together.\n"
    "\n"
    "Note that the functions fct1() and fct2() when called\n"
    "only say \"hello.\" When writing a serious program, the\n"
    "programmer sometimes does this in a first working\n"
    "version of the code.\n");
}

PROGRAM 533 : pgm.h

#include <stdio.h>
#include <stdlib.h>

#define N 3

void fct1(int k);
void fct2(void);
void wrt_info(char *);

PROGRAM 534 : makefile

CC = gcc
CFLAGS = -Wall
EXEC = a.out
INCLS = 
LIBS = 

OBJS = main.o fct.o wrt.o

$(EXEC): $(OBJS)
@echo "linking ..."
@$(CC) $(CFLAGS) -o $(EXEC) $(OBJS) $(LIBS)

$(OBJS): pgm.h
$(CC) $(CFLAGS) $(INCLS) -c $*.c

relink:
@echo "relinking ..."
@$(CC) $(CFLAGS) -o $(EXEC) $(OBJS) $(LIBS)

PROGRAM 54 : global.c

#include <stdio.h>

int a = 1, b = 2, c = 3; /* global variables */
int f(void); /* function prototype */

int main(void)
{
  printf("%3d\n", f()); /* 12 is printed */
  printf("%3d%3d%3d\n", a, b, c); /* 4 2 3 is printed */
  return 0;
}

int f(void)
{
  int b, c;   /* b and c are local */
              /* global b, c are masked */
  a = b = c = 4;
  return (a + b + c);
}

PROGRAM 55 :main.c

#include <stdio.h>

int a = 1, b = 2, c = 3; /* external variables */
int f(void);

int main(void)
{
  printf("%3d\n", f());
  printf("%3d%3d%3d\n", a, b, c);
  return 0;
}

PROGRAM 551 : fct.c

int f(void)
{
  extern int a; /* look for it elsewhere */
  int b, c;
 
  a = b = c = 4;
  return (a + b + c);
}

PROGRAM 552 : makefile

CC = gcc
CFLAGS = -Wall
EXEC = a.out
INCLS = 
LIBS = 

OBJS = main.o fct.o

$(EXEC): $(OBJS)
@echo "linking ..."
@$(CC) $(CFLAGS) -o $(EXEC) $(OBJS) $(LIBS)

$(OBJS):
$(CC) $(CFLAGS) $(INCLS) -c $*.c

relink:
@echo "relinking ..."
@$(CC) $(CFLAGS) -o $(EXEC) $(OBJS) $(LIBS)

PROGRAM 56 : wr_bkwrds.c

/* Write a line backwards. */

#include <stdio.h>

void wrt_it(void);

int main(void)
{

  printf("Input a line: ");
  wrt_it();
  printf("\n\n");
  return 0;
}

void wrt_it(void)
{
  int c;

  if ((c = getchar()) != '\n')
    wrt_it();
  putchar(c);
}

PROGRAM 57 : main.c

#include "hanoi.h"

int cnt = 0; /* count of the number of moves */

int main(void)
{
  int n; /* number of disks */

  n = get_n_from_user();
  assert(n > 0);

/*
// Move n disks from tower A to tower C,
// using tower B as an intermediate tower.
*/
  move(n, 'A', 'B', 'C'); /* recursive fct */
  return 0;
}

PROGRAM 571 : move.c

#include "hanoi.h"

void move(int n, char a, char b, char c)
{
  if (n == 1) {
    ++cnt;
    printf("%5d: %s%d%s%c%s%c.\n", cnt,
    "Move disk ", 1, " from tower ", a, " to tower ", c);
  }
  else {
    move(n - 1, a, c, b);
    ++cnt;
    printf("%5d: %s%d%s%c%s%c.\n", cnt,
    "Move disk ", n, " from tower ", a, " to tower ", c);
    move(n - 1, b, a, c);
  }
}

PROGRAM 572 : get.c 

#include "hanoi.h"

int get_n_from_user(void)
{
  int n;

  printf("%s",
    "---\n"
    "TOWERS OF HANOI:\n"
    "\n"
    "There are three towers: A, B, and C.\n"
    "\n"
    "The disks on tower A must be moved to tower C. Only one\n"
    "disk can be moved at a time, and the order on each tower\n"
    "must be preserved at each step. Any of the towers A, B,\n"
    "or C can be used for intermediate placement of a disk.\n"
    "\n"
    "The problem starts with n disks on Tower A.\n"
    "\n"
    "Input n: ");
  if (scanf("%d", &n) != 1 || n < 1) {
    printf("\nERROR: Positive integer not found - bye!\n\n");
    exit(1);
  }
  printf("\n");
  return n;
}

 

PROGRAM 572 : hanoi.h 

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

extern int cnt; /* count of the number of moves */

int get_n_from_user(void);
void move(int n, char a, char b, char c);

 

PROGRAM 573 : makefile

CC = cc
CFLAGS =
EXEC = a.out
INCLS = 
LIBS = 

OBJS = main.o get.o move.o

$(EXEC): $(OBJS)
@echo "linking ..."
@$(CC) $(CFLAGS) -o $(EXEC) $(OBJS) $(LIBS)

$(OBJS): hanoi.h
$(CC) $(CFLAGS) $(INCLS) -c $*.c

relink:
@echo "relinking ..."
@$(CC) $(CFLAGS) -o $(EXEC) $(OBJS) $(LIBS)

 


[Last Update: 2001.3.28] Dongseo University Cyber Campus