Docsity

Developing Algorithms for Generating Square Patterns, Study Guides, Projects, Research of Programming Languages

Partial preview of the text

Download Developing Algorithms for Generating Square Patterns and more Study Guides, Projects, Research Programming Languages in PDF only on Docsity!

PROGRAMMING FUNDAMENTAL(CSC-103)

NAME Ali Mumtaz Maaz ajmal ENROLLMENT FA23-EEE- FA23-EEE- SUBMISSION DATE 27/3/ SUBMITTED TO Dr.Irfan Ullah SECTION EEE-

DEPARTMENT OF ELECTRICAL AND

COMPUTER ENGINEERING

Lab-Report 2

In Lab

In-Lab Task 1

Q: Develop an Algorithm

Following figure shows a pattern of squares generated for different input values of N (for N=0 to N=5). Develop a general algorithm to fill in the grid for any input N. Use the 4 step process that was discussed in the class (and is given below for reference) The four – step algorithm development process:

**1. Work an example yourself.

  1. Write down what you just did.
  2. Generalize your steps.
  3. Test your algorithm.**

N Red Blue Green x y x y x y (^0 0 0 0 1 1 ) 1 0 1 1 1 0 2 1 0 - - 2 0 2 0 2 0 3 1 2 1 1 2 1 2 1 2 0 - - - - 3 0 3 1 3 0 4 1 2 3 1 2 2 2 1 - - 4 0 3 0 - - - - 4 0 4 0 5 1 4 1 3 2 3 3 2 2 2 4 1 5 0 3 1 - - - - 4 0 - - - - 5 0 5 1 5 0 6 1 4 3 3 2 4 2 3 5 1 4 2 3 2 - - 6 0 4 1 - - - - 5 0 - - - - 6 0 6 0 7 1 6 1 5 2 5 3 4 2 4 4 3 5 2 3 3 6 1 7 0 4 2 - - - - 5 1 - - - - 6 0 - - - -

Write down what you just did If N is input then consider x and y two variables as output For Red:

  • Start x from 0 to N by adding 1
  • Start y from N to 0 by subtracting 1 For Blue: If N is even:
  • Start x from 0 to N by adding 2
  • Start y from N+1 to 1 by subtracting 2 If N is odd:
  • Start x from 1 to N by adding 2
  • Start y from N to 1 by subtracting 2 For Green: If N is even:
  • Start x from 1 to N+1 by adding 2
  • Start y from N to 0 by subtracting 2 If N is odd:
  • Start x from 0 to N+1 by adding 2
  • Start y from N+1 to 0 by subtracting 2 Generalize your steps: o Fill the blocks with red when (x+y) is equal to N o Now see whether N is even or odd o If N is even:
  • When x is even, fill it with blue
  • Otherwise fill it with green o If N is odd:
  • When x is odd, fill it with blue
  • Otherwise fill it with green