[go: up one dir, main page]

0% found this document useful (0 votes)
223 views39 pages

CS102 Computer Programming I: Problem-Solving, Programs and Programming Languages

This document provides an overview of a computer programming lecture that discusses problem-solving, algorithms, programs, and programming languages. It defines key terms like problems, algorithms, and programs. It explains the different stages of problem-solving and provides an example of writing a program in C to determine if a number is even or odd. The document also briefly discusses what computers and programs are, how variables and functions work, and how C code is compiled into machine-readable code.

Uploaded by

Helen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
223 views39 pages

CS102 Computer Programming I: Problem-Solving, Programs and Programming Languages

This document provides an overview of a computer programming lecture that discusses problem-solving, algorithms, programs, and programming languages. It defines key terms like problems, algorithms, and programs. It explains the different stages of problem-solving and provides an example of writing a program in C to determine if a number is even or odd. The document also briefly discusses what computers and programs are, how variables and functions work, and how C code is compiled into machine-readable code.

Uploaded by

Helen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

CS102

Computer Programming I

Lecture 1:
Problem-Solving, Programs and
Programming Languages

Bicol University College of Science


CSIT Department
1st Semester, 2020-2021
Key Definitions/Concepts
Problem
•Definition of task to be performed (often by a computer)
Algorithm
•A particular sequence of steps that will solve a problem
•Steps must be precise and mechanical
•The notion of an algorithm is a (the?) fundamental
intellectual concept associated with computing
Program
•An algorithm expressed in a specific computer programming
language (C, C++, Java, Perl, …)
Programming vs. Cooking
Programming Cooking
Problem Make fudge brownies

Algorithm Recipe

Program Recipe written in a specific


language (English, Russian,
Chinese, Latvian, etc.)
Stages in Problem Solving
1.Clearly specify the problem
2.Analyze the problem
3.Design an algorithm to solve the problem
4.Implement the algorithm (write the
program)
5.Test and verify the completed program
A Sample Problem
Is a given number even or odd?
Analysis
What numbers are allowed?
Where does the number come
from?
What do “even” and “odd” mean?
How is the answer to be reported?
More Precise Problem
Restatement
Given an integer number typed in from the
keyboard,
If it is even, write “even” on the screen
If it is odd, write “odd” on the screen
An Algorithm
Read in the number
Divide the number by 2
If the remainder is 0, write “even”
Otherwise, write “odd”
An Algorithm
Read in the number
Divide the number by 2
If the remainder is 0, write “even”
Otherwise, write “odd”

Test: 234784832792543

An alternate algorithm:
If the rightmost digit is 0, 2, 4, 6, or 8, write “even”
Otherwise, write “odd”
Next, a C Program
Now that have an algorithm, we would
like to write a C program to carry it
out.

But first, what is a program? In fact,


what is a computer?
What’s a Computer?
Central Monitor
Processing
Disk Unit (CPU)

Main
mouse
Memory Keyboard

Network

CPU or processor: executes simple


instructions manipulating values in memory
What is a Program?
The CPU executes instructions one after the other.

Such a sequence of instructions is called a


“program”

Without a program, the computer is just useless


hardware

Complex programs may contain millions of


instructions
Memory
Memory is a collection of locations called variables

Each variable has


A name (an identifier)
A type (the kind of information it can contain)
Basic types include
int (integers – whole numbers: 17, -42)
double (floating-point numbers with optional fraction
and/or exponent: 3.14159, 6.02e23)
char (character data: ‘a’, ‘?’, ‘N’, ‘ ’, ‘9’)
Program Sketch
Ask the user to enter a number

Read the number and call it num

Divide num by 2 and call the remainder rem

If rem is 0 write “even” otherwise write “odd”

The actual program has LOTS of details – IGNORE THEM FOR NOW
Pay attention to the main ideas
The Program in C (part I)
/* read a number and report whether it is even or odd */
#include <stdio.h>

int main (void) {


int num; /* input number */
int rem; /* remainder after division by 2 */

/* get number from user */


printf(“Please enter a number: ”);
scanf(“%d”, &num);
The Program in C (part II)
/* calculate remainder and report even or odd */
rem = num % 2;
if (rem == 0) {
printf(“even\n”);
} else {
printf(“odd\n”);
}
/* terminate program */
return 0;
}

