need to create a program that adds multiples(confusing see inside more detail)
I was assigned this problem for java class and i have spent well over ten hours working on this including theory and testing.
so the problem goes like this the value for position 1 is 1, the value for position 2 is position 1 + 1*1, the value for position 3 is equal to position 2 + 2*1, the value for position 4 is equal to position 3 + 2*2 etc etc. the user needs to enter a position and you need to return the value here is an example of the math behind it
| Position |
Value |
addition to get the next value |
| 1 |
1 |
+1*1 |
| 2 |
2 |
+2*1 |
| 3 |
4 |
+2*2 |
| 4 |
8 |
+3*1 |
| 5 |
11 |
+3*2 |
| 6 |
17 |
+3*3 |
| 7 |
26 |
+4*1 |
| 8 |
30 |
+4*2 |
| 9 |
38 |
+4*3 |
| 10 |
50 |
+4*4 |
| 11 |
66 |
+5*1 |
]
in other terms to get from 1 -> 2
you add 1 + 1*1 = 2
to get from 2 -> 3
you add 2 + 2*1 = 4
to get from 3 -> 4
you add 4 + 2*2 = 8
to get from 4 -> 5
you add 8 + 3*1 = 11
thank you in advance for your help
Re: need to create a program that adds multiples(confusing see inside more detail)
Can you post your code and explain what your problem is?
Post the program's output or any error messages.
Re: need to create a program that adds multiples(confusing see inside more detail)
Quote:
Originally Posted by
Norm
Can you post your code and explain what your problem is?
Post the program's output or any error messages.
for clarification int l is the length, or the location the user wants to find the value of x and y are controlling variables and pos is the current position. val is the value.
Code :
import java.util.Scanner;
public class Problem07
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int val=2,pos=3,l,y=2,x=2;
System.out.println("Enter a Value");
l = in.nextInt();
if(l==1)
val=1;
else if(l==2)
val=2;
else
{
do
{
y=1;
if(x==y)
{
System.out.println("x is increased by 1");
x++;
System.out.println(" X = " + x);
}
do
{
System.out.println("The value is being increased by " + x + " * " + y);
val+=x*y;
System.out.println("The value is " + val);
y++;
System.out.println(" Y = " + y);
pos++;
System.out.println("Pos = " + pos);
}while(y<=x);
}
while(pos<=l);
}
System.out.println(" value "+val);
}
}
Re: need to create a program that adds multiples(confusing see inside more detail)
The single letter l is a terrible name for a variable. It looks to much like a 1
That is what comments in the code are for. If anyone copies the code for testing, your "for clarification" notes won't be in the code that is copied
Can you show what the code does when it is executed?
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'
Paste here.
Re: need to create a program that adds multiples(confusing see inside more detail)
Code :
Enter a Value
5
The value is being increased by 2 * 1
The value is 4
Y = 2
Pos = 4
The value is being increased by 2 * 2
The value is 8
Y = 3
Pos = 5
The value is being increased by 2 * 1
The value is 10
Y = 2
Pos = 6
The value is being increased by 2 * 2
The value is 14
Y = 3
Pos = 7
value 14
The println statements are to view changes to the variables.I hope this helps. Thank you for the help with this
Re: need to create a program that adds multiples(confusing see inside more detail)
Is the answer wrong? You don't say if is wrong and show what it should be.
If the answer is wrong then I'd say that your algorithm is not right.
Can you list the steps in pseudo code for finding the solution?
Re: need to create a program that adds multiples(confusing see inside more detail)
the answer for when a three is entered is 4 but an 8 was returned
Code :
Enter a Value
3
The value is being increased by 2 * 1
The value is 4
Y = 2
Pos = 4
The value is being increased by 2 * 2
The value is 8
Y = 3
Pos = 5
value 8
this is for when the value entered is 6 the answer should be 17 it is 14.
Code :
Enter a Value
6
The value is being increased by 2 * 1
The value is 4
Y = 2
Pos = 4
The value is being increased by 2 * 2
The value is 8
Y = 3
Pos = 5
The value is being increased by 2 * 1
The value is 10
Y = 2
Pos = 6
The value is being increased by 2 * 2
The value is 14
Y = 3
Pos = 7
value 14
this if for when the value entered is 7 the answer should be 26 the given answer is 20
Code :
Enter a Value
7
The value is being increased by 2 * 1
The value is 4
Y = 2
Pos = 4
The value is being increased by 2 * 2
The value is 8
Y = 3
Pos = 5
The value is being increased by 2 * 1
The value is 10
Y = 2
Pos = 6
The value is being increased by 2 * 2
The value is 14
Y = 3
Pos = 7
The value is being increased by 2 * 1
The value is 16
Y = 2
Pos = 8
The value is being increased by 2 * 2
The value is 20
Y = 3
Pos = 9
value 20
I appreciate your help with this
There was no pseudo code but this was the prompt (paraphrased)
1 2 4 8 11 17 26 30 38 50 66
1 2 3 4 5 6 7 8 9 10 11
The value of location 1 is 1. The value of location 2 is 1 plus the product of 1*1. The value of location 3 is the value of location 2 (2) plus 2*1. The value of location 4 is the value of location 3 (4) plus the product of 2*2. This pattern continues with the program adding the first multiple of the next number in the sequence until the second factor is equal to the first factor (3*3) then the first factor is increased by 1 (3*1, 3*2,3*3, 4*1 , 4*2) .Allow the user to enter a location n (length in my program) and calculate the value at that location using this pattern.
I am questioning what my algorithm for this would be or how should I go about it. I am very new and thus very lost with this. Thank you for sticking with me
Re: need to create a program that adds multiples(confusing see inside more detail)
Look at the table you posted earlier and in the column labeled: addition to get the next value
there appears a pattern in the equation. The first number is repeated a number of times equal to its value, the second number starts at one and increases by 1
This reminds me of a nested loop.
To see the pattern write a small simple program with a nested loop. On the inside print out the values of the outer loop:i and inner loop:j indexes: println(i + " "+ j).
Have the inner loop stop when its index equals the value of the outer loop index
Re: need to create a program that adds multiples(confusing see inside more detail)
test program
Code :
public class testLoop
{
public static void main(String[] args)
{
for(int j = 1; j<=5;j ++)
{
for(int i=1; i<=j;i++)
{
System.out.println(j + " * " + i);
}
}
}
}
output
Code :
--------------------Configuration: <Default>--------------------
1 * 1
2 * 1
2 * 2
3 * 1
3 * 2
3 * 3
4 * 1
4 * 2
4 * 3
4 * 4
5 * 1
5 * 2
5 * 3
5 * 4
5 * 5
Process completed.
I need to find a way to stop this loop when the desired position is reached. thank you for the guidance.
Tried this and it failed to kick me out of the loop
Code :
public class testLoop
{
public static void main(String[] args)
{
int pos = 0;
do
{
for(int j = 1; j<=5;j ++)
{
for(int i=1; i<=j;i++)
{
System.out.println(j + " * " + i);
pos++;
}
}
}while(pos<5);
}
}
Re: need to create a program that adds multiples(confusing see inside more detail)
Look at the break statement to get out of a loop
Re: need to create a program that adds multiples(confusing see inside more detail)
I figured it out. Thank you so much for this i appreciate all the help and you staying with me throughout my stupidity.
Re: need to create a program that adds multiples(confusing see inside more detail)
What do you mean by control? Using an if statement with the break statement would exit the loop.