Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main(void)
  5. {
  6.  
  7.     int fpArray[100]; // holds day1.txt elements
  8.     int i, new_i;
  9.     int total = 0;
  10.  
  11.     FILE *fp = fopen("day1.txt","r");
  12.  
  13.     while (fscanf(fp, "%d", &i) == 1) // fscanf goes through file until EOF, and assign read value to variable &i
  14.     {
  15.         printf("Module mass: %d\n", i);
  16.  
  17.         while (i >= 0)               // holds true while i (which holds the file contents) hits 0 or a negative number
  18.         {
  19.             new_i = ((i / 3) - 2);   // perform the calculations and store in new int variable
  20.             printf("\tReducing mass from %d to %d\n", i, new_i);
  21.             i = new_i;              // set i to the values inside new_i
  22.  
  23.             if (i > 0)              // if i is still greater than 0
  24.             {
  25.                 for (int x = 0; x < 100; x++)
  26.                 {
  27.                     printf("\tx = %d\n", x);     // not exactly sure what this is doing
  28.  
  29.                     new_i = ((i / 3) - 2);      // this will be performed up to 100 times as long as i > 0
  30.                     printf("\t\tReducing mass from %d to %d\n", i, new_i);
  31.                     i = new_i;                  // same as above
  32.  
  33.                     printf("\t\tTotal so far is %d, adding %d\n", total, i);
  34.                     total += i;               // maintain total
  35.                 }
  36.             }
  37.         }
  38.     }
  39.  
  40.     printf("%d\n", total);
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement