Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Fandamentals of programing 2

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>

Using namespace std;

int main() {

// Declare an integer variable age

int age = 25;

// Declare an integer pointer agePtr

int *agePtr;

// Assign the address of agePtr to the integer variable age

agePtr = &age;

// Output the value of age

std::cout << "Value of age: " << age << std::endl;

// Output the address held by agePtr

std::cout << "Address held by agePtr: " << agePtr << std::endl;

return 0;

2.#include <iostream>

int main()

int age = 30;

int *agePtr = &age;

// Assign the address of age to agePtr // Modify the value of age using pointer dereferencing
*agePtr= 35;

// Print the updated value of age

cout << "Updated age: " << age << endl;

return 0;

#include <iostream>

using namespace std;


int main() {

int numbers[5]; // Declare an integer array of size 5

// Print the address of the first element of the numbers array

cout << "Address of the first element: " << &numbers[0] << std::endl;

// Print the value stored in numbers[0]

cout << "Value at numbers[0]: " << 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.

Address of Array Elements:

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

for (int i = 0; i < 5; i++) {

cout << "Element " << i << ": " << *ptr << endl;

ptr++;

// Move the pointer to the next element } return 0;}

1.array and pointer: declare an integer array numbers of size 5?

Print the address of first element of numbers and the value stored in numbers[0]?
#include <iostream>

Using namespace std;

int main() {

int numbers[] = {10, 20, 30, 40, 50}; // Declare and initialize an integer array

// Using Array Indexing

int thirdElementArrayIndex = numbers[2]; // Access the third element using array indexing

Cout << "Third element using array indexing: " << thirdElementArrayIndex <<endl;

// Using Pointer Arithmetic

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;

the concept of pointer arithmetic:

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.

Increment and Decrement:

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.

Addition and Subtraction:

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.

Similarly, subtracting an integer n from a pointer moves it n positions backward 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>

using namespace std;

void swap(int *ptr1, int *ptr2) {

int temp = *ptr1;

*ptr1 = *ptr2;

*ptr2 = temp;

int main() {

int num1 = 10;

int num2 = 20;

int *ptr1 = &num1;

int *ptr2 = &num2;

Cout<<Before swapping"<<num1<<" "<<num2;

swap(ptr1, ptr2);
Cout<<"after swapping" <<num1<<" "<<num2;

return 0;

2.#include <iostream>

Using namespace std;

swap(int *ptr1, int *ptr2) {

int temp = *ptr1;

*ptr1 = *ptr2;

*ptr2 = temp;}

int main()

{ int age = 30;

int anotherNum = 50;

int *agePtr = &age;

int *anotherNumPtr = &anotherNum;

Cout<<"before swapping"<< age<<" "<<anotherNum;

swap(agePtr, anotherNumPtr);

Cout<<"After swapping"<<age<<" "<<anotherNum;

return 0;}

3...#include <iostream>

Using namespace std;

int* find_max(int arr[], int size) {

if (size == 0) {

return NULL; // Return NULL if the array is empty

int max_index = 0;

for (int i = 1; i < size; i++) {

if (arr[i] > arr[max_index]) {

max_index = i;

}
}

return &arr[max_index]; // Return a pointer to the element with the maximum value

int main() {

int arr[] = {5, 2, 8, 10, 1};

int size = sizeof(arr) / sizeof(arr[0]);

// Call the find_max function

int* max_ptr = find_max(arr, size);

if (max_ptr != NULL) {

Cout<<"Maximum element in the array<<*max_ptr;

} else {

printf("Array is empty!\n");

return 0;

You might also like