• No results found

CSE 1321L: Programming and Problem Solving I Lab Lab 9 Multi-Dimensional Arrays

N/A
N/A
Protected

Academic year: 2021

Share "CSE 1321L: Programming and Problem Solving I Lab Lab 9 Multi-Dimensional Arrays"

Copied!
5
0
0

Loading.... (view fulltext now)

Full text

(1)

CSE 1321L: Programming and Problem Solving I Lab Lab 9

Multi-Dimensional Arrays

Lab9A: N by N

Now we have moved from 1-D arrays onto 2-D arrays; please keep in mind that while with 1-D arrays we used one for-loop to traverse the array, with 2-D arrays we need to use a nested for-loop to traverse it.

Ideally, a rule of thumb, is that the outer loop (the “i” loop) would be representative of the row number, and the inner loop (the “j” loop) would be representative of the column number. Together the row number and column number decide what cell is being executed on.

Create a simple program that asks the user for a row and column size for a 2-D integer array, have the program then create such an array. At this point this array’s cells should be filled with zeros since nothing has been done to change the cell values.

Next, have that program dynamically adapt to the sizes of the array (that were provided by the user) and fill the array with numbers ascending from 1 → n (n being the number of the nth cell in that 2-D Array depending on the size). Please then print the array out. Remember, you cannot just print an array out, you have to use loops and print each cell individually.

Please refer to the sample output below for visualization and match its style.

As always:

• Remember, the class name should be Lab9A.

• The user input is indicated in bold.

Sample Output #1:

Please enter the number of rows: 4 Please enter the number of columns: 4

I have 4 rows and 4 columns. I need to fill-up 16 spaces.

The 4x4 array:

1|2|3|4|

5|6|7|8|

9|10|11|12|

13|14|15|16|

Sample Output #2:

Please enter the number of rows: 3 Please enter the number of columns: 2

I have 3 rows and 2 columns. I need to fill-up 6 spaces.

The 3x2 array:

1|2|

3|4|

5|6|

(2)

Lab9B: Lab9A, but now with more math

