21
Table of Contents Nested Conditional and Iterative Statements......................................................................................................... 2 Single Dimensional and Multidimensional Arrays................................................................................................... 4 Strings, String Arrays, and String Manipulation Functions...................................................................................... 7 Pointers................................................................................................................................................................... 9 Functions and Pass-by-Value................................................................................................................................ 11 Functions and Pass-by-Reference......................................................................................................................... 13 Structures, Structure Arrays, and Complex Data Types......................................................................................... 16 Structures, Structure Pointers, and Passing of Structure Reference to Functions.................................................18 Dynamic Memory Allocation................................................................................................................................ 20 LBYEC72 Lab Manual Prepared by N. Mallari ([email protected]) Page 1 of 21 ECCE Department, College of Engineering

LBYEC72 Lab Manual

Embed Size (px)

Citation preview

Page 1: LBYEC72 Lab Manual

Table of ContentsNested Conditional and Iterative Statements.........................................................................................................2Single Dimensional and Multidimensional Arrays...................................................................................................4Strings, String Arrays, and String Manipulation Functions......................................................................................7Pointers...................................................................................................................................................................9Functions and Pass-by-Value................................................................................................................................11Functions and Pass-by-Reference.........................................................................................................................13Structures, Structure Arrays, and Complex Data Types.........................................................................................16Structures, Structure Pointers, and Passing of Structure Reference to Functions.................................................18Dynamic Memory Allocation................................................................................................................................20

LBYEC72 Lab Manual Prepared by N. Mallari ([email protected])Page 1 of 21 ECCE Department, College of Engineering

Page 2: LBYEC72 Lab Manual

Nested Conditional and Iterative StatementsObjectives

At the end of the lab session, students should be able to:• Use conditional statements for decision-making• Use iterative statements for repeating operations• Use iterative statements for complex computations requiring iterations• Develop algorithms for engineering formulas and applications• Create a program that demonstrates the use of engineering equations

Preliminary Report Requirements

IntroductionProvide a brief background of conditional and iterative statements and explain its importance to

engineering problem-solving applications.

Conditional StatementsC has two statements that implement decision-making: if and switch. Research about the forms

of if (if, if-else, if-else if-else) and switch. Show the syntax of each statement, the equivalent algorithm/flowchart representation, and a simple example that demonstrates the use of each statement.

Iterative StatementsC has three statements that implement iterations and loops: while, do-while, and for. Show the

syntax of each statement, the equivalent algorithm/flowchart representation, and a simple example that demonstrates the use of each statement.

Engineering EquationsResearch about two complex engineering equations related to your field. Equations should have

at least four variables.

Final Program Requirements

• Program shows a formula selection screen that allows the user to select a formula. • Formula selection screen allows the user to quit the program.• Program asks the user what variable in the equation to compute. • Variable selection screen allows the user to go back to the formula selection screen.• Program asks values for input variables and computes the selected variable.• Program goes back to the variable selection screen. User can select another variable to compute or

select to go back to the formula selection screen.• Program goes back to the formula selection screen.• Do not use goto and/or global variables (this rule applies to all suceeding final programs).

LBYEC72 Lab Manual Prepared by N. Mallari ([email protected])Page 2 of 21 ECCE Department, College of Engineering

Page 3: LBYEC72 Lab Manual

Test Walkthrough

1. Select a formula from the formula selection screen.2. Select a variable from the variable selection screen.3. Compute for the variable. Provide at least 2 sets of test data per variable.4. Enter an invalid choice at the variable selection screen.4. Repeat step 2 until all variables are computed.5. Repeat step 1 until all formulas are demonstrated.6. Enter an invalid choice at the formula selection screen.7. Quit the program.

LBYEC72 Lab Manual Prepared by N. Mallari ([email protected])Page 3 of 21 ECCE Department, College of Engineering

Page 4: LBYEC72 Lab Manual

Single Dimensional and Multidimensional ArraysObjectives

At the end of the lab session, students should be able to:• Use single-dimensional arrays for representing collection of data.• Use single-dimensional arrays for representing polynomials.• Use multi-dimensional arrays for representing matrices.• Manipulate data in single-dimensional arrays for computing mean, standard deviation, summation, and

other statistical algorihtms.• Manipulate data in single-dimensional arrays to represent and evaluate polynomials.• Manipulate data in multi-dimensional arrays for basic matrix operations (addition, multiplication, and

transpose).

Preliminary Report Requirements

Introduction

Provide a brief background about arrays and its importance to C programming and engineering applications.

