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;
}
}
Related
Hi Im new to Java Im trying to make a counter. What Id like to do is calculate the numbers from 1 to x, for example if x is 5, calculate 1 + 2 + 3 + 4 + 5.
This is what I wrote, but I get 6 as output instead of 15.
public class Counter {
public int count (int x){
int contatore = 0;
int sum = 0;
for (int i = 0; i <= x; i++) {
sum = i;
System.out.println(i);
}
System.out.println(sum);
return sum;
}
}
You need to add i to the sum in each iteration of the loop:
public int count (int x) {
int contatore = 0;
int sum = 0;
for (int i = 0; i <= x; i++) {
sum += i;
System.out.println(i);
}
System.out.println(sum);
return sum;
}
You are assigning the loop variable to the sum - you need to add it to the sum
public int count (int x){
int sum = 0;
for (int i = 0; i <= x; i++) {
sum = sum + i;
}
return sum;
}
As others have noted, you can use the following shorthand notation for addition with assignment.
sum += i; // equivalent to sum = sum + i;
I am trying to create a program, which counts the minimum of each dimension in a two dimensional array. So for ex. if i had an array:
int[][] test = {{1,2,3},{2,3,4},{4,5,6}}
the program would display: [1,2,4] - the minimum of each dimension.
For that I've created a method called minimum, which looks like this
static int[] minimum(int[][] arr) {
int[] result = new int [arr.length];
for (int i = 0; i < arr.length; i++) {
for(int j = 0; j < arr[i].length; j++) {
int min = arr[i][0];
if(arr[i][j] < min) {
min = arr [i][j];
result [i] = min;
} else{
}
}
}
return result;
}
But when i call out this method in my main, with a sample array
public static void main(String[] args) {
int[][] arr = {{1,2,3,},{3,4,5},{6,6,6}};
System.out.println(Arrays.toString(minimum(arr)));
}
The program displays [0,0,0,]. Do You have any clue where is the problem and how to fix it?
The problem is that if the first element in the array is min, it never gets recorded to the result array. Try:
static int[] minimum(int[][] arr) {
int[] result = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
result[i] = arr[i][0];
for (int j = 1; j < arr[i].length; j++) {
if (arr[i][j] < result[i]) {
result[i] = arr[i][j];
}
}
}
return result;
}
Note that there needs to be at least one element per row in the input matrix for the above function; add a conditional or use Integer.MIN_VALUE to handle empty rows if you wish.
This should work. You reset the min to the first element every time. So you are basically comparing if there is any value smaller than the first one.
static int[] minimum(int[][] arr){
int[] result = new int [arr.length];
for (int i = 0; i < arr.length; i++){
result[i] = Integer.MAX_VALUE;
for(int j = 0; j < arr[i].length; j++){
if(arr[i][j] < result[i]) {
result [i] = arr[i][j];
}
}
}
return result;
}
I am working just recently in Java. And I am still struggling a little bit with Java. The task is to sum the columns and the rows respectively the example looks like that.
I managed to get the sum of the columns, but I always end up having the first row repeated in my desired output, how can I delete that error. And just have the desired column sums.
How does it work for sum of rows respectively?
Thank you very much for your help in advance
The code looks like that:
public class MatrixColumnSums {
public static void main(String[] args) {
// TODO Auto-generated method stub
double [][] a = new double [2][3];
a[0][0] = 1;
a[0][1] = 2;
a[0][2] = 3;
a[1][0] = 4;
a[1][1] = 5;
a[1][2] = 6;
System.out.println( "A");
for (int i = 0; i < a.length; i++) {
String str = "";
for (int j = 0; j < a[i].length; j++) {
str += a[i][j] + "\t";
}
System.out.println(str);
}
// column sums
// creating another matrix to store the sum of columns matrices
double c[] = new double [a[0].length];
// Adding and printing addition of 2 matrices
System.out.println("C");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++)
{
c[j] += a[i][j];
System.out.print(c[j] + "\t");
}
System.out.println();
}
}
}
The output looks like the following:
A
1.0 2.0 3.0
4.0 5.0 6.0
C
1.0 2.0 3.0 ( <= I want to get this row deleted)
5.0 7.0 9.0 ( I just want to have this as my output)
**Matrix column sum **
doOneColumnSum method sum one column.
private static double doOneColumnSum(double[][] arr, int col){
double total = 0;
for (int row = 0; row < arr.length; row += 1){
total += arr[row][col];
}
return total;
}
doColumnSums method sum all column using doOneColumnSum method
public static double[] doColumnSums(double[][] arr)
{
int numColumns = arr[0].length;
double[] result = new double[numColumns];
for (int col = 0; col < numColumns; col += 1)
{
result[col] = findOneColumnSum(arr, col);
}
return result;
}
Don't print when summing the c matrix, just print once at the end:
// sum the columns first
for (int i=0; i < a.length; i++) {
for (int j=0; j < a[0].length; j++) {
c[j] += a[i][j];
}
}
// then iterate c[] once at the end and print it
for (int j=0; j < a[0].length; j++) {
System.out.print(c[j] + "\t");
}
Note that we could have added if logic to your original nested loop to only print c after the summation is complete, but what I wrote above seems more appealing from a separation of concerns point of view.
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
This is what I want to do when A is a square matrix.
P - is power.
A & B are square matrices.
User will be asked to enter size of matrix A, and elements of matrix A and to what power they want to raise the matrix to.
Once they input what power, and what elements my program is supposed to calculate this:
(Assuming P = 5)
A^5 + A^4 + A^3 + A^2 + A
I have written a method that adds matrices a method that multiplies them, and a method that raises them to the power and they all work correctly.
The problem I am having is the final step which I showed above A^5 + A^4 + A^ 3...
This is where the problem gets even weirder, my program works when the elements in the matrix are all the same... such that a
2 2 2
2 2 2
2 2 2
matrix will give me the CORRECT output, BUT
1 2 3
4 5 6
7 8 9
matrix will give me the WRONG output and I have no idea why.
This is the method in which the problem is occuring
public static void addPowers(int [][] a, int[][] b, int p) {
while( p != 1){
b = addMatrices(powerMatrix(a,p), b) ;
addPowers(a,b,p-1) ;
return ;
}
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < b.length; j++)
System.out.print(b[i][j] + "\t") ;
System.out.println();
}
}
Just in case you ask, reason I have the recursive under a while loop is so it won't print over and over and over again.
Thanks for your time! :)
Edit: More clarifying information.
addMatrices is a method that adds matrices with an two int[][] arguments.
powerMatrix is a method that finds the power of a matrix with (int[][], int) arguments.
EDIT Methods being called...
public static int[][] multiplyMatrices(int matrixA[][], int matrixB[][]) {
int temp[][] = new int[matrixA.length][matrixB.length];
int matrix[][] = new int[matrixA.length][matrixB.length];
int sum = 0 ;
for (int i = 0; i < matrixA.length; i++)
{
for (int j = 0; j < matrixB.length; j++)
{
for (int l = 0; l < matrixA.length; l++)
{
sum += matrixA[i][l] * matrixB[l][j] ;
}
temp[i][j] = sum ;
sum = 0 ;
}
}
matrix = temp;
return matrix ;
}
public static int[][] addMatrices(int matrixA[][], int matrixB[][]) {
int temp[][] = new int[matrixA.length][matrixB.length];
int sum = 0 ;
for (int i = 0; i < matrixA.length; i++)
{
for (int j = 0; j < matrixB.length; j++) {
{
sum = matrixA[i][j] + matrixB[i][j] ;
}
temp[i][j] = sum ;
}
}
return temp ;
}
public static int[][] powerMatrix (int[][] a, int p) {
int[][] result = a;
for (int n = 1; n < p; n++)
result = multiplyMatrices(result, a);
return result;
}
In your addMatrices method, you should remove the third loop.
Like this:
for (int i = 0; i < matrixA.length; i++) {
for (int j = 0; j < matrixA[i].length; j++) {
temp[i][j] = matrixA[i][j] + matrixB[i][j] ;
}
}