Assuming an array of 10 integers, that is,
int[] nums = {5,4,2,7,6,8,5,2,8,14}; 
Write code to perform the following array operations (Note that the number of clues vary, just because a [____] is not explicitly written in does not mean there should not be brackets).
// Square each number ((i.e., multiply each by itself) 
for (int i ___; i < _____; i++)  {   
  ____[i] = ______*_____;  
}   
 

// Add a random number between zero and 10 to each number.  
_________________________________  
  _____ += int(________);  
__

// Add to each number the number that follows in the array. Skip the last value in the array.  
for (int i = 0; i < _____; i++)  {   
  _____ += ______[____];  
}

// Calculate the sum of all the numbers.  
_____ ________ = ____;  
for (int i = 0; i < nums.length; i++) {   
  ______ +=  ________;  
}
Answers:
int[] nums = {5,4,2,7,6,8,5,2,8,14}; 

// Square each number ((i.e., multiply each by itself) 
for (int i = 0; i < nums.length; i++)  {   
  nums[i] = nums[i]*nums[i];  
}   
 
// Add a random number between zero and 10 to each number.  
for (int i = 0; i < nums.length; i++)  {   
  nums[i] += int(random(10));  
}

// Add to each number the number that follows in the array. Skip the last value in the array.  
for (int i = 0; i < nums.length-1; i++)  {   
  nums[i] += nums[i+1]; 
}

// Calculate the sum of all the numbers.  
int sum = 0;
for (int i = 0; i < nums.length; i++) {   
  sum += nums[i];
}


»

No comments yet.

Leave a comment