0% found this document useful (0 votes)
4 views9 pages

Assignment 1

Uploaded by

nilgadhiya20
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
4 views9 pages

Assignment 1

Uploaded by

nilgadhiya20
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

DEEP KANANI Assignment 1 23SE02IE041

Aim 1 : Swap two numbers using pointers:

Input :

#include <stdio.h>

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

int main() {

int x = 5, y = 10;

printf("Before Swap: x = %d,y = %d\n", x, y);

swap(&x, &y);

printf("After Swap: x = %d, y = %d\n", x, y);

return 0;

Output :

Aim 2 : Find the sum of elements in an array using pointers:

Input :

#include <stdio.h>

int main() {

int arr[] = {1, 2, 3, 4, 5};


DEEP KANANI Assignment 1 23SE02IE041

int *ptr = arr;

int sum = 0;

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

sum += *ptr;

ptr++;

printf("Sum of elements: %d\n", sum); return 0;

}
Output :

Aim 3 : Print the address of a variable:

Input :

#include <stdio.h>

int main()

int x = 10;

printf("Address of x: %p\n", (void*)&x);

return 0;

Output :
DEEP KANANI Assignment 1 23SE02IE041

Aim 4 : Find the length of a string using pointers:

Input :

#include <stdio.h>

int stringLength(char *str) {

int length = 0;

while (*str != '\0') {

length++;

str++;

return length;

int main() {

char str[] = "Hello,World!";

printf("Length of the string: %d\n", stringLength(str));

return 0;

Output :

Aim 5 : Dynamic memory allocation using pointers:

Input :

#include <stdio.h>

#include <stdlib.h>

int main() {
DEEP KANANI Assignment 1 23SE02IE041

int *ptr;

ptr = (int *)malloc(sizeof(int));

if (ptr == NULL) {

printf("Memory allocation failed.\n");

return 1;

*ptr = 10;

printf("Value stored at dynamically allocated memory: %d\n", *ptr);

free(ptr);

return 0;

}
Output :

Aim 6 : Find the largest element in an array using pointers:

Input :

#include <stdio.h>

int findLargest(int *arr, int size) {

int max = *arr;

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

if (*(arr + i) > max) {

max = *(arr + i);

}
DEEP KANANI Assignment 1 23SE02IE041

return max;

int main() {

int arr[] = {5, 12, 3, 8, 15};

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

printf("Largest element in the array: %d\n", findLargest(arr, size));

return 0;

Output :

Aim 7 : Passing an array to a function using pointers:

Input :

#include <stdio>

void displayArray(int *arr, int size) {

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

printf("%d", *(arr + i));

printf("\n");

int main() {

int arr[] = {1, 2, 3, 4, 5};


DEEP KANANI Assignment 1 23SE02IE041

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

printf("Array elements:");

displayArray(arr, size);

return 0;

Output :

Aim 8 : Check if a number is even or odd using pointers:

Input :

#include <stdio.h>

int checkEvenOdd(int *num) {

if (*num % 2 == 0) {

return 1; // Even

} else {

return 0; // Odd

int main() {

int x = 7;

if (checkEvenOdd(&x)) {

printf("%d is even.\n", x);

} else {

printf("%d is odd.\n", x);

}
DEEP KANANI Assignment 1 23SE02IE041

return 0;

Output :

Aim 9 : Passing a pointer to a function:

Input :

#include <stdio.h>

void squareValue(int *num) {

*num = (*num) * (*num);

int main() {

int x = 5;

printf("Before: %d\n",x);

squareValue(&x);

printf("After: %d\n", x);

return 0;

Output :
DEEP KANANI Assignment 1 23SE02IE041

Aim 10 : Find the sum of two matrices using pointers:

Input :

#include <stdio.h>

void addMatrices(int *mat1, int *mat2, int *result, int rows, int cols) {

for (int i = 0; i < rows; ++i)

for (int j = 0; j< cols; ++j)

*(result + i * cols + j) = *(mat1 + i * cols + j) + *(mat2 + i * cols + j);

void displayMatrix(int *mat, int rows, int cols) {

for (int i = 0; i < rows; ++i)

for (int j = 0; j < cols; ++j) {

printf("%d ", *(mat + i * cols + j));

printf("\n");

int main() {

int mat1[2][2] = {{1, 2}, {3, 4}};

int mat2[2][2] = {{5, 6}, {7, 8}};

int result[2][2];
DEEP KANANI Assignment 1 23SE02IE041

int rows = 2, cols = 2;

addMatrices(&mat1[0][0], &mat2[0][0], &result[0][0], rows, cols);

printf("Matrix 1:\n");

displayMatrix(&mat1[0][0],rows, cols);

printf("\nMatrix 2:\n");

displayMatrix(&mat2[0][0],rows, cols);

printf("\nSum of Matrices:\n");

displayMatrix(&result[0][0],rows, cols);

return 0;

Output :

You might also like