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];
}
  • Baez Amada

    // 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];why is it that when we use a for loop we ask it to do i++ ( which is i = i + 1)we still have to tell it nums[i] += nums[i+1]; isnt this the same thing. why do we have to tel processing twice that we want to add I++

  • guest

    // 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];why is it that when we use a for loop we ask it
    to do i++ ( which is i = i + 1)we still have to tell it nums[i] +=
    nums[i+1]; isnt this the same thing. why do we have to tel processing
    twice that we want to add I++

  • Anonymous

    This is incrementing i, the counter  for the loop:

    i++;

    This is a different statement, not at all the same as the above.  It is adding the elements of the array themselves.

    nums[i] += nums[i+1];

  • guest

    sum+= nums[i]
    so every time1++,
    sum will add the number.
    en…
    however,compare to
    nums[i]+
    the only thing changed,i, is only there position in the array.
    it’s different..
    i c