SlideShare una empresa de Scribd logo
1 de 10
Descargar para leer sin conexión
CS111 Lab
Nested loop logic
Instructor: Michael Gordon
Conceptualize
Think of your printing space as a grid with
rows and columns
0

1

2

0

*

*

*

*

1

*

*

*

*

2

*

*

*

1

2

3

1

*

*

*

2

*

*

3

*

*
Simple right triangle
 Where

does a star appear?
 1,1; 2,1; 2,2; 3,1; 3,2; 3,3
 In each row, the col # with
a star starts at 1 and goes
until the row #.
 The same logic applies for a
larger grid.

1

2

1

*

2

*

*

3

*

3

*

1

2

*
3

1

*

2

*

*

3

*

*

*

4

*

*

*

4

*
Simple right triangle
 Rows

go from 1 to n.
 On each row we output a star in each
column until column equals row.
for(int r=1; r<=n; r++){
for(int c=1;c<=r;c++){
cout<<“*”;
}
cout<<endl;
}
Finding the middle
 You

don’t know what input size the user
will give, so how will you know where the
middle is?
 If n=3, then the middle is 2
 If n=5, then the middle is 3
1 2 3
 middle = n/2+1
1
*
1

2

3

2

*

1

*

3

*

2

*

4

*

3

*

5

*

4

5
Isosceles Triangle
 The

first thing you may notice
1
2
about this “triangle” is that it
1
doesn’t start at the top of the
2
*
grid. For the sake of space
3
*
*
(and other considerations)
let’s try to figure out (given the number of
columns) how many rows we need.

3

*
Column-to-row ratio
3

columns: 2 rows
 5 columns: 3 rows
 7 columns: 4 rows (not shown)
 rows = columns/2 + 1
 (same formula as finding
the middle)

1

2

3

1
2

*

3

*

1

2

*

3

*

4

5

1
2
3

*

4
5

*
*

*

*

*

*

*

*
Starting at Zero
 We

often start our rows and
columns at zero instead of
one.
 In that case, the formula for
the middle would be:
mid = n/2 instead of n/2+1
 At right: n = 3. 3/2=1 (int div)
and 1 is the middle column.

1

2

1

*

2

*

3

3

*

0

1

0

*

1

*

2

*