Arrays

Research about single-dimensional arrays and multi-dimensional arrays in C programming. Include the following concepts in your discussions: array declaration (for all basic data types), array size, index, lower-bound, and upper-bound. Show example code snippets.

Polynomials

Review your concept of polynomials. Study basic polynomial arithmetic and evaluation.

Arrays are used in the final program to represent polynomials. For example, a third-order polynomial requires a double array with size of three, such that it is declared:

double poly[3];

Constants of polynomials are assigned to elements of the array. For example. poly[0] is the constant for x^0, poly[1] is the constant for x[1], etc.

Statistical Algorithms

Review your basic statistical algorithms for finding the mean, standard deviation, and summation of a collection of data. You will be using an array to store data for these statistical operations.

Arrays are used to represent collection of data for statistical algorithms. A collection of data with 10 elements would require a double array with 10 elements:

LBYEC72 Lab Manual Prepared by N. Mallari ([email protected])Page 4 of 21 ECCE Department, College of Engineering

Page 5: LBYEC72 Lab Manual

double data[10];

Matrix Operations

Review your matrix operations (addition, multiplication, and transpose).

A multi-dimensional array is used to represent a matrix. For example, a 3x1 matrix would require an array declaration of:

double matrix[3][1];

Likewise, a 1x3 matrix would require an array declaration of :

double matrix[1][3];

Rows and columns can be interchanged in the multi-dimensional array declaration. Make sure you are consistent with the declaration such that if the first index is declared as rows and second index is declared as columns, the entire program should make use of the first index as row and second index as column.

Algorithms, Flowcharts, and Pseudocodes

Algorithms of operations should be presented in detail including per element manipulation of array elements.

Final Program Requirements

• Program shows a menu selection screen that allows the user to select between polynomial evaluation, statistical algorithms, and matrix operations.

• For polynomial evaluation:• Program asks for the order of the polynomial (max. 10).• Program asks for the constants of the polynomial.• Program asks for the value of the variable and evaluates the polynomial.• Program should ask if the user wants to repeat computation, else, program returns to the selection

screen.• For statistical algorithms:• Program asks for the number of data (max 10).• Program asks for the actual values of data.• Program allows the user to select a statistical algorithm: mean, standard deviation, or summation of

data. This screen also allows the user to go back to the menu selection screen.• Program computes formula selected by user and goes back to the statistical algorithm selection screen.• For matrix operations:• Program manipulates two MxN matrices (max of 3x3, can be 2x1, 1x3, etc.).• Program shows a matrix operation selection screen that allows the user to select between entering the

values of the first matrix, the second matrix, or to do matrix operations of addition, multiplication, or transpose. This screen should also allow the user to go back to the menu selection screen.

LBYEC72 Lab Manual Prepared by N. Mallari ([email protected])Page 5 of 21 ECCE Department, College of Engineering

Page 6: LBYEC72 Lab Manual

• Program computes for the operation selected by user and goes back to the matrix operation selection screen.

Test Walkthrough

1. Select polynomial evaluation.2. Enter a test polynomial. Repeat until two polynomials are tested.3. Select statistical algorithms.4. Enter a set of test data. Compute for mean, standard deviation, and summation. Repeat for two sets of test data.5. Enter an invalid choice at the statistical algorithm selection screen.6. Select matrix operations.7. Enter values for first matrix and second matrix.8. Do matrix operations. 9. Repeat step 7 and 8 by entering a 3x3 matrix for the first matrix with the second matrix being a 3x3 identity matrix.10. Enter an invalid choice at the matrix operation selection screen.11. Enter an invalid choice at the menu selection screen.12. Quit the program.

LBYEC72 Lab Manual Prepared by N. Mallari ([email protected])Page 6 of 21 ECCE Department, College of Engineering

Page 7: LBYEC72 Lab Manual

Strings, String Arrays, and String Manipulation Functions

ObjectivesAt the end of the lab session, students should be able to:

• Declare strings and string arrays.• Access elements of strings and string arrays.• Use basic I/O on strings and string arrays.• Demonstrate the use of linear search algorithm on string arrays utilizing string manipulation functions.

Preliminary Report Requirements

Introduction

Provide a brief background on strings and explain the importance of strings in C programming and engineering applications.

Strings

Research about strings in C programming. Include the following concepts in your discussions: character array, string declaration, null terminating character, string basic input/output. Show example code snippets.

String Arrays

Research about string arrays in C programming. Include the following concepts in your discussions: string array declaration, accessing strings in string arrays, string array basic input/output. Show example code snippets.

