Computer Programming (3)


administrivia

Web Page - http://www.cs.tau.ac.il/~efif/courses/ComputerProgramming


let’s look at that temperature example again

/*
 * Print a Fahrenheit-to-Celsius
 * conversion table
 */

#include <stdio.h>

int main (void)
{
  int fahr;
  int celsius;
  int lower = 0;
  int upper = 300;
  int step = 20;

  fahr = lower;
  while (fahr <= upper) {
    celsius = 5 * (fahr – 32) / 9;
    printf (“%d\t%d\n”, fahr, celsius);
    fahr = fahr + step;
  }

  return 0;
}

can we make it make sense?


how would you do it?


what might you do?


a few constraints the computer has


the program approaches it differently


the program once more

 
/*
 * Print a Fahrenheit-to-Celsius
 * conversion table
 */

#include <stdio.h>

int main (void)
{
  int fahr;
  int celsius;
  int lower = 0;
  int upper = 300;
  int step = 20;

  fahr = lower;
  while (fahr <= upper) {
    celsius = 5 * (fahr – 32) / 9;
    printf (“%d\t%d\n”, fahr, celsius);
    fahr = fahr + step;
  }

  return 0;
}

more data types


signed/unsigned


clarity


increased range


negative numbers


it doesn’t exactly work that way


signed/unsigned


type conversions


the program slightly modified


what does this mean?


constants: what is a constant?


numerical constants


using numerical constants


examples


character constants


escape sequences


escape sequences


character constants are integers


declarations


variables


variables


back to declarations


why declare variables?


declaration rules


simple declaration examples

char c;
int i;
double d;
int lower, upper, step;

initialization?


initialization and data types


placement of declarations


variables in statement blocks


another statement block

/*
 * Print a Fahrenheit-to-Celsius
 * conversion table
 */

#include <stdio.h>

int main (void)
{
  int fahr;
  int celsius;
  int lower = 0;
  int upper = 300;
  int step = 20;

  fahr = lower;
  while (fahr <= upper) {
    celsius = 5 * (fahr – 32) / 9;
    printf (“%d\t%d\n”, fahr, celsius);
    fahr = fahr + step;
  }

  return 0;
}

back to variables and statement blocks


global variables


function parameters


shell commands

CommandOperation
lsList files
cp <fromFile> <toFile> Copy fromFile to toFile
mv <fromFile> <toFile> Move fromFile to toFile
rm <theFile>Remove theFile
mkdir <theDir>Create theDir
cd <theDir>Change directory to theDir
cat <theFile>Display theFile
less <theFile>Display theFile one page at a time
more <theFile>Display theFile one page at a time
man <theCommand>Manual page for theCommand

lectures prev top next
Maintained by Efi Fogel. Last modified: October 29 2002.