0% found this document useful (0 votes)
317 views

Cit 736 Answers

The document provides information on various topics related to computer programming including: 1) Steps for dealing with errors in program compilation and execution such as writing programs carefully and checking for misspelled statements. 2) Four compilers used for FORTRAN programs and four standard identifiers in Pascal such as Integer and Input. 3) Ten steps for coding, compiling, and executing a Fortran program using MS-DEV such as starting the editor and compiler.

Uploaded by

Kini Amadi
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)
317 views

Cit 736 Answers

The document provides information on various topics related to computer programming including: 1) Steps for dealing with errors in program compilation and execution such as writing programs carefully and checking for misspelled statements. 2) Four compilers used for FORTRAN programs and four standard identifiers in Pascal such as Integer and Input. 3) Ten steps for coding, compiling, and executing a Fortran program using MS-DEV such as starting the editor and compiler.

Uploaded by

Kini Amadi
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/ 17

CIT 736 ANSWERS

The following steps are very necessary in dealing with errors in program
compilation and execution. They are:
a) Program writing and correction requires patience. The programmer
should carefully through the program for possible correction of any
error.
b) Use appropriate manuals and reference text books for the programming
language employed for possible assistance.
c) Double check for misspelled statements or omission of punctuation
marks
d) Check for proper use of data types
e) Make sure variables meant for counting purposes are properly initialized

Four compilers that are used to compile and execute a FORTRAN 90/95
programs
a) Lathey’s Fortran 90 compiler
b) Microsoft Fortran PowerStation 4.0 professional
c) NAGware Fortran 90 compiler
d) Microsoft Developer Studio (MS-DEV) Tool

Four (4) standards identifier in Pascal


a) Integer
b) Input
c) Output
d) False
e) True

What criteria would you use to assess the quality of the program?
Produce a large number, n say, of random numbers using the
RANDOM_NUMBER intrinsic subroutine as described above.
Now, count the number of random numbers falling in the range 0.0000000-
0.0999999, those between 0.1000000-0.1999999, and so on.
Print the values to a file. The distribution should be uniform

CIT 736 COMPUTER PROGRAMMING


Ten (10) steps to be followed when coding, compiling and executing a Fortran
90/95 program using MS-DEV
a) Start MS-DEV if available on your computer
b) Start the editor and type in your program
c) Click on File menu
d) Select New File
e) Save as text file with the extension (.f90)
f) Compile and execute your program
g) Click on BUILD menu
h) Click on Compile
i) Click on BUILD
j) Click on Execute when you have compiled successfully

Write a program in FORTRAN to find the total and product of three numbers
“T, F, V”/ (This is also an example of a program structure using FORTRAN, use
A, B, C instead of T, F, V)
Program SUMPRODUCT - Line 1
IMPLICIT NONE - Line 2
INTEGER:: T, F, V, SUM, PRODUCT - Line 3
READ (*, *) T, F, V - Line 4
SUM = T + F + V - Line 5
PRODUCT = T * F * V - Line 6
WRITE (*, *) SUM, PRODUCT - Line 7
STOP - Line 8
END PROGRAM SUMPRODUCT - Line 9

Eight standard input and output routines and extensions supported by


PASCAL
Append - Opens a file for writing at its end.
Close - Closes a file.
Open - Associates an external file with a file variable.
Remove - Removes the specified file.
Filesize - Returns the current size of a file.
Flush - Writes the output buffer for the specified Pascal file into the associated
operating system file.
getfile -Returns a pointer to the C standard I/O descriptor associated with the
specified Pascal file.
Message - Writes the specified information to stderr.

CIT 736 COMPUTER PROGRAMMING


Given a three-dimensional array A(X,Y,Z). Write a two line FORTRAN 90/95
code that
1) Determine the maximum value less than 5000 in this array.
REAL, DIMENSION(X,Y,Z) :: a
amax=MAXVAL(a,MASK=(a<5000))

2) Computes the average value greater than 1000 in an array.


av=SUM(a,MASK=(a>1000))/COUNT(MASK=(a>1000))

Analyze the effect of the following FORTRAN statements:


1) OPEN (UNIT = 6, FILE = „great.dat‟, STATUS =‟OLD‟, ACTION =‟READ‟)
The unit 6 specifies the I/O unit number associated with the file ‘green.dat’.
The Status=’old’ specifies that the file already exist. The action means read
only.

2) OPEN (UNIT = 8, STATUS =‟SCRATCH‟, IOSTAT =‟ierror‟)


A temporary file is created that is automatically deleted when the file is closed
or at program termination. The IOSTAT returns error code in variable ierror if
there is error.

3) 20 FORMAT (8F6.3)
20 is the format label.
8 - Values to be read
F - for floating point or real number
6 - Size of the value including the decimal point
3 - Number of decimal places

CIT 736 COMPUTER PROGRAMMING


A computer program is a series of instructions given to the computer at a
particular time to solve a problem.

The Programming Language is the language through which we can pass


instructions to user and the system.

A programmer is a person who writes a computer program using a


programming language.

The binary numeral system is a way to write numbers using only two digits: 0
and 1. These are used in computers as a series of "off" and "on" switches.

Integer Numbers: an integer number has no decimal point. Examples are 67, -
234, 900 etc.

Real Numbers: a real number must have a decimal point and the decimal point
must appear between two digits. Examples are 0.01, 11.94, -5.6 etc.

Briefly describe the role of debugging in a computer programming project


Debugging is an essential part of any development project because it helps you
find errors in code and in logic. Some Compilers have some sophisticated
debugger built right into the development environment.

Three (3) Major groups of errors


a) Design-time errors (also called syntax error)
b) Runtime errors
c) Logic errors

CIT 736 COMPUTER PROGRAMMING


Correcting Design-time Runtime errors
errors
Detection Easy to detect by compiler Hard to detect by compiler
Fixation A program with design-time Errors can be fixed after the
error cannot be compiled execution of the code
and run; you must locate
and correct the error
before you can continue
Reference Refers to the errors that Refers to the error that we
correspond to the syntax encounter during the code
execution during runtime

Five (5) types of errors


a) Conversion Error: numbers are mostly stored in their binary form. A decimal
number 0.5 is represented by the binary number 0.1 in such computers. Again,
a number 1/10 is represented by 0.0001100110011……. which has no finite
binary representation. Depending on the computer word length, errors will be
noticed if the arithmetic operations become bigger.

b) Round-off Error: this occurs when a specific significant digits are used, for
example, 1/3 which supposed to be 0.3333333….. could be stored as 0.333.

c) Syntax Error: errors due to misspelled (spelling errors) statements or


instructions or wrong use or non use of punctuation marks (such as commas,
colon etc.) where necessary.

d) Run-time Error: this error occurs when the translator come across wrong
mathematical operation during program execution. This could occur, as an
example, when a number is divided by zero.

e) Logical Error: program logic is always the cause of the error when the
sequences of programming steps are not executed properly. Logical error may
not have impact with the smooth translation process of the program but may
give wrong result. There could be successful interpretation or compilation of a
program that actually has a logical error. The logical errors are mostly noticed
upon execution of the program.

CIT 736 COMPUTER PROGRAMMING


State and explain the general form of a computer program main unit (main
program).
Programs are written in one or more units. The main unit is called the Main
Program while the other optional units are subroutines which could be either
subprograms or functions.

(Cont’d) The main unit is usually of the form:


a. Program Name – each program has a name
b. TYPE declaration statements – REAL and INTERGER statements, etc
c. Statements Performing Arithmetic and other Operations – where the
processes to execute the instructions are stated
d. STOP – in a FORTRAN language, the STOP statement precedes the END
statement
e. END PROGRAM – to inform computer of the physical end of the program

Assembler Compiler
We use Assembler to translate Compiler converts high level language
Assembly language to machine into machine language for the
language for the understanding of the understanding for the computer
computer

Interpreter Compiler
Reads the source program line by line Reads the source program as a whole

Machine language Symbolic language


a low level interface and the a middle level language between the
instructions are usually represented machine language and high level
by binary numbers languages

High level language is the use of “almost English” language in writing


computer programs which are also known as problem oriented languages.
Examples are FORTRAN, COBOL, PASCAL, C, etc.

CIT 736 COMPUTER PROGRAMMING


BASIC - Beginners All Purpose Symbolic Instruction Code
COBOL - Common Business Oriented Language
FORTRAN - Formula Translation
PASCAL - Named after French mathematician Blaise Pascal

Five (5) properties of a good computer program


a) Program Correctness: a good program must be able to solve the intended
problem with relevant results
b) Documentation: every module or procedure must be preceded with
comments on brief explanation of the module in the program.
c) Robustness and Scalability: programs that can survive various unexpected
events are said to be robust and those that can easily be upgraded are
scalable.
d) User Interface: a good look or design of the medium of interaction of the
user and the program must be well taken care so as to have a good user
interface.
e) Program Style: the programming language rules are to be adapted in writing
programs. You should not do what is not to be done with the programming
language in question.

Data types Binary system


Data types support various types of Binary system supports only two
data, including integer, real, character digits: 0 and 1
or string, and Boolean

A FORMAT statement provides the computer with the types of the


information and their respective locations within an input record or where to
be printed as the case may be.

A translator is a machine language program that translates source codes


written in either symbolic or high level language to machine language program
known as the object language

CIT 736 COMPUTER PROGRAMMING


READ/INPUT Statement to compute a student gpa
PROGRAM StudentGPA
IMPLICIT NONE
REAL :: gpa
INTEGER :: Matric
CHARACTER (Len = 25) :: Name
READ (*,*) Matric, Name, gpa
STOP
END PROGRAM

WRITE/OUTPUT Statement to compute a student gpa


PROGRAM StudentGPA
IMPLICIT NONE
REAL :: gpa
INTEGER :: Matric
CHARACTER (Len = 25) :: Name
READ (*,*) Matric, Name, gpa
WRITE (*,*) Matric, Name, gpa
STOP
END PROGRAM

General form of an OPEN Statement


OPEN (open list separated by commas)

To open a file for Input:


OPEN (UNIT = 8, FILE = ‘datain.dat’, STATUS = ‘OLD’, ACTION = ‘READ’)

To open file for Output:


OPEN(UNIT = 10, FILE = ‘datout.dat’,STATUS = ‘NEW’, ACTION=’WRITE’)
In case of avoiding overwriting an existing file, STATUS = ‘REPLACE’ is used.

To open a scratch file:


OPEN (UNIT = 12, STATUS = ‘SCRATCH’, IOSTAT = ierror)

CIT 736 COMPUTER PROGRAMMING


PASCAL
Pascal is a high-level language developed in the early 1970s by Niklaus Wirth, a
computer scientist at the Institute of Informatics, Technical University, Zurich,
Switzerland. The language was named in honour of Blaise Pascal (1623 -1662),
the brilliant French scientist and mathematician. Pascal is now widely used as
programming language for a variety of different applications.

FORTRAN
FORTRAN is an acronym for Formula Translator. This is a high level
programming designed primarily for use by scientist, engineers and designers.
The statements used in the language resemble the formulae normally used by
such people. Some of its dialects are now being used for business computing
and character manipulation. This course material covers the transition from
the programming language Fortran 77 to the more modern Fortran 90/95, and
is aimed at guiding learners who require an understanding of the principles
and new features of Fortran 90/95.

Complex data type Character data type


a complex number whose real and A string of characters
imaginary parts are REAL, not
INTEGER
Example – 3.56 Example – CAT, dog

A variable is an identifier whose value can change during program execution

The syntax for variable is:


VAR name : type OR
VAR name1, name2, … nameN : type

Constant keep their value or string unchanged for the whole program.

The syntax for constant is:


Const
age = 15;
age2 := age + 15;

CIT 736 COMPUTER PROGRAMMING


A flowchart is a graphical representation of an algorithm

An algorithm is a step-by-step method for solving a problem

ALLOCATABLE arrays are arrays that can have a given dimension, but with a
size that depends on something that happens in the code

