Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have this particular program in C which calculates the sum of squares of integers from 1 to 100 in two ways. The first way

I have this particular program in C which calculates the sum of squares of integers from 1 to 100 in two ways. The first way uses an array of 100 positions where in each element the square of the corresponding number from 1 to 100 is stored and then the elements of the array are added to find the final result. The second way uses the following closed mathematical relationship:

01: #include

02: #include

03:

04: int find_sum_squares(int);

05: void init_array(int size, int *array);

06:

07: int i;

08:

09: int main(void)

10: {

11: const int N = 100;

12: int *a;

13: int sum_array = 0, sum_formula = 0;

14:

15:

16: /* Allocate memory for array*/

17: a = (int *)malloc(N*4);

18:

19: /* Initialize array1 */

20: init_array(N, a);

21:

22: /* Find square of numbers and store it in the array */

23: for (i = 1; i <= N; i++)

24: {

25: a[i] = i*i;

26: }

27:

28: /* Sum the array elements */

29: for (i = 0; i <= N; i++)

30: {

31: sum_array = sum_array + a[i];

32: }

33:

34: /* Compare the two methods */

35: sum_formula = find_sum_squares(N);

36:

37: if (sum_array = sum_formula)

38: printf("Success! Both functions found: %d", sum_array);

39:

40: return 0;

41: }

42:

43: void init_array(const int size, int *array)

44: {

45: for (i = 0; i < size; i++)

46: {

47: array[i] = i+1;

48: }

49: }

50:

51: int find_sum_squares(int n)

52: {

53: n*(n+1)*(2*n+1)/6;

54: }

In the above code there are various errors, common or not, that a programmer would see before even running the code. The aim of the exercise is not simply to correct the errors empirically but to discover them following a specific methodology, which you should also present.

For the above program:

1) Run the code, record the results (give screenshots) and compare them to what was expected.

2) If we do not have the expected result, implement the following actions:

a. Estimate what are the variables whose changes should be tracked.

b. Insert statements in the code to print appropriate diagnostic messages to trace these variables. Provide the code with the print commands (make sure they are distinct from the rest commands, e.g. with yellow underlining), after first analyzing and document your reasoning.

c. Run the code again and record the results with the diagnostic messages (provide screenshots).

d. Locate the area of the code where the error is (and the error itself) by applying the backtracking technique. Start with the data causing the error and trace back through the code execution. Correct the error or errors you discover.

3) Once you have completed the above procedure having identified and corrected all possible errors, remove the print diagnostic messages commands and run the program again to ensure that no there is another error. However, there are also some errors in the code that do not affect its "correct" execution. These errors can only be detected by statically checking the code. Try to identify them and document the changes you will propose. Please provide its final code program.

Use the line numbering of the code given in the for statement refer to specific lines of code where you deem it necessary.

Even if half the questions are solved it will be ok, but if possible to solve all of them even better.

Thanks in advance.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What three major requirements resulted from the Sarbanes-Oxley Act

Answered: 1 week ago