Why is Y = 22 on the first line print? [closed] - c++

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 2 months ago.
Improve this question
Hey was just wondering why the first iteration of cout << x << " " << y; = 3 22 ? Im confused as why its not 3, 2? can someone explain ?
int fun(int x, int y) {
cout << x << " " << y;
if (x == 0) {
return y;
}
return fun(x-1, x*y);
}
int main() {
int result;
result = fun(3,2);
}
RESULT: 3 22 61 120 12

It's not "3 22" it's "3 2" followed by "2 6" from the next call to f(). Add a newline to separate the lines in the recursive calls.
std::cout << x << " " << y << '\n';
and you'll see the output
3 2
2 6
1 12
0 12
live link

Related

I cant seem to get the amount of decimals right using setprecision c++ [closed]

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 2 months ago.
Improve this question
I'm currently getting into subroutines/subprograms or whatever you call them in english and in this specific assignment that i'm trying to solve I have to calculate the average length of two words.
void Length(string const text_1,
string const text_2,
int & total_length,
double & mean_length)
{
total_length = text_1.length() + text_2.length();
mean_length = static_cast<double>(total_length) / 2;
}
void Length_Program(int val)
{
string text_1;
string text_2;
int total_length{};
double mean_length{};
cout << "Mata in två ord: ";
cin >> text_1 >> text_2;
cout << "Totallängd: ";
Length(text_1, text_2, total_length, mean_length);
cout << total_length << endl;
cout << "Medellängd: " << fixed << setprecision(1) << mean_length;
}
I have set the precision to setprecision(1) and I assume it will only write one decimal but I keep getting two decimals.
my example is: abcd E
it should say that it is an average of 2.5 words but it says 2.51 for some reason. Can someone help me understand what i'm doing wrong?
Your problem is that you forgot << endl on your last output line. The return code shown by the OS is appended to your output. The setprecision is working just fine.

Why does the range for loop iterator in C++ start at 1 and not 0? [closed]

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 2 years ago.
Improve this question
In C++ why does the range-for-loop iterate starting at 1 and not at 0? In this example you would expect it to iterate through the numbers just fine however it hits an out of bounds error because i starts at 1 and ends at the size of vector.
vector<int> numbers2 = { 1,2,3,4,5 };
cout << numbers2.size() << '\n';
for (int i : numbers2) {
cout << numbers2[i] << endl;
cout << i << endl;
}
To correctly cout the result it has to be re-written with
cout << numbers2[i - 1] << endl;
The range-based for loop loops through the values of the vector, there is no index. You are looping through {1, 2, 3, 4, 5} vector and i is each of those values throughout the iterations not the index.

C++ For Loop Won't Run [closed]

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 4 years ago.
Improve this question
I'm beginning to learn c++ with the free online codecademy course, and I'm not sure if it's a bug with their IDE or an error in my code.
#include <iostream>
#include <string>
int main() {
for (int i = 0; i > 0; i--) {
std::cout << i << " bottles of beer on the wall\n";
std::cout << i << " bottles of beer\n.";
std::cout << "take one down and guzzle it down\n";
std::cout << i - 1 << " bottles of beer on the wall.\n\n";
}
}
This is supposed to loop those strings until the number gets down to 1. Thank you for your help.
Your variable i is never bigger than 0.
I think that you meant to write int i = 10;
#include <string>
int main() {
for (int i = 10; i > 0; i--) {
std::cout << i << " bottles of beer on the wall\n";
std::cout << i << " bottles of beer\n.";
std::cout << "take one down and guzzle it down\n";
std::cout << i - 1 << " bottles of beer on the wall.\n\n";
}
}
I am not sure what your code is supposed to loop through. but this will run 10 times. you say you have a string. so try to get a variable to assign to a function string.length() to get the length assigned to i and then the loop will run as many times as the number of characters in the string.
the above code will run 10 times though

fill missing places in c++ program to get desired output [closed]

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 5 years ago.
Improve this question
Fill up the blanks with appropriate keyword to get the desired output according to the test cases. (Language use c++)
Sample Test Cases
input output
Test Case 1 4 square = 16, ++ square = 25
test case 2 -8 square =64, ++ square = 49
#include <iostream>
using namespace std;
______ int SQUARE(int x) { ______ x * x; }
​
int main() {
int a , b, c;
cin >> a ;
b = SQUARE(a);
cout << "Square = " << b << ", ";
c = SQUARE(++a);
cout << "++ Square = " << c ;
return 0;
}
Second blank place is "return"
First may be "inline" or nothing

I am learning C++ via a book. What I am doing wrong? [closed]

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 8 years ago.
Improve this question
I am learning C++ via a book. It told me to type this compile and link it in order for me to see if i can read it. But there are errors when I run this. What is the issue?
#include <iostream>
int main()
{
int x = 8;
int y = 6;
std::cout << std::end1;
std::cout << x - y << " " << x * y << " " << x + y;
std::cout << std::end1; return 0;
}
It should be std::endl; instead of std::end1;