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;

}

Solutions

Expert Solution

Answer

Here is your answer, if you have any doubt please comment, i am here to help you.

1)

#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;

}

When this code run in c, we will get the following error.

actual expect result is 55.

This happen because the following code


//This is the error happening statement in the above program
//In C there is no exception happening in indexoutofbound
//it will access some other memory location allotted for the array.
//That means indexoutofbound will return some garbage value here.

while (i<=A){  //limit should be lessthan 10 and not equal 10, because index start at 0, 
//in the assignment statement, we can see we only get value <10 not <=10. So,
//If access the index of 10 will be garbage.
   Total += numbers[i];

   i++;

}

Access non allocated location of memory: The program can access some piece of memory which is owned by it.

2) To make it functional just use the 'less than'(<) not use 'less than or equal'(<=) in while loop.

Here is final code and result.

#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){  //just change here < instead of <=

   Total += numbers[i];

   i++;

}

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

return 0;

}

output

Any doubt please comment,

Thanks in advance


Related Solutions

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...
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...
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...
Write a Pseudo Code to send an Array of 20 elements from 8051 to the computer...
Write a Pseudo Code to send an Array of 20 elements from 8051 to the computer via serial port at maximum baud rate possible with XTAL=11.0592MHz.
Write a java code segment to declare an array of size 10 of type String and...
Write a java code segment to declare an array of size 10 of type String and read data for them from a file, prompt user for the file name. Data is stored in a file with 1 string per line.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT