Question

In: Computer Science

Question 2. The following code defines an array size that sums elements of the defined array...

Question 2. The following code defines an array size that sums elements of the defined array through the loop.

  1. Analyze the following code, and demonstrate the type of error if found?
  2. What we can do to make this code function correctly?

#include <stdio.h>

#define A 10

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

int Total = 0;

int numbers[A];

for (int i=0; i < A; i++)

numbers[i] = i+1;

int i =0;

while (i<=A){

   Total += numbers[i];

   i++;

}

printf("Total =%d\n", Total);

return 0;

}

Submission

For this assignment, you need to write a report file of your work. The report should answer each question in details, providing outputs of the code to justify your answer.

Solutions

Expert Solution

Given Code :-

#include <stdio.h>

#define A 10

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

int Total = 0;

int numbers[A];

for (int i=0; i < A; i++)

numbers[i] = i+1;

int i =0;

while (i<=A){

   Total += numbers[i];

   i++;

}

printf("Total =%d\n", Total);

return 0;

}

Problem :- We know the value of A in above code 10 and the array(number) size is also 10. So, 10 position needs to be filled from index 0 to size-1=9. From the for loop we can understand the every index is filled.

Now when we are adding each number in array we have used while loop. The while is starting from 0 and ending at 10, i.e. it is running for 11 iteration and there no 10th index available in number array.

This problem is known as array out of bound error. In java, there is function that prevent the program from accessing array out of bound by generating a exception such as java.lang.ArrayIndexOutOfBoundsException.

But in case of c language there is no functionalaity available like the java. And if, programmer use the index out of the range then value at that index will be the garbage.

So the correct code is :-

#include <stdio.h>

#define A 10

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

int Total = 0;

int numbers[A];

for (int i=0; i < A; i++)

numbers[i] = i+1;

int i =0;

while (i<A){

   Total += numbers[i];

   i++;

}

printf("Total =%d\n", Total);

return 0;

}


Related Solutions

Question 2. The following code defines an array size that sums elements of the defined array...
Question 2. The following code defines an array size that sums elements of the defined array through the loop. Analyze the following code, and demonstrate the type of error if found? What we can do to make this code function correctly? #include <stdio.h> #define A 10 int main(int argc, char** argv) { int Total = 0; int numbers[A]; for (int i=0; i < A; i++) numbers[i] = i+1; int i =0; while (i<=A){    Total += numbers[i];    i++; }...
write the code that declares an array of booleans, called myarray, with size of 40 elements
write the code that declares an array of booleans, called myarray, with size of 40 elements
Using the C Programming language, write a program that sums an array of 50 elements. Next,...
Using the C Programming language, write a program that sums an array of 50 elements. Next, optimize the code using loop unrolling. Loop unrolling is a program transformation that reduces the number of iterations for a loop by increasing the number of elements computed on each iteration. Generate a graph of performance improvement. Tip: Figure 5.17 in the textbook provides an example of a graph depicting performance improvements associated with loop unrolling. Marking:- Optimize the code for an array of...
// Given an int array of size elements, determine if there are k elements that add...
// Given an int array of size elements, determine if there are k elements that add up to sum. // The array holds integers, both positive and negative and zero. // It is not possible to add zero elements (that's when k==0) to any sum, not even zero. // It is not possible to add any elements from an empty array. // Must be recursive and not iterative //bool K_element_sum(size_t k, int sum, int arr[], size_t size){}
The following code was meant to print out the elements in an array in reverse order....
The following code was meant to print out the elements in an array in reverse order. However, it does not behave correctly. public static void reverse(int[] a, int index) {       if (index == (a.length - 1))         System.out.printf("%d%n", a[index]);       else {         reverse(a, index); What does it do? Explain why it behaves in this way and There is more than one error in the code. Correct the code so that it will recursively print out the elements of...
c++ I need a code that will fill an array size of 1000, an array of...
c++ I need a code that will fill an array size of 1000, an array of size 2000, and an array size of 10000, with random int values. Basically like this: array1[1000] = filled all with random numbers array2[2000] = filled all with random numbers array3[10000] = filled all with random numbers C++ no need for print
1. Write a Python program that performs the following: 2. Defines an array of integers from...
1. Write a Python program that performs the following: 2. Defines an array of integers from 1 to 10. The numbers should be filled automatically without the need for user inputs 3. Find the sum of the numbers that are divisible by 3 (i.e., when a number is divided by 3, the remainder is zero) 4. Swap the positions of the maximum and minimum elements in the array. First, you need to find the maximum element as shown in the...
• In this script, write MATLAB code to create an array A of size 100 ×...
• In this script, write MATLAB code to create an array A of size 100 × 3. The first column should contain random numbers between 0 and 10. The second column should also contain random numbers between 0 and 10. The third column should contain random integers between 20 and 50. The first two columns represent the x and y coordinates of a map, respectively. The third column represents the height of buildings. For example, if the first row of...
Given an array A that contains the following elements in this order 45, 2, 18, 7,...
Given an array A that contains the following elements in this order 45, 2, 18, 7, 23, 47, 3, 15, 32: (a) Build a min-heap using the bottom-up (reverse) method, showing your work. How many comparisons do you make? (b) Build a min-heap using the top-down (forward) method, showing your work. How many comparisons do you make? (c) Are the two heaps that you get identical? Is this a coincidence of this particular instance? Or would the same happen for...
The following code was meant to print out the elements in an array in reverse order. However, it does not behave correctly.
  The following code was meant to print out the elements in an array in reverse order. However, it does not behave correctly. public static void reverse(int[] a, int index) {       if (index == (a.length - 1))         System.out.printf("%d%n", a[index]);       else {          reverse(a, index); What does it do? Explain why it behaves in this way.                                    There is more than one error in the code. Correct the code so that it will recursively print out...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT