0% found this document useful (0 votes)
3 views10 pages

Dsa 2

The document contains a series of C programming tasks that involve using pointers for various operations, such as storing and displaying variable values, finding the largest and smallest elements in an array, reversing an array, counting vowels and consonants in a string, and performing matrix addition. Each task includes a code snippet demonstrating the implementation of the required functionality. The document serves as a practical guide for learning pointer manipulation in C.

Uploaded by

manukushwah048
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Dsa 2

The document contains a series of C programming tasks that involve using pointers for various operations, such as storing and displaying variable values, finding the largest and smallest elements in an array, reversing an array, counting vowels and consonants in a string, and performing matrix addition. Each task includes a code snippet demonstrating the implementation of the required functionality. The document serves as a practical guide for learning pointer manipulation in C.

Uploaded by

manukushwah048
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

‭ .

Write a program to store the address of an integer variable in a pointer and display the‬
1
‭value of the variable using the pointer. Demonstrate the difference between the * operator‬
‭(dereferencing) and the & operator (address-of) with a suitable example.‬

‭#include <stdio.h>‬

‭int main() {‬
‭int num = 25;‬
‭int *ptr = &num;‬
‭printf("Value of num: %d\n", num);‬
‭printf("Address of num (using & operator): %p\n", &num);‬
‭printf("Address stored in ptr (using ptr): %p\n", ptr);‬
‭printf("Value of num (using * operator): %d\n", *ptr);‬
‭return 0;‬
‭}‬

‭ . Implement a function that accepts an array and its size as parameters and finds the largest‬
2
‭and smallest elements using pointers.‬

‭ include <stdio.h>‬
#
‭void findLargestAndSmallest(int *arr, int size, int *largest, int *smallest) {‬
‭*largest = *arr;‬
‭*smallest = *arr;‬
‭for (int i = 1; i < size; i++) {‬
‭if (*(arr + i) > *largest) {‬
‭*largest = *(arr + i);‬
‭}‬
‭if (*(arr + i) < *smallest) {‬
‭*smallest = *(arr + i);‬
‭}}}‬
‭int main() {‬
‭int arr[] = {34, 12, 9, 56, 21, 77, 3, 19};‬
‭int size = sizeof(arr) / sizeof(arr[0]);‬
‭int largest, smallest;‬
f‭indLargestAndSmallest(arr, size, &largest, &smallest);‬
‭printf("Largest element: %d\n", largest);‬
‭printf("Smallest element: %d\n", smallest);‬
‭return 0;‬
‭}‬

‭3. Write a program to find the second largest element in an array.‬

‭ include <stdio.h>‬
#
‭int findSecondLargest(int arr[], int size) {‬
‭int largest, secondLargest;‬
‭if (size < 2) {‬
‭printf("Array size should be at least 2.\n");‬
‭return -1;‬
‭}‬
‭largest = secondLargest = arr[0];‬
‭for (int i = 1; i < size; i++) {‬
‭if (arr[i] > largest) {‬
‭secondLargest = largest;‬
‭largest = arr[i];‬
‭} else if (arr[i] > secondLargest && arr[i] != largest) {‬
‭secondLargest = arr[i];‬
‭}}‬
‭return secondLargest;‬
‭}‬
‭int main() {‬
‭int size;‬
‭printf("Enter the size of the array: ");‬
‭scanf("%d", &size);‬
‭int arr[size];‬
‭printf("Enter %d elements:\n", size);‬
‭for (int i = 0; i < size; i++) {‬
‭scanf("%d", &arr[i]);‬
‭}‬
‭int secondLargest = findSecondLargest(arr, size);‬
‭if (secondLargest != -1) {‬
‭printf("The second largest element is: %d\n", secondLargest);‬
}‭ ‬
‭return 0;‬
‭}‬

‭4. Write a program to find the sum of elements in an array using pointers.‬

