Skip to content

Commit 1f3b375

Browse files
yusuf601slowy07
andauthored
add:z score algorithm (#126)
* feat:add algorithm z score * fix: create verif value x using linear search and fix formula z-score * Update math/z_score.cpp Co-authored-by: arfy slowy <slowy.arfy@proton.me> * fix: remove unused space on 38 to 41 * fix: remove unused space 45 to 46 --------- Co-authored-by: arfy slowy <slowy.arfy@proton.me>
1 parent 81967ed commit 1f3b375

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

algorithm/search_algorithm/linear_search.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ bool SearchName(std::vector<std::string> &nama, std::string &DataNama) {
2121
return false;
2222
}
2323

24-
bool SearchBook(const std::vector<std::string> &buku,
25-
const std::string &DataBuku) {
24+
bool SearchBook(const std::vector<std::string> &buku,const std::string &DataBuku) {
2625
// looping melalui vektor `buku` untuk mencari buku berdasarkan input
2726
for (const auto &book : buku) {
2827
if (DataBuku == book) {

math/z_score.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <cmath>
4+
5+
6+
bool Verification(std::vector<int>& data,int &x){
7+
for(int i = 0;i <= data.size() - 1; i++){
8+
if(x == data[i]){
9+
return true;
10+
}
11+
}
12+
return false;
13+
}
14+
15+
void Z_Score(std::vector<int>& data,int &x){
16+
//mean
17+
int resultMean;
18+
int Sum = 0;
19+
for(int i = 0; i <= data.size() - 1;i++){
20+
Sum += data[i];
21+
}
22+
resultMean = Sum / data.size();
23+
std::cout << "Nilai rata-rata: " << resultMean << std::endl;
24+
25+
//standart deviation
26+
int denominator = data.size() - 1;
27+
int numerator;
28+
float Standar_Dev;
29+
float sum_numerator = 0;
30+
for(int i = 0; i <= data.size() - 1;i++){
31+
int numerator = sqrt((data[i] - resultMean) * (data[i] - resultMean)); //sqrt(pow((data[i] - mean)));
32+
sum_numerator += numerator;
33+
}
34+
35+
Standar_Dev = static_cast<double>(sum_numerator) / denominator;
36+
std::cout << "nilai Standar Deviation: " << Standar_Dev << std::endl;
37+
38+
39+
40+
// Z score
41+
double score;
42+
score = (x - resultMean) / Standar_Dev;
43+
std::cout << "Nilai Z-Score: " << score << std::endl;
44+
}
45+
int main(){
46+
int value,count,x;
47+
std::vector<int> data;
48+
std::cout << "Masukkan panjang data: "; //masukkan panjang data
49+
std::cin >> count;
50+
51+
for(int i = 0; i <= count - 1; i++){ // for untuk memasukkan data satu per satu
52+
std::cout << "Masukkan data ke " << i + 1 << ": ";
53+
std::cin >> value;
54+
55+
data.push_back(value); //masukkan data yang di input di variabel value;
56+
}
57+
std::cout << "Masukkan nilai data(x): ";
58+
std::cin >> x;
59+
60+
if(Verification(data,x)){
61+
std::cout << "Nilai yang anda masukkan sudah benar! " << std::endl;
62+
}else{
63+
std::cout << "data yang anda masukkan salah! \n";
64+
std::cout << "Masukkan nilai kembali: ";
65+
std::cin >> x;
66+
}
67+
Z_Score(data,x);
68+
69+
}

0 commit comments

Comments
 (0)