Have my formula, but I don't know how to code it.

Hi,

I am working on some logic puzzles. I need to apply this formula, starting at zero and moving up to whatever number is entered. (+ .5) * 2

So, if the number entered is 1, the answer would be 1.

If it was 3, the answer would be 7.
0 + .5= .5 (.5*2)= 1
1+.5=1.5 (1.5*2)= 3
3+.5=3.5 (3.5*2)= 7
Hello jjordan33,

So what have you done so far? You are asking for someone else to do your work, but what have you done first.

It is easy to correct what is wrong, but there is nothing to correct.

Is what you have shown the output or the question?

Andy
Hello jjordan33,

Given:

If it was 3, the answer would be 7.
0 + .5= .5 (.5*2)= 1
1+.5=1.5 (1.5*2)= 3
3+.5=3.5 (3.5*2)= 7


What happen to 2?

Unless you hard code each line a for loop would do "0, 1, 2" or "0, 1, 2, 3" which gives the first group an answer of 5 and the second group an answer of 7.

Hope that helps,

Andy

P.S. I did not use any formula. I just coded a single "std::cout" statement.

Edit: I did add an if statement to print out "0, 1, 3".
Last edited on
Nah, I don't want the work done for me. I just don't know how to loop it, so I don't have to hardcode all the way up to 10,000.

1 and 3 were just examples. It could be any number, two included.

I have it where user inputs an int, say 8.
That indicates how many numbers I'll output with that formula, so they would then enter the 8 numbers, and the output line would be the formula answer to each of the 8 numbers entered.

And it's just a personal challenge. There's no prize, no grade, no money. I'm just using them to teach myself to code.

pseudo code

cin number
2
enter the two numbers now
1
3

output
1=1
3=7


Hello jjordan33,

This may help http://www.cplusplus.com/doc/tutorial/control/#for

I did the program with one for loop and one cout statement. All the math was done in the cout statement.

When you come up with something post the code and we can go from there.

Andy
Hello jjordan33,

This is what I get with one cout statement inside a for loop:

 Enter the number of iterations: 20

 0 + .5 = 0.5 -> (0.5 * 2) = 1
 1 + .5 = 1.5 -> (1.5 * 2) = 3
 2 + .5 = 2.5 -> (2.5 * 2) = 5
 3 + .5 = 3.5 -> (3.5 * 2) = 7
 4 + .5 = 4.5 -> (4.5 * 2) = 9
 5 + .5 = 5.5 -> (5.5 * 2) = 11
 6 + .5 = 6.5 -> (6.5 * 2) = 13
 7 + .5 = 7.5 -> (7.5 * 2) = 15
 8 + .5 = 8.5 -> (8.5 * 2) = 17
 9 + .5 = 9.5 -> (9.5 * 2) = 19
 10 + .5 = 10.5 -> (10.5 * 2) = 21
 11 + .5 = 11.5 -> (11.5 * 2) = 23
 12 + .5 = 12.5 -> (12.5 * 2) = 25
 13 + .5 = 13.5 -> (13.5 * 2) = 27
 14 + .5 = 14.5 -> (14.5 * 2) = 29
 15 + .5 = 15.5 -> (15.5 * 2) = 31
 16 + .5 = 16.5 -> (16.5 * 2) = 33
 17 + .5 = 17.5 -> (17.5 * 2) = 35
 18 + .5 = 18.5 -> (18.5 * 2) = 37
 19 + .5 = 19.5 -> (19.5 * 2) = 39
 20 + .5 = 20.5 -> (20.5 * 2) = 41


 Press Enter to continue



Andy
I have it where user inputs an int, say 8.
That indicates how many numbers I'll output with that formula,

The user gives a number. Your loop must repeat that many times.

Andy did give a link to explanation of for loop. To repeat N times should be easy.


they would then enter the 8 numbers, and the output line would be the formula answer to each of the 8 numbers entered

That is more interesting. Do you want to read N values one at a time and immediately after reading a value show the result for it?

OR

1. Read all N values
2. Show result for every value
Thanks Andy. I'm working on it now.

You should check out the challenge site. open.kattis.com
Hello jjordan33,

You are welcome.

In the future when you want to post a link in a message just copy the address and paste it into the message. The only rules are that there needs to be a space before the address and a space or new line at the end.

You gave what could look line this: https://open.kattis.com/problems , but the name of what you are working on would have helped. Notice the space after the colon and before the comma.

Andy
Hey,

Okay, I've figured out how to loop a cin input. While working on this, I had a thought and I'm not sure how it's done.

Can you get a program to just keep repeating a calculation that's cumulative?

example;

(0+15)/3= 5
(5+15)/3= 6
(6+15)/3= 7
7+....
Hello jjordan33,

Yes it is possible.

1
2
3
4
5
for (size_t lc = 0, total = 0; lc < 10; lc++)
{
	std::cout << " (" << total << " + 15) / 3 = " << (total + 15) / 3 << std::endl;
	total = (total + 15) / 3;
}

The only problem is with integer division when "total" reaches "7" it stops and just and repeats its self.

I know there is a better solution, just can not think of it at the moment.

Hope that helps,

Andy
**edit** I know std:: optimizes speed, but I started learning using namespace std, and I my OCD requires its continued use for now.***

It does. I took all that and tried to make it into one program. I had moderate success. The following code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main()
{
    int x;
    
    cin >> x;
    
    for (int i = 0; i <= x; i++) {
        cin  >> i; 
            for (size_t xx = 0, total = 0; xx <=i; xx++) {
                total = (total + .5) * 2;
	            cout << (total + .5) * 2 << endl;
            }}
	
}



… couts the solution to that formula for a bunch of numbers. I just want it to print out the solution for whatever numbers I enter.
(And, how many numbers that is, is dependent on the original int x.)


Does any of that make sense?
Last edited on
Hello jjordan33,

They must also teach the worst way to write your code. This is much easier to read and understand.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int main()
{
	int x;

	cin >> x;

	for (int i = 0; i <= x; i++)
	{
		cin >> i;  // <--- What is this for and why is it needed?

		for (size_t xx = 0, total = 0; xx <= i; xx++)
		{
			total = (total + .5) * 2;
			cout << (total + .5) * 2 << endl;
		}
	}

	return 0;
}

Now looking at the program.

Line 9 needs a prompt to let the user know what to enter.

The outer for loop is controlled by the value of "x", but line 13 inputs a new value for "i" which could shorten the loop or even cause it to loop only once. I am having a hard time understanding why you even need this outer loop. of course if you remove line 13 it makes more sense.

The program can work, but I do not know what the output should be or if what I get is what you want.

Based on x = 4 and broken up in groups based on the outer for loop.

4
3

3
7

3
7
15

3
7
15
31

3
7
15
31
63


 Press Enter to continue


Hope that helps,

Andy
The first line of input is to decide how many numbers the person wants to enter. int x

so say int x is 5.

The int i is set in the for loop to x so that they could then enter the 5 numbers. It's sort of a way for me to not use an array since I'm still confused by arrays.

So, in theory..
pseudo code:

how many numbers?
cin x= 3

enter the <<x<< numbers now
cin i, i, i
Names of variables can be informative:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
  int Cases {};
  cin >> Cases;
  for (int case = 0; case <= Cases; ++case ) // repeats Cases+1 times
  {
    size_t number {};
    cin >> number;
    for ( size_t xx = 0; xx <= number; xx++)
    // xx gets values [0..number], inclusive
    {
    }
  }
}



@chelsea:
What does your problem have to do with this thread?
Topic archived. No new replies allowed.