Classwork 10: For Loops

Tuesday, July 31, 2012     


[Previous Classwork] [Next Classwork]


Complete the following:

  1. Create the file cw10.c for a classwork assignment and start your file as normal.
  2. You need to use the "for" loop construct in this classwork assignment
  3. You need to use the math library, this will mean you need to #include <math.h>
  4. When you compile your program, make sure you compile as below:

    linux3[26]% gcc -Wall cw10.c -lm -o cw10
    

  5. You are going to write a program to print out a sine wave (rotated)
  6. You will use the sin function from the math library (NOTE: sin takes on double argument, and returns a double result)
  7. You will calculate the result of sin() for inputs ranging from 0 to (2 * PI)
  8. If you want to work with degrees instead of radians, make inputs ranges from 0 to 360, then convert to radians, before passing to sin() by multiplying variable in degrees by PI and dividing by 180
  9. Use a #define to define PI to be 3.14159265
  10. On each line of output, print out a set of *'s showing the amplitude of the sin result
  11. The output of sin() will range from -1 to 1, you will scale up the value to be between -20 to 20 by multiplying the result of calling sin() by 20 before printing
  12. The output should look similar to:

    linux3[33]% ./cw10 | more
                                  *****
                                  **********
                                  **************
                                  *******************
                                  **********************
                                  *************************
                                  ****************************
                                  *****************************
                                  ******************************
                                  *****************************
                                  ****************************
                                  *************************
                                  **********************
                                  *******************
                                  ***************
                                  **********
                                  *****
    
                             *****
                        **********
                    **************
               *******************
            **********************
         *************************
      ****************************
     *****************************
    ******************************
     *****************************
      ****************************
         *************************
            **********************
               *******************
                   ***************
                        **********
                             *****
    

  13. When I wrote this, I got the positive amplitude working correctly, then worked on the negative amplitude.
  14. The easiest way I found to structure my program was to follow the general form:

    main()
    {
      // Loop from 0 to 360 degrees, incrementing by 10 degrees
      {
        // Calculate sin result
        // Scale result of calling sin
        // Determine how to print line
        // Use for loops to print spaces and *'s
      }
    }
    

  15. EXTRA CREDIT: Allow the user to select and modify two options, 1) whether to display sin or cos wave. 2) The factor used to scale the output for display. The scaling factor says how many *'s will be printed when sin/cos return 1.
  16. Once you are sure the program is done and working well, start a script using script
  17. Once the script is running, show you running the program once.
  18. Exit from the script, which will save the log typescript
  19. Once you are done, use submit to submit two files, cw10.c and typescript

    linux3[26]% submit cs104 cw10 cw10.c typescript
    

  20. After submitting your files, you can verify your files were submitted by using the submitls command
  21. MAKE SURE YOU LOGOUT AFTER SUBMITTING THE ASSIGNMENT AND BEFORE LEAVING CLASS!