-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector_matrix.cpp
167 lines (127 loc) · 3.5 KB
/
vector_matrix.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
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
void Vector();
void Matrix();
//Function to set the return menu operation with an argument inside
int Return(int selecTask);
int main(){
//Builind the menu for selecting the subject[vector ou matrix]
menu:
int a;
cout << "\nSelect the subject:\nType 1 for vectors or 2 for matrices: ";
cin >> a;
if(a == 1){
Vector();
}
else if(a == 2){
Matrix();
}
else{
cout << "\n\nType again\n";
goto menu;
}
return 0;
}
void Vector(){
//Setting a variable to store the number of item in the vector
int num;
cout << "\nWhat's the lenght of the vector? ";
cin >> num;
//Setting vectors and variables
int vector[num], i;
//Building a for loop to register values into each item of the vector
for(i = 0; i < num; i++){
cout << "\nSet a value for the vector[" << i << "]: ";
cin >> vector[i];
}
//Printing the registered values for the vector's itens in the console
for(i = 0; i < num; i++){
cout << "\nValue of the vector[" << i << "] = " << vector[i];
}
char option;
repSearch:
cout << "\nDo you want to make a simple search on the vector?[Y to make or ANY KEY to skip]: ";
cin >> option;
if(option == 'Y' or option == 'y'){
goto simpleSearch;
}
else{
Return(1);
}
simpleSearch:
char Ok;
int foundValue, foundPosition;
bool searchedValue;
cout << "\nLet's do a simple search for the previous vector!\n";
for(i = 0; i < num; i++){
cout << "\nValue of the vector[" << i << "] = " << vector[i];
}
cout << "\n";
cout << "\nType a number of the vector: ";
cin >> foundValue;
for(i = 0; i < num; i++){
if(vector[i] == foundValue){
system("cls");
searchedValue = true;
cout << "\nThe value was found in the position " << i;
goto repSearch;
}
system("cls");
if(searchedValue == false){
cout << "\nThe value was not found! Type again";
goto simpleSearch;
}
}
}
void Matrix(){
//Setting the size of the matrix with two variables
int i, j, a, b;
cout << "\nWhat's the 'i' term of the matrix?: ";
cin >> a;
cout << "\nWhat's the 'j' term of the matrix?: ";
cin >> b;
//Setting the matrices and variables for the setting and printing
int matrix[a][b];
//Getting the data for the matrix' terms
for(i = 0; i < a; i++){
for(j = 0; j < b; j++){
cout << "\nEnter the [" << i+1 << "][" << j+1 << "] = ";
scanf("%d", &matrix[i][j]);
}
}
//Printing the data of the matrix
for(i = 0; i < a; i++){
for(j = 0; j < b; j++){
cout << " " << matrix[i][j];
}
cout << "\n";
}
Return(2);
}
//========================================================================================
//This next block is a code section that asks you if you want to go back to the main menu,
//to repeat the actual task or simply exit the program.
//========================================================================================
int Return(int selecTask){
//Variable that will store the menu option to return to menu, to the actual task or exit
int menu;
cout << "\nDo you want to return to the main menu[1], repeat the actual task[2] or exit[any key]\n";
cin >> menu;
if(menu == 1 ){
system("cls");
main();
}
else if(menu == 2 && selecTask == 1){
system("cls");
Vector();
}
else if(menu == 2 && selecTask == 2){
system("cls");
Matrix();
}
else{
exit(0);
}
}