String Manipulation Functions

Research about string manipulation functions in C programming. Discuss at least 8 string manipulation functions including strlen() and strcmp(). Show example code snippets that demonstrate the use of the functions.

Linear Search Algorithm

Linear search is a very simple search algorirthm with a complexity of O(n). Research about linear search and how linear search work on string arrays.

Final Program Requirements

• Program stores strings as string arrays. Strings can be names, addresses, etc.• Program shows a selection screen that allows the user to enter strings (max. of 16, strings have a

maximum of 128 characters), remove a string from the database, view strings in database, search a string, and quit the program.

LBYEC72 Lab Manual Prepared by N. Mallari ([email protected])Page 7 of 21 ECCE Department, College of Engineering

Page 8: LBYEC72 Lab Manual

• Bonus requirement (+10pts.) Program allows the user to save strings and load strings to and from a text file. This requirement requires the use of file I/O.

Test Walkthrough

1. Prior to checking of the lab instructor, enter a minimum of 8 strings in the program.2. Show the strings in the database.3. Search for a string in the database.4. Search for a string not in the database.5. Add an additional string to the database.6. Remove a string from the database. String should be in the database.7. Enter an invalid choice at the selection screen.8. Quit the program.

LBYEC72 Lab Manual Prepared by N. Mallari ([email protected])Page 8 of 21 ECCE Department, College of Engineering

Page 9: LBYEC72 Lab Manual

Pointers

ObjectivesAt the end of the lab session, students should be able to:

• Declare pointers for basic data types.• Use the addressof operator to store addresses of variables.• Use dereferencing to change the values of variables pointed by pointers.• Display addresses stored by pointers.• Familiarize themselves with the computer's memory map and memory addresses.

Preliminary Report Requirements

Introduction

Provide a brief background about pointers and explain its significance to C programming and engineering applications. Focus on the advantages of using pointers for faster processing.

Pointers

Research about pointers in C programming. Include the following concepts in your discussions: pointer declaration, addressof (&) operator, dereferencing, relationship of arrays to pointers, double pointers and two-dimensional arrays, and pointer basic input/output (displaying values of pointers, etc.) Show code snippets that demonstrate the use of pointers in a program. Limit your discussions to pointers for the basic data types.

For clarity, use the prefix p whenever you declare a pointer, that is, an integer pointer should be declared as:

int *pNum.

Arrays, Memory Map, Addresses, and Pointers

Research about the computer's memory map, memory addresses, and its relationship to pointers. Show discussions about how arrays (char, int, float, double) are represented in memory and how pointers are used to store addresses of data in memory.

Final Program Requirements

• Declare an int variable and int array with four elements in the main program. Initial values for the int variable and int array are hardcoded in the program.

• Declare an int pointer in the main program. • Program shows the values of the integer variable and integer array elements. • Program shows the addresses of the variable and each integer array element. Store addresses in

pointers temporarily prior to displaying. The addresses can change whenever the program is run.• Program asks the user to enter an address. The address is stored in a temporary int variable, and then

copied to the integer pointer.

LBYEC72 Lab Manual Prepared by N. Mallari ([email protected])Page 9 of 21 ECCE Department, College of Engineering

Page 10: LBYEC72 Lab Manual

• Program asks for an integer value X. Using derefencing, the program changes the value pointed by the pointer address entered by the user to X.

• Program asks if the user wants to run the program again.

Test Walkthrough1. Enter an address. (The address should be one of the addresses shown in the program. Entering an invalid address is very dangerous, may cause the program to hang, or worse, cause the computer to stop responding.)2. Enter an integer value.3. Repeat steps 1 and 2 until the integer variable and integer array elements are all changed.4. Enter an invalid choice.5. Quit the program.

LBYEC72 Lab Manual Prepared by N. Mallari ([email protected])Page 10 of 21 ECCE Department, College of Engineering

Page 11: LBYEC72 Lab Manual

Functions and Pass-by-Value

ObjectivesAt the end of the lab session, students should be able to:

• Declare functions and use functions in programs.• Understand pass-by-value and parameter lists.• Modularize programs with the use of functions and pass-by-value.• Convert equations previously done in nested conditional and iterative statements topic to equivalent

modular and reusable functions.• Create reusable functions that can be used in other programs.

Preliminary Report Requirements

Introduction

Provide a brief background about functions and modular programming and its importance to C programming and engineering applications.

Function Declaration