‭ include <stdio.h>‬
#
‭int sumOfElements(int *arr, int size) {‬
‭int sum = 0;‬
‭for (int i = 0; i < size; i++) {‬
‭sum += *(arr + i);‬
‭}‬
‭return sum;‬
‭}‬
‭int main() {‬
‭int size;‬
‭printf("Enter the size of the array: ");‬
‭scanf("%d", &size);‬
‭int arr[size];‬
‭printf("Enter %d elements:\n", size);‬
‭for (int i = 0; i < size; i++) {‬
‭scanf("%d", &arr[i]);‬
‭}‬
‭int sum = sumOfElements(arr, size);‬
‭printf("The sum of the elements in the array is: %d\n", sum);‬
‭return 0;‬
‭}‬
‭5. Write a program to copy one string to another using pointers.‬

‭ include <stdio.h>‬
#
‭void copyString(char *source, char *destination) {‬
‭while (*source != '\0') {‬
‭*destination = *source;‬
‭source++;‬
‭destination++;‬
‭}‬
‭*destination = '\0';‬
‭}‬
‭int main() {‬
‭char source[100], destination[100];‬
‭printf("Enter a string: ");‬
‭fgets(source, sizeof(source), stdin);‬
‭copyString(source, destination);‬
‭printf("The copied string is: %s\n", destination);‬
‭return 0;‬
‭}‬

‭6. Implement a program to reverse an array using pointers.‬

‭ include <stdio.h>‬
#
‭void reverseArray(int *arr, int size) {‬
‭int *start = arr;‬
‭int *end = arr + size - 1;‬
‭int temp;‬
‭while (start < end) {‬
‭temp = *start;‬
‭*start = *end;‬
‭*end = temp;‬
‭start++;‬
‭end--;‬
‭}}‬
‭int main() {‬
‭int size;‬
‭printf("Enter the size of the array: ");‬
‭scanf("%d", &size);‬
‭int arr[size];‬
‭ rintf("Enter %d elements:\n", size);‬
p
‭for (int i = 0; i < size; i++) {‬
‭scanf("%d", &arr[i]);‬
‭}‬
‭reverseArray(arr, size);‬
‭printf("Reversed array: ");‬
‭for (int i = 0; i < size; i++) {‬
‭printf("%d ", arr[i]);‬
‭}‬
‭printf("\n");‬
‭return 0;‬
‭}‬

‭7. Write a program to count vowels and consonants in a string using pointers.‬

‭ include <stdio.h>‬
#
‭#include <ctype.h>‬
‭void countVowelsAndConsonants(char *str, int *vowelCount, int *consonantCount) {‬
‭*vowelCount = 0;‬
‭*consonantCount = 0;‬
‭while (*str != '\0') {‬
‭char ch = tolower(*str);‬
‭if (ch >= 'a' && ch <= 'z') {‬
‭if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {‬
‭(*vowelCount)++;‬
‭} else {‬
‭(*consonantCount)++;‬
‭}}‬
‭str++;‬
‭}}‬
‭int main() {‬
‭char str[100];‬
‭int vowels, consonants;‬
‭printf("Enter a string: ");‬
‭fgets(str, sizeof(str), stdin);‬
‭countVowelsAndConsonants(str, &vowels, &consonants);‬
‭ rintf("Number of vowels: %d\n", vowels);‬
p
‭printf("Number of consonants: %d\n", consonants);‬
‭return 0;‬
‭}‬

‭ . Write a program to find the length of a string without using the strlen function. Use pointers‬
8
‭to traverse the string.‬

‭ include <stdio.h>‬
#
‭int stringLength(char *str) {‬
‭int length = 0;‬
‭while (*str != '\0') {‬
‭length++;‬
‭str++;‬
‭}‬
‭return length;‬
‭}‬
‭int main() {‬
‭char str[100];‬
‭printf("Enter a string: ");‬
‭fgets(str, sizeof(str), stdin);‬
‭int length = stringLength(str);‬
‭printf("The length of the string is: %d\n", length);‬
‭return 0;‬
‭}‬

‭9. Write a program to check whether a given number is a palindrome.‬

