Professional Documents
Culture Documents
Tesfay C++ Assignment
Tesfay C++ Assignment
Assignment 1;
Computer science department,schoole of
computing
Name-tesfay haftu hailemichael
Id-ugr/170036/12
Section-1
1.Declare a pointer:
1. #include<iostream>
int main() {
int *agePtr;
agePtr = &age;
std::cout << "Address held by agePtr: " << agePtr << std::endl;
return 0;
2.#include <iostream>
int main()
// Assign the address of age to agePtr // Modify the value of age using pointer dereferencing
*agePtr= 35;
return 0;
#include <iostream>
cout << "Address of the first element: " << &numbers[0] << std::endl;
return 0;
2.1Array Name:
when you declare an array, the array name represents the address of the first element of the array.For
example, if you declare an array numbers, just using numbers without an index refers to the memory
address where the first element of the array is stored.
Each element in the array has a memory address associated with it. The address of the first element of
the array is the same as the address represented by the array name.Subsequent elements in the array
have memory addresses that are contiguous to each other in memory.Pointer Arithmetic:
Due to the relationship between the array name and its address, you can also use the array name as a
pointer to the first element of the array.This relationship allows you to perform pointer arithmetic with
array names, such as incrementing the array name to access the next element in the array.
3.#include <iostream>
int main() {
int numbers[5] = {10, 20, 30, 40, 50}; // Declare and initialize an integer array numbers of size 5
int *ptr = numbers; // Pointer pointing to the first element of the array
// Iterate through the array using a loop with the pointer variable
cout << "Element " << i << ": " << *ptr << endl;
ptr++;
Print the address of first element of numbers and the value stored in numbers[0]?
#include <iostream>
int main() {
int numbers[] = {10, 20, 30, 40, 50}; // Declare and initialize an integer array
int thirdElementArrayIndex = numbers[2]; // Access the third element using array indexing
Cout << "Third element using array indexing: " << thirdElementArrayIndex <<endl;
int *ptr = numbers; // Pointer pointing to the first element of the array
int thirdElementPointerArithmetic = *(ptr + 2); // Access the third element using pointer arithmetic
Cout << "Third element using pointer arithmetic: " << thirdElementPointerArithmetic <<endl;
return 0;
Pointer Basics:
A pointer is a variable that stores the memory address of another variable. Pointers are used to
indirectly access the value stored at a particular memory address.
Pointer Arithmetic:
In C and C++, you can perform arithmetic operations on pointers to navigate through memory locations.
When you perform arithmetic operations on a pointer, the compiler automatically scales the operation
based on the size of the data type the pointer is pointing to.
When you increment a pointer by 1, the pointer moves to the next memory location of the data type it
points to.
For example, if a pointer points to an integer, incrementing the pointer by 1 moves it to the next
integer-sized memory location.
You can add or subtract integers from a pointer. When you add an integer n to a pointer, it moves n
positions forward in memory.
Array Access:
Arrays and pointers are closely related in C and C++. When you use array indexing, you are essentially
performing pointer arithmetic.
Accessing array[i] is equivalent to *(array + i), where array is the base address of the array.
Pointer Comparison:
Pointers can also be compared using relational operators (<, >, ==, etc.). When comparing pointers, the
comparison is based on the memory addresses they point to.
#include<iostream>
*ptr1 = *ptr2;
*ptr2 = temp;
int main() {
swap(ptr1, ptr2);
Cout<<"after swapping" <<num1<<" "<<num2;
return 0;
2.#include <iostream>
*ptr1 = *ptr2;
*ptr2 = temp;}
int main()
swap(agePtr, anotherNumPtr);
return 0;}
3...#include <iostream>
if (size == 0) {
int max_index = 0;
max_index = i;
}
}
return &arr[max_index]; // Return a pointer to the element with the maximum value
int main() {
if (max_ptr != NULL) {
} else {
printf("Array is empty!\n");
return 0;