Professional Documents
Culture Documents
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
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.
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.
Prepared by N. Mallari (noriel.mallari@dlsu.edu.ph) ECCE Department, College of Engineering
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.
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.
Pointers
Objectives At 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.
Prepared by N. Mallari (noriel.mallari@dlsu.edu.ph) ECCE Department, College of Engineering
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 Walkthrough 1. 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.
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.
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-byreference 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 Page 14 of 21 Prepared by N. Mallari (noriel.mallari@dlsu.edu.ph) ECCE Department, College of Engineering
11. Enter an invalid choide at the menu selection screen. 12. Quit the program.
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.
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.
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]).