For this exercise you will be writing a program that will be building upon the previous program. So please make a new class file, copy, and rename the code you have already written for Lab9A. Please also remember to rename the file and (and in the case of Java and C# students) class name to Lab9B.

For this exercise, please calculate two values from all of the numbers in the two dimensional array. The first value should be the sum of all numbers in the array. This should be stored in a separate “int” variable that is created outside the nested FOR loops. The second value should be the total average of all numbers in the Array. This should be stored in a separate “float” variable, also outside the nested FOR loop structure.

So, in short:

• Display the sum of the values in the array

• Then display the average of the values in the array

Please refer to the sample output below for visualization and match its style.

As always:

• Remember, the class name should be Lab9B.

• The user input is indicated in bold.

Sample Output #1:

Please enter the number of rows: 3 Please enter the number of columns: 2

I have 3 rows and 2 columns. I need to fill-up 6 spaces.

The 3x2 array:

1|2|

3|4|

5|6|

The sum of all the numbers in the 3x2 array: 21 The average of all the numbers in the 3x2 array: 3.5 Sample Output #2:

Please enter the number of rows: 4 Please enter the number of columns: 4

I have 4 rows and 4 columns. I need to fill-up 16 spaces.

The 4x4 array:

1|2|3|4|

5|6|7|8|

9|10|11|12|

13|14|15|16|

The sum of all the numbers in the 4x4 array: 136 The average of all the numbers in the 4x4 array: 8.5

(3)

Lab9C: 2D or not 2D

For this exercise you will also be writing a program that will be building upon Lab9A. So please make a new class file, copy, and rename the code you have already written for Lab9A. Please also remember to rename the file and (and in the case of Java and C# students) class name to Lab9C.

For this exercise, please take the array that was filled up with values and flatten it i.e., convert the 2-D array to a 1-D array that contains the same values. Please do not just print the 2-D array to look like a 1-D array

Hint: You will have to take the 2-D array’s total amount of cells and make a 1-D array of the same size.

Please refer to the sample output below for visualization and match its style.

As always:

• Remember, the class name should be Lab9C.

• The user input is indicated in bold.

Sample Output #1:

Please enter the number of rows: 4 Please enter the number of columns: 4

I have 4 rows and 4 columns. I need to fill-up 16 spaces.

The 4x4 array:

1|2|3|4|

5|6|7|8|

9|10|11|12|

13|14|15|16|

The 4x4 2-D array flattened into a 16 cell 1-D array:

1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|

Sample Output #2:

Please enter the number of rows: 3 Please enter the number of columns: 2

I have 3 rows and 2 columns. I need to fill-up 6 spaces.

The 3x2 array:

1|2|

3|4|

5|6|

The 3x2 2-D array flattened into a 6 cell 1-D array:

1|2|3|4|5|6|

Instructions:

• Programs must be working correctly.

Programs must be saved in files with the correct file name.

• If working in Java or C#, class names must be correct.

• Programs must be working and checked by the end of the designated lab session.

(4)

Appendix – Referencing row or column length C# int[][] grid = new

int[4][5];

To get the row length use arrayname.GetLength(0) E.g.: grid.GetLength(0)

To get the column length use arrayname.GetLength(1) E.g.: grid.GetLength(1)

Java:

int[][] grid = new int[4][5];

To get the row length use arrayname.length E.g.: grid.length

To get the column length use arrayname[0].length E.g.: grid[0].length

(5)

Appendix for C++ Students

One of the most common (and simplest) file formats for storing data is “comma separated values” – or .csv files. You can make these in Microsoft Excel or something even simpler like Notepad or TextEdit. They look like this:

1, 2, 3 4, 5, 6 7, 8, 9

Each data element is separated from the others by comma; note that there’s no comma at the end of the line.

You’ve probably used getline a lot now, but there’s a variation of it that you probably haven’t used. If we pass it three parameters instead of two, the third represents the “delimiter” between the data elements which, in this case, is the comma. However, getline gives us a string, not a number – so we have to convert it to a number using “stoi” (or “string to int”). The code below reads from a file called “matrices.csv” which is located in the “Debug” folder of the project (so it can be found). Note: make this file before coding.

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

void main() {

// Create a filestream object – just like last lab.

fstream fs;

// Open the file for reading using “in”.

fs.open("matrices.csv", fstream::in);

// When we read in from a file, it's a string (s). //

We have to convert that string to a number (i).

string s; int i; int sum = 0;

// While we can read a row/string from the file... continue processing each row while (getline(fs, s, ',')) { // Get the first string, reading up until a comma (1st)

i = stoi(s); sum += i; // Convert it to an int and add to sum cout << i <<"|"; // Print it out

getline(fs, s, ','); // Get the next value, reading until a comma (2nd) i = stoi(s); sum += i; // Convert it to an int...

cout << i << "|"; // Print it out

getline(fs, s); // Get the remainder of the string (3rd) i = stoi(s); sum += i;

cout << i << endl;

}

cout << sum << endl;

// Close the file fs.close(); cin >> s;

// Convert it to an int...

}

References

Related documents

Implementation of the findings of the survey are largely a matter for the ANI, however the Australian Government Department of Agriculture will maintain an interest in the report

• Follow up with your employer each reporting period to ensure your hours are reported on a regular basis?. • Discuss your progress with

For the broad samples of men and women in East and West Germany every first graph in Fig- ure 1 to 4 (Appendix) shows considerable and well-determined locking-in effects during the

The corona radiata consists of one or more layers of follicular cells that surround the zona pellucida, the polar body, and the secondary oocyte.. The corona radiata is dispersed

○ If BP elevated, think primary aldosteronism, Cushing’s, renal artery stenosis, ○ If BP normal, think hypomagnesemia, severe hypoK, Bartter’s, NaHCO3,

2) Rotate manual shift lever back toward rear of vehicle 2 notches to Neutral position. Ensure gearshift is in Neutral. While holding manual shift lever lightly toward the

organisasjonslæring, arbeidsplasslæring, uformell og formell læring, læring gjennom praksis, sosial praksis og så videre vil derfor være nyttige når man skal foreta en studie

Two sample sites were selected from each of the strata of the Custom Lighting projects and one sample site was selected from each of the strata for the Other measures and