An example of a program that ALLOCATE/DEALLOCATE an array with a size


based on user input that can generate many random numbers
PROGRAM allocate_example
IMPLICIT NONE
REAL, DIMENSION(:), ALLOCATABLE :: a
INTEGER :: i
WRITE(6,*) 'How many random numbers do you want to calculate ?'
READ(5,*) i
ALLOCATE(a(i))
CALL RANDOM_NUMBER(a)
WRITE(6,*) a
DEALLOCATE(a)
END PROGRAM allocate_example

COMMON Statement is used to share common storage location in the main


program and subprograms.

The COMMON statement format is


COMMON list

Example of COMMON statement


PROGRAM area
COMMON i, j, k, l
READ (*,*) i, k
j=i*k
CALL areaplus
WRITE (*,*) l
STOP
END

CIT 736 COMPUTER PROGRAMMING


Dependency of a machine language
The machine language is a low level interface, natural to the hardware of the
computer. Since the machine language depends on the particular computer
model, it is said to be machine dependent.

Caching Buffering
cache improves the access speed to Buffering is mainly used to match the
frequently used data communication speed between the
sender and the receiver

Five (5) major features of FORTRAN 90/95


a) Array processing
b) Dynamic memory allocation, including dynamic arrays
c) Modules
d) Procedures
e) Pointers

Additional features of FORTRAN 90/95


a) Free format source code
b) Specifications/IMPLICIT NONE
c) Parameterised data types
d) Derived types
e) Operator overloading
f) CASE statement
g) EXIT and CYCLE
h) Many new intrinsic functions
i) New I/O features

Declare and assign 3.142 and 7.8 to constant variables C and D in FORTRAN
90/95
REAL :: C=3.142, D=7.8

CIT 736 COMPUTER PROGRAMMING


Difference between Function and Subroutine
The main difference between a SUBROUTINE and a FUNCTION is that a
FUNCTION can only return a single value, whereas a SUBROUTINE can do more
complicated things, like alter the value of many variables

User-defined data types Pointer data types


Customized data types A pointer has the POINTER attribute

Formatted I/O statements Unformatted I/O statements


data is formatted or transformed data in its raw form or binary
representation without any
conversions

Compare an algorithm with a flowchart


Before you begin to write code, you need a clear understanding of the tasks
the program must perform: the requirements and the steps it should take to
perform them. The steps a program must take to perform a task is known as
the program’s flow. You can design a program’s flow by using algorithms and
flowcharts

Internal procedure
A subprogram which is placed inside a module procedure, an external
procedure, or a main program; inside a program unit

External procedure
A self-contained procedure (subroutine or function)

CIT 736 COMPUTER PROGRAMMING


A data structure is a specialized format for organizing, processing, retrieving
and storing data

Explain the types of Data structure


a) Boolean, which stores logical values that are either true or false.
b) Integer, which stores a range on mathematical integers -- or counting
numbers. Character, which uses symbols from a defined mapping of
integer values to symbols.
c) Pointers, which are reference values that point to other values.
d) String, which is an array of characters followed by a stop code -- usually
a "0" value -- or is managed using a length field that is an integer value.

Application areas of Data structures


a) Compiler Design.
b) Operating System.
c) DBMS.
d) Graphics.
e) Simulation.
f) Numerical Analysis.

DO Loop and example


This type of loop is executed while the condition is true. It is different from the
'Repeat-Until' loop since the loop might not be executed for at least one time.

Program WhileDo;
Uses Crt;
Var Ch : Char;
Begin
Writeln('Press ''q'' to exit...');
Ch := Readkey;
While Ch <> 'q' do
Begin
Writeln('I told you press ''q'' to exit!!');
Ch := Readkey;
End;
End.

CIT 736 COMPUTER PROGRAMMING


Format of a Case statement
Case {variable of type: integer or character ONLY} of
{input statement- within inverted commas if of type char} : {code..}
{input statement- within inverted commas if of type char} : {code..}
...
End; {End Case}

Use and structure of IF-THEN-ELSE statement


