Lab_Programs
Lab_Programs
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
}
}
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
Full Matrix:
0500
0000
0080
7000
#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;
// Copy a string
strcpy(str2, str1); // Copy the content of str1 to str2
cout << "Copied string (str2): " << str2 << endl;
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;
}
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.
#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