I'm wanting to find the average difference of the array totals.
My array structures are as follows (pulled from a database):
day, 1, 2, 3, 4
So you can see the 5 arrays how it's pulled from the database.
Now for instance, the Java algorithm I need is as follows:
To add each of the 1,2,3,4 values up, then loop to the next day (if there is one), repeating the same addition. Then eventually, calculate the 'average difference' between the totals of each day.
So for example, i've done some sample data to try and make it clearer for you!
1 (Mon) 135 90 105 150
2 (Tues) 143 86 117 163
3 (Wed) 129 100 140 158
So what the algorithm needs to do is:
135+90+105+150 = 500,
143+86+117+163 = 509,
129+100+140+158 = 527
But the only problem, is that, column 1 is its own array and column 2,3,4 preceding.
Calculate the 'average increase/decrease' which would be:
https://math.stackexchange.com/questions/16554/what-is-average-increase-percentage-and-how-to-calculate-it
I'm unsure how to implement it into Java! Any help would be appreciated.
If i can provide any more information please let me know.
Many thanks.
Have you arrived at this stage first of all to be able to access all the elements? (if I understood your question correctly?)
//first for loop loops through the number of days
for (int i = 0; i < day.length; i++)
{ //second for loop loops through all integers in Ith day
for (int j = 0; j < day[i].length; j++)
{
print day[i][j]
}
}
Since we know the location of the items in the database, the following might work:
/*Psudeocode:
ArrayList<Integer> firstCol = //get first column
ArrayList<Integer> secondCol = //get second column
....
ArrayList<Integer> nCol = //get n column
*/
ArrayList<Integer> values = new ArrayList<Integer>();
int currentRow;
for(int i=0;i<n;i++)
{
currentRow = 0;
currentRow += firstCol.get(i);
currentRow += secondCol.get(i);
//etc, etc...
values.add(currentRow);
}
//At this point, values contains all the sums of the different rows.
//To calculate the average percent change:
//PR= (((Vpresent - Vpast) / Vpast) x 100) / N
for(int i=0;i<values.size() -1;i++)
{
System.out.println(((values.get(i+1) - values.get(i)) * 100) / (i+1))
}
I hope this helps. Help with Average Percent Change
Related
I want to create an array and using a for loop and I want to assign values with an increment of 1o. So far here is what I have and I am not getting the result I want...
public class ArrayDemo1 {
public static void main(String[] args) {
int array[] = new int[10];
System.out.println("The array elements are: ");
for (int i = 0; i <= array.length - 1; i++) { // Controls index...
for (int j = 0; j < 101; j += 10) { // Controls value of each array index...
array[i] = j;
System.out.println(array[i]);
}
}
}
}
Here is my output [Stack overflow won't let me have the whole output so here is the shortened version; This output (starting at /* and ending at */) was repeated 10 full times. It printed from 0 to 100 ten times with an increment of 10]:
The array elements are:
/*
0
10
20
30
40
50
60
70
80
90
100
*/
Process finished with exit code 0
Here is what I want. For every index, I want one value and the next value should be an increment of 1o:
The array elements are:
0
10
20
30
40
50
60
70
80
90
You don't need a nested loop - the value is always the index multiplied by 10:
for (int i = 0; i <= array.length - 1; i++) {
array[i] = i * 10;
}
EDIT:
If you don't have to use loops, this could arguably be done more elegantly with a stream:
int[] array = IntStream.range(0, 10).map(i -> i * 10).toArray();
You don't need a nested for loop to assign values into a single array.
Populate each index by i*10 rather than introduce j.
You should also print the array in a new loop, otherwise, there's not much purpose for keeping the array and printing it in the same location
And rather than <= array.length - 1, you'll want < array.length
You can also use an IntStream, which is the modern way to achieve the same result
I'm trying to add all of the values for each column in a 2D array and these sums become values that overwrite the last row of the array
for example:
4 5 6 7 8
1 2 3 4 5
0 0 0 0 0 //this row will be replaced by the sum of each column
4 5 6 7 8
1 2 3 4 5
5 7 9 11 13
public static void fillTotals(int[][] scores)
{
int count = 0;
for (int r = 0; r < scores.length - 1; r++)
{
scores[r][0] += count;
scores[scores.length - 1][scores[0].length - 1] = count;
}
}
I thought I could keep the columns the same and add it down with the changing rows but it isn't rewriting the last row. Also I don't know how to change the values at the bottom
You need to iterate once over all rows and columns, actually iterate over all rows, for every column. If you assume that the number of columns is the same for every row, then you can use scores[0].length as a fixed value.
public static void fillTotals(int[][] scores) {
for (int c=0; c < scores[0].length; ++c) {
int sum = 0;
for (int r=0; r < scores.length - 1; ++r) {
sum += scores[r][c];
}
scores[scores.length - 1][c] = sum;
}
}
This assumes that the final row of the 2D array is not part of the sum and is available to be overwritten with the sum of all preceding values, for each column.
Well, the reason that nothing is being updated is that you never change count, so you just end up adding 0 to everything. I think what you want instead of:
scores[r][0] += count;
is:
count += scores[r][0];
That way count will contain the summation of every element in the first column.
To be clear, scores[r][0] += count; is the same as scores[r][0] = scores[r][0] + count;, whereas I think you probably want count = scores[r][0] + count;
That being said, Im still pretty sure this code isnt actually going to work (sorry), since you only ever actually sum values from the first column. However, for the sake of not just doing way may be a school assignment for you, Im just going to leave it there. If you're still stuck let me know, and I'll try to help!
This question is translated into English by me from another forum, I found it interesting and then just write a Java solution. And found there's some heap size problem when dealing with large number like 10000000. And I would like to seek some really smart solution compared with my own.
Original Post is in Chinese. And I kind of revised it a little based on my understanding to make it clearer.
http://zhidao.baidu.com/question/1637660984282265740.html?sort=6&old=1#here
Below is the puzzle:
10000 rows of numbers;
1 row: 2,4,6,8...2K(2K<=10000000); (numbers no repeats for this row)
2 row: 3,3,6,6,9,9...3K(3K<=10000000); (starting from this row, each number repeats 2 times and a multiple which has something to do with row number (2XrowNumber-1) to be specificaly)
3 row: 5,5,10,10,15,15...5K(5K<=10000000);
and following 7K,9K,11K,13K....until
10000 row: 19999,19999,39998,39998....19999K,19999K (19999K<=10000000);
That's all the rows to be used in the following part. And now we will calculate the repeat times of numbers starting from row 1 and row 2:
Integer w1 is the repeat times of numbers in row 1 and row2. For example, consider row 1 numbers 2,4,6 and row 2 numbers 3,3,6,6. Then the repeat times up to this point would be 3 since 6 is already in row 1 and appears 2 times in row 2, and 3 appears 2 times in row 2;
Integer w2 is the repeat times of numbers in row 1 and row 2 and row 3.
Integer w3 is the repeat times of numbers in row 1 and row 2 and row 3 and row 4.
......
Integer w9999 is the repeat times of numbers of row 1,row 2,row 3 .....row 10000.
And now print out all integers, w1,w2....w9999;
I have come up with one Java solution, but I have heap size problem since 10000000 is too large and the memory is not enough. So I just use 10000 instead of 10000000, and 10 instead of 10000. Below is what I write in Java. I guess it should be right (if not, please point it out):
Set nums = new HashSet();
int max = 10000;
int row = 10;
for (int i=2;i<=max;i+=2){
nums.add(new Integer(i));
}
int nums_size = nums.size();
int w = 0;
for (int i=2;i<=(row);i++){
int tmp_count = 0;
int self_count = 0;
for (int j=(2*i-1);j<=max;j+=(2*i-1)){
nums.add(new Integer(j));
self_count++;
if (nums.size()==nums_size){
tmp_count++;
} else {
nums_size = nums.size();
}
}
w += tmp_count;
w += self_count;
System.out.println("w"+(i-1)+": "+w);
}
My question is
How to get a better solution in Java (if any)?
How to do it in C
since there would be no Set class in C as I remember. (importing 3rd
party library would not be preferred)?
Thanks.
Here is a simplified version of your code. Since it doesn’t use HashSet, creating a C version out of it should be no problem anymore.
int max = 10000;
int row = 10;
boolean[] seen=new boolean[max+1];
for(int i=2;i<=max;i+=2) seen[i]=true;
int w = 0;
for(int i=2;i<=(row);i++) {
int self_count = 0;
for(int j=(2*i-1);j<=max;j+=(2*i-1)) {
self_count++;
if(seen[j]) w++; else seen[j]=true;
}
w += self_count/2;
System.out.println("w"+(i-1)+": "+w);
}
Not an answer, but a hint: try using least common multiples.
For example, LCM(5,3)=15, and 15 is the first element in both rows 2 and 3. LCM(2,15)=30, and 30 is the first element in each of the first 3 rows.
You'll eventually get to a row r where the LCM of the first element of the first r rows is beyond 10,000,000, at which point no number shows up each of those rows.
I need help, I am trying to make use of Lattice Multiplication in java for use in a BigInt class I am having to write for a class.
Right now I have the code storing the digits needed for the adding part of the algorithm in a 2 dimensional array. From there though I am at a lose as to how to make a loop to go through the array and add the numbers in what would be a diagonal.
For instance here is the test numbers etc:
200
*311
= 62200
The array is holding:
6 0 0
2 0 0
2 0 0
6 is (2,2) in the array and the bottom right is (0,0)
I need to add in a diagonal, such as (1,0) + (0,1) = 0
The issue is how do I do this, since not only is it moving up and left in different ways, but it goes from 1 element to 2 elements to 3 elements, then back the other way, and of course this will get bigger the longer the number is.
This is the code that I have:
public int multiply(BigInt val){
int[] bigger;
int[] smaller;
int[] dStore;
int lengthMax = (val.getSize()+this.getSize()) - 1;
int first = 0;
int second = 0;
int[][] tempResult;
//Checks to see which is bigger and then adds that to bigger
if(val.getSize() >= this.getSize()){
bigger = val.getData();
smaller = this.getData();
dStore = new int[val.getSize()+this.getSize()];
}else{
bigger = this.getData();
smaller = val.getData();
dStore = new int[val.getSize()+this.getSize()];
}
tempResult = new int[smaller.length][bigger.length];
for(int i=0;i < smaller.length;i++){
for(int j = 0;j < bigger.length;j++){
tempResult[i][j] = smaller[i] * bigger[j];
}
}
** there is the return statement etc below
This might help as to explain lattice multi better: Lattice Multi Video
I would try a different approach. Look at the lattice in the video and imagine that you rotates the array a little bit to the left so that the diagonals becomes vertical. The array would then look like this:
2 3 5
8 3
2 4 0
Now, just summarize the columns and you would have the total.
You would of course have to split the numbers into arrays of digits first. The easiest way to do that (but not the most efficient) is to convert them into strings ...
Good luck!
To move diagonally, you'd increment both x and y:
// Loop though the diagonal of an array
x = 0;
y = 0;
while (x < ARR_X_SIZE && y < ARR_Y_SIZE) {
// Do something with arr[x][y]
x++;
y++;
}
This is the basic loop; you can change the x and y increments to determine the direction you need to go. The key to going through the whole array is the value of the coordinates going into the loop. Array:
1 2 3
4 5 6
7 8 9
If you set x = 1; y=0 at the beginning of the loop, you'll get 2 6. Set x = 0, y = 1 and you'll get 4 8.
I hope this helps you with your assignment. Good luck on the rest! That is definately an interesting algorithm to implement.
I am trying to copy certain elements of an array into another. For example, I want to copy index 0 of lines into index 0 of links, index 3 of lines into index 1 of links, and so on (every 3 element of lines basically basically).
What I have so far keeps getting me an ArrayIndexOutOfBound error. Thank you for your help!
String[] lines = inputString.split(System.getProperty("line.separator"));
String[] links = new String[lines.length];
int j = 0;
for (int i = 0; i < lines.length; i++) {
links[i] = lines[j+3];
j++;
System.out.println(links[i]);
}
It sounds like you need to be incrementing i by 3, rather than adding 3 to j (but then incrementing j by just 1). In fact, you don't need two variables at all:
for (int i = 0; i < lines.length; i += 3) {
links[i / 3] = lines[i];
}
You should also change your code to only create the array to be as large as you need:
String[] links = new String[lines.length / 3];
Just for the sake of interest, let's have a look at what your code was actually trying to do, in terms of assignments:
links[0] = lines[3];
links[1] = lines[4];
links[2] = lines[5];
// etc
As you can see, this is just offsetting the index, rather than multiplying it... and as soon as i was lines.length-3, you'd end up with
links[lines.length - 3] = lines[lines.length]; // Bang!
A few problems:
1.
You probably want this:
for (int i = 0; i < lines.length; i++) {
links[i] = lines[j+3];
To look like this:
for (int i = 0; i < links.length; i++) {
links[i] = lines[j * 3];
2.
Well, yes. You are going out of bounds. Let's say lines is 12 elements. links is the same size, so you're trying to read from element 15 / 36 (depending on my correction in #1)
The solution is to make links smaller:
String[] links = new String[(int)(lines.length / 3)];
Along with the correction in #1.
Jon Skeet has the right idea. What might be useful is to learn how to debug these things yourself. Say that lines.length is 5, meaning also that links.length is 5. What happens if you trace every iteration of the for loop?
i | j | j + 3
-----------------------------------------
0 0 3
1 1 4
2 2 5
It'll break on this 3rd iteration. You can see that you're not accessing every three elements at all, but every element, and you're storing it in an offset position, which causes your index out of bounds exception.