Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

13. Examples on arrays and pointers

Arrays

Uploaded by

Kiran Mamnani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

13. Examples on arrays and pointers

Arrays

Uploaded by

Kiran Mamnani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

C Array and Pointer Examples

Program to Add Two Matrices


#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);

printf("\nEnter elements of 1st matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}

printf("Enter elements of 2nd matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}

// adding two matrices


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}

// printing the result


printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}

return 0;
}

Output

Enter the number of rows (between 1 and 100): 2


Enter the number of columns (between 1 and 100): 3

Enter elements of 1st matrix:


Enter element a11: 2
Enter element a12: 3
Enter element a13: 4
Enter element a21: 5
Enter element a22: 2
Enter element a23: 3
Enter elements of 2nd matrix:
Enter element a11: -4
Enter element a12: 5
Enter element a13: 3
Enter element a21: 5
Enter element a22: 6
Enter element a23: 3

Sum of two matrices:


-2 8 7

10 8 6

In this program, the user is asked to enter the number of rows r and columns c. Then, the user is
asked to enter the elements of the two matrices (of order r*c).

We then added corresponding elements of two matrices and saved it in another matrix (two-
dimensional array). Finally, the result is printed on the screen.

C Program to Multiply Two Matrices Using Multi-dimensional Arrays


This program asks the user to enter the size (rows and columns) of two matrices.

To multiply two matrices, the number of columns of the first matrix should be equal to the
number of rows of the second matrix.

The program below asks for the number of rows and columns of two matrices until the above
condition is satisfied.

Then, the multiplication of two matrices is performed, and the result is displayed on the screen.

To perform this, we have created three functions:

• enterData() - to take matrix elements from the user.


• multiplyMatrices() - to multiply two matrices.
• display() - to display the resultant matrix after multiplication.

Multiply Matrices by Passing it to a Function


#include <stdio.h>
void enterData(int first[][10], int second[][10], int r1, int c1, int r2, int
c2);
void multiplyMatrices(int first[][10], int second[][10], int
multResult[][10], int r1, int c1, int r2, int c2);
void display(int mult[][10], int r1, int c2);

int main() {
int first[10][10], second[10][10], mult[10][10], r1, c1, r2, c2;
printf("Enter rows and column for the first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for the second matrix: ");
scanf("%d %d", &r2, &c2);

// Taking input until columns of the first matrix is equal to the rows of
the second matrix
while (c1 != r2) {
printf("Error! Enter rows and columns again.\n");
printf("Enter rows and columns for the first matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and columns for the second matrix: ");
scanf("%d%d", &r2, &c2);
}

// Function to take matrices data


enterData(first, second, r1, c1, r2, c2);

// Function to multiply two matrices.


multiplyMatrices(first, second, mult, r1, c1, r2, c2);

// Function to display resultant matrix after multiplication.


display(mult, r1, c2);

return 0;
}

void enterData(int first[][10], int second[][10], int r1, int c1, int r2, int
c2) {
printf("\nEnter elements of matrix 1:\n");

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


for (int j = 0; j < c1; ++j) {
printf("Enter a%d%d: ", i + 1, j + 1);
scanf("%d", &first[i][j]);
}
}
printf("\nEnter elements of matrix 2:\n");

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


for (int j = 0; j < c2; ++j) {
printf("Enter b%d%d: ", i + 1, j + 1);
scanf("%d", &second[i][j]);
}
}
}

void multiplyMatrices(int first[][10], int second[][10], int mult[][10], int


r1, int c1, int r2, int c2) {

// Initializing elements of matrix mult to 0.


for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
mult[i][j] = 0;
}
}
// Multiplying first and second matrices and storing in mult.
for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
for (int k = 0; k < c1; ++k) {
mult[i][j] += first[i][k] * second[k][j];
}
}
}
}

void display(int mult[][10], int r1, int c2) {

printf("\nOutput Matrix:\n");
for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
printf("%d ", mult[i][j]);
if (j == c2 - 1)
printf("\n");
}
}
}

Output

Enter rows and column for the first matrix: 2


3
Enter rows and column for the second matrix: 3
2

Enter elements of matrix 1:


Enter a11: 3
Enter a12: -2
Enter a13: 5
Enter a21: 3
Enter a22: 0
Enter a23: 4

Enter elements of matrix 2:


Enter b11: 2
Enter b12: 3
Enter b21: -9
Enter b22: 0
Enter b31: 0
Enter b32: 4

Output Matrix:
24 29
6 25

The transpose of a matrix is a new matrix that is obtained by exchanging the rows and columns.
In this program, the user is asked to enter the number of rows r and columns c. Their values
should be less than 10 in this program.

Then, the user is asked to enter the elements of the matrix (of order r*c).

The program below then computes the transpose of the matrix and prints it on the screen.

Program to Find the Transpose of a Matrix


#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);

// Assigning elements to the matrix


printf("\nEnter matrix elements:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}

// Displaying the matrix a[][]


printf("\nEntered matrix: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}

// Finding the transpose of matrix a


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}

// Displaying the transpose of matrix a


printf("\nTranspose of the matrix:\n");
for (i = 0; i < c; ++i)
for (j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}

Output

Enter rows and columns: 2


3
Enter matrix elements:
Enter element a11: 1
Enter element a12: 4
Enter element a13: 0
Enter element a21: -5
Enter element a22: 2
Enter element a23: 7

Entered matrix:
1 4 0
-5 2 7

Transpose of the matrix:


1 -5
4 2
0 7

Find the Largest Element in a Dynamically Allocated Memory


#include <stdio.h>
#include <stdlib.h>
int main() {
int num;
float *data;
printf("Enter the total number of elements: ");
scanf("%d", &num);

// Allocating memory for num elements


data = (float *)calloc(num, sizeof(float));
if (data == NULL) {
printf("Error!!! memory not allocated.");
exit(0);
}

// Storing numbers entered by the user.


for (int i = 0; i < num; ++i) {
printf("Enter Number %d: ", i + 1);
scanf("%f", data + i);
}

// Finding the largest number


for (int i = 1; i < num; ++i) {
if (*data < *(data + i))
*data = *(data + i);
}
printf("Largest number = %.2f", *data);

return 0;
}

Output

Enter the total number of elements: 5


Enter Number 1: 3.4
Enter Number 2: 2.4
Enter Number 3: -5
Enter Number 4: 24.2
Enter Number 5: 6.7
Largest number = 24.20

In the program, the user is asked to enter the number of elements, which is stored in variable
num. We will allocate memory for num number of float values.

Then, the user is asked to enter num numbers. These numbers are stored in the dynamically
allocated memory.

Finally, the largest number among these numbers is determined and printed on the screen.

You might also like