‭ include <stdio.h>‬
#
‭int isPalindrome(int num) {‬
‭int originalNum = num;‬
‭int reversedNum = 0;‬
‭int remainder;‬
‭while (num != 0) {‬
‭remainder = num % 10;‬
‭reversedNum = reversedNum * 10 + remainder;‬
‭num /= 10;‬
‭}‬
‭if (originalNum == reversedNum) {‬
‭return 1;‬
‭} else {‬
‭return 0;‬
‭}}‬
‭int main() {‬
‭int num;‬
‭printf("Enter a number: ");‬
‭scanf("%d", &num);‬
‭if (isPalindrome(num)) {‬
‭printf("%d is a palindrome.\n", num);‬
‭} else {‬
‭printf("%d is not a palindrome.\n", num);‬
‭}‬
‭return 0;‬
‭}‬

‭10. Create a program to perform the addition of two matrices using pointers.‬

‭ include <stdio.h>‬
#
‭#define ROWS 3‬
‭#define COLS 3‬
‭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[ROWS][COLS], mat2[ROWS][COLS], result[ROWS][COLS];‬
‭printf("Enter elements of matrix 1:\n");‬
‭for (int i = 0; i < ROWS; i++) {‬
‭for (int j = 0; j < COLS; j++) {‬
‭scanf("%d", &mat1[i][j]);‬
‭}}‬
‭printf("Enter elements of matrix 2:\n");‬
‭for (int i = 0; i < ROWS; i++) {‬
‭for (int j = 0; j < COLS; j++) {‬
‭scanf("%d", &mat2[i][j]);‬
‭}}‬
‭addMatrices(&mat1[0][0], &mat2[0][0], &result[0][0], ROWS, COLS);‬
‭printf("The result of matrix addition is:\n");‬
‭displayMatrix(&result[0][0], ROWS, COLS);‬
‭return 0;‬
‭}‬

‭11. Write a program to concatenate two strings using pointers.‬

‭ include <stdio.h>‬
#
‭void concatenateStrings(char *str1, char *str2) {‬
‭while (*str1 != '\0') {‬
‭str1++;‬
‭}‬
‭while (*str2 != '\0') {‬
‭*str1 = *str2;‬
‭str1++;‬
‭str2++;‬
}‭ ‬
‭*str1 = '\0';‬
}‭ ‬
‭int main() {‬
‭char str1[100], str2[100];‬

‭ rintf("Enter the first string: ");‬


p
‭fgets(str1, sizeof(str1), stdin);‬

‭ rintf("Enter the second string: ");‬


p
‭fgets(str2, sizeof(str2), stdin);‬
‭str1[strcspn(str1, "\n")] = 0;‬
‭str2[strcspn(str2, "\n")] = 0;‬
‭concatenateStrings(str1, str2);‬
‭printf("The concatenated string is: %s\n", str1);‬
‭return 0;‬
‭}‬

‭12. Create a program to find the intersection of two arrays using pointers.‬

‭ include <stdio.h>‬
#
‭void findIntersection(int *arr1, int *arr2, int *result, int size1, int size2) {‬
‭int k = 0;‬
‭for (int i = 0; i < size1; i++) {‬
‭for (int j = 0; j < size2; j++) {‬
‭if (*(arr1 + i) == *(arr2 + j)) {‬
‭int isDuplicate = 0;‬
‭for (int l = 0; l < k; l++) {‬
‭if (*(result + l) == *(arr1 + i)) {‬
‭isDuplicate = 1;‬
‭break;‬
‭}}‬
‭if (!isDuplicate) {‬
‭*(result + k) = *(arr1 + i);‬
‭k++;‬
‭}}}}‬
‭result[k] = -1;‬
}‭ ‬
‭void displayArray(int *arr) {‬
‭int i = 0;‬
‭while (*(arr + i) != -1) {‬
‭printf("%d ", *(arr + i));‬
‭i++;‬
‭}‬
‭printf("\n");‬
‭}‬
‭int main() {‬
‭int arr1[] = {1, 2, 3, 4, 5};‬
‭int arr2[] = {3, 4, 5, 6, 7};‬
‭int result[5];‬
‭findIntersection(arr1, arr2, result, 5, 5);‬
‭printf("Intersection of the two arrays: ");‬
‭displayArray(result);‬
‭return 0;‬
‭}‬

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy