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".
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.
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.
**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>
usingnamespace 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.)
#include <iostream>
usingnamespace 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
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.
int main()
{
int Cases {};
cin >> Cases;
for (intcase = 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?