x2311

Untitled

Jun 7th, 2022
900
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define INPUT "..\\tess.txt"
  6. #define OUTPUT "output.txt"
  7.  
  8. void populateArrayFromFile(FILE *myFile, const int *numberArray, int i);
  9.  
  10. /* Дано текстовий файл, елементами якого є дійсні числа a1, a2, a3, ..., an. Створити файл
  11. дійсних чисел b1, b2, b3,..., bn, де*/
  12.  
  13. int fileIsOpen(FILE *file, char fileName[]) {
  14.     if (file == NULL) {
  15.         printf("%s is not open", fileName);
  16.         return -1;
  17.     }
  18. }
  19.  
  20. int findSize() {
  21.     FILE *myFile;
  22.     myFile = fopen(INPUT, "r");
  23.     int size = 0;
  24.     int k = 0;
  25.     while (!feof(myFile)) {
  26.         size++;
  27.         fscanf(myFile, "%d", k);
  28.     }
  29.     fclose(myFile);
  30.     return size;
  31. }
  32.  
  33. int main() {
  34.  
  35.     int size = 5;
  36.  
  37.     FILE *myFile;
  38.     myFile = fopen(INPUT, "r");
  39.  
  40.     //read file into array
  41.     int inputArray[size];
  42.  
  43.     for (int i = 0; i < size; i++) {
  44.         fscanf(myFile, "%d", &inputArray[i]);
  45.     }
  46.  
  47.     for (int i = 0; i < size; i++) {
  48.         printf("%d\n", inputArray[i]);
  49.     }
  50.  
  51.     FILE *o = fopen("output.txt", "w");
  52.     double outputArray[size];
  53.  
  54.     printf("\n");
  55.     for (int i = 0; i < sizeof(inputArray) / sizeof(inputArray[0]); ++i) {
  56.         int t = 0;
  57.         for (int j = 0; j < i+1; ++j) {
  58.             t += inputArray[j];
  59.         }
  60.         printf("j[%d]: %d\n", i, t);
  61.  
  62.         float  d = (float)inputArray[i]/(float)(1+t*t);
  63.         printf("%lf\n", (double )d);
  64.         outputArray[i] = d;
  65.  
  66.  
  67.         fprintf(o, "%.2f ", outputArray[i]);
  68.     }
  69.     fclose(o);
  70. }
  71.  
  72.  
  73.  
Advertisement
Add Comment
Please, Sign In to add comment