Week 6 Solutions
Week 6 Solutions
// Given a 2D array and an element ‘key’, this program finds if the element is
present in the 2D array
#include <stdio.h>
int main(void)
{
int n, m;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &n, &m);
int arr[n][m];
printf("Enter the elements of the array: ");
int key;
printf("Enter the element to be searched: ");
scanf("%d", &key);
// Linear search on the 2D array across rows and columns to find key
int found = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (arr[i][j] == key)
{
found = 1;
break;
}
}
}
if (found == 1)
{
printf("Element found");
}
else
{
printf("Element not found");
}
}
Practice_Lab_1/Q2
// Given a 2D array, this program computes the sum of the products of the
elements in each row in the matrix
#include <stdio.h>
int main(void)
{
int n, m;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &n, &m);
int arr[n][m];
printf("Enter the elements of the array: ");
int prod = 0;
int term = 0;
for (int i = 0; i < n; i++)
{
prod = arr[i][0];
for (int j = 1; j < m; j++)
{
prod = prod * arr[i][j];
}
term = term + prod;
prod = 1;
}
int main(void)
{
int n, m;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &n, &m);
int arr1[n][m];
int arr2[n][m];
printf("Enter the elements of the first array: ");
int arr31[n][m];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
arr31[i][j] = arr1[i][j] - arr2[i][j];
}
}
int arr32[n][m];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
arr32[i][j] = arr1[i][j] + arr2[i][j];
}
}
printf("The result of matrix subtraction (by the first method) is: \n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
printf("%d ", arr31[i][j]);
}
printf("\n");
}
printf("The result of matrix subtraction (by the second method) is: \n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
printf("%d ", arr32[i][j]);
}
printf("\n");
}
}
Practice_Lab_2/Q2
// This program computes the average number of times pieces have been captured at
a location on a chessboard in three different tournaments
#include <stdio.h>
int main(void)
{
int A[3][3];
int B[3][3];
int C[3][3];
float D[3][3];
printf("Enter the array elements from the data of the first tournament: \n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
scanf("%d", &A[i][j]);
}
}
printf("Enter the array elements from the data of the second tournament:
\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
scanf("%d", &B[i][j]);
}
}
printf("Enter the array elements from the data of the third tournament: \n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
scanf("%d", &C[i][j]);
}
}
#include <stdio.h>
int main(void)
{
int n, m, p; // n x m and m x p matrices
printf("Enter n: ");
scanf("%d", &n);
printf("Enter m: ");
scanf("%d", &m);
printf("Enter p: ");
scanf("%d", &p);
// Given a square matrix of nxn and a column vector of n values perform matrix
multiplication on them
#include <stdio.h>
int main(void)
{
int A[5][5] = {{0,1,0,1,0}, {1,0,1,0,1}, {1,1,0,1,1}, {0,0,1,0,1},
{0,1,1,1,0}};
float P_i[5] = {0.75,0.666,0.45,0.33,0.832};
float P_i1[5] = {0,0,0,0,0};
// Given a 3D array, this code runs matrix addition on each of the constituent 2D
arrays and returns final sum matrix
#include <stdio.h>
int main(void)
{
int x,y,z;
printf("Enter the dimensions of the 3D array: ");
scanf("%d %d %d", &x, &y, &z);
int arr[x][y][z], sum[x][y];
printf("Enter the elements of the 3D array: ");
for(int i=0; i<x; i++)
{
for(int j=0; j<y; j++)
{
for(int k=0; k<z; k++)
{
scanf("%d", &arr[i][j][k]);
}
}
}
int line = 0;
for(int i=0; i<x; i++)
{
for(int j=0; j<y; j++)
{
line = 0;
for(int k=0; k<z; k++)
{
line += arr[k][i][j];
}
sum[i][j] = line;
}
}
printf("The sum of the constituent 2D arrays is: ");
for(int i=0; i<x; i++)
{
for(int j=0; j<y; j++)
{
printf("%d ", sum[i][j]);
}
printf("\n");
}
}
Practice_Lab_4/Q2
#include <stdio.h>
int main(void)
{
int x,y,z;
printf("Enter the dimensions of the array: ");
scanf("%d %d %d", &x, &y, &z);
int arr[x][y][z];
printf("Enter the elements of the array: ");
for(int i=0; i<x; i++)
{
for(int j=0; j<y; j++)
{
for(int k=0; k<z; k++)
{
scanf("%d", &arr[i][j][k]);
}
}
}
int key;
printf("Enter the key to be searched: ");
scanf("%d", &key);
// int found = 0;
for(int i=0; i<x; i++)
{
for(int j=0; j<y; j++)
{
for(int k=0; k<z; k++)
{
if(arr[i][j][k] == key)
{
printf("The key was found");
return 0;
}
}
}
}
printf("The key was not found in the array");
}
Practice_Lab_5/Q1
// For a numeric string stored in a string variable, this code converts that and
stores it into an integer variable
#include <stdio.h>
int main(void)
{
int n;
printf("Enter the length of the string (number of digits in the numeric
string): ");
scanf("%d", &n);
char str[n];
printf("Enter the numeric string: ");
scanf("%s", str);
int num = 0;
int i = 0;
while(str[i] != '\0')
{
num = num * 10 + (str[i] - '0');
i++;
}
printf("%d", num);
}
Practice_Lab_5/Q2
int main(void)
{
int n,m;
printf("Enter the length of the first string: ");
scanf("%d", &n);
printf("Enter the length of the second string: ");
scanf("%d", &m);
if (m>n)
{
printf("The second string cannot be a substring of the first string.");
return 0;
}
char str1[n], str2[m];
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
int found = 0;
for(int i=0; i<n; i++)
{
if(str1[i] == str2[0])
{
for(int j=0; j<m; j++)
{
if(str1[i+j] != str2[j])
{
break;
}
if(j == m-1)
{
found = 1;
}
}
}
}
if(found == 1)
{
printf("The second string is a substring of the first string.");
}
else
{
printf("The second string is not a substring of the first string.");
}
}
Practice_Lab_6/Q1
// Given a student’s first name and last name as strings (without spaces) as
inputs, this code concatenates them and displays the full name in block letters
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(void)
{
char first[50], last[50];
int i;
printf("Enter first name: ");
scanf("%s", first);
printf("Enter last name: ");
scanf("%s", last);
char full[100] = "";
strcat(full, first);
strcat(full, " ");
strcat(full, last);
for (i = 0; i < strlen(full); i++) {
printf("%c", toupper(full[i]));
}
}
Practice_Lab_6/Q2
//Counts the number of alphabets and digits and other symbols in an input string,
and reports those numbers by printing them.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int n;
printf("Enter length of the string: ");
scanf("%d", &n);
char str[n];
printf("Enter the string: ");
scanf("%s", str);
// Given two strings that represent floating point numbers, this program performs
multiplication on them by first converting them from strings to floating point
numbers and then multiplying them. The result is then printed.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char str1[10] = "-14.20";
char str2[10] = "3.141";
char str3[10];
double num1 = atof(str1);
double num2 = atof(str2);
double num3 = num1 * num2;
printf("Sum: %f", num3);
}
Practice_Lab_7/Q1
// Given an array of words arranged alphabetically and a key word this code finds
out where in that order the key should be inserted
#include <stdio.h>
#include <string.h>
int main(void)
{
char words[10][20] = {"apple", "banana", "cherry", "grape", "kiwi", "lemon",
"orange", "pear", "pineapple", "watermelon"};
char key[20];
char temp[20];
int i, j, k, result;
// From an array of 10 strings given as an input, this code finds out which of
the strings in the array has the smallest length.
#include <stdio.h>
#include <string.h>
int main(void)
{
char words[10][50];
int i, j, min = 0;
for (i = 0; i < 10; i++) {
printf("Enter string %d: ", i + 1);
scanf("%s", words[i]);
}
for (i = 0; i < 10; i++) {
if (strlen(words[i]) < strlen(words[min])) {
min = i;
}
}
printf("The first string with the smallest length is %s", words[min]);
}
Practice_Lab_7/Q3
// Given an array of strings, this code separates the array into two arrays, one
for strings that start with an alphabet, and another for those that do not
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(void)
{
char strings[10][100];
char alpha[10][100];
char nonalpha[10][100];
printf("Enter 10 strings: ");
for (int i = 0; i < 10; i++)
{
scanf("%s", strings[i]);
}
int alpha_count = 0;
int nonalpha_count = 0;
for (int i = 0; i < 10; i++)
{
if (isalpha(strings[i][0]))
{
strcpy(alpha[alpha_count], strings[i]);
alpha_count++;
}
else
{
strcpy(nonalpha[nonalpha_count], strings[i]);
nonalpha_count++;
}
}
printf("The strings that start with an alphabet are: ");
for (int i = 0; i < alpha_count; i++)
{
printf("%s ", alpha[i]);
}
printf("\nThe strings that do not start with an alphabet are: ");
for (int i = 0; i < nonalpha_count; i++)
{
printf("%s ", nonalpha[i]);
}
}