Uploaded by Saad Gujjar

Lab1

advertisement
EXPERIMENT 1
Date Perform: _________
To manipulate C++ pointers for writing useful codes and revision
of OOP concepts
SUBMISSION GUIDLINES:
• Submit only .pdf files as your submission
o Put your code along with output/error screenshot under clear heading of each
question number
o Upload it on google classroom before the deadline
OBJECTIVE:
• Pointer arithmetic
• Pointer and functions
• Dynamic memory allocation
EQUIPMENT:
• Computer equipped with Microsoft Visual Studio 15 (or latest) and Office 16 (or latest)
BACKGROUND:
A pointer is a variable that holds a memory address where a value resides. A pointer is declared
using the * operator before an identifier. As C++ is a strictly typed language, the type is required
to declare a pointer. This is the type of data that will live at the memory address it points to. We
can set the memory address, the pointer points to using the memory address of the variable. The
memory address of any variable can be accessed using the & operator.
The value at the memory address pointed to can be accessed with the dereference operator, *.
When calling a function (by value) with an argument, a new variable is instantiated internally
and assigned the value passed in the function call. Any modifications to the value inside the
function are performed to this new variable; the variable that was invoked with the function call
is unchanged. A function can be written to perform the same task but instead accept a pointer as
the argument. This lowers the memory footprint of the program. Unnecessary duplicate variables
are not created. The function can modify the variable's value directly. Any modifications to the
variable in the function affect the variable here in this scope too.
LAB TASKS:
Problem 1:
Write a function to calculate the 𝐢𝑒𝑏𝑒 × πΆπ‘’π‘π‘’ of a number C. Your function should take an
integer pointer to C. Display the original value of C and its 𝐢𝑒𝑏𝑒 × πΆπ‘’π‘π‘’ cube in the main
function.
e.g., For example, if C=3, then output should be like: The original number is: 3
Cube times cube of the number is: 27 * 27 = 729
Problem 2:
Declare an array ‘list’ of length 15.
• Input the values from user.
• Pass that array to a function sorting.
• Your function should only accept a pointer as an argument.
• Sort the values of the array in ascending order using any of the sorting techniques.
Problem 3:
Declare a double array of length 13 dArray.
• Input the values of dArray from user.
• Display the values of even indexes using pointer arithmetic only.
Problem 4:
Declare and initialize (zeros) an integer array intArr of length 10.
• Add 2*N to the values on even indexes of the array where N is the index number.
• Pass this array to a function odd() (using a pointer as formal parameter in the function).
o Add 2*N+1 to the values on odd indexes of the array.
o Do not return anything from your function.
• Display intArr before and after function call to odd() in the main().
Problem 5:
Declare a character array cnicArray of length 14.
• Input the CNIC from user without dashes.
• Pass that array to a function ‘copy’ using a pointer.
• Copy the values of cnicArray in c1Array using dereferencing.
• Print c1Array in copy function
• Note that c1Array is declared in function copy
Problem 6:
We need to implement intArray data structure that can store real numbers and have the following
public functionalities:
• Constructor: with the size of the array as the parameter [it also tackles dynamic allocations]
• Destructor: to destroy the dynamic allocation done for the array
• arrSize: returning the current size of the array
• Read: reading the content of the array from the user
• Print: printing the content of the array
• Avg: returning the arithmetic mean of the values of the array
• Min: returning the smallest element present in the array
• Max: returning the largest element present in the array
Note: Implement this data structure using classes.
CL210 – Data Structures and Algorithms Lab
The focus of this lab is to cover some of the commonly used data structures and algorithms in
software development. It is aimed at helping the students to understand the reasons for choosing
certain structures or algorithms while implementing them using Microsoft Visual Studio.
CLO
Statement ↓
Score →
Exemplary
(5)
Proficient
(4)
Developing
(3)
Beginning
(2)
Novice
(1)
2
Demonstrate
elementary skills Attempts all
to manage
lab tasks with
various data
correct output
structures.
Attempts
partial lab
tasks with
correct output
Attempts lab
tasks with
incorrect
output
Understand
the given
concept but
unable to
apply it
No attempt
3
Behaves
responsibly,
Completes
and individually the lab task on
performs the
his own
lab.
Completes
the lab task
with minor
help of
instructor
Completes
the lab task
with help of
instructor and
students
around
Poorly
attempts lab
tasks
Does not
work
CLO
MARKS OBTAINED
2
3
Instructor’s Signature: _______________________
Download