Pesky Patterns

 

Modules - Structured Programming 2, Data Structures 1

 

Teacher’s Notes:

========================================================================

I use this at the beginning of the Data Structures unit because we will be using some of these number patterns with arrays.  Specifically, we use the patterns where we generate numbers from 1 to 13 four times (Pattern 3) to generate the card numbers for a deck of cards. 

We also use the pattern where we generate the numbers from 1 to 4 in groups of 13 (Pattern 4) to generate the suits in a deck of cards.  In this case, a 1 would be a spade, 2 would be a heart, etc.

You can add whatever patterns you would like the students to make.

Some of the number patterns might be too long for the page and might word wrap to the next line.  Students should know that the new line is from word wrapping and is NOT an end line command from the program.

The assignment here is written in C++ but to convert it to another language, you just have to change the output statements from cout to whatever syntax is appropriate for the language you are using.  The FOR loop may need modification as well depending on what language you are coding in.

 

Assignment:

========================================================================

 
Pesky Patterns
 
Patterns are very important in programming and problem solving.  In each of the following partial codes, erase the section labelled as /* BLANK */ and replace it with a single line of code that will generate the given pattern.
 
Pattern 1
 
0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
 
Code for Pattern 1
 
for(int x = 0; x < 52; /* BLANK */)
       cout << x << " ";
cout << endl;
 
 
Pattern 2

52 48 44 40 36 32 28 24 20 16 12 8 4 0 
 
Code for Pattern 2

for(/* BLANK */)
      cout << x << " ";
cout << endl;
 
 
Pattern 3

1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 
 
Code for Pattern 3
 
for(int x = 0; x < 52; x++)
      cout << /* BLANK */ << " ";
cout << endl;
 
 
Pattern 4
1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 
 
Code for Pattern 4
for(int x = 0; x < 52; x++)
      cout << /* BLANK */ << " ";
cout << endl;
  

 

 

 

Solution (JAVA):

========================================================================

 
 
public class Main
{
    public static void main(String[] args) {
 
        for(int x = 0; x < 52; x+=2)
            System.out.print(x + " ");
        System.out.println();
        
        for(int x = 52; x >= 0; x-=4)
            System.out.print(x + " ");
        System.out.println();
        
        for(int x = 0; x < 52; x++)
            System.out.print( x%13+1 + " ");
        System.out.println();
        
        for(int x = 0; x < 52; x++)
            System.out.print( x/13+1 + " ");
        System.out.println();
        
        
        
    }
}
 
 

Solution (C++):

========================================================================

 
#include <iostream>
using namespace std;
int main()
{
    for(int x = 0; x < 52; x+=2)
        cout << x << " ";
    cout << endl;
 
    for(int x = 52; x >= 0; x-=4)
        cout << x << " ";
    cout << endl;
 
    for(int x = 0; x < 52; x++)
        cout << x%13+1 << " ";
    cout << endl;
 
    for(int x = 0; x < 52; x++)
        cout << x/13+1 << " ";
    cout << endl;
 
    return 0;
}