0% found this document useful (0 votes)
7 views9 pages

Practical Question Till 8

The document contains a series of C programming exercises focused on matrix operations, string manipulations, and student information management. It includes code examples for multiplying matrices, finding transposes, sorting strings, checking substrings, concatenating strings, ranking students, multiplying sparse matrices, and counting word occurrences in a paragraph. Each exercise is accompanied by a brief description and the corresponding C code implementation.

Uploaded by

ssonikumarikashi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

Practical Question Till 8

The document contains a series of C programming exercises focused on matrix operations, string manipulations, and student information management. It includes code examples for multiplying matrices, finding transposes, sorting strings, checking substrings, concatenating strings, ranking students, multiplying sparse matrices, and counting word occurrences in a paragraph. Each exercise is accompanied by a brief description and the corresponding C code implementation.

Uploaded by

ssonikumarikashi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

DSA lab Prac cal Ques ons

Q1 WRITE A C PROGRAM TO FIND MULTIPLICATION OF TWO MATRICES

#include <stdio.h>

int main() {

int a[10][10], b[10][10], mul[10][10], r1, c1, r2, c2;

prin ("Enter rows and columns for first matrix: ");

scanf("%d%d", &r1, &c1);

prin ("Enter rows and columns for second matrix: ");

scanf("%d%d", &r2, &c2);

if (c1 != r2) {

prin ("Matrix mul plica on not possible.\n");

return 0;

prin ("Enter elements of first matrix:\n");

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

for(int j = 0; j < c1; j++)

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

prin ("Enter elements of second matrix:\n");

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

for(int j = 0; j < c2; j++)

scanf("%d", &b[i][j]);
// Ini alize mul to 0

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

for(int j = 0; j < c2; j++)

mul[i][j] = 0;

// Mul ply matrices

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

for(int j = 0; j < c2; j++)

for(int k = 0; k < c1; k++)

mul[i][j] += a[i][k] * b[k][j];

prin ("Resultant Matrix:\n");

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

for(int j = 0; j < c2; j++)

prin ("%d ", mul[i][j]);

prin ("\n");

return 0;

Q2 WRITE A C PROGRAM TO PRINT TRANSPOSE OF A MATRIX

#include <stdio.h>

int main() {

int a[10][10], trans[10][10], row, col;

prin ("Enter rows and columns of matrix: ");

scanf("%d%d", &row, &col);

prin ("Enter elements:\n");

for(int i = 0; i < row; i++)


for(int j = 0; j < col; j++)

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

for(int i = 0; i < row; i++)

for(int j = 0; j < col; j++)

trans[j][i] = a[i][j];

prin ("Transpose of matrix:\n");

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

for(int j = 0; j < row; j++)

prin ("%d ", trans[i][j]);

prin ("\n");

return 0;

Q3 WRITE A C PROGRAM TO ACCEPT 10 STRINGS AS INPUT & PRINT IN LEXICOGRAPHIC ORDER

#include <stdio.h>

#include <string.h>

int main() {

char str[10][100], temp[100];

prin ("Enter 10 strings:\n");

for(int i = 0; i < 10; i++)

scanf("%s", str[i]);

// Bubble sort

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

for(int j = i + 1; j < 10; j++) {

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


strcpy(temp, str[i]);

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

strcpy(str[j], temp);

prin ("Strings in lexicographic order:\n");

for(int i = 0; i < 10; i++)

prin ("%s\n", str[i]);

return 0;

Q4 WRITE A C PROGRAM TO TWO STRINGS S1, S2 & CHECK IF S2 IS SUBSTRING OF S1 & ALSO THE POSITION
OF THE SUB STRING IN S1

#include <stdio.h>

#include <string.h>

int main() {

char s1[100], s2[100];

prin ("Enter main string (s1): ");

gets(s1);

prin ("Enter substring (s2): ");

gets(s2);

char *pos = strstr(s1, s2);

if (pos) {

prin ("Substring found at posi on: %ld\n", pos - s1);

} else {

prin ("Substring not found.\n");


}

return 0;

Q5 WRITE A C PROGRAM TO CONCATENATE TWO STRINGS S1 & S2

#include <stdio.h>

#include <string.h>

int main() {

char s1[100], s2[100];

prin ("Enter first string: ");

gets(s1);

prin ("Enter second string: ");

gets(s2);

strcat(s1, s2);

prin ("Concatenated string: %s\n", s1);

return 0;

Q6 WRITE A C PROGRAM TO FIND THE STUDENT INFORMATION & PRINT THE STUDENT INFORMATION &
RANK SECURED IN ASCENDING ORDER

#include <stdio.h>

#include <string.h>

struct Student {

char name[50];

int marks;

};
int main() {

struct Student s[5], temp;

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

prin ("Enter name and marks of student %d: ", i+1);

scanf("%s %d", s[i].name, &s[i].marks);

// Sort by marks

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

for(int j = i + 1; j < 5; j++) {

if (s[i].marks > s[j].marks) {

temp = s[i];

s[i] = s[j];

s[j] = temp;

prin ("Students ranked in ascending order:\n");

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

prin ("Name: %s, Marks: %d, Rank: %d\n", s[i].name, s[i].marks, i+1);

return 0;

Q7 WRITE A C PROGRAM TO MULTIPLY TWO SPARCE MATRICES

#include <stdio.h>

#define MAX 100


typedef struct {

int row, col, value;

} Element;

void mul plySparse(Element a[], Element b[], Element result[]) {

int k = 1;

for (int i = 1; i <= a[0].value; i++) {

for (int j = 1; j <= b[0].value; j++) {

if (a[i].col == b[j].row) {

result[k].row = a[i].row;

result[k].col = b[j].col;

result[k].value = a[i].value * b[j].value;

k++;

result[0].row = a[0].row;

result[0].col = b[0].col;

result[0].value = k - 1;

int main() {

Element a[MAX] = {{2, 2, 2}, {0, 1, 3}, {1, 0, 4}};

Element b[MAX] = {{2, 2, 2}, {0, 0, 5}, {1, 1, 6}};

Element result[MAX];

mul plySparse(a, b, result);

prin ("Resultant sparse matrix:\n");

for(int i = 0; i <= result[0].value; i++) {


prin ("%d %d %d\n", result[i].row, result[i].col, result[i].value);

return 0;

Q8 WRITE A C PROGRAM TO TAKE PARAGRAPH AS INPUT AND OUTPUT NO, OF OCCURRENCES OF STRING IN
PARAGRAPH

#include <stdio.h>

#include <string.h>

#include <ctype.h>

int main() {

char paragraph[1000], word[50];

prin ("Enter paragraph:\n");

fgets(paragraph, sizeof(paragraph), stdin);

prin ("Enter word to search: ");

scanf("%s", word);

int count = 0;

char *token = strtok(paragraph, " ,.!?\n");

while (token != NULL) {

if (strcmp(token, word) == 0)

count++;

token = strtok(NULL, " ,.!?\n");

prin ("The word '%s' occurs %d mes.\n", word, count);

return 0;

You might also like