CpSc 1110 Fall 2016, Programming Assignment 1 Finding Square Root Due 11:59 pm Friday, September 30th 1. Overview For this assignment, you will write a program that will determine the square root of a number entered by the user. The C concepts used in this program include: loops, conditionals, random number generators, as well as printf & scanf, declaring variables, and using the math library. 2. More Specifics To determine the square root of a number, you will implement the method known as the Babylonian method, which was used by the Babylonian people of Mesopotamia maybe as early as 1900 BC. The Babylonian method involves making a guess for what the square root of a number might be, dividing the number by the guess, taking the average of those two numbers, and then using that average as the next guess. This method can be represented by the following formula: guessk+1 = ½(guessk + (N/guessk)) It turns out that the sequence produced converges very quickly to the square root of N. Write a program called findSquareRoot.c . Prompt the user to enter a number for which the square root will be determined. Use a random number generator to generate a guess to start with that will be between 1 and the number entered by the user. Write a loop that uses the above formula, keeping track of the number of iterations, to determine the square root of that number. Show the value entered by the user, each value of the new result, and how many steps it took to get to the square root. Have your program do this 3 times with 3 random guesses for the number entered by the user. The output should look similar to the following (user input in bold): Enter a number: 72 Using the math library, the square root of 72.000000 is 8.485281 step 1: 26.692308 step 2: 14.694857 step 3: 9.797265 step 4: 8.573127 step 5: 8.485731 step 6: 8.485281 Using the Babylonian method, the square root of 72 with a starting guess of 52 was found in 6 steps. step 1: 9.571429 step 2: 8.546908 step 3: 8.485504 step 4: 8.485281 Using the Babylonian method, the square root of 72 with a starting guess of 14 was found in 4 steps. step 1: 29.131579 step 2: 15.801562 step 3: 10.179037 step 4: 8.626199 step 5: 8.486432 step 6: 8.485281 Using the Babylonian method, the square root of 72 with a starting guess of 57 was found in 6 steps. The number of steps (should) vary depending on the starting guess value. Values for the input and intermediate results should be doubles, as well as the square root. The sqrt() function from the math.h library should be used to show before the steps what the square root is. But the calculation at each step should be accomplished with the formula shown above on the first page. You can see the value at the end of the loop does in fact match the value from using the sqrt() function. When you compile a program that uses any of the math functions from the math.h library, you must include the –lm flag. For example: gcc findSquareRoot.c –lm Notice that the values shown in each of the three runs that use the Babylonian method line up in two columns. You should format your output similarly. To ensure that your columns remain properly aligned, your second column should have a width of 15. To review output formatting placeholder from chapter 3 and lab 4: %[flags][width][.precision][length]type where the width represents the minimum number of characters to generate padding with leading spaces. 3. What to Hand In Your file MUST BE NAMED findSquareRoot.c and submitted on Vocareum by midnight Friday, September 30th. The code will be evaluated not only on its correctness but also on its adherence to the coding style standards “Programming Assignment Requirements” provided on Blackboard. You should also take a look at “Formatting Code Examples” also provided on Blackboard. Don’t forget to use meaningful variable names, and don’t forget about indentation and comments. 4. Notes on Collaboration You are required to work individually on this assignment. Please do not consult anyone other than your instructors or the lab assistants on any aspect of this assignment. Other tutors that are available may be able to help with more general C concepts that you may be struggling with, but not with questions specific to the assignment. Review the section on “Academic Integrity” on the Course Syllabus for the policy on what happens when copying or sharing code. For any of the programming assignments in lecture and/or lab, it is considered cheating to do any of the following: • discuss in detail the C code in your program with another student • show or share the C code in your program with another student • use C code obtained from another student, or any other unauthorized source, either modified or unmodified (each student is responsible for protecting his or her files from access by others) • use re-engineering tools • submit work of others, from the Internet or any other source, even if attributing the work to others 5. Grading Rubric for Phase 1 functionality formatting of code formatting of output random number generator comparison of doubles 70% (program works, implements Babylonian method 3 times for a number entered by user) 10% (header comment + other comments, indentation, meaningful variable names) 10% (output like the example shown, and values in columns that have width of 15) 5% (guesses between 1 and the number entered by user) 5% (way to ensure that the comparing doubles does not result in an infinite loop)
© Copyright 2026 Paperzz