0% found this document useful (0 votes)
3 views

pointers sample code

The document provides a series of exercises focused on the use of pointers in programming. It includes pseudocode for various tasks such as declaring variables and pointers, traversing arrays, counting vowels and consonants in a string, swapping values using pointers, and dynamically allocating memory for an array. Each exercise is structured with a clear pseudocode outline to guide implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

pointers sample code

The document provides a series of exercises focused on the use of pointers in programming. It includes pseudocode for various tasks such as declaring variables and pointers, traversing arrays, counting vowels and consonants in a string, swapping values using pointers, and dynamically allocating memory for an array. Each exercise is structured with a clear pseudocode outline to guide implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

EXERCISE 12 - POINTERS

1. Write a program that declares an integer variable and a pointer to that integer. Initialize the integer with a value, assign
its address to the pointer, and print:
The value of the integer using the pointer

The address of the integer using the pointer.

The value of the pointer itself.

PSEUDO CODE:

BEGIN

// Step 1: Declare and initialize variable


DECLARE INTEGER value
ASSIGN value = 10

// Step 2: Declare pointer


DECLARE POINTER TO INTEGER ptr1

// Step 3: Assign the address of `value` to `ptr1`


ASSIGN ptr1 = ADDRESS OF value

// Step 4: Output the value using the variable


PRINT "The value using variable is: ", value

// Step 5: Output the value using the pointer


PRINT "The value using pointer is: ", DEREFERENCE ptr1

// Step 6: Output the address of `value` using the pointer


PRINT "The address using pointer is: ", ptr1

END

SOURCE CODE:
2. Create a program that declares an array of 5 integers and initializes it. Use a pointer to traverse the array and print each
element.

PSEUDOCODE:
BEGIN

// Step 1: Declare and initialize an array


DECLARE INTEGER ARRAY arr1[5]
INITIALIZE arr1 WITH VALUES {1, 2, 3, 4, 5}

// Step 2: Declare a pointer and assign it to the array


DECLARE POINTER TO INTEGER ptr1
ASSIGN ptr1 = ADDRESS OF arr1[0]

// Step 3: Loop through the array elements


FOR i FROM 0 TO 4 DO
PRINT "The element ", i, " is: ", DEREFERENCE (ptr1 + i)
END FOR

END

SOURCE CODE:
3. Write a program that counts the number of vowels and consonants in a string entered by the user. Use a pointer to
traverse the string and check each character.

Note: You must use the following


functions

·
void countVowelsAndConsonants(char *str, int
*vowelCount, int *consonantCount)

·
int isVowel(char c)// to find if it is vowel or
not

·
isalpha() //library function

PSEUDOCODE:

BEGIN

// Function isVowel to count vowels and consonants


FUNCTION isVowel(str1)

DECLARE POINTER TO CHAR ptr1


ASSIGN ptr1 = ADDRESS OF str1[0]

DECLARE INTEGER vcount, ccount, len1, len2


INITIALIZE vcount = 0
INITIALIZE ccount = 0
INITIALIZE len1 = LENGTH OF str1
INITIALIZE len2 = 0

// Step 1: Count alphabetical characters in str1


FOR EACH CHARACTER str1[i] UNTIL str1[i] IS NULL
IF str1[i] IS AN ALPHABET CHARACTER
INCREMENT len2
END IF
END FOR

// Step 2: Check if all characters are alphabetical


IF len1 IS EQUAL TO len2 THEN
// Step 3: Count vowels and consonants
FOR EACH CHARACTER str1[i] UNTIL str1[i] IS NULL
IF *(ptr1 + i) IS A VOWEL (CASE INSENSITIVE)
INCREMENT vcount
ELSE
INCREMENT ccount
END IF
END FOR

// Step 4: Display counts


PRINT "The number of vowels is: ", vcount
PRINT "The number of consonants is: ", ccount

ELSE
PRINT "Enter a proper alphabetical string."
END IF
END FUNCTION

// Main function
BEGIN MAIN

DECLARE STRING str OF SIZE 1000


PRINT "Enter the string: "
READ str
CALL isVowel(str)

END MAIN
END

SOURCE CODE:
4. Write a function void swap(int *a, int *b) that swaps the values of two integers using pointers. Create a main() function to
test your swap() function by passing the addresses of two integer variables.

PSEUDO CODE:

BEGIN

// Function to swap two integers


FUNCTION swap(a, b)
DECLARE INTEGER temp
ASSIGN temp = VALUE POINTED TO BY a
ASSIGN VALUE POINTED TO BY a = VALUE POINTED TO BY b
ASSIGN VALUE POINTED TO BY b = temp
PRINT "The elements after swapping are ", VALUE POINTED TO BY a, VALUE POINTED TO BY b
END FUNCTION

// Main function
BEGIN MAIN
DECLARE INTEGER x, y
INITIALIZE x = 10
INITIALIZE y = 20

DECLARE POINTER TO INTEGER ptr1, ptr2


ASSIGN ptr1 = ADDRESS OF x
ASSIGN ptr2 = ADDRESS OF y

CALL swap(ptr1, ptr2)

END MAIN

END

SOURCE CODE:
5. Write a program that allocates memory dynamically for an array of n integers using malloc(). Populate the array with
values from user input and then print the array. Finally, free the allocated
memory.

PESUDO CODE:
BEGIN

// Step 1: Declare variable for array size


DECLARE INTEGER n

// Step 2: Get the number of integers from the user


PRINT "Enter the number of integers: "
READ n

// Step 3: Dynamically allocate memory for an array of size n


DECLARE POINTER TO INTEGER arr
ASSIGN arr = ALLOCATE MEMORY FOR n INTEGERS

// Step 4: Prompt user to enter elements


PRINT "Enter ", n, " integers:"

// Step 5: Loop to read n integers into the array


FOR i FROM 0 TO n - 1 DO
READ arr[i]
END FOR

// Step 6: Display the elements of the array


PRINT "The elements of the array are:"
FOR i FROM 0 TO n - 1 DO
PRINT arr[i], " "
END FOR
PRINT NEWLINE

// Step 7: Free the allocated memory


FREE arr
END

SOURCE CODE:

You might also like