Research about functions. Include the following concepts in your discussions: function prototype, function declaration, parameter list, and return type. Show code snippets that demonstrate functions with no return values, with different return values, and with different numbers of parameters in the parameter list.

Modular Programming

Modular programming focuses on creating reusable functions that can be reused not just in the program where it is declared, but in other programs as well. Examples of modular functions are printf(), scanf(), and math functions. Research additional concepts about modular programming and how it is implemented in C programming.

Using Functions for Equations

Functions for equations should be declared such that the parameter list includes the variables of the equation. Such functions are reusable and can be used in other programs.

For example, to compute for the area of a rectangle with formula area = length x width, the function prototype should be:

double areaRectangle(double length, double width);

The function declared above returns the result of length x width.

Note that functions for equations should not have input and output (printf(), scanf(), etc.) inside the

LBYEC72 Lab Manual Prepared by N. Mallari ([email protected])Page 11 of 21 ECCE Department, College of Engineering

Page 12: LBYEC72 Lab Manual

function as the goal of converting equations to functions is to come up with reusable functions that can be used in other programs later on.

Using Functions for Subdividing Programs

Functions can also be used to subdivide programs into smaller units. You can take out the code that computes for the area of the rectangle from the main program and put it into a function declared as:

void computeRectangle();

This technique, however, does not produce reusable functions as the functions only work in the program where the functions are taken from.

Final Program Requirements

• Program is a modified version of the collection of formulas from Nested Conditional and Iterative Statements.

• Convert the formulas previously done in the collection of formulas to equivalent functions. • Program can be subdivided into smaller units by creating functions that compute for different variables.• Requirements from Nested Conditional and Iterative Statements apply.

Test Walkthrough

1. Select a formula from the formula selection screen.2. Select a variable from the variable selection screen.3. Compute for the variable. Provide at least 2 sets of test data per variable.4. Enter an invalid choice at the variable selection screen.4. Repeat step 2 until all variables are computed.5. Repeat step 1 until all formulas are demonstrated.6. Enter an invalid choice at the formula selection screen.7. Quit the program.

LBYEC72 Lab Manual Prepared by N. Mallari ([email protected])Page 12 of 21 ECCE Department, College of Engineering

Page 13: LBYEC72 Lab Manual

Functions and Pass-by-Reference

ObjectivesAt the end of the lab session, students should be able to:

• Understand pass-by-reference.• Understand the use of local variables, scope, and global variables.• Modularize programs with the use of functions and pass-by-reference.• Convert algorithms previously done in single-dimensional and multi-dimensional arrays to modular and

reusable functions.• Create reusable functions that can be used in other programs.

Preliminary Report Requirements

Introduction

Provide a brief background on functions and pass-by-reference. Discuss its importance in C programming and engineering applications, and discuss the advantages of using pass-by-reference for faster processing.

Pass-by-reference

Review your concept of pointers prior to doing research on pass-by-reference. Research about pass-by-reference and discuss how pointers are used in pass-by-reference. Explain how variables passed using pass-by-reference can be modified by the function. Show code snippets that demonstrate this concept.

Local Variables, Scope, and Global Variables

Research about local variables, scope, and global variables. Differentiate local variables from global variables, and explain why functions cannot change the value of variables passed using pass-by-value. Show code snippets that demonstrate this concept. Hint: This concept is being used in scanf().

Passing Arrays to Functions

Arrays are special when doing pass-by-reference as they do not include pointer declarations in the parameter list. Consider a polynomial evaluation function evalPoly():

double evalPoly(double poly[10], double x, double order);

The parameter double poly[10] is already considered pass-by-reference and can be directly manipulated in the function's scope.

Note that double x and double order need not be passed using pass-by-reference, although you can also opt to do so.

Declaring Functions

LBYEC72 Lab Manual Prepared by N. Mallari ([email protected])Page 13 of 21 ECCE Department, College of Engineering

Page 14: LBYEC72 Lab Manual

Functions in the final program should be declared using the following formats:

For polynomial evaluation, the return type is double because the result of a polynomial evaluation is a single double value.

double evalPoly(double poly[MAX_ORDER], double x, double order);

For statistical algorithms, the return types are double because the results of the algorithms are single double values.

double evalMean(double data[MAX_DATA], double count);double evalStandardDeviation(double data[MAX_DATA], double count);

double evalSum(double data[MAX_DATA], double count);

For matrix operations, the return type is void. The result of the operation should not be returned as a return type as this is inefficient. Instead, the result should be passed using pass-by-reference so that the function can store the result to an array outside its scope.

