-2

Any help would really be appreciated, majorly stuck at this problem it simply wont run i am at a lost point, the criteria is :

Write a program that returns the sum of all elements in a specified column of a two-dimensional array.

Firstly ask the user to enter a 3 by 4 array.  The user should enter the array as follows:   2.6  5.1  6  8   5.4   4.4   7   1   9.5   7.9   2   3

The program should then calculate the sum of each column in the array.

 

Tried casting to double or ints nothing works

package com.company;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        double[] Array1 = new double[5];
        double[] Array2 = new double[5];
        boolean Equal = true;

        Scanner input = new Scanner(System.in);
        System.out.print("Please enter " + Array1.length + " values");
        for (int i = 0; i < Array1.length; i++) {
            Array1[i] = input.nextDouble();
        }
        Scanner input2 = new Scanner(System.in);
        System.out.print("Please enter " + Array2.length + " values for your second array:");
        for (int i = 0; i < Array2.length; i++)
            Array2[i] = input.nextDouble();



        if(Array1.length == Array2.length)
        {
            for (int i = 0; i < Array1.length; i++)
            {
                if(Array1[i] != Array2[i])
                {
                    Equal = false;
                }
            }
        }
        else
        {
            Equal = false;
        }

        if (Equal)
        {
            System.out.println("Two Arrays Are Equal");
        }
        else
        {
            System.out.println("Two Arrays Are Not equal");
        }
    }
}
Makusium
  • 201
  • 1
  • 2
  • 15
  • it would be easier if you showed the code that you used, and explained what went wrong with it – Stultuske Apr 02 '19 at 13:41
  • Visiting every element of a 2d array usually comes down to two nested for loops with one increment for the row and the other for a column, you could simply add up each element inside the internal loop. I could give a proper answer if you provided the actual code you've been trying. – Patrick Apr 02 '19 at 13:44
  • Hi and welcome to stackexchange. [You may want to consider reading this open letter](https://softwareengineering.meta.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems) – user1129682 Apr 02 '19 at 13:46
  • Pretty sure ive gone way astray in my code but have listed it – Mark Rubbinson Apr 02 '19 at 13:48
  • 3
    This code does not seem to belong to that problem. – user1129682 Apr 02 '19 at 13:48
  • 1
    Are you looking for sum of each column or check equality? – Muhammed Imran Hussain Apr 02 '19 at 13:48
  • Also you may want to [check here](https://stackoverflow.com/q/8081827/1129682) – user1129682 Apr 02 '19 at 13:49
  • Sum each column but nothing i tried worked to so i just wanted to try and atleast get part of the program to work by checking equality – Mark Rubbinson Apr 02 '19 at 13:50
  • 1
    What do you mean 'nothing I tried worked'? What was the error? Did you get an exception? Did you get a compilation error? Where is the code you tried? – jbx Apr 02 '19 at 14:03

1 Answers1

0
public class Main{

    static double[][] mat;

    public static void main(String[] args){
           Scanner input=new Scanner(System.in);
           System.out.println("Enter number of rows and number of columns");

           int n=input.nextInt();
           int m=input.nextInt();
           mat=new double[n][m];
           System.out.println("Please enter elements of matrix");

           for(int i=0;i<n;i++){
               for(int i=0;i<m;i++){
                   mat[i][j]=input.nextDouble();
               }
           }

          System.out.println("Enter column number to get sum");
          double sum=0D;
          int col=input.nextInt();
          for(int i=0;i<m;i++){
              sum+=mat[i][col];
          }

         System.out.println("sum of elements of "+col+"in the mat is "+sum);
    }
}
vijay
  • 187
  • 10