fork download
  1. #include <iostream>
  2. #include <time.h>
  3. #include <cstdlib>
  4. #include <math.h>
  5. #include <stdio.h>
  6. using namespace std;
  7.  
  8. struct NVec
  9. {
  10. int coord[99];
  11. };
  12. struct NVec initialize (int N)
  13. {
  14. struct NVec temp;
  15. for(int i=0; i<N; i++)
  16. temp.coord[i]=rand()%100+1;
  17. return temp;
  18. }
  19. struct NVec addition (struct NVec V1, struct NVec V2, int N)
  20. {
  21. struct NVec temp;
  22. for(int i=0; i<N; i++)
  23. temp.coord[i]=V1.coord[i]+V2.coord[i];
  24. return temp;
  25. }
  26. struct NVec subtraction (struct NVec V1, struct NVec V2, int N)
  27. {
  28. struct NVec temp;
  29. for(int i=0; i<N; i++)
  30. temp.coord[i]=V1.coord[i]-V2.coord[i];
  31. return temp;
  32. }
  33. int scalarMultiplication (struct NVec V1, struct NVec V2, int N)
  34. {
  35. int outcome=0;
  36. for(int i=0; i<N; i++)
  37. outcome+=V1.coord[i]*V2.coord[i];
  38. return outcome;
  39. }
  40. double length (struct NVec V1, int N)
  41. {
  42. double l=0;
  43. for(int i=0; i<N; i++)
  44. l+=pow(V1.coord[i],2);
  45. return sqrt(l);
  46. }
  47. void CompareVectors(struct NVec *V)
  48. {
  49. FILE *file;
  50. if((file=fopen("vectorCmp.txt", "w"))==NULL)
  51. {
  52. cout<<"Couldn't open the file!";
  53. exit(1);
  54. }
  55. for(int i=0; i<100; i+=2)
  56. {
  57. struct NVec temp;
  58. for(int j=0; j<2; j++)
  59. {
  60. fprintf(file,"[%d]",V[i].coord[j]);
  61. }
  62. fprintf(file,"\t");
  63. for(int j=0; j<2; j++)
  64. {
  65. fprintf(file,"[%d]",V[i+1].coord[j]);
  66. }
  67. fprintf(file,"\nAddition:");
  68. temp=addition(V[i],V[i+1],2);
  69. for(int j=0; j<2; j++)
  70. {
  71. fprintf(file,"[%d]",temp.coord[j]);
  72. }
  73.  
  74. temp=subtraction(V[i],V[i+1],2);
  75. fprintf(file,"\nSubtraction:");
  76. for(int j=0; j<2; j++)
  77. {
  78. fprintf(file,"[%d]",temp.coord[j]);
  79. }
  80. int ScalarMult=scalarMultiplication(V[i],V[i+1],2);
  81. fprintf(file,"\nScalar multiplication:");
  82. fprintf(file,"[%d]",ScalarMult);
  83. fprintf(file,"\nV1 length:%lf\tV2 length:%lf\n",length(V[i],2),length(V[i+1],2));
  84. }
  85. fclose(file);
  86. }
  87. void SaveVectors(struct NVec *V, int N)
  88. {
  89. FILE *file;
  90. file=fopen("vector.txt", "w");
  91. for(int i=0;i<100;i++)
  92. {
  93. for(int j=0;j<N;j++)
  94. fprintf(file,"[%d] ",V[i].coord[j]);
  95. fprintf(file,"\n");
  96. }
  97. fclose(file);
  98. }
  99. int main()
  100. {
  101. srand(time(NULL));
  102. struct NVec V[100];
  103. struct NVec X[100];
  104. for(int i=0; i<100; i++)
  105. V[i]=initialize(2);
  106. CompareVectors(V);
  107. SaveVectors(V,2);
  108. FILE *file;
  109. if((file=fopen("vector.txt.","r"))==NULL)
  110. {
  111. cout<<"Couldn't open the file!";
  112. exit(1);
  113. }
  114. char data;
  115. int i=0,j=0,k=0;
  116. char temp[3];
  117. data=getc(file);
  118. while(data!=EOF)
  119. {
  120. if(isdigit(data))
  121. {
  122. temp[k]=data;
  123. k++;
  124. }
  125. else if(data==']')
  126. {
  127. temp[k]='\0';
  128. k=0;
  129. X[i].coord[j]=atoi(temp);
  130. j++;
  131. }
  132. else if(data=='\n')
  133. {
  134. i++;
  135. j=0;
  136. }
  137. if(X[0].coord[0]==0)
  138. cout<<i<<" "<<j<<endl;
  139. data=getc(file);
  140. }
  141. fclose(file);
  142. cout<<endl<<"Writing the array"<<endl;
  143. for(int i=0;i<100;i++)
  144. {
  145. for(int j=0;j<2;j++)
  146. {
  147. cout<<X[i].coord[j]<<" ";
  148. }
  149. cout<<endl;
  150. }
  151. return 0;
  152. }
  153.  
Runtime error #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Couldn't open the file!