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

Using Python, use the following list (Temperature = [56.2,31.8,81.7,45.6,71.3,62.9,59.0,92.5,95.0,19.2,15.0]) to: - Create a loop to iterate...
Using Python, use the following list (Temperature = [56.2,31.8,81.7,45.6,71.3,62.9,59.0,92.5,95.0,19.2,15.0]) to: - Create a loop to iterate through each of the elements in the temperature list. - Convert each element of this list to a Celsius temperature and then, for each valid temperature in the list, print out both the original Fahrenheit temperature and the Celsius equivalent in this format: "32 degrees Fahrenheit is equivalent with 0 degrees Celsius."
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...
Introduction to Java Programing Using Loop Create a simple calculator program using loop Ask user to...
Introduction to Java Programing Using Loop Create a simple calculator program using loop Ask user to input two numbers using scanner class Print the instruction of the menu for the calculator program Ask user to press 0 to Quit Ask user to press 1 to Add Ask user to press 2 to Substract Ask user to press 3 to Multiply Ask user to press 4 to Divide Perform correct calcuation based on user inputs and print the result Print error...
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
What do you understand by Circular wait? Is it possible to create a deadlock using only...
What do you understand by Circular wait? Is it possible to create a deadlock using only the Mutual exclusion? Explain in your own way.
In C++ using a single dimensional array Create a program that uses a for loop to...
In C++ using a single dimensional array Create a program that uses a for loop to input the day, the high temperature, and low temperature for each day of the week. The day, high, and low will be placed into three elements of the array. For each loop the day, high, and low will be placed into the next set of elements of the array. After the days and temps for all seven days have been entered into the array,...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT