Σημειώσεις - Εισαγωγή στον Προγραμματισμό

Κεφάλαιο 1: Πρώτα προγράμματα σε C

Η γλώσσα προγραμματισμού C

Καλημέρα κόσμε της C

/* File: helloworld.c */
#include <stdio.h>

int main()
{ printf("Hello world\n");
}
% gcc -o helloworld helloworld.c
% ./helloworld
Hello world
%

Πόσο είναι το \(\pi\);

/* File: picomp.c */
#include <stdio.h>            /* Header file for standard I/O library */
#include <math.h>                     /* Header file for math library */

int main()
{ long i;
  double sum, current, pi;
  i = 1;                               /* Denominator of current term */
  sum = 0.0;                                            /* So far sum */
  do {
    current = 1/(((double) i)*((double) i));          /* Current term */
    sum = sum+current;                     /* Add current term to sum */
    i++;                                             /* Next term now */
  } while (current > 1.0e-15);  /* Stop if current term is very small */
  pi = sqrt(6*sum);                    /* Compute approximation of pi */
  printf("Summed %8ld terms, pi is %10.8f\n", i-1, pi);
}
% gcc -o picomp picomp.c -lm
% ./picomp
Summed 31622777 terms, pi is 3.14159262
%

Γράψτε προγράμματα C που να υπολογίζουν με αρκετή ακρίβεια τα παρακάτω αθροίσματα. Μπορείτε να βρείτε με ποιους ενδιαφέροντες αριθμούς ισούνται;

\[S_1=\frac{1}{1^2}-\frac{1}{2^2}+\frac{1}{3^2}- \frac{1}{4^2}+\frac{1}{5^2}-\frac{1}{6^2}+\frac{1}{7^2}- \frac{1}{8^2}+\ldots\] \[S_2=\frac{1}{1}-\frac{1}{2}+\frac{1}{3}-\frac{1}{4}+\frac{1}{5}- \frac{1}{6}+\frac{1}{7}-\frac{1}{8}+\frac{1}{9}-\ldots\] \[S_3=\frac{1}{1}-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\frac{1}{9}- \frac{1}{11}+\frac{1}{13}-\frac{1}{15}+\frac{1}{17}-\ldots\] \[S_4=\frac{1}{1 \times 3}+\frac{1}{3 \times 5}+\frac{1}{5 \times 7}+ \frac{1}{7 \times 9}+\frac{1}{9 \times 11}+\ldots\] \[S_5=\frac{1}{2 \times 3 \times 4}-\frac{1}{4 \times 5 \times 6}+ \frac{1}{6 \times 7 \times 8}-\frac{1}{8 \times 9 \times 10}+ \ldots\] \[S_6=\frac{1}{1^4}+\frac{1}{2^4}+\frac{1}{3^4}+\frac{1}{4^4}+ \frac{1}{5^4}+\frac{1}{6^4}+\frac{1}{7^4}+\frac{1}{8^4}+\ldots\]

Το ίδιο και για το γινόμενο:

\[P=\frac{2}{1} \times \frac{2}{3} \times \frac{4}{3} \times \frac{4}{5} \times \frac{6}{5} \times \frac{6}{7} \times \frac{8}{7} \times \frac{8}{9} \ldots\]

Πότε είναι το Ορθόδοξο Πάσχα;

/* File: easter.c */
#include <stdio.h>
#define STARTYEAR 2010
#define ENDYEAR   2025

int main()
{ int year, a, b, c, d, e;
  for (year = STARTYEAR ; year <= ENDYEAR ; year++) {
    a = year % 19;
    b = year % 4;    /* Gauss method for Easter date computation */
    c = year % 7;
    d = (19*a+15) % 30;
    e = (2*b+4*c+6*d+6) % 7;
    printf("Easter in the year %d: ", year);
    if (d+e+4 > 30)                             /* Easter in May */
      printf("  May %d\n", d+e-26);
    else                                      /* Easter in April */
      printf("April %d\n", d+e+4);
  }
}
% gcc -o easter easter.c
% ./easter
Easter in the year 2010: April 4
Easter in the year 2011: April 24
Easter in the year 2012: April 15
Easter in the year 2013:   May 5
Easter in the year 2014: April 20
Easter in the year 2015: April 12
Easter in the year 2016:   May 1
Easter in the year 2017: April 16
Easter in the year 2018: April 8
Easter in the year 2019: April 28
Easter in the year 2020: April 19
Easter in the year 2021:   May 2
Easter in the year 2022: April 24
Easter in the year 2023: April 16
Easter in the year 2024:   May 5
Easter in the year 2025: April 20
%

Να κατασκευασθεί μαγικό τετράγωνο \(5 \times 5\)

/* File: magic.c */
#include <stdio.h>
#define SIZE 5

int main()
{ int i, j, k;
  int x[SIZE][SIZE];                      /* The board to be filled */
  for (i=0 ; i < SIZE ; i++)
    for (j=0 ; j < SIZE ; j++)
      x[i][j] = 0;                     /* Mark all squares as empty */
  i = (SIZE-1)/2;                                   /* Go to middle */
  j = SIZE-1;                                       /* right square */
  for (k=1 ; k <= SIZE*SIZE ; k++) {
    if (x[i][j] != 0) {                     /* If current not empty */
      i = i-1;                                /* Move one square up */
      j = j-2;                              /* and two squares left */
      if (i < 0) i = i+SIZE;                  /* If you are outside */
      if (j < 0) j = j+SIZE;  /* go to the respective square inside */
    }
    x[i][j] = k;               /* Fill current square with number k */
    i++;                                    /* Move one square down */
    if (i == SIZE) i = 0;                  /* If outside get inside */
    j++;                                   /* Move one square right */
    if (j == SIZE) j = 0;                  /* If outside get inside */
  }
  for (i=0 ; i < SIZE ; i++) {
    for (j=0 ; j < SIZE ; j++)
      printf("%4d ", x[i][j]);                   /* Print out board */
    printf("\n");
  }
}
% gcc -o magic magic.c
% ./magic
  11   10    4   23   17
  18   12    6    5   24
  25   19   13    7    1
   2   21   20   14    8
   9    3   22   16   15
%

Μετατροπή πεζών γραμμάτων σε κεφαλαία

/* File: capitalize.c */
#include <stdio.h>

int main()
{ int ch; /* Be careful! Declare ch as int because of getchar() and EOF */
  ch = getchar();                               /* Read first character */
  while (ch != EOF) {           /* Go on if we didn't reach end of file */
    if (ch >= 'a' && ch <= 'z')                 /* If lower case letter */
      ch = ch - ('a'-'A'); /* Move 'a'-'A' positions in the ASCII table */
    putchar(ch);                                 /* Print out character */
    ch = getchar();                              /* Read next character */
  }
}
% gcc -o capitalize capitalize.c
% cat capinp.txt
This is a text file with 2 lines to
test the C program "capitalize.c".
% ./capitalize < capinp.txt
THIS IS A TEXT FILE WITH 2 LINES TO
TEST THE C PROGRAM "CAPITALIZE.C".
%

Κατεβάστε το κεφάλαιο: PDF · Markdown