Element Modification in Multi-Dimensional Arrays in Java - java

I have created a 2-D array in Java and I wish to change the value of each element.
Here is what I am trying to accomplish
-number each student from 1-10
-give each student 5 random marks from 40-100
int[][] students = new int[10][5];
Random numGen = new Random();
for (int i=0; i < students.length; i++){
students[i] = i; //Problem here..
for (int j=0; j<5; j++){
students[i][j] = numGen.nextInt(40)+61
}
}
I am having issues assigning each student a number from 1-10.
Where I wrote '//Problem here', is where the compiler keeps giving me trouble.
What is the appropriate method for modifying a single element in multi-dimension arrays?

Just use the index into the students (and return id as index+1 when you need to).
int[][] students = new int[10][5];
Random numGen = new Random();
for (int i=0; i < students.length; i++){
for (int j=0; j<5; j++){
students[i][j] = numGen.nextInt(40)+61
}
}

Array indices in java starts from 0. Which means 1st row is represented as 0. To edit an element in a 2D array you need to specify the both indices.
For example to edit an element at 5th row and 4th column you should use myArray[4][3]. Because you start counting from 0. myArray[0][0] is first element of the first row.
int[][] students = new int[10][5];
Random numGen = new Random();
for (int i=0; i < students.length; i++){
students[i][0] = i; //Change student[i] = i to student[i] = i+i
// because i starts from 0 but student number starts from 1.
for (int j=0; j<5; j++){
students[i][j] = numGen.nextInt(40)+61
}
}

Related

Separating an array into three

I currently have a 2d array, could anyone explain how i would extract the following:
the 0th to the 4th row the whole way down the array
then the 5th-10th row the whole way down the array
then the 11th-16th row the whole way down the array
I then want to hold these in 3 seperate arrays.
and then once I have carried out calculations on each individual array how do i sum them all back to one array?
This is the calculations I will carry out on the full array, that I would like to carry out on each individual array.
double[][] newjagssw = jaggedarr;
for(int i=0; i<jaggedarr.length; i++){
for(int j = 0; j<jaggedarr[i].length; j++){
newjagssw[i][j] = jaggedarr[i][j] - average[i];
double hold = newjagssw[i][j];
newjagssw[i][j] = Math.pow(hold, 2);
}
}
sst = new double[record];
for(int i = 0; i < sst.length; i++){
double hold = 0;
for(int j= 0; j < newjagssw[i].length; j++){
hold = hold + newjagssw[i][j];
}
sst[i] = hold;
System.out.println(probename[i] + ": " + ssw[i]);
}return ssw;
is there anyway of adapting it?
Thanks.

How to print a 2D array with random numbers

I am trying to write a program that prints a 2d array with random numbers ranging from 100-10000 and prints out the max number in the array,average,and min. The program will ask the user for the number of rows and column and print random numbers in that array.
Here is my code:
Random rand = new Random();
int randomnumber = rand.nextInt(9901) + 100;
Scanner console = new Scanner(System.in);
System.out.println("Enter row");
int n = console.nextInt();
System.out.println("Enter column");
int y = console.nextInt();
int[][] array = new int[n][y];
array[n][y] = randomnumber;
int k;
for (int i = 0; i <= array.length; i++) {
for (k = 0; k <= array[i].length; k++) {
System.out.print(array[i][k]);
}
}
If you want to fill the array with random values, you need to generate random values in a loop, and then write them to the array in that loop. So far you are only generating one value (and putting it in an invalid location).
Additionally, since arrays are 0-based, your loops should be for(i=0; i<arr.length; i++);, not <=.
Here's some code:
// don't declare k here
for(int i=0;i<array.length;i++){
for(int k=0;k<array[i].length;k++){
array[i][k]=rand.nextInt(9901)+100;
System.out.print(array[i][k]);
}
System.out.println(); // separate rows
}

Java 2-D selection sort