void addMatrix(double m1[M][N], double m2[M][N], double result[M][N]);void multiplyMatrix(double m1[M][N], double m2[M][N], double result[M][N]);

void transposeMatrix(double m[M][N], double result[M][N]);

You can use function names, different variable and array names, and different names for the constants.

Final Program Requirements

• Program is a modified version of final program from Single and Multi-dimensional Arrays. • Convert the functions previously done from Single and Multi-dimensional Arrays to equivalent pass-by-

reference functions.• Program can be subdivided into smaller units by creating functions that compute for different variables.• Requirements from Single and Multi-dimensional Arrays apply.

Test Walkthrough

1. Select polynomial evaluation.2. Enter a test polynomial. Repeat until two polynomials are tested.3. Select statistical algorithms.4. Enter a set of test data. Compute for mean, standard deviation, and summation. Repeat for two sets of test data.5. Enter an invalid choice at the statistical algorithm selection screen.6. Select matrix operations.7. Enter values for first matrix and second matrix.8. Do matrix operations. 9. Repeat step 7 and 8 by entering a 3x3 matrix for the first matrix with the second matrix being a 3x3 identity matrix.10. Enter an invalid choice at the matrix operation selection screen.

LBYEC72 Lab Manual Prepared by N. Mallari ([email protected])Page 14 of 21 ECCE Department, College of Engineering

Page 15: LBYEC72 Lab Manual

11. Enter an invalid choide at the menu selection screen.12. Quit the program.

LBYEC72 Lab Manual Prepared by N. Mallari ([email protected])Page 15 of 21 ECCE Department, College of Engineering

Page 16: LBYEC72 Lab Manual

Structures, Structure Arrays, and Complex Data Types

ObjectivesAt the end of the lab session, students should be able to:

• Declare and use structures and structure arrays.• Represent collections of complex data as structures and/or structure arrays.• Apply linear search algorithm to find entries in structure arrays.• Manipulate data in structures and structure arrays.

Preliminary Report Requirements

Introduction

Structures are similar to arrays as these are also collection of data. Provide a brief introduction about structures, structure arrays, and complex data types. Discuss the importance of the concept, particularly the need to collect different types of data. Identify and explain the difference between structures and arrays.

Structures

Research about structures. Include the following concepts in your discussions: structure declaration, struct keyword, union keyword, structure variable, and accessing structure parameters/attributes. Show code snippets that demonstrate the use of structures.

Structure Array

Research about structure arrays. Include the following concepts in your disussions: structure array declaration and accessing structures via indeces. Show code snippets that demonstrate the use of structure arrays.

Typedef Keyword

Research about the typedef keyword and explain how it can be used to simplify the declaration of structures. Use typedef in your final program.

Complex Data Types

Complex data types are collection of data with different parameters. For example, in engineering applications, a point in 2-D space can be represented as a structure such as:

// Structure declaration of a point in 2-D Spacetypedef struct {

double x;double y;

} Point_t;

LBYEC72 Lab Manual Prepared by N. Mallari ([email protected])Page 16 of 21 ECCE Department, College of Engineering

Page 17: LBYEC72 Lab Manual

Final Program Requirements

• Program is a database of complex information, e.g. Students, Employees, etc.• Program shows a selection screen that allows the user to add an entry to the database, display entries,

remove an entry, edit an entry, or search an entry using a particular parameter, e.g. ID number, name, etc.

• Program has support for a maximum of 10 data set.• Program shows the selection screen again after an operation.• Program searches for entries using Linear Search Algorithm.

Test Walkthrough

1. Prior to checking of the lab instructor, enter at least 4 dummy data in the database.2. Display the entered data.3. Remove an entry from the database.4. Add an entry to the database.5. Edit the first entry of the database.6. Enter an invalid choice at the selection screen.7. Quit the program.

LBYEC72 Lab Manual Prepared by N. Mallari ([email protected])Page 17 of 21 ECCE Department, College of Engineering

Page 18: LBYEC72 Lab Manual

Structures, Structure Pointers, and Passing of Structure Reference to Functions

ObjectivesAt the end of the lab session, students should be able to

• Declare and use structure pointers.• Understand passing structures to functions using pass-by-reference.• Create modular functions that manipulate structures.• Manipulate complex numbers represented as structures.

Preliminary Report Requirements

Introduction

Provide a brief introduction about structure pointers and passing of structures to functions using pass-by-reference. Discuss the advantage of passing structures to functions using pass-by-reference in terms of speed.

Structure Pointers

