Docsity

Arrays in C Programming, Slides of Computer Science

Partial preview of the text

Download Arrays in C Programming and more Slides Computer Science in PDF only on Docsity!

Arrays

サルバド - ル・フロランテ

/* Read Chapter 1 of the Course Notes for details. */

Visual Examples of Arrays 2 https://pixabay.com/vectors/chessboard-chess-board-game-29630/ https://en.wikipedia.org/wiki/Rubik%27s_Cube#/media/ File:Rubiks_cube_solved.jpg https://www.photos-public-domain.com/2011/08/30/truth/

Visual Examples of Arrays 4 https://pixabay.com/vectors/chessboard-chess-board-game-29630/ https://en.wikipedia.org/wiki/Rubik%27s_Cube#/media/ File:Rubiks_cube_solved.jpg https://www.photos-public-domain.com/2011/08/30/truth/ 2D array 8 rows by 8 columns = group of 64 squares

1 st^ row

8 th^ row

st

col 8 th^ col

Visual Examples of Arrays 5 https://pixabay.com/vectors/chessboard-chess-board-game-29630/ https://en.wikipedia.org/wiki/Rubik%27s_Cube#/media/ File:Rubiks_cube_solved.jpg https://www.photos-public-domain.com/2011/08/30/truth/

1 st^ row

8 th^ row

st

col 8 th^ col

3D array 3 (length) by 3 (width) by 3 (height) = group of 27 small cubes

Q: How do you declare an array in C?

  • 1D array // also called “list” [ ]
  • 2D array // also called “table”, “matrix” [ ][]
  • 3D array [ ][][] size must be a non- negative integer

Example: Array Declarations

// example 1D array declaration

char str[5]; int A[5], B[10]; float Grades[40]; double D[1000]; name : str dimensionality : 1D size : 5 element type : char

Example: Array Declarations

// example 1D array declaration

char str[5]; int A[5], B[10]; float Grades[40]; double D[1000];

// example 2D array declaration

char CrossWord[10][10]; // 10 rows x 10 columns int Sudoku[9][9]; float Table[2][4]; double Matrix[3][5];

// example 3D array declaration

int RubikCube[3][3][3]; name : RubikCube dimensionality : 3D size : 3x3x element type : int

Exercise: Declare array variables for the ff. https://pixabay.com/vectors/chessboard-chess-board-game-29630/ https://www.photos-public-domain.com/2011/08/30/truth/ https://en.wikipedia.org/wiki/Rubik%27s_Revenge# /media/File:Rubiks_revenge_solved.jpg https://www.goodfreephotos.com/other- photos/stack-of-flat-rocks.jpg.php

Q: How do you visualize/draw a 1D Array? Option 1: draw it vertically

Q: How do you visualize/draw a 1D Array? Option 2: or draw it horizontally

Q: How do you visualize/draw a 1D Array? Drawing an array of floating point values

Q: How do you access an array element? Declaration: int A[8];

Q: How do you access an array element? index is from 0 to size - 1 Declaration: int A[8];

Q: How do you access an array element? Access an element by giving the array name, and its index enclosed in [ ]. A[0] A[3] A[7] A[4] A[5] A[6] A[2] A[1] Declaration: int A[8];