Question

In: Computer Science

Could you modify the code make output arranged into 5x5, like this: 1 2 3 4...

Could you modify the code make output arranged into 5x5, like this:

1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

16 17 18 19 20

21 22 23 24 25

The code is:

import java.util.*;

public class RandomGen {

public static void main(String[] args) {
int[][] ArrayRand = new int [5][5];
Random rand = new Random();
int sum = 0;
System.out.println("The generated random matris is: ");
  
for(int i = 0; i < ArrayRand.length;i++){
for(int j = 0; j < ArrayRand[i].length;j++){
ArrayRand[i][j] = rand.nextInt(100)+1;
System.out.print(" "+ ArrayRand[i][j]);
sum += ArrayRand[i][j];
}
}
System.out.println("\nThe sum is: "+ sum);
System.out.println();
}
  
}

Solutions

Expert Solution

We just have to add a System.out.println() statement after it finishes printing a row outside the inner for loop

import java.util.*;

public class RandomGen {

public static void main(String[] args) {
int[][] ArrayRand = new int [5][5];
Random rand = new Random();
int sum = 0;
System.out.println("The generated random matris is: ");

for(int i = 0; i < ArrayRand.length;i++){
for(int j = 0; j < ArrayRand[i].length;j++){
ArrayRand[i][j] = rand.nextInt(100)+1;;
System.out.print(ArrayRand[i][j]+" ");
sum += ArrayRand[i][j];
}
System.out.println();//add a new line before next row
}
System.out.println("\nThe sum is: "+ sum);
System.out.println();
}
  
}

The output is

DO give thumbs up and in case there are doubts leave a comment.


Related Solutions

Modify the following code to make the input and output look like this. Input 5 Vader...
Modify the following code to make the input and output look like this. Input 5 Vader 1300 Kirk 1250 Adama 1000 Reynolds 1615 Oneill 1470 Output Enter the number of candidates: 5 Enter candidate's name :Vader 1300 Enter votes received :Enter candidate's name :Kirk 1250 Enter votes received :Enter candidate's name :Adama 1000 Enter votes received :Enter candidate's name :Reynolds 1615 Enter votes received :Enter candidate's name :Oneill 1470 Enter votes received : Name Votes Percentage Vader 1300.00 19.59% Kirk...
modify code to write the output as an HTML table to a file in the output...
modify code to write the output as an HTML table to a file in the output directory. The file that is saying to work at : SOURCE CODE IN PERL: print "Enter principal amount: "; $P=; while($P<=0) { print "Principal must be positive. Try again: "; $P=; } print "Enter number of times interest is applied in a year: "; $n=; while($n!=12 && $n!=4 && $n!=2 && $n!=1) { print "It must be 12, 4, 2 or 1. Try again:...
The code file is attached. Portion of the code is given (1, 2, 3, 4). You...
The code file is attached. Portion of the code is given (1, 2, 3, 4). You have to write the code for the remaining items (5, 6, 7). #include <iostream> using namespace std; struct node { int data; node* next; }; node* head=NULL; void appendNode(int value) { node *newNode, *curr; newNode = new node(); newNode->data=value; newNode->next=NULL; if (!head) head=newNode; else { curr=head; while (curr->next) curr = curr->next; curr->next = newNode; } } void insertNode(int value) { node *newNode, *curr, *previous;...
a_list = [1, 2, 3, 4, 5] list_index = 7 #----------------------------------------------------------- #You may modify the lines...
a_list = [1, 2, 3, 4, 5] list_index = 7 #----------------------------------------------------------- #You may modify the lines of code above, but don't move them! #When you Submit your code, we'll change these lines to #assign different values to the variables. #In this problem, we're going to use some unfamiliar syntax. #You'll learn more about this syntax in Unit 4. For now, #though, you don't need to understand the syntax. All you #need to know is that right now, this code will...
MATLAB question! 4. (a) Modify the code, to compute and plot the quantity E = 1/2mv^2...
MATLAB question! 4. (a) Modify the code, to compute and plot the quantity E = 1/2mv^2 + 1/2ky^2 as a function of time. What do you observe? Is the energy conserved in this case? (b) Show analytically that dE/dt < 0 for c > 0 while dE/dt > 0 for c < 0. (c) Modify the code to plot v vs y (phase plot). Comment on the behavior of the curve in the context of the motion of the spring....
Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 +...
Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 + … 1/n. Let’s estimate natural using n = 20. sum_thread.cpp #include <chrono> #include <iostream> #include <mutex> #include <random> #include <utility> #include <vector> #include <thread> using namespace std; constexpr long long size= 1000000; mutex myMutex; void sumUp(unsigned long long& sum, const vector<int>& val, unsigned long long beg, unsigned long long end){ long long localSum = 0; for (auto it= beg; it < end; ++it){ localSum+=...
Fill in the blanks of the following segment of code, so that the output would be 1 3 4.
Fill in the blanks of the following segment of code, so that the output would be 1 3 4.int count = 0;do{++ count;if (count == 2)Blank;cout << count << " ";} while (count <= Blank);cout << endl;
Prove that if the integers 1, 2, 3, . . . , 65 are arranged in...
Prove that if the integers 1, 2, 3, . . . , 65 are arranged in any order, then it is possible to look either left to right or right to left through the list and find nine numbers that are in increasing order
Currently the code outputs 3. Is there a way to make it output 32 writing logic...
Currently the code outputs 3. Is there a way to make it output 32 writing logic in the parseint and not the main function? #include <stdio.h> #include <ctype.h> int main() { printf("%d\n", parseint("32")); return 0; } int parseint(char *str) { int i=0; while(str[i] != '\0'){ return (str[i] - '0'); i++; } return 1;   
please use linux or unix to complete, and include pictures of the output. Modify the code...
please use linux or unix to complete, and include pictures of the output. Modify the code below to implement the program that will sum up 1000 numbers using 5 threads. 1st thread will sum up numbers from 1-200 2nd thread will sum up numbers from 201 - 400 ... 5th thread will sum up numbers from 801 - 1000 Make main thread wait for other threads to finish execution and sum up all the results. Display the total to the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT