Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am having problems solving this: (C++ Program-recusion) The ‘hailstones’ use 1

ID: 3588361 • Letter: I

Question

I am having problems solving this: (C++ Program-recusion)

The ‘hailstones’ use 1859 and 119570. Print the number of iterations needed to reach the base case. Algorithm given below:

Douglas Hofstadter's Pulitzer-prize-winning book, Gödel, Escher, Bach, poses the following mathematical puzzle:

Pick a positive integer n as the start.

If n is even, divide it by 2.

If n is odd, multiply it by 3 and add 1.

Continue this process until n is 1.

The number n will travel up and down but eventually end at 1 (at least for all numbers that have ever been tried -- nobody has ever proved that the sequence will terminate). Analogously, hailstone travels up and down in the atmosphere before eventually landing on earth.
The sequence of values of n is often called a Hailstone sequence, because hailstones also travel up and down in the atmosphere before falling to earth

Explanation / Answer

#define read_buffer_size 8192


int main(int argc, char* argv[])
{

   //file descriptor for opening and closing

   int file_descriptor; //size of buffer for file reading
   char read_buffer[read_buffer_size];
   ssize_t read_bytes;


   //looping through all files given in argument
   int i;
   for(i=1;i    {
           //using linux system call open file with readonly flag
       file_descriptor = open (argv [i], O_RDONLY);
           //if file not opened
       if (file_descriptor == -1)
       {
           perror ("open");
           printf("file can not be opened : %s ",argv[i]);
       }
  
           else
           {
               while((read_bytes = read (file_descriptor, &read_buffer, read_buffer_size)) > 0)
               {  
                   printf("%s",read_buffer);
               }
               //once done with file closig the file using file descriptor

               close (file_descriptor);
           }
     }

   return (EXIT_SUCCESS);
}