Adding arrays in Java - java

Say I have a 2D array:
1.007, 0.003, 0.003
0.0095, 2.003, 0.007
1.005, 0.008, 0.001
0.003, 6.884, 0.007
How can I go through the columns such that I get an average of the numbers greater than 1? Such as: (1.007+1.005)/2 = 1.006
[1.006, 4.4435, 0]
I wrote this so far. Been working on it for days, but I can't get it to work.
double sum=0;
for(int j = 0; j <data[0].length; ++j) { // col
for( int i = 0; i < data.length; ++i) { // row
if (data[i][j]>=1){
sum=sum+data[i][j];
System.out.println(sum);
// j+=1;
}
}
}
data is my 2D array.
Thanks!

If you need to go through columns, switch the for-loop order
public class ArrayExample {
public static void main(String[] args) {
double[][] data = {
{1.007, 0.003, 0.003},
{0.0095, 2.003, 0.007},
{1.005, 0.008, 0.001},
{0.003, 6.884, 0.007}
};
double sum = 0;
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[0].length; j++) {
if (data[i][j] >= 1.0) {
sum += data[i][j];
System.out.println(sum);
}
}
}
}
}
Gives
1.007
3.01
4.015
10.899000000000001

Edited according last edit of question:
double[][] data = {
{1.007, 0.003, 0.003},
{0.0095, 2.003, 0.007},
{1.005, 0.008, 0.001},
{0.003, 6.884, 0.007}
};
double sum;
int count;
for (int j = 0; j < data[0].length; j++) {
sum = 0;
count = 0;
for (int i = 0; i < data.length; i++) {
if (data[i][j] >= 1.0) {
sum += data[i][j];
count++;
}
}
if (count!=0){
System.out.print(sum/count + " ");
} else {
System.out.println(0);
}
}
}
Result:
1.0059999999999998 4.4435 0

Related

Uses command line arguments to accept the desired number of rows and columns in java

it contains two java classes: TwoDMethods.java and TwoDTest.java
for TwoDMethods.java
The fill method fills the entire array with randomly-selected double values
between 0.0 and 100.0
The toString method returns a string representation of the two-dimensional
array, with the rows separated by "\n" and the columns vertically aligned
The sumColumns method returns a one-dimensional array containing the sum
of each column of the given two-dimensional array
The sumRows method returns a one-dimensional array containing the sum of
each row of the given two-dimensional array
The minValue method returns the smallest number in the given array
The maxValue method returns the largest number in the given array
I finished TwoDMethods.java but I am not sure how to write TwoDTest.java
and all methods should be static but the toString method always goes wrong when I add static.
For example, in TwoDTest.java using the following command:
java TwoDTest 3 4
the output will be:
toString() result:
41.23 72.99 8.60 38.62
70.32 52.00 38.63 90.60
72.98 6.54 94.50 91.34
Column sums:
184.53 131.54 141.73 220.56
Row sums:
161.45 251.55 265.36
Smallest number: 6.54
Largest number: 94.50
this is TwoDMethods.java:
public class TwoDMethods {
static int row, col;
static double sumRow, sumCol;
static double[][] array = new double[row][col];
public static void fill() {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
array[i][j] = (double) (Math.random() * 100.00);
}
}
public String toString() {
String method2 ="";
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
method2 += String.format("%8.2f", array[row][col]) + " ";
}
method2 += "\n";
}
return method2;
}
public static double[] sumRows() {
double[] rowArray = new double[row];
for (int i = 0; i < row; i++) {
sumRow = 0;
for (int j = 0; j < col; j++) {
sumRow = sumRow + array[i][j];
}
rowArray[i] = sumRow;
}
return rowArray;
}
public static double[] sumColumns() {
double[] colArray = new double[col];
for (int i = 0; i < col; i++) {
sumCol = 0;
for (int j = 0; j < row; j++) {
sumCol = sumCol + array[j][i];
}
colArray[i] = sumCol;
}
return colArray;
}
public static double minValue() {
double minValue = array[0][0];
for (int j = 0; j < array.length; j++) {
for (int i = 0; i < array[j].length; i++) {
if (array[j][i] < minValue) {
minValue = array[j][i];
}
}
}
return minValue;
}
public static double maxValue() {
double maxValue = array[0][0];
for (int j = 0; j < array.length; j++) {
for (int i = 0; i < array[j].length; i++) {
if (array[j][i] > maxValue) {
maxValue = array[j][i];
}
}
}
return maxValue;
}
}
I suggest not to make the methods static nor the members of class TwoDMethods. Here is my suggested code. It is basically your code with occurrences of static removed plus I added a constructor. The constructor initializes the class members. Note that you had an error in method toString in this line:
method2 += String.format("%8.2f", array[row][col]) + " ";
It should be:
method2 += String.format("%8.2f", array[i][j]) + " ";
I changed it in the below code.
public class TwoDMethods {
int row, col;
double sumRow, sumCol;
double[][] array = new double[row][col];
public TwoDMethods(int numRows, int numColumns) {
row = numRows;
col = numColumns;
array = new double[row][col];
}
public void fill() {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
array[i][j] = (double) (Math.random() * 100.00);
}
}
public String toString() {
String method2 = "";
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
method2 += String.format("%8.2f", array[i][j]) + " ";
}
method2 += "\n";
}
return method2;
}
public double[] sumRows() {
double[] rowArray = new double[row];
for (int i = 0; i < row; i++) {
sumRow = 0;
for (int j = 0; j < col; j++) {
sumRow = sumRow + array[i][j];
}
rowArray[i] = sumRow;
}
return rowArray;
}
public double[] sumColumns() {
double[] colArray = new double[col];
for (int i = 0; i < col; i++) {
sumCol = 0;
for (int j = 0; j < row; j++) {
sumCol = sumCol + array[j][i];
}
colArray[i] = sumCol;
}
return colArray;
}
public double minValue() {
double minValue = array[0][0];
for (int j = 0; j < array.length; j++) {
for (int i = 0; i < array[j].length; i++) {
if (array[j][i] < minValue) {
minValue = array[j][i];
}
}
}
return minValue;
}
public double maxValue() {
double maxValue = array[0][0];
for (int j = 0; j < array.length; j++) {
for (int i = 0; i < array[j].length; i++) {
if (array[j][i] > maxValue) {
maxValue = array[j][i];
}
}
}
return maxValue;
}
}
And here is the class you requested, TwoDTest.
public class TwoDTest {
public static void main(String[] args) {
if (args.length < 2) {
System.out.println("Usage: TwoDTest row cols");
}
else {
int numRows = Integer.parseInt(args[0]);
int numColumns = Integer.parseInt(args[1]);
TwoDMethods twoD = new TwoDMethods(numRows, numColumns);
twoD.fill();
System.out.print(twoD);
System.out.println("Column sums:");
double[] sumColumns = twoD.sumColumns();
for (double sum : sumColumns) {
System.out.printf("%8.2f ", sum);
}
System.out.println();
System.out.println("Row sums:");
double[] sumRows = twoD.sumRows();
for (double sum : sumRows) {
System.out.printf("%8.2f ", sum);
}
System.out.println();
System.out.println("Smallest number: " + twoD.minValue());
System.out.println("Largest number: " + twoD.maxValue());
}
}
}
I launch class TwoDTest as follows:
java TwoDTest 3 4
And I get the following output:
96.83 11.96 50.86 43.26
89.06 84.86 30.68 59.61
63.51 96.09 49.93 32.19
Column sums:
249.39 192.92 131.47 135.05
Row sums:
202.90 264.21 241.72
Smallest number: 11.962167522763945
Largest number: 96.82621497529537

Is there a way to fix my two dimensional array problem

How can I find the total sum for each two dimensional array row? I'm completely stuck...
public static void main(String[] args) {
int [][] grid = new int [10][10];
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
grid[i][j] = (int)(Math.random()*99);
}
}
for(int i = 0; i < 10; i++) {
for(int j = 0; j < 10; j++) {
System.out.print("1.");
System.out.printf("%5d ", grid[i][j]);
}
System.out.println();
}
}
My current output is:
How can I show the total sum for each row in the end of the row and show column numbers
For the sum of a row, this should do. In a similar way within the i loop, if you need to count the column as well;
for(int i = 0; i < 10; i++) {
int jSum = 0;
for(int j = 0; j < 10; j++) {
jSum += grid[i][j];
System.out.print("1.");
System.out.printf("%5d ", grid[i][j]);
}
System.out.printf(" %5d", jSum);
System.out.println();
}
On the column numbering:
Either you just pust put a static print in the beginning (like print "1 2 3 4..."), or you put the following with the j loop:
if (i == 0) System.out.printf("%5d ", j); // only prints in first loop / row - print 1,2,3,4,5....

Why does my program produce no output while it's compiling fine?

I want to create a two dimensional array. I am able to compile but not able to run
public class Arraytest1 {
public static void main(String[] args) {
int i, j, k = 0;
int test[][] = new int[4][5];
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++) {
test[i][j] = k;
k++;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; k++)
System.out.print(test[i][j] + " ");
System.out.println();
}
}
}
You have an endless loop: for(j=0;j<5;k++), you have to write for(j=0;j<5;j++)
You increment k instead of j
You have an endless loop. You are incrementing k instead of j:
for(j=0;j<5;k++)
You should change it both times to
for(j=0;j<5;j++)
Here... this should work. Just change your sub-loops making it j++ instead of k++ both top and bottom
public static void main(String[] args) {
int i, j, k = 0;
int test[][] = new int[4][5];
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++) {
test[i][j] = k;
k++;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++)
System.out.print(test[i][j] + " ");
System.out.println();
}
}
I think you've mixed up the k and j variables in the second for-loop "block". When I alter it to:
...
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++)
System.out.print(test[i][j] + " ");
System.out.println();
}
...
I get the following printed to my console:
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
Is it what you wanted?
public class Arraytest1 {
public static void main(String[] args) {
int i, j, k = 0;
int test[][] = new int[4][5];
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++) {
test[i][j] = k;
k++;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++) {
System.out.print(test[i][j] + " ");
System.out.println();
}
}
}
}
you can resolve this problem

