C Programming

These notes are in addition to the text book used in the class of BAA2013 Computer Programming in C++.

Tuesday, May 09, 2006

Program Data

PROGRAM DATA

Variables, constants and literals
A variable is the name given to a collection of memory cells, designed to store a particular data item. It is called a variable because the value stored in those memory cells may change or vary as the program executes. For example, the variable total_amount may contain several values during the execution of the program.

A constant is a data item with a name and a value that remain the same during the execution of the program. For example, the name 'fifty' may be given to a data item that contains the value 50.

A literal is a constant whose name is the written representation of its value. For example, the program may contain the literal '50'.

Data Types
At the beginning of a program, the programmer must clearly define the form or type of the data to be collected. The data types can be elementary data items or data structures.

Elementary data items
An elementary data item is one containing a single variable that is always treated as a unit. These data items are usually classified into data types. A data type consists of a set of data values and a set of operations that can be perfiormed on these values. The most common elementary data types are:

  • integer :
    representating a set of whole numbers, positive, negative or zero.
    eg. 4, 123, -56
  • real :
    representing a set of numbers, positive or negative, which may include values before or after a decimal point. These are sometimes referred to as floating point numbers.
    eg. 12..5 , 2.3E+04 , -98
  • character :
    representing the set of characters on the keyboard, plus some special characters
    eg. 'A' , 'c' , '$'
  • Boolean:
    representing a control flag or switch, which may contain one of only two possible values ; true or false.

Data Structures
A data structure is an aggregate of other data items. The data items that it contains are its components, which may be elementary data items or another data structure. In a data structure, data is grouped together in a particular way, which reflects the situation with which the program is concerned. The most common data structures are:

  • record:
    a collection of data items or fields that all bear some relationship to one another. For example, a student record may contain the student's number, name, address and enrolled subjects.
  • file:
    a collection of records. For example, a student file may contain a collection of the above student records.
  • array:
    a data structure that is made uo of a number of variabkes r data itenms that all have the same data tyoe and are accessed by the same nane. For example, an array called 'scores' may contain a collection of students' exam scores. Access to the individual items in the array is made by the use of an index or subscript beside the name of the array - for example, scores (3).
  • string:
    a collection of characters that can be fixed or variable. For example, the string 'Jenny Parker' may repersent a student's name.

Files
A popular method of storing information is to enter and store data on a file. The major advantages of using files are:

  • several different programs can access the same data
  • the data can be entered and reused several times
  • the data can be easily updated and maintained
  • the accurary of the data is easier to enforce.

There are two different methods of storing data on files:

  • sequential or text files, where data is stored and retrieved sequentially
  • direct or random-access files, where data is stored and retrieved randomly, by a key or index.

Sequential files may be opened to read or to write, but not both operations on the same file.
Random-access files can be opened to read and write on the same file.


0 Comments:

Post a Comment

<< Home