Chapter 1. An Overview of C 

Home

PROGRAM 11 : see_box.c


#include <stdio.h>

int main(void)
{
  printf("\n\n\n\n\n\n\n\n\n\n");
  printf(" ***********************\n");
  printf(" * from sea *\n");
  printf(" * to shining C *\n");
  printf(" ***********************\n");
  printf("\n\n\n\n\n\n\n\n\n\n");
  return 0;
}

 

PROGRAM 12 : marathon.c


/* The distance of a marathon in kilometers. */

#include <stdio.h>

int main(void)
{
  int miles, yards;
  float kilometers;

  miles = 26;
  yards = 385;
  kilometers = 1.609 * (miles + yards / 1760.0);
  printf("\nA marathon is %f kilometers.\n\n", kilometers);
  return 0;
}

 

PROGRAM 13 : pacific_sea.c 


/* Measuring the Pacific Sea. */

#include "pacific_sea.h"

int main(void)
{
  const int pacific_sea = AREA; /* in sq kilometers */
  double acres, sq_miles, sq_feet, sq_inches;

  printf("\nThe Pacific Sea covers an area");
  printf(" of %d square kilometers.\n", pacific_sea);
  sq_miles = SQ_MILES_PER_SQ_KILOMETER * pacific_sea;
  sq_feet = SQ_FEET_PER_SQ_MILE * sq_miles;
  sq_inches = SQ_INCHES_PER_SQ_FOOT * sq_feet;
  acres = ACRES_PER_SQ_MILE * sq_miles;
  printf("In other units of measure this is:\n\n");
  printf("%22.7e acres\n", acres);
  printf("%22.7e square miles\n", sq_miles);
  printf("%22.7e square feet\n", sq_feet);
  printf("%22.7e square inches\n\n", sq_inches);
  return 0;
}

 

PROGRAM 13 : pacific_sea_h


#include <stdio.h>

#define AREA 2337
#define SQ_MILES_PER_SQ_KILOMETER 0.3861021585424458
#define SQ_FEET_PER_SQ_MILE (5280 * 5280)
#define SQ_INCHES_PER_SQ_FOOT 144
#define ACRES_PER_SQ_MILE 640

 

PROGRAM 14 : in_out.c


#include <stdio.h>

int main(void)
{
  char c1, c2, c3;
  int i;
  float x;
  double y;

  printf("\n%s\n%s",
    "Input three characters,"
    "an int, a float, and a double: ");
  scanf("%c%c%c%d%f%lf", &c1, &c2, &c3, &i, &x, &y);
  printf("\nHere is the data that you typed in:\n");
  printf("%3c%3c%3c%5d%17e%17e\n\n", c1, c2, c3, i, x, y);
  return 0;
}

 

PROGRAM 15 : sum.c


#include <stdio.h>

int main(void)
{
  int i = 1, sum = 0;

  while (i <= 5) {
    sum += i;
    ++i;
  }
  printf("sum = %d\n", sum);
  return 0;
}

 

PROGRAM 16 : running_sum.c


//* Compute the minimum, maximum, sum, and average. */

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

int main(void)
{
  int i;
  double x, min, max, sum, avg;

  if (scanf("%lf", &x) != 1) {
    printf("No data found - bye!\n");
    exit(1);
  }
  min = max = sum = avg = x;
  printf("%5s%9s%9s%9s%12s%12s\n%5s%9s%9s%9s%12s%12s\n\n",
    "Count", "Item", "Min", "Max", "Sum", "Average",
    "-----", "----", "---", "---", "---", "-------");
  printf("%5d%9.1f%9.1f%9.1f%12.3f%12.3f\n",
    1, x, min, max, sum, avg);
  for (i = 2; scanf("%lf", &x) == 1; ++i) {
    if (x < min)
      min = x;
    else if (x > max)
      max = x;
    sum += x;
    avg = sum / i;
    printf("%5d%9.1f%9.1f%9.1f%12.3f%12.3f\n",
      i, x, min, max, sum, avg);
  }
  return 0;
}

 

PROGRAM 17 : maxmin.c


#include <stdio.h>

float maximum(float x, float y);
float minimum(float x, float y);
void prn_info(void);

int main(void)
{
  int i, n;
  float max, min, x;

  prn_info();
  printf("Input n: ");
  scanf("%d", &n);
  printf("\nInput %d real numbers: ", n);
  scanf("%f", &x);
  max = min = x;
  for (i = 2; i <= n; ++i) {
    scanf("%f", &x);
    max = maximum(max, x);
    min = minimum(min, x);
  }
  printf("\n%s%11.3f\n%s%11.3f\n\n",
    "Maximum value:", max,
    "Minimum value:", min);
  return 0;
}