Having Trouble using For Loops to make two triangles of different characters fit into a rectangle?

Examples of input:
3
4
Examples of output (assume that spaces = new lines.)
QQQH
QQHH
QHHH
QQQQH
QQQHH
QQHHH
QHHHH
So far, the fragment of code that attempts to print this is (Assume that all variables are pre-defined):
public int getSize()
{
for (int i = size; i > 0; i--){
for (int j = 1; j < size; j++){
out.print("Q");
out.print("H");
}
out.println("");
}
return 0;
}
It just prints: (assume that spaces = new lines.)
QHQHQHQHQH
QHQHQHQHQH
QHQHQHQHQH
QHQHQHQHQH
QHQHQHQHQH
For input of 5. I'm not quite sure how to make it print only the number of times of its respective integer value. Can someone explain?
You could break the inner loop it two, like this:
for (int i = size; i > 0; i--) {
for (int j = 0; j < i; j++) {
out.print("Q");
}
for (int j = i; j < size + 1; j++) {
out.print("H");
}
out.println();
}
Output:
QQQH
QQHH
QHHH
QQQQH
QQQHH
QQHHH
QHHHH
Or if you don't want to break the loop, you can use the ternary operator:
for (int i = size; i > 0; i--) {
for (int j = 0; j < size + 1; j++) {
out.print(j < i ? 'Q' : 'H');
}
out.println();
}
Try this
for (int i = 0; i < size; i++) {
for (int j = 1; j <= size-i; j++) {
System.out.print("Q");
}
for (int k = 0; k <= i; k++) {
System.out.print("H");
}
System.out.println("");
}
try this code block instead:
int j=0;
for (int i = size; i > 0; i--)
{
j=0;
while(j < i)
{
out.print("Q");
j++;
}
j=i;
while(j < size+ 1)
{
out.print("H");
j++;
}
out.println();
}
Tested with sample inputs. Working fine
public int getSize() {
for (int i = 1; i < size+1; i++) {
for (int j = 0; j < size+1; j++) {
int Qtimes = size-i;
if(j <= Qtimes) {
System.out.print("Q");
} else{
System.out.print("H");
}
}
System.out.println("");
}
return 0;
}
This works if the input is 4 - for example -change it to any number
public int getSize()
{
int cnt = 0;
int i,j,k = 0;
for ( i = 4; i > 0; i--){
for ( j = 0; j < i; j++){
System.out.print("Q");
}
cnt ++;
for( k = 0 ; k <cnt ; k++) {
System.out.print("H");
}
System.out.println("");
}
return 0;
}
output is
QQQQH
QQQHH
QQHHH
QHHHH

Double array operation to single array

public void calculatePercentage(int exam[][])
{
int perc = 0;
for (int i = 0; i < examen.length; i++)
{
for (int[] score : exam)
perc += score;
perc /= exam.length;
}
}
Hello,
I am really stuck at this one. I want to create a new mathod calculatePercentages given the parameter exam[][]. The double array exam holds 3 rows of elements. What the method has to do is simple calculate the sum of each row. The answer is probably quite simple, but I just don't know how to do it.
For a single array, I guess the code is:
double perc = 0;
for(int score : exam)
{
perc += score;
}
perc /= (exam.length);
return perc;
The exam[][] could look like:
|10 12 18 5 3 |
|12 3 5 15 20 |
|20 15 13 11 9 |
The output percentage[] should like:
{48,55,68} Each element of percentage[] is the sum of the elements of 1 row of exam[]
The double array exam holds 3 rows of elements. What the method has to do is simple calculate the sum of each row.
The name makes no sense, but it does what you want it to do.
public int[] calculatePercentage(int exam[][]) {
int[] sums = new int[exam.length];
for (int i = 0; i < exam.length; i++) {
int sum = 0;
for (int j = 0; j < exam[i].length; j++) {
sum += exam[i][j];
}
sums[i] = sum;
}
return sums;
}
Also a double array would be double[], you are talking about two dimensional int arrays int[][]
EDIT
Pshemo pointed out a shorter solution is possible:
public int[] calculatePercentage(int exam[][]) {
int[] sums = new int[exam.length];
for (int i = 0; i < exam.length; i++) {
for (int j = 0; j < exam[i].length; j++) {
sums[i] += exam[i][j];
}
}
return sums;
}
or even just
public int[] calculatePercentage(int exam[][]) {
int[] sums = new int[exam.length];
for (int i = 0; i < exam.length; i++)
for (int j = 0; j < exam[i].length; j++)
sums[i] += exam[i][j];
return sums;
}
but I still prefer the first one, for it's readability.
for (int i = 0; i < exam.length; i++) {
int sum = 0;
for (int j = 0; j < exam[i].length; j++) {
sum += exam[i][j];
}
}
using for each
for (int x[] : exam) {
for (int y : x) {
sum += y;
}
}

Categories