41
IT104 Discrete Structures COMPILATION OF TOPICS 1

IT104 Discrete Structures

Embed Size (px)

DESCRIPTION

Compilation of Topics

Citation preview

IT104 Discrete Structures

COMPILATION OF TOPICS

Name: Aina Maree E. Siason

Instructor: Ms. Lisa Rebusa

Table of Contents

Topic Page

1. Arrays 4

2. Statistical Array 8

3. Pointers 10

4. Functions 14

5. Relationship Between Pointers and Arrays 18

6. Passing Pointers to Functions 20

7. Pointers to Functions 21

8. Array of Functions 23

9. Enumeration 25

10. Character Handling Library Functions 27

Introduction

This project aims to create a simplified description of the topics discussed in our IT104 Discrete Structures class. Using C programming, data structures and algorithms are particularly tackled wherein logic and theory are needed in order to create programs. Topics such as Array, Pointers, and Functions will be discussed. Arithmetical and logical operators such +, +=, ++, &, ~ are frequently used to solve various equations.

C programming, which is an imperative programming language, is one of the most widely used programming languages of all time, and C compilers are available for the majority of computer architectures and operating systems. In C, all executable codes are contained within subroutines, which are called functions. Functional parameters are always passed by value. Pass-by-reference is simulated in C explicitly by passing pointer values. The latter will be discussed thoroughly as we progress further.

For each topic, the theory and the application of theory will be presented. Under the application of theory the following will be shown: 1) problem, 2) program code, and 3) sample output.

Arrays

Array in C programming is a data structure that can store a fixed size sequential collection of elements of the same type. An array is used to store a collection of data. Thus, it is a collective name given to a group of similar quantities. These similar quantities could be the number of students enrolled in a university, the grades of 100 students enrolled in a class, or the ages of employees in a company.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

First Element Last Element

age[0]

age[1]

age[2]

age[3]

Arrays and pointers have a special relationship because arrays use pointers to reference memory locations.

Declaration of an Array

Arrays must be declared first before they can be used in a program. The standard array declaration is as follows:

type arrayname[arraysize];

Type specifies the variable type of the element that is going to be stored in the array. The following are the basic variable types in C Language:

Type

Description

char

Typically a single octet (one byte). This is an integer type.

int

The most natural size of integer for the machine.

float

A single-precision floating value.

double

A double-precision floating value.

void

Represents the absence of type.

Some examples of array declaration are the following:

char name[30];

int min[10];

float width[20];

double height[15];

Arrays and pointers have a special relationship because arrays use pointers to reference memory locations.

Arrays start at position 0 in C Language. The elements of the array occupy adjacent locations in memory. C Language treats the name of the array as if it were a pointer to the first element, which is important in understanding how to do arithmetic with arrays.

Single-dimensional Array

The single-dimensional array in C is the simplest form of array that contains only one row for storing data. It has a single set of square brackets ([]).

type arrayName[arraySize];

Multi-dimensional Array

The C language supports multi-dimensional arrays. The simplest data structure for organizing tabular data is a two-dimensional array. C compilers treat a two-dimensional array as an array of arrays. To declare a two-dimensional integer array size x,y you would write something as follows:

type arrayName[x][y];

Where type can be any valid C data type and arrayName will be a valid C identifier. A two-dimensional array can be think as a table which will have x number of rows and y number of columns. A 2-dimensional array a, which contains three rows and four columns can be shown as below:

Thus, every element in array a is identified by an element name of the form a[i][j], where a is the name of the array, and i and j are the subscripts that uniquely identify each element in a.

Example Problem

Problem

Forty Students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 means awful and 10 means excellent). Place the 40 responses in an integer array and summarize the results of the poll.

Code

#include

#define RESPONSE_SIZE 40

#define FREQUENCY_SIZE 11

int main()

{

int answer, rating, frequency[ FREQUENCY_SIZE ] = { 0 };

int responses[ RESPONSE_SIZE ] =

{ 1, 2, 6, 4, 8, 5, 9, 7, 8, 10,

1, 6, 3, 8, 6, 10, 3, 8, 2, 7,

6, 5, 7, 6, 8, 6, 7, 5, 6, 6,

5, 6, 7, 5, 6, 4, 8, 6, 8, 10 };

for ( answer = 0; answer