Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP 3275 – Finishing Loops and Beginning Arrays Instructor: Diego Rivera-Gutierrez.

Similar presentations


Presentation on theme: "COP 3275 – Finishing Loops and Beginning Arrays Instructor: Diego Rivera-Gutierrez."— Presentation transcript:

1 COP 3275 – Finishing Loops and Beginning Arrays Instructor: Diego Rivera-Gutierrez

2 Administrative stuff Third Quiz on Friday (6/5) 5 questions. 1) Follow a piece of code including a for and switch statement. Provide output and variable values at the end. 2) Theory on arrays (things we will cover today) 3) Follow a piece of code with repeated print statements and provide output (doesn’t use loops or ifs or anything) 4) Take the piece of code in problem 3 and remove repetition using a loop. 5) Create a for loop to compute the powers of a number.

3 Administrative stuff Homework #3 is assigned. Minesweeper! Check the specification. You will need arrays which we are covering today. Our board size will be static for this homework 9x9 (the beginner board). We will receive a seed value for the random generator. THIS IS EXTREMELY IMPORTANT. Follow the specific process for assigning mines precisely! We will print only the solution to the output (no user interactivity yet)

4 While loops General structure while( ) { }

5 while( ) { } Remember! Order of execution of a for : if( ){ if( ){ … }

6 while( ) { } Order of execution of a while if( ){ if( ){ … }

7 Do-while loops General structure do { } while( ) ; In my opinion the MOST forgotten semicolon in all of C

8 do { } while( ) ; Remember! Order of execution of a while if( ){ if( ){ … }

9 do { } while( ) ; Order of execution of a do-while if( ){ if( ){ … }

10 Let’s do some catching of user errors using while loops! Let’s add a loop to our simple calculator to do some user error checking

11 Modulus % (again) 2%3… from the quiz.

12 Modulus (again) 2%3… from the quiz. Because of that 3! We have 3 positions 0 12

13 Modulus (again) 2%3… from the quiz. Because of that 3! We have 3 positions 0 12

14 Arrays

15 Groups of Data Sometimes we want to deal with groups of data that are related. Examples? Vectors Sets of numbers Lists of people Lists of objects

16 Declaring one-dimensional arrays Similar to declaring variables! [ ]; needs to be a constant in most C implementations. (not a variable) Examples: int vector[3]; float vectorf[3]; char name[100];

17 Accessing elements in an array float f; float vectorf[3]; How do I access each element one individually? f vectorf elements

18 Accessing elements in an array Very easy! If is an array [ ] refers to the element at

19 Accessing elements in an array [ ] has to be: An integer (not necessarily a constant, can be a variable) Between 0 (first element) and size-1 (last element)

20 Accessing elements in an array float vectorf[3]; vectorf 0 1 2

21 Initializing an array float vectorf[3]; Option 1: (avoid this one on the quiz!) vectorf[0] = 0.0f; vectorf[1] = 0.0f; vectorf[2] = 0.0f; vectorf 0 1 2

22 Initializing an array Option 2: (using loops) for (i = 0; i < 3; i++) { vectorf[i] = 0.0f; } Option 3: (literal value) float vectorf[3] = {0.0f, 0.0f, 0.0f};

23 Groups of Data Examples? Vectors Sets of numbers Lists of people Lists of objects More dimensions? Matrices Images/Photos Tables Minesweeper boards…

24 Multidimensional arrays [ ][ ]; [ ][ ][ ]; [ ][ ][ ][ ]; char board[9][9]; or int board[9][9];


Download ppt "COP 3275 – Finishing Loops and Beginning Arrays Instructor: Diego Rivera-Gutierrez."

Similar presentations


Ads by Google