0% found this document useful (0 votes)
3 views

Lab_Programs

Uploaded by

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

Lab_Programs

Uploaded by

playmaker1901
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

3.

Write a C++ program for the implementation of Sparse Matrices


#include <iostream>
#include <vector>
using namespace std;

// Function to input a sparse matrix


void inputSparseMatrix(vector<vector<int>>& matrix, int& nonZeroCount, int rows, int cols) {
cout << "Enter the number of non-zero elements: ";
cin >> nonZeroCount;

cout << "Enter the non-zero elements (row, column, value):" << endl;
for (int i = 0; i < nonZeroCount; i++) {
int row, col, value;
cin >> row >> col >> value;
matrix.push_back({row, col, value}); // Store the row, column, and value
}
}

// Function to display the sparse matrix in compact form


void displaySparseMatrix(const vector<vector<int>>& matrix) {
cout << "\nSparse Matrix Representation:" << endl;
cout << "Row Column Value" << endl;
for (const auto& element : matrix) {
cout << element[0] << " " << element[1] << " " << element[2] << endl;
}
}

// Function to display the full matrix


void displayFullMatrix(int rows, int cols, const vector<vector<int>>& matrix) {
// Initialize a matrix with all elements as 0
vector<vector<int>> fullMatrix(rows, vector<int>(cols, 0));
// Insert the non-zero elements into the full matrix
for (const auto& element : matrix) {
fullMatrix[element[0]][element[1]] = element[2];
}

cout << "\nFull Matrix:" << endl;


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << fullMatrix[i][j] << " ";
}
cout << endl;
}
}

int main() {
int rows, cols;
cout << "Enter the number of rows and columns of the matrix: ";
cin >> rows >> cols;
vector<vector<int>> sparseMatrix; // To store non-zero elements (row, col, value)
int nonZeroCount;
// Input the sparse matrix
inputSparseMatrix(sparseMatrix, nonZeroCount, rows, cols);
// Display the sparse matrix in its compact form
displaySparseMatrix(sparseMatrix);
// Display the full matrix with zeros in place of missing elements
displayFullMatrix(rows, cols, sparseMatrix);
return 0;
}

Output:
Enter the number of rows and columns of the matrix: 4 4
Enter the number of non-zero elements: 3
Enter the non-zero elements (row, column, value):
015
228
307

Sparse Matrix Representation:


Row Column Value
0 1 5
2 2 8
3 0 7

Full Matrix:
0500
0000
0080
7000

4. Write a C++ program for the implementation of String

#include <iostream.h>
#include <string.h> // For using string functions like strlen, strcpy, strcat, etc;
#include<ctype.h>
int main() {
// Declare and initialize strings
char str1[100], str2[100], str3[100];
// Input a string from the user
cout << "Enter a string: ";
cin.getline(str1, 100); // Using getline to read a string with spaces
// Display the string entered
cout << "You entered: " << str1 << endl;

// Get the length of the string


int length = strlen(str1); // strlen returns the length of the string excluding the null terminator
cout << "Length of the string: " << length << endl;

// Copy a string
strcpy(str2, str1); // Copy the content of str1 to str2
cout << "Copied string (str2): " << str2 << endl;

// Concatenate two strings


cout << "Enter another string to concatenate: ";
cin.getline(str3, 100); // Read another string

strcat(str1, str3); // Concatenate str3 to the end of str1


cout << "After concatenation, str1: " << str1 << endl;

// Compare two strings


if (strcmp(str1, str2) == 0) { // strcmp compares two strings
cout << "str1 and str2 are equal." << endl;
} else {
cout << "str1 and str2 are not equal." << endl;
}

// Find a character in the string


char ch;
cout << "Enter a character to find in str1: ";
cin >> ch;

char* position = strchr(str1, ch); // strchr finds the first occurrence of a character
if (position != NULL) {
cout << "Character '" << ch << "' found at position: " << (position - str1) << endl;
} else {
cout << "Character '" << ch << "' not found in str1." << endl;
}

// Convert a string to uppercase


for (int i = 0; str1[i] != '\0'; i++) {
str1[i] = toupper(str1[i]); // Convert each character to uppercase
}
cout << "str1 in uppercase: " << str1 << endl;
// Convert a string to lowercase
for (int i = 0; str2[i] != '\0'; i++) {
str2[i] = tolower(str2[i]); // Convert each character to lowercase
}
cout << "str2 in lowercase: " << str2 << endl;

return 0;
}
Output :
Enter a string: Hello World
You entered: Hello World
Length of the string: 11
Copied string (str2): Hello World
Enter another string to concatenate: C++
After concatenation, str1: Hello WorldC++
str1 and str2 are not equal.
Enter a character to find in str1: W
Character 'W' found at position: 6
str1 in uppercase: HELLO WORLD C++
str2 in lowercase: hello world
5. Write a C++ program to implement the following using array
a) Stack

