Tuesday, October 20, 2015

for loop test corrections

6)The array a has elements of data type int. The following code is intended to calculate the index of the first occurrence of the value that is least among all the elements of a, and to store that index in the variable minindex.
    int k;
    int minindex = 0;

    for ( k = 1 ; k < a.length ; k++ )
    {
      if ( condition )
        statement
    }
Which of the following replacements for condition and statement will cause the code to work as intended?


 condition statement
a[ k ] < minindex
minindex = a[ k ];
a[ k ] < a[ minindex ]
minindex = a[ minindex ];
a[ k ] < a[ minindex ]
minindex = k;
a[ k ] > minindex
minindex = a[ k ];
a[ k ] < minindex
minindex = k;
6) I got 7 wrong because I forgot that minindex stored an index and thus it had to refrence a to compare itself to a[k].

1 comment:

  1. So one way to begin to minimize this error, which is a common one, is to keep making it over and over until somehow you just begin to check for it as part of your thought process. I'm wondering if there is a quicker way to arrive at the same desired result? Would writing down a "check list" of common errors and committing the check list to memory help speed up learning? Just a thought...

    ReplyDelete