Remember: Don’t sweat the details!!! (for now)


Sample Execution
A Quick Look at the Program
/* read a number … */
Text surrounded by /* #include <stdio.h>
int main (void) {
and */ are comments int num; /* input number */
int rem; /* remainder …*/
Used to help the reader /* get number from user */
printf(“Please enter a number: ”);
understand the program scanf(“%d”, &num);
/* calculate remainder … */
Ignored during program rem = num % 2;

execution if (rem == 0) {
printf(“even\n”);
} else {
printf(“odd\n”);
}

Programs change over /* terminate program */


return 0;
time. It’s important }

that programmers be
able to understand old
code - good comments
are essential.
Variables
Variable declarations /* read a number … */
#include <stdio.h>

create new int main (void) {


int num; /* input number */

variables and int rem; /* remainder …*/


/* get number from user */

specify their names


printf(“Please enter a number: ”);
scanf(“%d”, &num);
/* calculate remainder … */
and types. rem = num % 2;
if (rem == 0) {
printf(“even\n”);
} else {
printf(“odd\n”);
}
/* terminate program */
return 0;
}
Statements
/* read a number … */
Following the declarations are #include <stdio.h>

statements that specify the int main (void) {


int num; /* input number */
operations the program is int rem; /* remainder …*/
/* get number from user */
to carry out printf(“Please enter a number: ”);
scanf(“%d”, &num);
Lots of different kinds /* calculate remainder … */
rem = num % 2;
Some (if, else, return) are part of if (rem == 0) {
printf(“even\n”);
the C language proper } else {
printf(“odd\n”);
Others (scanf, printf) are }

contained in libraries of /* terminate program */


return 0;
routines that are available for }

use in our programs


For now, don’t worry too much
about the distinction
Functions
/* read a number … */
Functions are sequences #include <stdio.h>
int main (void) {
of statements defined int num;
int rem;
/* input number */
/* remainder …*/
elsewhere. Some /* get number from user */
printf(“Please enter a number: ”);

functions (such as scanf(“%d”, &num);


/* calculate remainder … */

printf and scanf here) rem = num % 2;


if (rem == 0) {

are provided with the printf(“even\n”);


} else {

system. We will also }


printf(“odd\n”);

learn how to write and /* terminate program */


return 0;

use our own functions. }


Boilerplate
/* read a number … */

Some parts of the #include <stdio.h>


int main (void) {

program are standard int num;


int rem;
/* input number */
/* remainder …*/
utterances that need /* get number from user */
printf(“Please enter a number: ”);
to be included at the scanf(“%d”, &num);
/* calculate remainder … */
beginning and end. rem = num % 2;
if (rem == 0) {
We’ll explain all of this printf(“even\n”);
} else {
eventually printf(“odd\n”);
}
Just copy it for now in each /* terminate program */

of your programs }
return 0;
From C to Machine
Language
The computer’s processor only understands
programs written in its own machine language
Sequences of 1’s and 0’s
Different for each processor family (x86, PowerPC, SPARC, ARM, …)

How is it that it can obey instructions written in


C?
Compilers and Linkers
There are two steps in creating an executable program
starting from C source code
A program called the C compiler translates the C code into an
equivalent program in the processor’s machine language (1’s
and 0’s)

A program called the linker combines this translated program with


any library files it references (printf, scanf, etc.) to produce an
executable machine language program (.exe file)

Environments like Visual Studio do both steps when you


“build” the program
Compilers, Linkers, etc.
.c file c 0110
1000 l
o
m 1101 i
p n executable
i k program
l library e
header (ANSI)
(stdio.h) e r
r

object
source code
code
What Could Possibly Go Wrong?
Lots!
Things are rarely perfect on the first attempt

Both the compiler and linker could detect errors

Even if no errors are are detected, logic errors (“bugs”) could be


lurking in the code

Getting the bugs out is a challenge even for professional software


developers
Terms: Syntax vs Semantics
Syntax: the required form of the program
punctuation, keywords (int, if, return, …), word
order, etc.
The C compiler always catches these “syntax
errors” or “compiler errors”
Semantics (logic): what the program means
what you want it to do
The C compiler cannot catch these kinds of errors!
They can be extremely difficult to find
They may not show up right away
Try It Yourself!
Type in the even/odd program and see what
happens when you:
Leave off a few semicolons or misspell something (syntax)

