forked from shivaylamba/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SparseMatrix.cpp
192 lines (168 loc) · 4.51 KB
/
SparseMatrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <iostream>
#include <math.h>
using namespace std;
int size, dem;
void addElement(int *arr, int num, int i)
{
arr[i] = num;
}
void addTrigonal(int *arr, int num, int i, int j)
{
arr[2 * i + j] = num;
}
void addElement(int *arr, int num, int i, int j)
{
arr[(i * (i + 1)) / 2 + j] = num;
}
bool search(int *arr, int el)
{
for (int i = 0; i < size; i++)
if (arr[i] == el)
return true;
return false;
}
void replace(int *arr, int prevEl, int newEl)
{
for (int i = 0; i < size; i++)
if (arr[i] == prevEl)
arr[i] = newEl;
}
void remove(int *arr, int el)
{
for (int i = 0; i < size; i++)
if (arr[i] == el)
arr[i] = 0;
}
void display(int *arr, int a)
{
int counter = 0;
cout << "\nmatrix is (0 represents empty cell and n represents invalid cell):";
for (int i = 0; i < dem; i++)
{
cout << "\n";
for (int j = 0; j < dem; j++)
{
if (a == 1)
(i == j) ? cout << " " << arr[counter++] : cout << " n";
else if (a == 2)
(i >= j) ? cout << " " << arr[counter++] : cout << " n";
else if (a == 3)
(j >= i) ? cout << " " << arr[counter++] : cout << " n";
else if (a == 5)
(abs(i - j) <= 1) ? cout << " " << arr[counter++] : cout << " n";
else
(i >= j) ? cout << " " << arr[(i * (i + 1)) / 2 + j] : cout << " " << arr[(j * (j + 1)) / 2 + i];
}
}
cout << "\n";
}
void p()
{
cout << "\ninvalid cell";
}
void menu(int *array, int a)
{
int num, i, j, choice;
do
{
cout << "\nMenu : \n1. insert\n2. search\n3. delete\n4. replace\n5. display\n\nEnter your choice : ";
cin >> choice;
switch (choice)
{
case 1:
{
cout << "\nEnter the number to be added(except 0) : ";
cin >> num;
cout << "\nSpecify cell (i and j) : ";
cin >> i >> j;
if (a == 1)
(i == j) ? addElement(array, num, i) : p();
else if (a == 2)
(i >= j) ? addElement(array, num, i, j) : p();
else if (a == 3)
(j >= i) ? addElement(array, num, j, i) : p();
else if (a == 4)
(j <= i) ? addElement(array, num, i, j) : addElement(array, num, j, i);
else
(abs(i - j) <= 1) ? addTrigonal(array, num, i, j) : p();
}
break;
case 2:
{
cout << "\nEnter the number to be searched : ";
cin >> num;
(search(array, num)) ? cout << "\ndata is present" : cout << "\ndata is not present";
}
break;
case 3:
{
cout << "\nEnter the number to be deleted : ";
cin >> num;
remove(array, num);
cout << "\nall " << num << " are deleted";
}
break;
case 4:
{
int prevEl, newEl;
cout << "\nEnter the number to be replaced and new number : ";
cin >> prevEl >> newEl;
replace(array, prevEl, newEl);
cout << "\nall " << prevEl << " are replaced";
}
break;
case 5:
{
display(array, a);
}
break;
default:
cout << "\nInvalid choice!";
}
cout << "\nEnter 0 to exit : ";
cin >> choice;
} while (choice != 0);
}
int main()
{
cout << "\nEnter dimension of matrix : ";
cin >> dem;
char ch;
do
{
cout << "\nEnter type of matrix : \n\n1. diagonal matrix\n2. lower triangular matrix\n3 .upper triangular matrix\n4. symmetric matrix\n5. tridiagonal matrix";
cout << "\n\nEnter your choice : ";
int choice;
cin >> choice;
int *array;
switch (choice)
{
case 1:
{
size = dem;
array = (int *)calloc(size, sizeof(int));
}
break;
case 2:
case 3:
case 4:
{
size = (dem * dem + dem) / 2;
array = (int *)calloc(size, sizeof(int));
}
break;
case 5:
{
size = 3 * dem - 2;
array = (int *)calloc(size, sizeof(int));
}
break;
default:
cout << "\ninvalid choice!";
}
menu(array, choice);
free(array);
cout << "\ntry another matrix, enter 0 to exit : ";
cin >> ch;
} while (ch != 0);
}