Ask Question

Assume that an array of ints named a has been declared with 12 elements. The integer variable k holds a value between 0 and 6.

Assign 15 to the array element whose index is k.

+1
Answers (1)
  1. 14 January, 00:00
    0
    The codes (written in Java) that implement the logic as described in the question are presented below:

    import java. util. Random; public class Main { public static void main (String[] args) { int a[] = new int[12]; Random rand = new Random (); int k = rand. nextInt (7); a[k] = 15; } }

    Explanation:

    An integer array with a specific size 12 is declared and named as a (Line 7).

    Since the variable k is expected to hold a value between 0 and 6, one way to achieve the aim is to randomly generate an integer between 0 to 6 and then assign it to the variable k. We can create a random integer within a specific range using Java Random Generator. The steps are as follows:

    import Java Random class (Line 1) create a Random object (Line 9) use nextInt () method of the Random object to generate a random integer. To ensure the generated integer is within the range 0 - 6, just simply pass 7 as the argument of the nextInt () method. nextInt (7) will generate an integer in the range of 0 < = x < 7 Assign generated random number to variable k (Line 10) Assign 15 to a[k] (Line 12)
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Assume that an array of ints named a has been declared with 12 elements. The integer variable k holds a value between 0 and 6. Assign 15 to ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers