USING DR.JAVA Extra Credit (worth an extra 30 points Only do this extra credit i
ID: 3717199 • Letter: U
Question
USING DR.JAVA
Extra Credit (worth an extra 30 points Only do this extra credit if you have completely done all of the above. Do this in a separate file and call it PA4ExtraCredit. Declare a two- dimensional array of type integers (call it Numbers) and make it of size 10 X 10. Populate the array using a random number generator for numbers between 1 and 100. Sum up the left to right down diagonal elements of this array. For example, in the array below, you would sum up the numbers 10, 48, 4, and 63 (those highlighted) 10 2 87 14 17 48 62 23 34 91 4 71 25 59 18 63 Simply print out each of these elements and the final sum of these elements to the console. The final sum for this example would be 125 For this program, you are to turn in your Programming Assignment plan (PA4 Plan Template V2 file) and the source code. You will copy both into the folder called A04 that you created earlier. Thus, your A04 folder will contain three documents for this assignment (or four if you do the extra credit). Now you are to compress the folder A04. Finally, it is this compressed folder that you must submit via Blackboard for this assignment.Explanation / Answer
import java.util.*;
class PA4ExtraCredit
{
public static void main (String[] args)
{
Random rand = new Random();
int[][] Numbers = new int[10][10];
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
Numbers[i][j] = rand.nextInt(100) + 1;
}
}
System.out.println("Array : ");
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
System.out.print(Numbers[i][j]+" ");
}
System.out.println();
}
int sum = 0;
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
if(i==j) // diagonal element
sum = sum + Numbers[i][j];
}
}
System.out.println("Sum of diagonal elements : "+sum);
}
}
Output:
Array :
56 5 53 89 56 72 29 70 42 87
20 63 90 77 82 69 89 8 30 84
33 26 42 9 98 5 40 29 66 14
3 67 63 6 68 43 48 59 99 32
76 54 70 66 80 76 96 97 70 74
72 17 95 94 81 11 65 43 6 4
19 81 91 1 59 18 70 75 21 13
25 66 86 98 37 82 52 7 19 42
45 42 20 13 28 18 78 54 35 72
47 47 59 48 70 74 30 42 12 21
Sum of diagonal elements : 391
Do ask if any doubt. Please upvote.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.