0% found this document useful (0 votes)
12 views18 pages

C Programs for Pointers and Arrays

The document contains a series of C programming tasks focused on pointers, arrays, and dynamic memory allocation. It includes code examples for printing arrays using pointers, managing student names with a menu-driven interface, performing dynamic array operations, and adding two matrices using pointers. Each section provides detailed function implementations and user interactions for various operations.

Uploaded by

5485kartheekssk
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)
12 views18 pages

C Programs for Pointers and Arrays

The document contains a series of C programming tasks focused on pointers, arrays, and dynamic memory allocation. It includes code examples for printing arrays using pointers, managing student names with a menu-driven interface, performing dynamic array operations, and adding two matrices using pointers. Each section provides detailed function implementations and user interactions for various operations.

Uploaded by

5485kartheekssk
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

Week-9

a) Write a C program to demonstrate pointers and arrays (print array using


pointers).
b) Write a menu driven with options(using Two-dimensional character arrays
and functions)
a)To insert a student name
b)To delete a name
c) To sort names in alphabetical order
d)To print list of names
c) Write a C program to demonstrate dynamic memory allocations functions
with pointers.
a)To insert an element into array
b)to delete an element
c)To print elements
d)To remove duplicates
d) Write a C program to perform addition of two matrices using pointers.

a) Write a C program to demonstrate pointers and arrays (print array


using pointers).

#include <stdio.h>

main()

int arr[5], i;

int *ptr;

// Reading array elements

printf("Enter 5 integers:\n");

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

scanf("%d", &arr[i]);

}
// Pointer to the first element of array

ptr = arr; // same as ptr = &arr[0];

// Printing array elements using pointer

printf("\nArray elements using pointer:\n");

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

printf("%d ", *(ptr + i)); // accessing using pointer arithmetic

b) Write a menu driven with options(using Two-dimensional character


arrays and functions)
a)To insert a student name
b)To delete a name
c) To sort names in alphabetical order
d)To print list of names

#include <stdio.h>

#include <string.h>

#define MAX 100 // maximum number of names

#define LEN 50 // maximum characters per name

// Function prototypes

void insertName(char names[][LEN], int *count);

void deleteName(char names[][LEN], int *count);

void sortNames(char names[][LEN], int count);

void printNames(char names[][LEN], int count);


main()

char names[MAX][LEN];

int count = 0;

int choice;

