I need to populate a double array with user input by using nested while loops only. This is what I have so far:
public static double[][] score() {
int col = 3;
int row = 3;
int size = 0;
Scanner in = new Scanner(System.in);
double[][] scores = new double[row][col];
System.out.println("Enter your scores: ");
while (in.hasNextDouble() && size < scores.length) {
while (size < scores[size].length) {
scores[][] = in.hasNextDouble();
size++;
}
return scores;
}
The most common way to do this is through for loops since they allow you to specify the index counters you need in a concise way:
for(int i = 0; i < scores.length; i++){
for(int j = 0; j < scores[i].length; j++){
scores[i][j] = in.nextDouble();
}
}
If you specifically need to use while loops you can do pretty much the same thing, its just broken up into multiple lines:
int i = 0;
while(i < scores.length){
int j = 0;
while(j < scores[i].length){
scores[i][j] = in.nextDouble();
j++;
}
i++;
}
Related
I have a 2D array of students, each with 4 grades. I've established a way to find the lowest grade of each student, but I now need to remove the lowest and put the remaining grades into a new array.
The code that I currently have repeats values for each grade and is not accurate. Also, if a student has two lowest grades (i.e 64.3 and 64.3) how could we only remove one?
double studentGrades[][] = {
{100.0, 98.8, 78.9, 90.3},
{76.6, 90.8, 78.9, 78.3},
{78.7, 78.7, 91.2, 100.0},
{65.9, 94.2, 81.6, 93.6},
{78.7, 100.0, 93.2, 76.7}};
double temp = 0;
double lowest = 0;
double finalGrades[][] = new double[5][3];
for (int row = 0; row < studentGrades.length; row++) {
lowest = studentGrades[row][0];
for (int s = 0; s < studentGrades[0].length; s++) {
temp = studentGrades[row][s];
if (lowest > temp) {
lowest = temp;
} else {
for (int m = 0; m < 3; m++) {
finalGrades[row][m] = studentGrades[row][s];
}
}
}
}
Ideally, the finalGrades array would be set as such:
{100.0, 98.8, 90.3},
{90.8, 78.9, 78.3},
{78.7, 91.2, 100.0},
{94.2, 81.6, 93.6},
{78.7, 100.0, 93.2}
try:
for (int row = 0; row < studentGrades.length; row++) {
int lowest_index = 0;
for (int s = 1; s < studentGrades[0].length; s++) {
if (studentGrades[row][s] < studentGrades[row][lowest_index]) {
lowest_index = s;
}
}
int m = 0;
for (int s = 0; s < studentGrades[0].length; s++) {
if (s != lowest_index) {
finalGrades[row][m] = studentGrades[row][s];
m++;
}
}
}
I had an assignment for a class where I had to develop a simple number sort program.
My main is supposed to receive the user input and my sort class is supposed to interrupt and spit out the resulting numbers in ascending and descending order. The problem is that my main is taking the input but it's not putting it in order at all and I'm unsure why.
package main;
import sort.Sort;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int arr[] = new int[5];
Scanner myScanner = new Scanner(System.in);
for(int i=0; i<5; i++) {
System.out.print("Enter a number: ");
myScanner.nextLine();
}
Sort sortObj = new Sort();
sortObj.ascendingsort(arr);
}
}
package sort;
public class Sort {
public void ascendingsort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
{
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
void descendingsort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
{
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
}
I know I'm missing something in my Main, because I'm fairly certain I don't need to put anything else into the sort class.
You're code is perfect. You just missed one assignment in your main method.
Instead of myScanner.nextLine(); you should have written arr[i] = myScanner.nextLine();
myScanner.nextLine(); is getting the next line you enter in the console but it isn't saving it anywhere. arr[i] = myScanner.nextLine(); will save the value of the console in the arr array.
Everything else should work after that.
First you are getting user input using myScanner.nextLine() but not storing it. myScanner.nextLine() won't save anything which user enters .
Thus you need to store it in an array and then use it later. So your main method after the changes should be like this.
public static void main(String[] args) {
int arr[] = new int[3];
Scanner myScanner = new Scanner(System.in);
for(int i=0; i<3; i++) {
System.out.print("Enter a number: ");
arr[i] = myScanner.nextInt();
}
Sort sortObj = new Sort();
sortObj.ascendingsort(arr);
}
And your sorting functions are also wrong. There you do some mis-calculations and return / log nothing. Thus you will not see anything in the console if you are not logging or returning anything.
public void descendingsort(int array[]) {
int n = array.length;
int i, j, temp;
for (i = 0; i < ( n- 1 ); i++) {
for (j = 0; j < n - i - 1; j++) {
if (array[j] < array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
for (i = 0; i < n; i++){
System.out.println(array[i]);
}
}
public void ascendingsort(int array[]) {
int n = array.length;
int i, j, temp;
for (i = 0; i < ( n- 1 ); i++) {
for (j = 0; j < n - i - 1; j++) {
if (array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
for (i = 0; i < n; i++){
System.out.println(array[i]);
}
}
I hope this functions will work for your use case of ascending and descending sort.
I made 2D arrray which prints some random elements.
Now i need a method which calculates the sum of that elements but just elements below the main diagonal.
Here is my code...
class Init {
public static void main(String[] args) {
int n = 0;
int m = 0;
int aray[][];
Random random = new Random();
Scanner tastatura = new Scanner(System.in);
int[][] array = new int[n][m];
n = tastatura.nextInt();
m = tastatura.nextInt();
array = new int[n][m];
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
array[i][j] = random.nextInt(20);
}
}
for (int[] a : array) {
System.out.println(Arrays.toString(a));
}
}
}
I did it like this... Now i can sum, but when i try to multyply same numbers i am geting 0 Why is that?
Scanner scanner = new Scanner(System.in);
System.out.print("Unesite duzinu kolona i redova : ");
int rows = scanner.nextInt();
int columns = rows;
int[][] matrix = new int[rows][rows];
Random random = new Random();
System.out.println("Nasumicni/random brojevi su :");
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = random.nextInt(20);
}
}
for (int[] a : matrix) {
System.out.println(Arrays.toString(a));
}
//here is the logic which sum those elements
int sum = 0;
for (int i = 1; i < rows; i++) {
for (int j = i - 1; j >= 0; j--) {
sum = sum + matrix[i][j];
}
}
System.out.println("\nMatrix is : ");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
System.out.println("Proizvod elemenata ispod glavne dijagonale je: " + sum);
What about this?
int s = 0;
for(int i = 1; i < m; ++i)
for(int j = 0; j < i; ++j)
s += a[i][j];
This selectively loops through the elements below the main diagonal and sums them up, without looping through the entire matrix and making it lengthier.
The main diagonal of a matrix consists of those elements that lie on the diagonal that runs from top left to bottom right. But since you want those elements "below" the main diagonal, here is an algorithm I came up with for that.
int sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (i == j && (i + 1 < n))
{
int temp = i + 1;
while (temp < n)
{
sum += arr[temp][j];
temp++;
}
}
Also, you declare int[][] array multiple times. You need to declare it only once, after you get the values for n and m.
for(i=0;i
for(j=0;j
{
if(j>i)
d1+=a[i][j];. // Above the diagon
else
if(i>j)
d2+=a[i][j];. // Below the diagonal
}
public class Sort {
public static void main(String[] args) {
//fill the array with random numbers
int[] unsorted = new int[100];
for(int i = 0; i < 100; i++) {
unsorted[i] = (int) (Math.random() * 100);
}
System.out.println("Here are the unsorted numbers:");
for(int i = 0; i < 100; i++) {
System.out.print(unsorted[i] + " ");
}
System.out.println();
int[] sorted = new int[100];
for(int i = 0; i < 100; i++) {
int hi = -1;
int hiIndex = -1;
for(int j = 0; j < 100; j++) {
if(unsorted[j] > hi) {
hi = unsorted[j];
hiIndex = j;
}
}
sorted[i] = hi;
unsorted[hiIndex] = -1;
}
System.out.println("Here are the sorted numbers: ");
for(int i = 0; i < 100; i++) {
System.out.print(sorted[i] + " ");
}
System.out.println();
}
}
So this is in descending order but I want to reverse it.
I tried changing the if(unsorted[j] > hi) {
to a if(unsorted[j] < hi) {
[edit:changed greater than to less than, both were same]
Okay, you want the numbers to be in ascending order. So for descending, you assume that compared number would be -1 and all other number must be grater than this -1, now instead of -1 use the maximum value a number could be. Assign Integer.MAX_VALUE where you were assigning -1. So change your code like this:
int[] sorted = new int[100];
for(int i = 0; i < 100; i++) {
int hi = Integer.MAX_VALUE;
int hiIndex = i;
for(int j = 0; j < 100; j++) {
if(unsorted[j] < hi) {
hi = unsorted[j];
hiIndex = j;
}
}
sorted[i] = hi;
unsorted[hiIndex] = Integer.MAX_VALUE;
public static void main(String[] args) {
int rows, cols, j, i;
Scanner sc = new Scanner(System.in);
System.out.println("enter no of rows and columns respectively");
rows = sc.nextInt();
cols = sc.nextInt();
int[][] matrix = new int[rows+1][cols+1];
System.out.println("enter elements");
for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
{
matrix[i][j] = sc.nextInt();
}
}
System.out.println("after sum");
int grandtotal = 0;
for(i = 0; i < rows; i++)
{
int rowsum = 0;
int colsum = 0;
for(j = 0; j < cols; j++)
{
rowsum += matrix[i][j];
colsum += matrix[j][i];
}
matrix[i][j] = rowsum;
grandtotal += rowsum;
matrix[j][i] = colsum;
}
j = cols;
matrix[i][j] = grandtotal;
The above code is working fine, but my question is: using this logic, how can I find the grandtotal for a matrix if the number of rows and columns are not the same? And tell me if there are any flaws with my current code.
There is no flaw in your code.Now coming to the point when the number of rows and columns are different.
The statement
matrix[j][i] = colsum;
now would give incorrect result because now number of rows and number of columns are different.
This can be solved very easily by just changing the statement
colsum += matrix[j][i];
to:
matrix[rows][j] += matrix[i][j];
and now you do not need the statement
matrix[j][i] = colsum;
So with these simple modifications, the code for different number of columns and rows would be:
for( i = 0;i < rows;i++)
{
int rowsum=0;
for( j = 0;j < cols;j++)
{
rowsum += matrix[i][j];
matrix[rows][j] += matrix[i][j];
}
matrix[i][j] = rowsum;
grandtotal += rowsum;
}
Keep Rest of the code same.