Trying to find the average of EACH row in a 2d array
A couple of corrections to two of the methods. See the code comments for further explanation:
public static int display2DArray(int[][] a2) {
for (int row = 0; row < 3; row++){
for (int column = 0; column < 4; column++){
System.out.print(a2[row][column] + " "); // better display
}
System.out.println(); // better display
}
return 0;
}
and:
public static double averageRow(int[][] a2) {
int rowTotal = 0;
double average = 0;
for (int row = 0; row < a2.length; row++){
for (int column = 0; column < a2[row].length; column++){
rowTotal += a2[row][column];
}
average = rowTotal / a2[row].length; // calc average
System.out.println(average); // print the row average
rowTotal = 0; // start over (for next row)
}
return rowTotal;
}
Output of averageRow
:
25.0
65.0
105.0
How to calculate the average of each row in multidimensional array
You can do it something like this
for(int i = 0; i < 3; i++)
{
int Avg = 0;
for(int x = 0; x < 3; x++)
{
Console.Write(number[x, i] + " ");
Avg += number[x, i];
}
Avg = Avg / 3;
Console.Write("Average is" + Avg);
Console.WriteLine(" ");
Console.ReadLine();
}
Calculate the average of a row in a two dimensional array
You should create a sum while looping over each array:
public static void printtda() {
int[][] temps = { {27,28,26,29,30}, {26,25,25,37,40} };
for (int i=0; i<temps.length; i++) {
int sum = 0;
for (int g=0; g< temps[i].length; g++) {
sum+=temps[i][g];
System.out.print(temps[i][g] +" ");
}
System.out.println(sum/temps[i].length);
System.out.println();
}
}
How do I to print out the average of each row of a 2D array separately?
Not the best, but the easiest solution would be to simply change the type of sum to float or double
float sum = 0;
This will make the division that happens on the println to change from integer division to a floating point one, if only one of the operands has a floating point then your print will have a dot
Average of a row in a two-dimensional array in C?
Most errors were present in your dAvg
function:
Namely:
- You should not pass the entire 2D array if you only need one row there
- You should pass the length of the array instead of hardcoding it everywhere (good practice, not a bug)
- Your
r
was kept uninitialised therefore your indexing was not working - You were returning the average in every iteration therefore instead of summing the values you added the first and then you returned before adding the others.
double dAvg(double array[], size_t length){
size_t c;
double sum = 0;
// Calculate sum first
for (c = 0; c < length; c++){
sum += array[c];
}
// Find average by dividing the sum by the number of numbers in a row
return sum / (double) length;
}
int main(void){
int i;
//Initiallize array
double array[3][5] = {{3.0,5.0,2.0,1.0,0.0}, {4.0,8.0,6.0,3.0,3.0}, {7.0,6.0,2.0,3.0,5.0}};
//Computes the average value per row of array
for(i = 0; i < 3; i++){
printf("The average of row %d is %f\n", i, dAvg(array[i], 5));
}
return 0;
}
Trying to get the average for each element in a two dimensional array
I would use List for this
public void studentAverage() throws FileNotFoundException {
File infile= new File("qdata.txt");
Scanner sc = new Scanner(infile);
double rowNum = 0;
List<List<Integer>> grades = new ArrayList<>();
double totalCount = 0.0;
while (sc.hasNext()) {
List<Integer> row = new ArrayList<>();
Scanner lineScanner= new Scanner(sc.nextLine());
while (lineScanner.hasNextInt()) {
row.add(lineScanner.nextInt());
}
grades.add(row);
totalCount += row.size();
}
int index = 0;
for (List<Integer> list : grades) {
for (Integer grade : list) {
System.out.println("Student average is " + (double)Math.round(grade.doubleValue() / totalCount * 10) / 10);
}
}
}
How to find a total average on a multidimensional array (java)
I would try nested for loops. Something like this..
int sum = 0;
int indexCounter = 0; // Use this to keep track of the number of 'entries' in the arrays since they may not be perfectly rectangular.
for(int column = 0; column < temps.length; column++) {
for(int row = 0; row < temps[column].length; row++) {
sum+= temps[column][row];
indexCounter++;
}
}
int average = sum / indexCounter;
System.out.println("The average is " + average);
If you knew for sure that each row had the same number of columns then you could do something like
int average = sum / (temps.length * temps[0].length)
Related Topics
Create a C# Method to Generate Auto Increment Id
Convert Byte Array to Wav File
How to Check If a Socket Is Connected/Disconnected in C#
Multidimensional Array from a Txt File
How to Use a Nuget Package Within a Powershell Script
How to Redirect to Login Page If Session Is Not Available in MVC
How to Rename Keys Within a Json File
How to Pass Jquery Variable Value to C# MVC
How to Insert Programmatically a New Line in an Excel Cell in C#
Programmatically Create and Launch and Rdp Session (Without Gui)
How to Calculate the Average of Each Row in Multidimensional Array
Using Recursion to Add Odd Numbers
How to Correctly Write Parallel.For With Async Methods
Get Properties and Values from Unknown Object
Generate Xml for <Govtalkmessage Xmlns="Http://Www.Govtalk.Gov.Uk/Cm/Envelope">