Question

In: Computer Science

Create a 6 by 6 checkered pattern without using a loop. Also use the wait and...

Create a 6 by 6 checkered pattern without using a loop. Also use the wait and offset properties. Create every checkered spot with a 1 second wait time. VBA Excel

Solutions

Expert Solution

Sub loops_exercise()

  Const NB_CELLS As Integer = 6 'Number of cells to which we want to add background colors

  '...

End Sub

STEP1:

Let's start out by adding a For loop to add black backgrounds to the cells in column A (The NB_CELLS constant being 6). See below:

Sub loops_exercise()

  Const NB_CELLS As Integer = 6 'Number of cells to which we want to add background colors

  For r = 1 To NB_CELLS 'r => row number

Cells(r, 1).Interior.Color = RGB(0, 0, 0) 'Black

  Next
  
End Sub

STEP2:

The next step is making every other cell's background red with an If instruction (based on whether the row numbers are even or odd). See below :

Sub loops_exercise()

  Const NB_CELLS As Integer = 6 'Number of cells to which we want to add background colors

  For r = 1 To NB_CELLS 'r => row number

If r Mod 2 = 0 Then 'Mod => is the remainder from division
Cells(r, 1).Interior.Color = RGB(200, 0, 0) 'Red
Else
Cells(r, 1).Interior.Color = RGB(0, 0, 0) 'Black
End If

  Next
  
End Sub

The condition If r Mod 2 = 0 means : if the remained when we divide r by 2 equals 0 ...

Only row numbers that are even will have a remainder of 0 when they are divided by 2.

STEP3:

Now create a loop that executes the loop we already have for the 6 columns. See below :

Sub loops_exercise()

  Const NB_CELLS As Integer = 6 '6x6 checkerboard of cells

  For r = 1 To NB_CELLS 'r => row number

  For c = 1 To NB_CELLS 'c => column number

If r Mod 2 = 0 Then
Cells(r, c).Interior.Color = RGB(200, 0, 0) 'Red
Else
Cells(r, c).Interior.Color = RGB(0, 0, 0) 'Black
End If
  
  Next
  Next
  
End Sub

(Notice the second loop is nested within the first one)

STEP4:

To achieve 6X6 chess board

replace

If r Mod 2 = 0 Then

With :

If (r + c) Mod 2 = 0 Then

All that's left to do is to edit the code so that the checkerboard is created starting from the currently selected cell (rather than A1). See below :

Sub loops_exercise()

Const NB_CELLS As Integer = 6 '6x6 checkerboard of cells
Dim offset_row As Integer, offset_col As Integer ' => adding 2 variables

'Shift (rows) starting from the first cell = the row number of the active cell - 1
offset_row = ActiveCell.Row - 1
'Shift (columns) starting from the first cell = the column number of the active cell - 1
offset_col = ActiveCell.Column - 1
  
For r = 1 To NB_CELLS 'Row number

  For c = 1 To NB_CELLS 'Column number

  If (r + c) Mod 2 = 0 Then
  'Cells(row number + number of rows to shift, column number + number of columns to shift)
Cells(r + offset_row, c + offset_col).Interior.Color = RGB(200, 0, 0) 'Red
  Else
Cells(r + offset_row, c + offset_col).Interior.Color = RGB(0, 0, 0) 'Black
  End If
  
  Next
Next
  
End Sub


Related Solutions

Assignment 2- 9   USING A FOR LOOP Create a PL/SQL block using a FOR loop to...
Assignment 2- 9   USING A FOR LOOP Create a PL/SQL block using a FOR loop to generate a payment schedule for a donor’s pledge, which is to be paid monthly in equal increments. The values variable for the block is starting payment due date, monthly payment amount, and a number of total monthly payments for the pledge. The list that’s generated should display a line for each monthly payment showing payment number, date due, payment amount, and donation balance (remaining...
ORACLE TASK 2-2 USING A FOR LOOP Create a PL/SQL block using a FOR loop to...
ORACLE TASK 2-2 USING A FOR LOOP Create a PL/SQL block using a FOR loop to generate a payment schedule for a donor’s pledge, which is to be paid monthly in equal increments. Values variable for the block are starting payment due date, monthly payment amount and number of total monthly payments for the pledge. The list that’s generated should display a line for each monthly payment showing payment number, date due, payment amount, and donation balance (remaining amount of...
Using for loop . Input an integer and identify whether it's EVEN OR ODD ( without...
Using for loop . Input an integer and identify whether it's EVEN OR ODD ( without using modulo operator ) using only <iostream> . C++ programming
Create a stimulation using the abstract factory pattern by using any of the topics/ideas you like.to...
Create a stimulation using the abstract factory pattern by using any of the topics/ideas you like.to use that subject. The scenario just has to make sense as an application using a factory and the related pieces of template/pattern. Your simulation should include, at a minimum: a. A driver class. b. A factory producer class with a method that returns the appropriate factory. c. An abstract factory interface with at least two abstract methods. d. At least two factory classes that...
create a program using IDLE where you will gather input from the user using a loop....
create a program using IDLE where you will gather input from the user using a loop. The user needs to provide you with a list of test scores for two categories (two different classrooms). Test scores must be integer values between 0 and 10. You need to process each score so that you can output the following: Number of test scores entered for classroom A. Number of test scores entered for classroom B. Average of test scores entered for classroom...
create a program using IDLE where you will gather input from the user using a loop....
create a program using IDLE where you will gather input from the user using a loop. The user needs to provide you with a list of test scores for two categories (two different classrooms). Test scores must be integer values between 0 and 10. You need to process each score so that you can output the following: Number of test scores entered for classroom A. Number of test scores entered for classroom B. Average of test scores entered for classroom...
Create a python code that calculates fixed point iteration method using a for loop.
Create a python code that calculates fixed point iteration method using a for loop.
Using C++ language, create a program that uses a struct with array variables that will loop...
Using C++ language, create a program that uses a struct with array variables that will loop at least 3 times and get the below information: First Name Last Name Job Title Employee Number Hours Worked Hourly Wage Number of Deductions Claimed Then, determine if the person is entitled to overtime and gross pay. Afterwards, determine the tax and net pay. Output everything to the screen. Use functions wherever possible. Bonus Points: Use an input file to read in an unknown...
-create a magic 8 ball program in Javascript. -use a loop to ask for the question...
-create a magic 8 ball program in Javascript. -use a loop to ask for the question -use a random number to get the answer let randAnswer = Math.round(Math.random()*10); -must have at least10 different answers -must use elseif or switch statement -must outputs both answers and user to input the console -program should repeat indefinitely until either blank input or cancel is selected
In C++, Create an array of 10 characters. Use a loop to prompt for 10 letters...
In C++, Create an array of 10 characters. Use a loop to prompt for 10 letters (a-z|A-Z) and store each in the array. If an invalid character is entered, re-prompt until a valid char is entered. After 10 characters are stored in the array, use a loop to print out all characters in the array from the first to the last element. Then, use another loop starting at the last element to print all characters in the array in reverse...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT