Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
The program should be print the numbers 0 through 10, along with their values doubled and tripled. The data file contains the necessary variable declarations and output statements.
Example the output:
single 1 double 2 triple 3
single 2 double 4 triple 6
single 3 double 6 triple 9
single 4 double 8 triple 12
here my code tell me if correct
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
int x, double, triple;
int x = 0
while (x<=10)
{
cout << "x" << double = (x*2) << triple = (x*3) << endl;
x++;
}
return EXIT_SUCCESS
I'll attempt to put a guiding answer so I'm not going to give you a straight code that you can just copy and paste into your homework, but if you read and follow it, it should be the answer. (And next time, do go find your lecturer, or your tutor).
Some issues:
You are not printing the "Single" and "double" and "Triple" text which you should (based on your expected answer) So add that in.
You did your calculation to get the number for double, and triple - good. But again, you did not print them out.
Also C++ allows you to stack multiple cout all on one line, so for example:
cout << "My name is " << myname << endl;
Will print out:
My name is (content of variable myname)
And then print an end of line (endl). You can use that same technique to do part of your assignment to print out the results to meet the expected output.
Credit to Synetech
you miss a lot of code in there. You didn't print nothing what you want
try again with this inside while loop:
cout << “single “ << x << double << x*x
<<“ triple “<< x*x*x << endl;
x++;
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I've just started learning C++ and I came across a simple problem but couldn't solve. Help me please,
In geometric progression, b1=2, r=3.
Create recursive function that outputs initial n sequences of progression. n should be input.
Assuming b1 is the initial number and r is the multiplier (ratio?), that would give 2, 6, 18, 54, ....
The pseudo-code for such a beast would be:
def geometric (limit, current, multiplier):
if limit <= 0: return
print(current)
geometric(limit - 1, current * multiplier, multiplier)
def main():
b1 = 2
r = 3
n = input("How many terms? ")
geometric(n, b1, r)
The reason it's pseudo-code is that I tend to prefer for educational questions since people learn a lot more by converting it rather than just being given an answer.
So, in terms of how the recursive function works, you pass it the number of terms remaining along with the current term and the multiplier.
Provided it hasn't reached its base case (number of terms remaining is zero), it prints the current term then recursively calls itself, adjusting the arguments so as to approach the base case.
Once it has reached the base case, it will return (from all recursive levels).
Turning that into C++ is an exercise I'll leave for the reader, but I can provide something similar so you will see the approach taken.
Let's say you wanted to sum all the numbers between 1 and a given number. It has the same features as any recursive solution, including the one posed in your question:
an operation that can be defined in terms of a simpler operation; and
the base case.
In C++, a complete program to do that would be something like:
#include <iostream>
void recurse(int number, int sum) {
// Base case for printing and returning.
if (number <= 0) {
std::cout << "Sum is " << sum << '\n';
return;
}
// Recurse to next level.
recurse(number - 1, sum + number);
}
int main() {
int myNum;
std::cout << "Enter your number: ";
std::cin >> myNum;
recurse(myNum, 0);
}
Some sample runs:
Enter your number: 3
Sum is 6
Enter your number: 4
Sum is 10
Enter your number: 5
Sum is 15
Enter your number: 6
Sum is 21
Enter your number: 100
Sum is 5050
The explanatory text on your specific question, along with the C++ code showing how to do it for a different question, should hopefully be enough for you to work it out.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
My first stackoverflow post!
After entering a value for age into a declared and initialized int,
something weird happens and the value explodes. I test my code and could not see why it happens. After rechecking I can see that it is the last peice of code that did something to my int value.
I ask the stackoverflow gods "Why".
My code here:
int main()
{
cout << "Please enter your name and age\n\n";
string first_name;
int age(0);
cout << age << "\n\n"; // for testing why i get a huge number for age
cin>> first_name >> age;
cout << age << "\n\n"; // for testing why i get a huge number for age
cout << "Hello, " << first_name << " age " << age << '`\n';
keep_window_open(); // window must be closed manually
return 0;
}
This seems to be the offending bit:
'`\n';
This is the output I would get:
Please enter your name and age
0
et
23
23
Hello, et age 2324586
'`\n'
That's actually two characters, not only the newline feed. Plus you use single quotation marks, these are only used for single characters since char literals are of type const char.
The standard says:
The value of an integer character constant containing more than one
character (e.g., 'ab'), or containing a character or escape sequence
that does not map to a single-byte execution character, is
implementation-defined.
And thus the numbers after 23 : 24586 is the implementation-defined part that's causing weird output here. Use double quotes or '\n'.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
The output of the following code is 51 instead of the expected 5. I use Xcode 6.4 with the LLVM compiler on Mac OS Yosemite 10.10.4.
#include <iostream>
int main(int argc, const char * argv[]) {
std::cout << std::printf("%u", 5) << std::endl;
return 0;
}
If have tried to supply an int, unsigned long and uint32_t, and I have swapped the %u for a %d and %lu, but I always get the same result: A strange number being after the intended printf output. Swapping the 5 for another number results in another unexpected number being added to the output. This is in a fresh project with no other code added. What am I overlooking?
printf returns an int. In your case it is returning 1 which is the number of characters written (5).
cout is printing that 1, after printf prints 5 because you're chaining them together.
To just get 5, say:
std::printf("%u\n", 5)
printf returns number of printed characters, so '5' is printed by printf itself, then '1' is printed as result of cout << result_of_print
what you probably want is
std::cout << 5u << std::endl;
Also: avoid mixing C style output (printf) with C++ style (std::cout)
Your using both std::cout and std::printf.
Just one one of them, like this:
std::cout << 5 << std::endl;
OR
std::printf("%u", t);
You should not mix cout and printf together... what is happening is that printf returns the number of characters printed (in your case one character, which is 5). So the printf statement is executed, printing 5, but then cout prints the return value of printf, adding an extra 1.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm really fresh to C++, and I faced difficulties with the next question :
Write a program that asks the user to enter a number : n
THEN calculate f(n).
Notice that :
f(0)=3 and f(n+1) = 3/4 * f(n) + 4 ?
For example :
f(1)= 6.25
f(2)= 8.69
f(3)= 10.52
f(4)= 11.89
f(5)= 12.92
====================================================
So, how can I solve this?
thanks for all..
I try this code depending on Mr. paxdiablo answer :
#include<iostream>
using namespace std;
int main()
{
double x=0.0;
cout<<" enter an integer N:";
cin>> x;
double f1(double x)
{
if x==0.0
return 3;
return 3 / 4 * f1 (x-1) + 4;
}
return 0;
}
But the program never runs!
================================================
The Correct Solution is :
#include<iostream>
#include<iomanip> //To enable "setprecision" tool
using namespace std;
double f(int x){
if (x==0)
{return 3;}
return (3.0/4.0) * f(x-1) + 4.0; //we add zeros to get "double" results
}
int main()
{
int n=0;
cout<<" Please, enter an integer :";
cin>> n;
cout<<fixed<<setprecision(2)<<f(n); //"setprecision" used to get only two digits after the point
return 0;
}
BIG thanks to everyone gave me a hand and special thank to Mr. paxdiablo.
This sounds like a job for ... Recursion Man!
Simply define a recursive function that returns 3 when n is zero or calls itself with a reduced problem.
Pseudo-code would be something like
def f(x):
if x == 0:
return 3
return 3 / 4 * f(n-1) + 4
You can use std::cin >> dv to get a value from the user and that function (recoded in C++) to do the grunt work. Then std::cout << result to output the result. Just remember to use doubles rather than ints, including constants like 3.0.
You have to define the recursive function f, then read the input from the user, then apply the function to the input you've read (probably as an integer?), and print the output.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I want to make a function which takes an integer with the number like 113, and separates the one's digit "3" and and the hundreds and tens places "11" and returns the both of them in two separate integers.
x%10 for the first digit (from right) and x/10 for the rest.
#include <iostream>
#include <utility>
std::pair<int,int> split(int x)
{
return std::make_pair(x/10, x%10);
}
int main()
{
std::pair<int,int> z = split(113);
std::cout << z.first << " " << z.second;
}
I also used std::pair to return the result.
You want N % 10 to get the one's digit. For the other digits N / 10.