Software
Software is the part of the computer you can't touch; it's the ideas
of programmers codified into machine language, represented by electrical
impulses, that tell the computer what to do (via the CPU). (There
are other parts of the computer you shouldn't touch, like the
power supply and the inside of the monitor, but software is something
you can't touch because, in a sense, software doesn't really
exist in the real world.) Programming, the topic of this course, is the
process of writing programs (software).
There are two kinds of software: application and system
software. Application software does what people use the computer for, e.g.,
word processors, spreadsheet programs, video games, etc. System software
exists for the sake of the computer, from the operating system that organizes
and allocates resources to a program to list the files in your account.
The operating system is a big piece of system software that is
loaded into main memory when the computer first comes up, and stays there
until you turn off the computer. It manages the activities of the computer,
allocating and controlling access to resources (such as memory, CPU time,
storage, the printer, etc.) On the main computers we use, the operating system is
a version of Linux, which is an operating system designed to be very similar to the Unix operating system. Other operating systems are
BSD Unix, Solaris, MacOS, VMS, and Windows NT.
Computer Languages
In the beginning, programmers simply programmed in machine language.
This can be quite tedious, so computer scientists invented computer
languages that could be used instead of machine language. A program would
be written in this high-level language, then automatically translated
to machine language by another program.
The first computer languages were simply human-readable forms of machine
language called assembly languages. For instance, if the machine
language instruction for "add two numbers" was 100101010, the assembly
language version might be "ADD". A program called an assembler
(hence the name "assembly language") would translate the program into
machine language. Assembly language is still used today for specialized
applications.
Higher level languages were developed that used mathematical and human
language notation in place of machine instructions. Two of the first
were FORTRAN, for scientific programming, and LISP,
for list processing (used mostly for artificial intelligence).
A FORTRAN
program looks like this:
program number
integer i
do 10 i=1,10
print*, i
10 continue
stop
end
This simple program prints the numbers from 1 through 10.
The same program in LISP:
(defun number (n)
(cond ((equal n 10) n)
(t (print n) (number (+ n 1)))))
(number 10)
The same program in C, the language we're using for this class:
#include <stdio.h>
#include <stdlib.h>
int main () {
int i;
for (i=1; i<=10; i++) printf ("%d\n", i);
exit (0);
}
A text file containing a program written in assembly or a high level
language is called a source file. It can be translated by a
compiler into a machine language file, called an object file,
that must then be linked with some other object files, and can
then be executed. On the main computers, the compiler and linker are accessed
through the cc command; cc stands for "C Compiler."
Those "other object files" are libraries of standard routines
used to access input and output devices and do certain common computations.
The C Language
C is the computer language we will be using in this course. It is about
20 years old. The original purpose of C was to be the systems programming
language of the Unix operating system, which is written almost entirely
in C with a little assembly language. C has evolved over the years into
a robust language used for a wide variety of applications. It is
succeeded by two new object oriented languages, C++ and
Objective C. It is also the language upon which Java is based.
We will be using ANSI C, the "official" version of C codified by
the American National Standards Institute.
A source program written in C is a text file containing words and
punctuation conforming to a proper syntax or grammar,
as well as certain semantic rules governing the meaning of
the program. The syntactic rules are somewhat loose; for instance,
a C program can be all on one line, or can have each word on a separate
line; C doesn't care much where you begin a new line (however, there is
a conventional style most people use to make their programs readable).
Here is a simple C program. It prints Hello, World! on the
screen:
#include <stdio.h>
int main () {
printf ("Hello, World!\n");
exit (0);
}
Elements of a C program:
Let's look at some more simple C programs.
This program prints the square of 123:
#include <stdio.h>
int main () {
float num;
num = 123;
printf ("%f squared is equal to %lf\n", num, num * num);
return 0;
}
output:
123.000000 squared is equal to 15129.000000
When doing aerobic exercise, your heart rate should be between 65% and
80% of your maximum heart rate, which is determined by subtracting
your age from 220. The following program does these calculations,
asking you for your age and giving you your aerobic heart rate range.
#include <stdio.h>
int main () {
int age;
float mhr, r1, r2;
printf ("How old are you? ");
scanf ("%d", &age);
mhr = 220.0 - age;
r1 = 0.65 * mhr;
r2 = 0.80 * mhr;
printf ("Your heart rate should be"
" between %0.2f and %0.2f during aerobic exercise\n", r1, r2);
return 0;
}
input: 20
output:
Your heart rate should be between 130.00 and 160.00 during aerobic exercise
This program prompts for and reads an angle in degrees, converts the angle
to radians, then prints the result on the screen.
#include <stdio.h>
int main () {
float degrees, radians;
printf ("Enter the angle in degrees: ");
scanf ("%f", °rees);
radians = (degrees / 360.0) * 2 * 3.14159;
printf ("%f degrees = %f radians.\n", degrees, radians);
return 0;
}
This program prompts for a letter, then prints the ASCII code for that
letter, in both uppercase and lowercase.
#include <stdio.h>
#include <ctype.h>
int main () {
char in_char, up, lo;
int code;
printf ("Enter a letter: ");
scanf ("%c", &in_char);
up = toupper (in_char);
lo = tolower (in_char);
printf ("lowercase is %c, uppercase is %c\n", lo, up);
code = (int) in_char;
printf ("ASCII code is %d\n", code);
return 0;
}
input: f
output:
Enter a letter: f
lowercase is f, uppercase is F
ASCII code is 102
This program converts the input decimal number to hexadecimal, printing
the result.
#include <stdio.h>
int main () {
int a;
printf ("Enter a number: ");
scanf ("%d", &a);
printf ("%d in hexadecimal is %x\n", a, a);
}
output:
Enter a number: 31
31 in hexadecimal is 1f
Make sure for next time that you have read through Chapter 2 of the book, and
begin reading Chapter 3.