Research about structure pointers. Include the following concepts in your discussions: declaration of structure pointers, arrow notation, structure pointers and arrays. Include code snippets that demonstrate the use of structure pointers. Use typedef when declaring your structures and structure pointers.

Structure Pointers and Pass-by-Reference

Research about how structures are passed to functions using pass-by-reference. Show code snippets that demonstrate passing structures to functions using structure pointers.

Complex Numbers

Review your concepts of complex numbers and complex number operations: representation in rectangular and polar form, converting from one form to another, and complex number arithmetic.

Declaring Functions

All functions should be declared modular. Pass operands as structure pointers, and pass the storage of the result also as a structure pointer. Do not return the complex number as a return type as this is inefficient. Consider the example function prototype for an add operation:

void addComplexRect(ComplexRect_t *pC1, ComplexRect_t *pC2, ComplexRect_2 *pSum);

Where ComplexRect_t is a structure declaration for a complex number in rectangular form. The return type can also be modified to int to return error codes, e.g. return 0 if success, return -1 if operation

LBYEC72 Lab Manual Prepared by N. Mallari ([email protected])Page 18 of 21 ECCE Department, College of Engineering

Page 19: LBYEC72 Lab Manual

cannot proceed, etc.

Final Program Requirements

• Program demonstrates the use of structure pointers and complex number operations. • Program initializes three complex numbers in rectangular form. These complex numbers are declared in

the main program. (No global variables)• Program shows a selection screen that allows the user to change the real and imaginary values of the

first complex number, change the real and imaginary values of the second complex number, add the two complex numbers, multiply the two complex numbers, display all the complex numbers, and quit the program.

• The third complex number is always the result of the operation.• Program makes use of modular functions for add, multiply, and display of complex numbers.• Bonus Requirement (+10pts.) Program can manipulate rectangular and polar forms of complex

numbers and can do operations on different forms (rectangular-rectangular, polar-rectangular, etc.)

Test Walkthrough

1. Prior to checking of the lab instructor, enter two complex numbers in the program.2. Add the complex numbers.3. Multiply the complex numbers.4. Show all complex numbers.5. Repeat steps 2, 3, and 4 until two sets of complex numbers are tested.6. Enter an invalid choice at selection screen.7. Quit the program.

LBYEC72 Lab Manual Prepared by N. Mallari ([email protected])Page 19 of 21 ECCE Department, College of Engineering

Page 20: LBYEC72 Lab Manual

Dynamic Memory AllocationObjectives

At the end of the lab session, students should be able to:• Differentiate dynamic memory allocation from static memory allocation.• Understand the use of typecasting.• Understand the use of sizeof.• Use malloc(), realloc(), and calloc() for allocating memory.• Use free() to free allocated memory.• Create programs that use arrays that dynamically change size during program execution.

Preliminary Report Requirements

Introduction

Provide a brief introduction about dynamic memory allocation. Differentiate dynamic memory allocation from static memory allocation and discuss its importance in C programming and engineering applications.

malloc(), realloc(), calloc(), and free()

Research about the identified dynamic memory allocation functions. Show code snippets that demonstrate the use of the functions.

sizeof keyword

Research about the sizeof keyword and how it is used in dynamic memory allocation. Show code snippets that demonstrate the use of sizeof.

Typecasting

Typecasting is used to explicitly tell the compiler to change the data type of a variable or data. Consider the example below:

pStr = (char*) malloc(sizeof(char) * length);

Where pStr is a char pointer. The characters in boldface, (char*), is a typecast that converts the memory location returned by malloc() to a char* equivalent.

Note that some compilers require the use of typecast for dynamic memory allocation, while some compilers do not.

Research about typecasting and show code snippets that demonstrate its use.

LBYEC72 Lab Manual Prepared by N. Mallari ([email protected])Page 20 of 21 ECCE Department, College of Engineering

Page 21: LBYEC72 Lab Manual

Final Program Requirements

• Program demonstrates the use of dynamic memory allocation.• Select a final program that was done in previous exercises. Selected final program should make use of

arrays. Modify the program so that its arrays are allocated using dynamic memory allocation instead of static memory allocation.

• Program should allow the user to resize the arrays.• Program functions should be modular.• Design your own test walkthrough for this final program.• Note that passing arrays to functions require the use of pointers instead of the usual static array

declaration, e.g. void func(int* pArray) instead of void func(int array[10]).

LBYEC72 Lab Manual Prepared by N. Mallari ([email protected])Page 21 of 21 ECCE Department, College of Engineering