Objective:
- Pointers in C
- Reference Operator
- Dynamic Memory Allocation
Activity 1:
Given two integers x and y, find and print their sum using pointers.
Solution:
#include <stdio.h>
int main()
{
int x, y, sum = 0;
printf("Enter First Integer: \n");
scanf("%d", &x);
printf("Enter Second Integer: \n");
scanf("%d", &y);
int *p = &x;
int *q = &y;
sum = *p + *q;
printf("Sum = %d\n", sum);
return 0;
}
Activity 2:
A C program where you create an integer array of size 10. Your program will add 3 to each element of the array. You have to add to the elements using pointer only. Array subscript notation cannot be used (neither in addition nor while printing resultant array).
Note: *(ptr+i) is same as ptr[i]
Solution:
#include <stdio.h>
int main()
{
int arr[10];
printf("Enter 10 values for Array: \n");
for (int i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
int *p = arr;
printf("New Array after Addition of 3: \n");
for (int j = 0; j < 10; j++)
{
printf("%d\n", (*(p + j)) + 3);
}
return 0;
}
Activity 3:
- Write a function int* InputArray(int* size) that asks user to enter size of required array allocates the memory on heap, takes input in array and returns its pointer.
- Write a program void OutputArray(int* myArray, const int size) that takes a pointer to an integer array and prints its data.
- Implement a function int* Intersection(int* array1, int size1, int* array2, int size2, int* size3) that finds intersection (common elements) of two sets (stored using arrays).
Solution:
#include <stdio.h>
#include <stdlib.h>
int* InputArray(int* size)
{
printf("Enter the Size of the Array: \n");
scanf("%d", size);
int* array = (int*)malloc(*size * sizeof(int));
printf("Enter Values for the Array: \n");
for (int i = 0; i < *size; i++)
{
scanf("%d", &array[i]);
}
return array;
}
void OutputArray(int* array, const int size)
{
printf("\n");
printf(" { ");
for (int i = 0; i < size; i++)
{
printf("%d", array[i]);
if (i < size - 1)
{
printf(", ");
}
}
printf(" }\n");
}
int* Intersection(int* array1, int size1, int* array2, int size2, int* size3)
{
int* array = (int*)malloc((*size3) * sizeof(int));
int k = 0;
for (int i = 0; i < size1; i++)
{
for (int j = 0; j < size2; j++)
{
if (array1[i] == array2[j])
{
array[k] = array1[i];
k++;
}
}
}
*size3 = k;
return array;
}
int main()
{
int size1;
int size2;
int* array1;
int* array2;
printf("For Array 1: \n");
array1 = InputArray(&size1);
printf("For Array 2: \n");
array2 = InputArray(&size2);
printf("Array 1: \n");
OutputArray(array1, size1);
printf("Array 2: \n");
OutputArray(array2, size2);
int* intersection;
int size3 = size1 + size2;
intersection = Intersection(array1, size1, array2, size2, &size3);
printf("Intersection: \n");
OutputArray(intersection, size3);
free(array1);
free(array2);
free(intersection);
return 0;
}
Activity 4:
a) Write a function int** AllocateMemory(int* rows, int* cols) that takes size of matrix (rows and columns) from user, allocates memory for the matrix and return its pointer.
b) Write a function void InputMatrix(int*** matrix, const int rows, const int cols) which takes input the values in matrix from user(console).
c) Write a function void DisplayMatrix(int** matrix, const int rows, const int cols) that displays the matrix in proper format.
d) Write a function called maxRow that takes as parameters a pointer to a 2D array and its dimensions. It should return the largest element in each row of the array. Since there is more than one row in 2D array, you have to return a dynamic array that contains largest of each row.
For example, if the Sample Matrix is
1 4 8
9 1 6
5 7 2
Your function will return array containing maximum elements of all the columns i.e.
8, 9, 7
e) Write a function void DeallocateMemory(int** matrix, const int rows) that deallocates all the memory.
f) (Concatinate tables ) It takes two 2D arrays and gives a 2D array that is concation on y or x axis.
void concat(int** A, int** B, int row1, int column1, int row2, int column2) //axis 0 mean x axis 1 means Y axis.
Array 1
1,2,3
4,4,5
3,4,6
Array 2
5,8,9
4,9,4
4,6,0
If axis = 0 (x-axis)
Output
1,2,3,5,8,9
4,4,5,4,9,4
3,4,6,4,6,0
If axis = 1 (y-axis)
Output
1,2,3
4,4,5
3,4,6
5,8,9
4,9,4
4,6,0
Solution:
#include <stdio.h>
#include <stdlib.h>
int** AllocateMemory(int* rows, int* columns)
{
printf("Enter the Number of Rows: \n");
scanf("%d", rows);
printf("Enter the Number of Columns: \n");
scanf("%d", columns);
int** array = (int**)malloc((*rows) * sizeof(int*));
for (int i = 0; i < *rows; i++)
{
array[i] = (int*)malloc((*columns) * sizeof(int));
}
return array;
}
void InputMatrix(int*** Array2D, const int rows, const int columns)
{
printf("Enter Values for Matrix: \n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
printf("[%d][%d] = ", i, j);
scanf("%d", &(*Array2D)[i][j]);
}
}
}
void DisplayMatrix(int** Array2D, const int rows, const int columns)
{
printf("Given Matrix: \n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
printf("%d ", Array2D[i][j]);
}
printf("\n");
}
printf("\n");
}
int* maxRow(int** Array2D, const int rows, const int columns, int* size)
{
int* array = (int*)malloc(columns * sizeof(int));
for (int i = 0; i < rows; i++)
{
int largest = Array2D[i][0];
for (int j = 1; j < columns; j++)
{
if (largest < Array2D[i][j])
{
largest = Array2D[i][j];
}
}
array[i] = largest;
}
*size = rows;
return array;
}
void DeallocateMemory(int** Array2D, const int rows)
{
for (int i = 0; i < rows; i++)
{
free(Array2D[i]);
}
free(Array2D);
}
void concat(int** A, int** B, int row1, int column1, int row2, int column2)
{
int choice;
printf("Press 0: For x-axis\n");
printf("Press 1: For y-axis\n");
scanf("%d", &choice);
if (choice == 0)
{
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < column1; j++)
{
printf("%d ", A[i][j]);
}
for (int j = 0; j < column2; j++)
{
printf("%d ", B[i][j]);
}
printf("\n");
}
}
else if (choice == 1)
{
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < column1; j++)
{
printf("%d ", A[i][j]);
}
printf("\n");
}
for (int i = 0; i < row2; i++)
{
for (int j = 0; j < column2; j++)
{
printf("%d ", B[i][j]);
}
printf("\n");
}
}
}
int main()
{
int rows, columns;
int** Array2D;
Array2D = AllocateMemory(&rows, &columns);
InputMatrix(&Array2D, rows, columns);
DisplayMatrix(Array2D, rows, columns);
int size;
int* maxRowArray;
maxRowArray = maxRow(Array2D, rows, columns, &size);
printf("The Maximum Values in each Row are: \n");
for (int i = 0; i < size; i++)
{
printf("%d ", maxRowArray[i]);
}
printf("\n");
DeallocateMemory(Array2D, rows);
printf("\nConcatenate Table: \n");
int** A;
int** B;
int row1, row2, column1, column2;
printf("For A: \n");
A = AllocateMemory(&row1, &column1);
printf("For B: \n");
B = AllocateMemory(&row2, &column2);
printf("For A: \n");
InputMatrix(&A, row1, column1);
DisplayMatrix(A, row1, column1);
printf("\nFor B: \n");
InputMatrix(&B, row2, column2);
DisplayMatrix(B, row2, column2);
concat(A, B, row1, column1, row2, column2);
DeallocateMemory(A, row1);
DeallocateMemory(B, row2);
return 0;
}