2
Center+diagonals
for (int r = 0; r <= rows; r++) {
for (int c = 0; c <= cols; c++) {
if (c==cntr+r || c==cntr-r || c==cntr)
cout << "*";
else cout << " ";
}
0

1

0

3

4

*

1
2

2

*
*

*
*

*
*
Center+diagonals
for (int r = 0; r <= rows; r++) {
for (int c = 0; c <= cols; c++) {
if (c>=cntr-r && c<=cntr+r) cout << "*";
else cout << " ";
}
cout<<endl;
}

0

1

0

3

4

*

1
2

2

*
*

*

*

*

*

*

*

Más contenido relacionado

La actualidad más candente (20)

3.6 notes
3.6 notes3.6 notes
3.6 notes
 
Coordinate Geometry
Coordinate GeometryCoordinate Geometry
Coordinate Geometry
 
Solving Systems
Solving SystemsSolving Systems
Solving Systems
 
Alegebra Powers Substitution
Alegebra Powers SubstitutionAlegebra Powers Substitution
Alegebra Powers Substitution
 
07 cube and cube roots
07 cube and cube roots07 cube and cube roots
07 cube and cube roots
 
October 21. 2014
October 21. 2014October 21. 2014
October 21. 2014
 
5 5a Slope Intercept from Point & Slope
5 5a Slope Intercept from Point & Slope5 5a Slope Intercept from Point & Slope
5 5a Slope Intercept from Point & Slope
 
Writing linear equations
Writing linear equationsWriting linear equations
Writing linear equations
 
Square roots
Square rootsSquare roots
Square roots
 
AA Section 5-7
AA Section 5-7AA Section 5-7
AA Section 5-7
 
Qa05 square root and cube root
Qa05 square root and cube rootQa05 square root and cube root
Qa05 square root and cube root
 
Expressions 2
Expressions 2Expressions 2
Expressions 2
 
Expressions
ExpressionsExpressions
Expressions
 
Understanding exponents
Understanding exponentsUnderstanding exponents
Understanding exponents
 
Nth term
Nth termNth term
Nth term
 
Cube Roots from CCSS
Cube Roots from CCSSCube Roots from CCSS
Cube Roots from CCSS
 
Pre Calculus Math.. Proving
Pre Calculus Math.. ProvingPre Calculus Math.. Proving
Pre Calculus Math.. Proving
 
Short Division: 3 Digit by 1 Digit (No Remainder)
Short Division: 3 Digit by 1 Digit (No Remainder)Short Division: 3 Digit by 1 Digit (No Remainder)
Short Division: 3 Digit by 1 Digit (No Remainder)
 
OTP basic
OTP basicOTP basic
OTP basic
 
Latihan 1 Matematika
Latihan 1 MatematikaLatihan 1 Matematika
Latihan 1 Matematika
 

Destacado (9)

Scope of variables
Scope of variablesScope of variables
Scope of variables
 
Writing edit free billing entries2 (3) (2)
Writing edit free billing entries2 (3) (2)Writing edit free billing entries2 (3) (2)
Writing edit free billing entries2 (3) (2)
 
Sage crm introduction by Triad Software
Sage crm introduction by Triad SoftwareSage crm introduction by Triad Software
Sage crm introduction by Triad Software
 
Functions
FunctionsFunctions
Functions
 
If statements
If statementsIf statements
If statements
 
Introduction to Linux, Pico, G++
Introduction to Linux, Pico, G++Introduction to Linux, Pico, G++
Introduction to Linux, Pico, G++
 
While loops
While loopsWhile loops
While loops
 
貧窮
貧窮貧窮
貧窮
 
Output
OutputOutput
Output
 

Similar a Shape logic 1

Oct8 - 131 slid
Oct8 - 131 slidOct8 - 131 slid
Oct8 - 131 slid
Tak Lee
 
Delaunay triangulation from 2-d delaunay to 3-d delaunay
Delaunay triangulation   from 2-d delaunay to 3-d delaunayDelaunay triangulation   from 2-d delaunay to 3-d delaunay
Delaunay triangulation from 2-d delaunay to 3-d delaunay
greentask
 
6.3 matrix algebra
6.3 matrix algebra6.3 matrix algebra
6.3 matrix algebra
math260
 
Coordinates y3 y4
Coordinates y3 y4Coordinates y3 y4
Coordinates y3 y4
daliamada
 
[Maths] arithmetic
[Maths] arithmetic[Maths] arithmetic
[Maths] arithmetic
Ourutopy
 

Similar a Shape logic 1 (20)

Oct8 - 131 slid
Oct8 - 131 slidOct8 - 131 slid
Oct8 - 131 slid
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
 
Number system
Number systemNumber system
Number system
 
Delaunay triangulation from 2-d delaunay to 3-d delaunay
Delaunay triangulation   from 2-d delaunay to 3-d delaunayDelaunay triangulation   from 2-d delaunay to 3-d delaunay
Delaunay triangulation from 2-d delaunay to 3-d delaunay
 
R_CheatSheet.pdf
R_CheatSheet.pdfR_CheatSheet.pdf
R_CheatSheet.pdf
 
Math blocks
Math blocksMath blocks
Math blocks
 
PVEB Tree.pptx
PVEB Tree.pptxPVEB Tree.pptx
PVEB Tree.pptx
 
time_complexity_list_02_04_2024_22_pages.pdf
time_complexity_list_02_04_2024_22_pages.pdftime_complexity_list_02_04_2024_22_pages.pdf
time_complexity_list_02_04_2024_22_pages.pdf
 
The Ring programming language version 1.9 book - Part 30 of 210
The Ring programming language version 1.9 book - Part 30 of 210The Ring programming language version 1.9 book - Part 30 of 210
The Ring programming language version 1.9 book - Part 30 of 210
 
Mrongraphs acm-sig-2 (1)
Mrongraphs acm-sig-2 (1)Mrongraphs acm-sig-2 (1)
Mrongraphs acm-sig-2 (1)
 
6.3 matrix algebra
6.3 matrix algebra6.3 matrix algebra
6.3 matrix algebra
 
Coordinates y3 y4
Coordinates y3 y4Coordinates y3 y4
Coordinates y3 y4
 
[Maths] arithmetic
[Maths] arithmetic[Maths] arithmetic
[Maths] arithmetic
 
Semana 31 matrices álgebra uni ccesa007
Semana 31 matrices  álgebra uni ccesa007Semana 31 matrices  álgebra uni ccesa007
Semana 31 matrices álgebra uni ccesa007
 
36 Matrix Algebra-x.pptx
36 Matrix Algebra-x.pptx36 Matrix Algebra-x.pptx
36 Matrix Algebra-x.pptx
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conq
 
The Ring programming language version 1.8 book - Part 28 of 202
The Ring programming language version 1.8 book - Part 28 of 202The Ring programming language version 1.8 book - Part 28 of 202
The Ring programming language version 1.8 book - Part 28 of 202
 
Arithmetic Progressions and the Construction of Doubly Even Magic Squares and...
Arithmetic Progressions and the Construction of Doubly Even Magic Squares and...Arithmetic Progressions and the Construction of Doubly Even Magic Squares and...
Arithmetic Progressions and the Construction of Doubly Even Magic Squares and...
 
Classical programming interview questions
Classical programming interview questionsClassical programming interview questions
Classical programming interview questions
 

Más de Michael Gordon (12)

Raspberry Pi presentation for Computer Architecture class
Raspberry Pi presentation for Computer Architecture classRaspberry Pi presentation for Computer Architecture class
Raspberry Pi presentation for Computer Architecture class
 
Strings2
Strings2Strings2
Strings2
 
Strings1
Strings1Strings1
Strings1
 
Introduction to Computer Science 111 Lab
Introduction to Computer Science 111 LabIntroduction to Computer Science 111 Lab
Introduction to Computer Science 111 Lab
 
Strings
StringsStrings
Strings
 
Arrays, continued
Arrays, continuedArrays, continued
Arrays, continued
 
Arrays
ArraysArrays
Arrays
 
For loops
For loopsFor loops
For loops
 
Arithmetic
ArithmeticArithmetic
Arithmetic
 
Variables
VariablesVariables
Variables
 
Word cloud
Word cloudWord cloud
Word cloud
 
Millennial white paper
Millennial white paperMillennial white paper
Millennial white paper
 

Shape logic 1

  • 1. CS111 Lab Nested loop logic Instructor: Michael Gordon
  • 2. Conceptualize Think of your printing space as a grid with rows and columns 0 1 2 0 * * * * 1 * * * * 2 * * * 1 2 3 1 * * * 2 * * 3 * *
  • 3. Simple right triangle  Where does a star appear?  1,1; 2,1; 2,2; 3,1; 3,2; 3,3  In each row, the col # with a star starts at 1 and goes until the row #.  The same logic applies for a larger grid. 1 2 1 * 2 * * 3 * 3 * 1 2 * 3 1 * 2 * * 3 * * * 4 * * * 4 *
  • 4. Simple right triangle  Rows go from 1 to n.  On each row we output a star in each column until column equals row. for(int r=1; r<=n; r++){ for(int c=1;c<=r;c++){ cout<<“*”; } cout<<endl; }
  • 5. Finding the middle  You don’t know what input size the user will give, so how will you know where the middle is?  If n=3, then the middle is 2  If n=5, then the middle is 3 1 2 3  middle = n/2+1 1 * 1 2 3 2 * 1 * 3 * 2 * 4 * 3 * 5 * 4 5
  • 6. Isosceles Triangle  The first thing you may notice 1 2 about this “triangle” is that it 1 doesn’t start at the top of the 2 * grid. For the sake of space 3 * * (and other considerations) let’s try to figure out (given the number of columns) how many rows we need. 3 *
  • 7. Column-to-row ratio 3 columns: 2 rows  5 columns: 3 rows  7 columns: 4 rows (not shown)  rows = columns/2 + 1  (same formula as finding the middle) 1 2 3 1 2 * 3 * 1 2 * 3 * 4 5 1 2 3 * 4 5 * * * * * * * *
  • 8. Starting at Zero  We often start our rows and columns at zero instead of one.  In that case, the formula for the middle would be: mid = n/2 instead of n/2+1  At right: n = 3. 3/2=1 (int div) and 1 is the middle column. 1 2 1 * 2 * 3 3 * 0 1 0 * 1 * 2 * 2
  • 9. Center+diagonals for (int r = 0; r <= rows; r++) { for (int c = 0; c <= cols; c++) { if (c==cntr+r || c==cntr-r || c==cntr) cout << "*"; else cout << " "; } 0 1 0 3 4 * 1 2 2 * * * * * *
  • 10. Center+diagonals for (int r = 0; r <= rows; r++) { for (int c = 0; c <= cols; c++) { if (c>=cntr-r && c<=cntr+r) cout << "*"; else cout << " "; } cout<<endl; } 0 1 0 3 4 * 1 2 2 * * * * * * * *