float maximum(float x, float y)
{
  if (x > y)
    return x;
  else
    return y;
}

float minimum(float x, float y)
{
  if (x < y)
    return x;
  else
    return y;
}

void prn_info(void)
{
  printf("\n%s\n%s\n\n",
    "This program reads an integer value for n, and then",
    "processes n real numbers to find max and min values.");
}

 

PROGRAM 18 : try_to.c


#include <stdio.h>

int main(void)
{
  int a = 1;
  void try_to_change_it(int);

  printf("%d\n", a); /* 1 is printed */
  try_to_change_it(a);
  printf("%d\n", a); /* 1 is printed again! */
  return 0;
}

void try_to_change_it(int a)
{
  a = 777;
}

 

PROGRAM 19 : bubble_sort.c


#include <stdio.h>

#define CLASS_SIZE 5

int main(void)
{
  int i, j, score[CLASS_SIZE], sum = 0, tmp;

  printf("Input %d scores: ", CLASS_SIZE);
  for (i = 0; i < CLASS_SIZE; ++i) {
    scanf("%d", &score[i]);
    sum += score[i];
  }
  for (i = 0; i < CLASS_SIZE - 1; ++i) /* bubble sort */
  for (j = CLASS_SIZE - 1; j > i; --j)
  if (score[j-1] < score[j]) { /* check the order */
    tmp = score[j-1];
    score[j-1] = score[j];
    score[j] = tmp;
  }
  printf("\nOrdered scores:\n\n");
  for (i = 0; i < CLASS_SIZE; ++i)
    printf(" score[%d] =%5d\n", i, score[i]);
  printf("\n%18d%s\n%18.1f%s\n\n",
    sum, " is the sum of all the scores",
    (double) sum / CLASS_SIZE, " is the class average");
  return 0;
}

 

PROGRAM 110 : nice_day.c


/* Have a nice day! */

#include <ctype.h>
#include <stdio.h>

#define MAXSTRING 100

int main(void)
{
  char c, name[MAXSTRING];
  int i, sum = 0;

  printf("\nHi! What is your name? ");
  for (i = 0; (c = getchar()) != '\n'; ++i) {
    name[i] = c;
    if (isalpha(c))
    sum += c;
  }
  name[i] = '\0';
  printf("\n%s%s%s\n%s",
    "Nice to meet you ", name, ".",
    "Your name spelled backwards is ");
  for (--i; i >= 0; --i)
    putchar(name[i]);
  printf("\n%s%d%s\n\n%s\n",
    "and the letters in your name sum to ", sum, ".",
    "Have a nice day!");
  return 0;
}

 

PROGRAM 111 : abc.c


#include <stdio.h>
#include <string.h>

#define MAXSTRING 100

int main(void)
{
  char c = 'a', *p, s[MAXSTRING];

  p = &c;
  printf("%c%c%c ", *p, *p + 1, *p + 2);
  strcpy(s, "ABC");
  printf("%s %c%c%s\n", s, *s + 6, *s + 7, s + 1);
  strcpy(s, "she sells sea shells by the seashore");
  p = s + 14;
  for ( ; *p != '\0'; ++p) {
    if (*p == 'e')
      *p = 'E';
    if (*p == ' ')
      *p = '\n';
  }
  printf("%s\n", s);
  return 0;
}

 

PROGRAM 112 : cnt_letters.c


/* Count uppercase letters in a file. */

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

int main(int argc, char *argv[])
{
  int c, i, letter[26];
  FILE *ifp, *ofp;

  if (argc != 3) {
    printf("\n%s%s%s\n\n%s\n%s\n\n",
      "Usage: ", argv[0], " infile outfile",
      "The uppercase letters in infile will be counted.",
      "The results will be written in outfile.");
    exit(1);
  }
  ifp = fopen(argv[1], "r");
  ofp = fopen(argv[2], "w");
  for (i = 0; i < 26; ++i) /* initialize array to zero */
    letter[i] = 0;
  while ((c = getc(ifp)) != EOF) 
    if (c >= 'A' && c <= 'Z') /* find uppercase letters */
      ++letter[c - 'A'];
  for (i = 0; i < 26; ++i) { /* print results */
    if (i % 6 == 0)
      putc('\n', ofp);
    fprintf(ofp, "%c:%5d ", 'A' + i, letter[i]);
  }
  putc('\n', ofp);
  return 0;
}

 

PROGRAM 113 : double_out.c


#include <stdio.h>

int main(void)
{
  char c;

  while (scanf("%c", &c) == 1) {
    printf("%c", c);
    printf("%c", c);
  }
  return 0;
}

 


[Last Update: 2001.2.20] Dongseo University Cyber Campus