0% found this document useful (0 votes)
43 views

Arrays - One+ Multi Dimensional: Introduction To Programming CSE141 (3+1) Lab Dated: 23 Dec 2010

This document provides an introduction to one-dimensional, two-dimensional, and n-dimensional arrays in CSE141. It includes examples of sorting and searching arrays, practice problems involving modifying arrays, and an explanation of declaring and accessing multi-dimensional arrays. It also describes a simplified Minesweeper game that uses a 2D array to represent the game board and track bomb locations.

Uploaded by

Hammad Shams
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Arrays - One+ Multi Dimensional: Introduction To Programming CSE141 (3+1) Lab Dated: 23 Dec 2010

This document provides an introduction to one-dimensional, two-dimensional, and n-dimensional arrays in CSE141. It includes examples of sorting and searching arrays, practice problems involving modifying arrays, and an explanation of declaring and accessing multi-dimensional arrays. It also describes a simplified Minesweeper game that uses a 2D array to represent the game board and track bomb locations.

Uploaded by

Hammad Shams
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Introduction to Programming CSE141 (3+1)

Lab Dated: 23rd Dec 2010

Arrays – One+ Multi dimensional

Objective
This lab provides understanding of one dimensional , two dimensional and n dimensional arrays

// Example of built in function sort and Binary Search

int[] array= {12,1,3,-4,-5,2,3,7};


Arrays.sort(array);
for (int i=0;i<array.length;i++)
System.out.println(array[i]);
int index=Arrays.binarySearch(array, 12);
System.out.print(index);

Practice Questions:

1) Return a version of the given array where all the 10's have been removed. Replace the
10’s with zeros
2) Return a version of the given array where each zero value in the array is replaced by
the largest odd value to the right of the zero in the array. If there is no odd value to
the right of the zero, leave the zero as a zero.

Example:
zeroMax({0, 5, 0, 3}) → {5, 5, 3, 3}
zeroMax({0, 4, 0, 3}) → {3, 4, 3, 3}
zeroMax({0, 1, 0}) → {1, 1, 0}

Multi- dimensional Arrays:

Arrays may have more than one dimension, for such things as:

 a graphic image that is x pixels across and y pixels vertically


 weather information modeled in a 3-dimensional space, with measurements for these
axes: North/South, East/West, and altitude

Declare a multidimensional array as:

int[][] picture = new int[480][640];


int[][][] picture = new int[3][480][640];
// Printing Picture
char[][] imgData =
new char[][] {
{ ' ',' ',' ',' ',' ',' ',' ' },
{ ' ',' ',' ','0',' ',' ',' ' },
{ ' ',' ',' ','|',' ',' ',' ' },
{ ' ','0','-','+','-','0',' ' },
{ ' ',' ',' ','|',' ',' ',' ' },
{ ' ',' ',' ','0',' ',' ',' ' },
{ ' ',' ',' ',' ',' ',' ',' ' }
};

for (int row = 0; row < imgData.length ; row++ ) {


for (int col = 0; col < imgData[row].length; col++ ) {
System.out.print(imgData[row][col]);
}
System.out.println();
}

// Implementing Minesweeper

Minesweeper
This is a one player game of a simplified version of the popular computer game minesweeper. First,
the user is asked if he or she wants to play on a 5x5 grid or 10x10 grid. You have two 2-dimensional
arrays that contain information about your grid. An entry in the array should either contain a 0 or 1.
A 1 signifies that there is a bomb in that location, and a 0 if none.
For example, given the array:
int bombList5by5[][]={
{0, 0, 1, 0, 0},
{0, 0, 0, 0, 0},
{0, 1, 0, 0, 0},
{0, 0, 0, 1, 1},
{0, 1, 1, 0, 0}};
Given the bomb list, we have 6 bombs on our list. The bombs are located in (row,col)
cells, (0,2), (2,1), (3,3), (3,4), (4,1) and (4,2).
If the user chooses a cell that contains a bomb, the game ends and all the bombs are displayed. If the
user chooses a cell that does not contain a bomb, a number appears at that location indicating the
number of neighbours that contain bombs. The game should end when all the cells that do not
contain bombs have been marked (player wins) or when the user steps on a bomb(player loses).

Here's a sample output of the game, given the bombList5by5.


Welcome to Minesweeper!
Choose size of grid (Press 1 for 5x5, Press 2 for 10x10): 1
[][][][][]
[][][][][]
[][][][][]
[][][][][]
[][][][][]
Enter row and column of the cell you want to open[row col]: 1 1
[][][][][]
[ ] [2] [ ] [ ] [ ]
[ ] [ ] [] [ ] [ ]
[][][][][]
[][][][][]
Enter row and column of the cell you want to open[row col]: 3 2
[][][][][]
[ ] [2 ] [ ] [] [ ]
[][][][][]
[ ] [ ] [4 ] [ ] [ ]
[][][][][]
Enter row and column of the cell you want to open[row col]: 0 2
[] [ ] [ ] [ ] [ ]
[ ] [2] [ ] [] [ ]
[ ] [X ] [ ] [ ] [ ]
[ ] [ ] [4] [ ] [ ]
[][][][][]
Ooppps! You stepped on a bomb. Sorry, game over!

--------------------------------- The End----------------------------------------------------------

You might also like