In Data Structures (DS), a stack is a linear data structure that follows the Last In, First Out (LIFO)
principle. This means that the last element added to the stack is the first one to be removed.

Key Operations in a Stack:


1. Push: Adds an element to the top of the stack.
2. Pop: Removes the top element of the stack.
3. Peek (or Top): Returns the top element of the stack without removing it.
4. isEmpty: Checks whether the stack is empty.

#include <iostream.h>
class Stack
{
private:
int Stack[50];
int MaxCapacity;
int top;
public:
Stack()
{
MaxCapacity = 50;
top = -1;
}
int getTop();
int pop();
void push(int Element);
int Empty();
int CurrSize();
int IsFull();
};
int Stack :: getTop()
{
if(!Empty())
return(Stack[top]);
else
return 0;
}
int Stack :: pop()
{
if(!Empty())
return(Stack[top--]);
else
return 0;
}
int Stack :: Empty()
{
if(top == -1)
return 1;
else
return 0;
}
int Stack :: IsFull()
{
if(top == MaxCapacity - 1)
return 1;
else
return 0;
}
int Stack :: CurrSize()
{
return(top + 1);
}
void Stack :: push(int Element)
{
if(!IsFull())
Stack[++top] = Element;
}
void main()
{
Stack S;
S.push(10);
S.push(20);
S.push(30);
cout << S.getTop() << endl;
cout << S.pop() << endl;
cout << S.pop() << endl;
}

Output:
30
30
20

b) Queue
A queue in Data Structures (DS) is a linear data structure that operates on the First In, First Out (FIFO)
principle. This means the element that is inserted first will be the first to be removed, similar to a
queue of people waiting in line.

#include<iostream.h>
class queue
{
private:
int Rear, Front;
int Queue[50];
int max;
int Size;
public:
queue()
{
Size = 0; max = 50;
Rear = Front = -1 ;
}
int Empty();
int IsFull();
void Add(int Element);
int Delete();
int getFront();
};
int queue :: Empty()
{
if(Front == Rear)
return 1;
else
return 0;
}
int queue :: IsFull()
{
if(Rear == max - 1)
return 1;
else
return 0;
}
void queue :: Add(int Element)
{
if(!IsFull())
Queue[++Rear] = Element;
Size++;
}
int queue :: Delete()
{
if(!Empty())
{
Size--;
return(Queue[++Front]);
}
else
return -1;
}
int queue :: getFront()
{
if(!Empty())
return(Queue[Front + 1]);
else
return -1;
}
void main( )
{
queue Q;
Q.Add(11);
Q.Add(12);
Q.Add(13);
cout << Q.Delete() << endl;
Q.Add(14);
cout << Q.Delete() << endl;
cout << Q.Delete() << endl;
cout << Q.Delete() << endl;
cout << Q.Delete() << endl;
Q.Add(15);
Q.Add(16);
cout << Q.Delete() << endl;
}

Output :
11
12
13
14
15

You might also like