Lab 01
Lab 01
Introduction to pointer:
The pointer in C++ language is a variable, it is also known as locator or indicator
that points to an address of a value. The symbol of an address is represented by a
pointer. In addition to creating and modifying dynamic data structures, they allow
programs to emulate call-by-reference. One of the principal applications of pointers
is iterating through the components of arrays or other data structures. The pointer
variable that refers to the same data type as the variable you're dealing with has the
address of that variable set to it (such as an int or string).
Example:
Output:
Working:
Structure
As structure is a group of data elements grouped together under one name. These data elements, known as
members, can have different types and different lengths. Data structures are declared in C++ using the
following syntax:
struct structure_name
{ member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3; . .
} object_names;
Example:
struct product
{
int weight;
float price;
};
product apple;
Access Structure Members:
To access members of a structure, use the dot syntax (.):
struct product
{
int weight;
float price;
};
product apple;
Cin>>apple.weight;
Cin>>apple.price;
TASK 3
Write a recursive program that takes an integer as an argument for example n. It should calculate
the sum of all natural numbers up to n. (3 Marks)
For example:
n=7 then it should calculate 1+2+3+4+5+6+7