do {

printf("\n===== Student Name Management =====\n");

printf("1. Insert a student name\n");

printf("2. Delete a name\n");

printf("3. Sort names alphabetically\n");

printf("4. Print list of names\n");

printf("5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

getchar(); // to consume newline from input buffer

switch (choice)

case 1:

insertName(names, &count);

break;

case 2:

deleteName(names, &count);
break;

case 3:

sortNames(names, count);

printf("Names sorted successfully.\n");

break;

case 4:

printNames(names, count);

break;

case 5:

printf("Exiting program...\n");

break;

default:

printf("Invalid choice! Try again.\n");

} while (choice != 5);

// ================= INSERT NAME =================

void insertName(char names[][LEN], int *count)

if (*count >= MAX)

{
printf("List is full! Cannot insert more names.\n");

return;

printf("Enter student name: ");

fgets(names[*count], LEN, stdin);

names[*count][strcspn(names[*count], "\n")] = '\0'; // remove newline

(*count)++;

printf("Name inserted successfully.\n");

// ================= DELETE NAME =================

void deleteName(char names[][LEN], int *count)

if (*count == 0)

printf("List is empty! No names to delete.\n");

return;

char delName[LEN];

printf("Enter name to delete: ");

fgets(delName, LEN, stdin);

delName[strcspn(delName, "\n")] = '\0';

int i, found = 0;
for (i = 0; i < *count; i++)

if (strcmp(names[i], delName) == 0) {

found = 1;

int j;

for (j = i; j < *count - 1; j++) {

strcpy(names[j], names[j + 1]);

(*count)--;

printf("Name deleted successfully.\n");

break;

if (!found)

printf("Name not found in the list.\n");

// ================= SORT NAMES =================

void sortNames(char names[][LEN], int count)

int i, j;

char temp[LEN];

for (i = 0; i < count - 1; i++) {

for (j = i + 1; j < count; j++) {


if (strcmp(names[i], names[j]) > 0) {

strcpy(temp, names[i]);

strcpy(names[i], names[j]);

strcpy(names[j], temp);

// ================= PRINT NAMES =================

void printNames(char names[][LEN], int count) {

if (count == 0) {

printf("No names to display.\n");

return;

printf("\nList of Student Names:\n");

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

printf("%d. %s\n", i + 1, names[i]);

}
c) Write a C program to demonstrate dynamic memory allocations
functions with pointers.
a)To insert an element into array
b)to delete an element
c)To print elements
d)To remove duplicates

#include <stdio.h>

#include <stdlib.h>

// Function prototypes

int* insertElement(int *arr, int *size, int value);

int* deleteElement(int *arr, int *size, int value);

void printArray(int *arr, int size);

int* removeDuplicates(int *arr, int *size);

int main() {

int *arr = NULL;

int size = 0;

int choice, val;

do {

printf("\n=== Dynamic Array Operations ===\n");

printf("1. Insert element\n");

printf("2. Delete element (by value)\n");


printf("3. Print elements\n");

printf("4. Remove duplicates\n");

printf("5. Exit\n");

printf("Enter your choice: ");

if (scanf("%d", &choice) != 1) {

// invalid input

printf("Invalid input. Exiting.\n");

break;

switch (choice) {

case 1:

printf("Enter value to insert: ");

scanf("%d", &val);

arr = insertElement(arr, &size, val);

break;

case 2:

if (size == 0) {

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

} else {

printf("Enter value to delete: ");

scanf("%d", &val);

arr = deleteElement(arr, &size, val);


}

break;

case 3:

printArray(arr, size);

break;

case 4:

if (size == 0) {

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

} else {

arr = removeDuplicates(arr, &size);

printf("Duplicates removed (if any).\n");

break;

case 5:

printf("Exiting. Freeing memory.\n");

break;

default:

printf("Invalid choice! Try again.\n");

} while (choice != 5);

free(arr);
return 0;

// ------------------ INSERT ------------------

// Adds a new value at the end of the dynamic array

int* insertElement(int *arr, int *size, int value) {

int newSize = *size + 1;

int *temp = realloc(arr, newSize * sizeof(int));

if (temp == NULL) {

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

return arr; // return old pointer (though memory might be invalid) -- but
simple handling

arr = temp;

arr[newSize - 1] = value;

*size = newSize;

printf("Inserted %d. New size = %d\n", value, *size);

return arr;

// ------------------ DELETE ------------------

// Deletes first occurrence of value (if found), shifts the remaining, shrinks
memory
int* deleteElement(int *arr, int *size, int value) {

int i, pos = -1;

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

if (arr[i] == value) {

pos = i;

break;

if (pos == -1) {

printf("Value %d not found in array.\n", value);

return arr;

// shift elements left from pos

for (i = pos; i < *size - 1; i++) {

arr[i] = arr[i + 1];

*size = *size - 1;

if (*size == 0) {

free(arr);

printf("Deleted %d. Array is now empty.\n", value);

return NULL;
} else {

int *temp = realloc(arr, (*size) * sizeof(int));

if (temp == NULL) {

printf("Memory reallocation failed after deletion!\n");

// we still retain old array pointer

printf("Deleted %d. Size now = %d (memory not shrunk)\n", value,


*size);

return arr;

arr = temp;

printf("Deleted %d. New size = %d\n", value, *size);

return arr;

// ------------------ PRINT ------------------

void printArray(int *arr, int size) {

if (size == 0) {

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

return;

printf("Array elements (%d): ", size);

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


printf("%d ", arr[i]);

printf("\n");

// ------------------ REMOVE DUPLICATES ------------------

// Removes duplicate values in the array — keeps first occurrence

int* removeDuplicates(int *arr, int *size) {

if (*size <= 1) return arr;

int i, j, k;

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

j = i + 1;

while (j < *size) {

if (arr[i] == arr[j]) {

// shift left from j

for (k = j; k < *size - 1; k++) {

arr[k] = arr[k + 1];

(*size)--;

// we intentionally do not increment j, because new arr[j] needs


checking

} else {
j++;

if (*size == 0) {

free(arr);

return NULL;

int *temp = realloc(arr, (*size) * sizeof(int));

if (temp == NULL)

printf("Memory reallocation failed after removing duplicates!\n");

return arr;

arr = temp;

return arr;

}
d) Write a C program to perform addition of two matrices using
pointers.

#include <stdio.h>

#include <stdlib.h>

main()

int r, c;

int i, j;

printf("Enter number of rows: ");

scanf("%d", &r);

printf("Enter number of columns: ");

scanf("%d", &c);

// Allocate memory for matrices

int *A = (int *)malloc(r * c * sizeof(int));

int *B = (int *)malloc(r * c * sizeof(int));

int *C = (int *)malloc(r * c * sizeof(int));

if (A == NULL || B == NULL || C == NULL) {

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

return 1;

// Input for Matrix A


printf("\nEnter elements of Matrix A:\n");

for (i = 0; i < r; i++) {

for (j = 0; j < c; j++) {

scanf("%d", (A + i * c + j)); // pointer access

// Input for Matrix B

printf("\nEnter elements of Matrix B:\n");

for (i = 0; i < r; i++) {

for (j = 0; j < c; j++) {

scanf("%d", (B + i * c + j));

// Matrix Addition: C = A + B

for (i = 0; i < r; i++) {

for (j = 0; j < c; j++) {

*(C + i * c + j) = *(A + i * c + j) + *(B + i * c + j);

// Print Result

printf("\nResultant Matrix (A + B):\n");

for (i = 0; i < r; i++) {


for (j = 0; j < c; j++) {

printf("%d ", *(C + i * c + j));

printf("\n");

// Free allocated memory

free(A);

free(B);

free(C);

You might also like