fojtasd

Untitled

Nov 25th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. void readVector(double *vec, int length) {
  9. for (int i = 0; i < length; i++) {
  10. cin >> vec[i];
  11.  
  12. if (cin.fail()) {
  13. cout << "Nespravny vstup." << endl;
  14. exit(-1);
  15. }
  16. }
  17. }
  18.  
  19.  
  20.  
  21.  
  22.  
  23. int main(int argc, char **argv) {
  24. int vecLength = 0;
  25.  
  26.  
  27. cin >> vecLength;
  28.  
  29. if (cin.fail()) {
  30. cout << "Nespravny vstup." << endl;
  31. return -1;
  32. }
  33.  
  34. if (vecLength <= 0) {
  35. cout << "Nespravny vstup." << endl;
  36. return -1;
  37. }
  38.  
  39. if (vecLength <= 0) {
  40. cout << "Nespravny vstup." << endl;
  41. return -1;
  42. }
  43.  
  44. double *vecA = new double[vecLength];
  45. double *vecB = new double[vecLength];
  46.  
  47. readVector(vecA, vecLength);
  48. readVector(vecB, vecLength);
  49.  
  50. double mult = 0.0;
  51. for (int i = 0; i < vecLength; i++) {
  52. mult += vecA[i] * vecB[i];
  53. }
  54.  
  55. double sumSquareA = 0.0;
  56. for (int i = 0; i < vecLength; i++) {
  57. sumSquareA += vecA[i] * vecA[i];
  58. }
  59.  
  60. double sumSquareB = 0.0;
  61. for (int i = 0; i < vecLength; i++) {
  62. sumSquareB += vecB[i] * vecB[i];
  63. }
  64.  
  65. double csm = mult / (sqrt(sumSquareA) * sqrt(sumSquareB));
  66.  
  67. cout << "CSM: " << fixed << setprecision(3) << csm << endl;
  68.  
  69. return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment