x2311

Untitled

Jun 7th, 2022
741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.30 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. #define N 5
  8.  
  9. void populateArrayFromFile(FILE *myFile, const int *numberArray, int i);
  10.  
  11. /* Given a text file, the elements of which are real numbers a1, a2, a3, ..., an. Create a file
  12. real numbers b1, b2, b3, ..., bn, where*/
  13.  
  14. int fileIsOpen(FILE *file, char fileName[]) {
  15.     if (file == NULL) {
  16.         printf("%s is not open", fileName);
  17.         return -1;
  18.     }
  19. }
  20.  
  21. int main() {
  22.     int size = N;
  23.  
  24.     FILE *myFile;
  25.     myFile = fopen(INPUT, "r");
  26.     //if(fileIsOpen(myFile, INPUT) == -1) return -1;
  27.     //read file into array
  28.     int inputArray[size];
  29.  
  30.     for (int i = 0; i < size; i++) {
  31.         fscanf(myFile, "%d", &inputArray[i]);
  32.     }
  33.  
  34.     FILE *o = fopen(OUTPUT, "w");
  35.     //if(fileIsOpen(myFile, INPUT) == -1) return -2;
  36.     double outputArray[size];
  37.  
  38.     printf("\n");
  39.     float f;
  40.     for (int i = 0, t = 0; i < sizeof(inputArray) / sizeof(inputArray[0]); ++i, t = 0) {
  41.         for (int j = 0; j < i + 1; ++j) {
  42.             t += inputArray[j];
  43.         }
  44.  
  45.         f = (float) inputArray[i] / (float) (1 + t * t);
  46.         printf("%lf\n", (double) f);
  47.         outputArray[i] = f;
  48.  
  49.         fprintf(o, "%.2f ", outputArray[i]);
  50.     }
  51.     fclose(o);
  52. }
  53.  
  54.  
  55.  
Advertisement
Add Comment
Please, Sign In to add comment