In the last printf statements, change “odd” to “even”. Run


the program. What happens if you enter 17? (semantics)

Experiment and see what happens


Programming

Programming

Planning or scheduling the performance of a task or an
event.

Computer

A programmable device that can store, retrieve, and
process data.

Computer program

A sequence of instructions to be performed by a
computer.

Computer programming

The process of planning a sequence of steps for a
computer to follow.
How Do We Write a Program?

Writing a program is a two-phase process:
● Problem-solving phase

1.Analysis and specification. Understand (define) the problem and


what the solution must do.
2.General solution (algorithm). Develop a logical sequence of steps
that solves the problem
3.Verify. Follow the steps exactly to see if the solution really does
solve the problem.
● Implementation phase

1.Program. Translate the algorithm into a programming language.


• Computer (or Programming) language is a set of rules,
symbols, and special words used to construct a computer
program.
2.Test. Have the computer follow the instructions. Then manually
check the results. If you find errors, analyze the program and
the algorithm to determine the source of the errors, and then
make corrections.
How Do We Write a Program?

Once a program has been written, it enters a third process:

Maintenance
1.Use. Use the program.
2.Maintain. Modify the program to meet changing requirements or to
correct any errors that show up in using it.

Together, the problem-solving, implementation, and maintenance


phases constitute the program’s life cycle.

Documentation is the written text and comments that make
a program easier for others to understand, use and modify.

Data and Information

Data are raw, unorganized facts.

Can be in the form of text, graphics, audio, or video.

Information is data that has been processed into a useful form.
Computer Software
Computer software is divided into two broad
categories: system software and application
software.
System software consists of programs that
manage the hardware resources of a computer
and perform required information processing
tasks.

Operating system

System support software

System development software

Application software is divided into two classes:



general purpose software

application specific software
Computer Languages

Machine languages

The only language understood by a computer is machine language.


Symbolic languages (assembly languages)

High level languages – FORTRAN, COBOL, C, C++,
Java, Pascal

Natural languages such as English or French (still quite
limited)
Computer Languages

Machine languages
• Strings of numbers giving machine specific instructions.
• Example: 11000100 11111000 00001000


Assembly languages
• English-like abbreviations representing elementary computer
operations (translated via assemblers).
• Example: LOAD BASEPAY
» ADD OVERPAY
» STORE GROSSPAY

High level languages – FORTRAN, COBOL, C, C++, Java, Pascal
• Codes similar to everyday English
• Use mathematical notations (translated via compilers)
• Example: grossPay = basePay + overTimePay
Programming Language
The instructions in a programming language
reflect the operations a computer can perform:

A computer can transfer data from one place to another.

A computer can input data from an input device (a
keyboard or a mouse, for example) and output data to
an output device (a screen, for example).

A computer can store data into and retrieve data from its
memory and secondary storage.

A computer can compare two data values for equality or
inequality.

A computer can perform arithmetic operations (addition
and subtraction, for example) very quickly.
Programming Language
Programming languages require that we use certain control
structures to express algorithms as programs.
Edsger Dijkstra proposed in 1968 that any program can be written
with only three constructs or types of instructions: (1) sequences,
(2) the if...else selection statement, and (3) the while loop.
There are four basic ways of structuring statements (instructions) in
most programming languages: sequentially, conditionally,
repetitively, and with subprograms.
• A sequence is a series of statements that are executed one after another.
• Selection (also called branch or decision), the conditional control
structure, executes different repeats statements while certain conditions
are met.
• The repetitive control structure, the loop (also called repetition or
iteration), repeats statements until certain conditions are met.
• The subprogram (also called procedure, function, method, or
subroutine) allows us to structure a program by braking it into a smaller
units.
Writing, Editing, Compiling, and
Linking Programs

Writing and editing programs
text editor, source file

Compiling programs
Compiler translates the source code into
machine language. The C/C++ compiler is
two separate programs:
• Preprocessor
• preprocessor directives
• the result of preprocessing is
called translation unit.
• Translator reads the translation unit
and writes the resulting object
module.
• Linking programs
• The linker assembles all functions
into a final executable program.
Program Execution
End of Lecture 1

You might also like