It is conditional statement that gives you once the possibility to do something
or not (or something else)

if var1 = 0 then
writeln('var1 is 0!') (*No semicolon before an 'Else' keyword*)
else if var1 = 1 then
begin
writeln('var1 is 1!');
(*More code*)
end (*No semicolon before an 'Else' keyword*)
else if var1 = 2 then
begin
writeln('var1 is 2!');
125
(*More code*)
end (*No semicolon before an 'Else' keyword*)
else
begin
writeln('var1 is not 0, 1, or 2!');
(*More code*)
end; (*Semicolon used to indicate the end of the If-then-else*)

CIT 736 COMPUTER PROGRAMMING


A variable is an identifier whose value can change during program execution

An identifier is a name such as constant, variable or procedure given to some


program element. They are comprised of sequence of letters or digits with the
first character being a letter. Examples of identifiers are B1, Product, Figure12
etc.

Numbers are bunch of numerical figures which can include signs, decimal point
and exponent or scale factor. Commas and blank spaces are not included in
numbers but it can be preceded by plus (+) or negative (-) signs.

The following program will convert centimetres to inches.


! This program converts centimetres to inches
PROGRAM cm_to_ins
IMPLICIT none
REAL :: cm, ins
WRITE(6,*) 'Please enter the number of centimetres'
WRITE(6,*) 'to be converted to inches'
READ(5,*) cm
ins = cm/2.54
WRITE(6,*) cm,' cm = ',ins,' inches'
END PROGRAM cm_to_ins

String variable in Pascal expression Integer variable in Pascal expression


A string is a sequence of characters An integer number has no decimal
(i.e. letters, digits and special point.
characters) enclosed by apostrophe.
Example: ‘OPEN UNIVERSITY’ Example: 67, -234, 900 etc.

One-dimensional array Two-dimensional array


S(1), S(2), S(3), …., S(N) Two-dimensional array is a matrix
Where S is the array name and 1, 2, 3, A(1,1) A(1,2) …… A(1,N)
.., N are the subscripts. S(1), S(2), .. A(2,1) A(2,2) ……. A(2,N)
S(N) are the :::
elements of the array A(M,1) A(M,2) …… A(M,N)
where m and n are integer number

CIT 736 COMPUTER PROGRAMMING


Six (6) programming methodologies
a) Caching
b) Buffering
c) Logical layering
d) Centralized resource management
e) Object-Oriented programming (OOP)
f) Lookup tables

How are procedures and functions interrelated?


Procedures and functions are subprograms or set of instructions to be
executed within a program to avoid repetition. A procedure executes, with no
return value, while a function is a procedure with a return value

An array is a set (or collection) of related quantities of the same type with the
same symbolic name.

Common variable data types used in FORTRAN 95


a) REAL (a number with a decimal point, such as 3.142, 1.0, -0.002)
b) INTEGER (a whole number, without a decimal point, such as 0, 5654, -
26472)
c) COMPLEX (a complex number (whose real and imaginary parts are REAL,
not INTEGER))
d) CHARACTER (a string of characters)
e) LOGICAL (can take the values .TRUE. or .FALSE. only)

Differentiate between static and dynamic arrays


An array is said to be a static data structure because, once declared, its original
size that is specified by the programmer will remain the same throughout the
whole program and cannot be changed

A dynamic array is declared when the lists contain unlimited number of items,
depending on the amount of computer memory.

CIT 736 COMPUTER PROGRAMMING


Modular programming
Modular programming is a way of dividing a more involved program into areas
or modules that perform specific tasks. This module is sometimes referred to
as subprogram. The subprogram which is small program can also be referred to
as procedure.

Before we begin, let us first clarify the key difference between functions and
procedures. A procedure is set of instructions to be executed, with no return
value. A function is a procedure with a return value.

Categories of subprograms
a) Procedures
b) Functions

IF-THEN-ELSE statement Do WHILE loop


An if statement gives you once the A while loop does things as long as the
possibility to do something or not (or condition is true.
something else)

CIT 736 COMPUTER PROGRAMMING

You might also like