I am preparing for an exam, and we are likely to be tasked with sorting a two dimensional array of ints. Sorted meaning the first row, first column is the lowest and the last row, last column is the highest.
I approached the task by creating a 1-D array, and then populating it with all the values in the 2d array. Then, I passed it through a sorting method. Finally, I wrote each value of the sorted array back into the 2d array and returned it.
This sort of worked, as you can see from the output below. I am confused as to where my process is breaking down, I would appreciate any input.
Input:
int[][] nums = {{1,5,9},{8,9,3},{0,7,6}};
output:
0 0 0
0 0 0
0 0 1
public int[][] sort2D(int[][]nums){
int[] temp = new int[((nums.length+1)*(nums.length+1))];//make one long array
for(int i =0; i<nums.length; i++){ //populate
int counter = 0; //indices for long array
for(int j = 0; j<nums[i].length; j++){
System.out.println(temp[counter]);
temp[counter] = (int)nums[i][j];
counter++;
}
}
temp = sort(temp); //sort it (verified code)
for(int i = 0; i<nums.length; i++){ //reverse the above process
int counter = 0;
for(int j = 0; j<nums.length; j++){
nums[i][j] = temp[counter];
counter++;
}
}
return nums;
}
The first thing I think you're doing wrong is this:
int[] temp = new int[((nums.length+1)*(nums.length+1))];//make one long array
nums.length +1 = 4 in your case. you are copying your 3*3 array into a 4*4 array.
This has the funny effect of adding 5 0's to your result. (temp[9] till temp[15] will be 0 filled)
After you sort, these 0's will show up
the place where your code breaks down is:
for(int i =0; i<nums.length; i++){ //populate
>> int counter = 0; //indices for long array
for(int j = 0; j<nums[i].length; j++){
System.out.println(temp[counter]);
temp[counter] = (int)nums[i][j];
counter++;
}
}
You initialize the counter to 0 every time you go through one of your outer arrays.
this means that on the 3rd pass (last array) you overwrite temp[0], temp1 and temp[2] with nums[2][0], nums2, nums[2][2]
I assume in your actual code, you do
int counter = 0; //indices for long array
for(int i =0; i<nums.length; i++){ //populate
for(int j = 0; j<nums[i].length; j++){
System.out.println(temp[counter]);
temp[counter++] = (int)nums[i][j];
}
}
You could also use System.arrayCopy, especially if you know the length and suchlike.

2D Array in Java with number manipulation

My goal is to
Create a 5x5 array and fill it with random integers in the range of 1 to 25.
Print this original array
Process the array, counting the number of odds, evens, and summing all of the elements in the array.
Print the total odds, evens, and the sum.
Im not sure how to do it and my teacher is very confused and cannot help me. I was wondering if i could get some guidance.
Here is my code:
public class Processing {
public static void main(String[] args) {
Random Random = new Random();
int[][] Processing = new int[5][5];
for (int x = 0; x < 5; x++) {
int number = Random.nextInt(25);
Processing[x] = number;
}
for (int i = 0; i < 5; i++) {
Processing[i] = new int[10];
}
}
}
Please follow naming conventions for your variables. See here: http://en.wikipedia.org/wiki/Naming_convention_(programming)#Java
Anyways, you have to nest your loops as follows:
for(int i = 0; i < 5; i ++) {
for(int j = 0; j < 5; j++) {
yourArray[i][j] = random.nextInt(25);
}
}
i is the row number and j is the column number, so this would assign values to each element in a row, then move on to the next row.
I'm guessing this is homework so I won't just give away the answer to your other questions, but to set you on the right track, here's how you would print the elements. Again, use two nested loops:
for(int i = 0; i < 5; i ++) {
for(int j = 0; j < 5; j++) {
// print elements in one row in a single line
System.out.print(yourArray[i][j] + " ");
}
System.out.println(); //return to the next line to print next row.
}

Scanning for input without storing the values in a variable

I'm declaring a 2d Array with 100 rows and columns. Im trying to get the user to dictate the numbers that go into the array. Im supposed to store the values without storing them in a variable. This is what I have so far but I don't think this is correct
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int [][] nums = new int[100][100];
int digits;
for (int i = 0; i < nums.length; ++i)
{
int[scan.nextInt()][scan.nextInt()];
}
You'll need to use nested for loops for a 2-d array (one for rows and one for columns):
for (int i = 0; i < nums.length; ++i)
for (int j = 0; j < nums[i].length; ++j)
{
nums[i][j] = scan.nextInt();
}
Well, first of all, you are dealing with a two dimensional array, so you will need two loops, one for the rows and the other for the colums.
for(int i=0; i<100; i++)
{
for(int j=0;j<100;j++)
{
nums[i][j] = scan.nextInt();
}
}
This syntax - int[scan.nextInt()][scan.nextInt()]; is not even legal.

Categories