WEEK 8:
Objective: Explore the difference between other arrays and character arrays that can be used as Strings
by using null character and get comfortable with string by doing experiments that will reverse a string
and concatenate two strings. Explore sorting solution bubble sort using integer arrays.
Suggested Experiments/Activities:
Tutorial 8: 2 D arrays, sorting and Strings.
Lab 8: Matrix problems, String operations, Bubble sort
i) Addition of two matrices
ii) Multiplication two matrices
iii) Sort array elements using bubble sort
iv) Concatenate two strings without built-in functions
v) Reverse a string using built-in and without built-in string functions
Write C program for Addition of 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 b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
}
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{ sum[i][j] = a[i][j] + b[i][j];
}
}
printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
{
for (j = 0; j < c; ++j)
{ printf("%d ", sum[i][j]);
}
Printf(("\n ");
}
return 0;
}
Input / :
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 b11: 6
Enter element b12: 5
Enter element b13: 3
Enter element b21: 5
Enter element b22: 6
Enter element b23: 3
Sum of two matrices:
8 8 7
10 8 6
ii) Multiplication two matrices
#include<stdio.h>
int main()
{
int m, n, p, q, i, j, k;
printf("Enter the order of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the order of second matrix\n");
scanf("%d%d", &p, &q);
int a[m][n], b[p][q], c[m][q];
if (n != p)
{ printf("Matrix is incompatible for multiplication\n");
}
else
{
printf("Enter the elements of Matrix-A:\n");
for (i = 0; i < m; i++)
{ for (j = 0; j < n; j++)
{ scanf("%d", & a[i][j]);
}
}
printf("Enter the elements of Matrix-B:\n");
for (i = 0; i < p; i++)
{ for (j = 0; j < q; j++)
{ scanf("%d", &b[i][j]);
}
}
for (i = 0; i < m; i++)
{
for (j = 0; j < q; j++)
{ c[i][j] = 0;
for (k = 0; k < p; k++)
{c[i][j] += a[i][k] * b[k][j];
}
}
}
printf("The product of the two matrices is:-\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < q; j++)
{
printf("%d\t", c[i][j]);
}
printf("\n");
}
}
return 0;
}
Input /Output:
Enter the order of first matrix
23
Enter the order of second matrix
32
Enter the elements of Matrix-A:
12
12
12
Enter the elements of Matrix-B:
123
123
Product of the two matrices is
The product of the two matrices is:-
9 7
9 11
iii) Sort array elements using bubble sort
#include<stdio.h>
int main()
{
int a[10],i,j,temp,n;
printf("\n Enter the max no.of Elements to Sort: \n");
scanf("%d",&n);
printf("Enter %d Integers Elements \n", n);
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
for(i=0; i<n; i++)
for(j=i+1; j<n; j++)
{
if(a[i]>a[j])
{ temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("Sorted list in ascending order:\n");
for(i=0; i<n; i++)
{ printf("%d\t",a[i]);
}
return 0;
}
Output:
Enter the max no.of Elements to Sort:
5
Enter the 5 Integer Elements :
45
21
89
78
99
Sorted list in ascending order:
21 45 78 89 99
iv) Concatenate two strings without built-in functions
#include <stdio.h>
int main()
{ char str1[100], str2[100];
int i = 0, j = 0;
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
// Traverse to the end of the first string
while (str1[i] != '\0') {
i++;\\ length of the string
}
// Copy characters of the second string to the end of the first string
while (str2[j] != '\0') {
str1[i] = str2[j];
i++;
j++;
}
str1[i] = '\0'; // Terminate the concatenated string with a null character
// Display the concatenated string
printf("Concatenated String: %s\n", str1);
return 0;
}
Output:
Enter the first string: Hello
Enter the second string: World
Concatenated String: HelloWorld
v) Reverse a string using built-in and without built-in string functions
Reverse a string in C using strrev
#include <stdio.h>
#include <string.h>
int main()
{ char s[100];
printf("Enter a string to reverse\n");
gets(s);
strrev(s);
printf("Reverse of the string: %s\n", s);
return 0;
}
Output:
Enter a string: Hello
The reverse of the string: olleH
Reverse a string without using built-in string functions
#include <stdio.h>
int main()
{ char s[100],rev[100];
int i,len,j=0;
printf("Enter a string to reverse\n");
gets(s);
for(i=0;s[i]!=’\0’;i++)
len++;
for(i=len-1;i>=0;i++)
{ rev[j]=s[i];
j++;
}
printf("Reverse of the string: %s\n", rev);
return 0;
}
Output:
Enter a string: Hello
The reverse of the string: olleH