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 days ago.
Improve this question
I have an exercise to do to implement the Gregory-Leibniz series approximation of Pi. This is the code I have:
#include <iostream>
#include <iomanip>
#include <cmath>
int main() {
int k = 0;
int d = 0;
std::cin >> k;
std::cin >> d;
double sum = 0;
for(int i;i<=k;i++){
sum = sum + (pow(-1, i) / (2*i + 1));
}
sum = 4 * sum;
std::cout << std::fixed << std::setprecision(d) << sum << "\n";
return 0;
}
On my computer, for example, the input "1 3" outputs result "2.667", but on the school coderunner website, the output is always 0s, so in this it's "0.000". Can someone please help me?
Related
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
I'm trying to write a program using a while loop that gives an output based on range of input variable declared.
#include <iostream>
using namespace std;
int main() {
float fahren = 0;
float celsius;
while (fahren < 400) {
// convert fahreneheit to celsius
// Subtract 32, then multiply it by 5, then divide by 9
celsius = 5 * (fahren - 32) / 9;
cout << celsius;
fahren+= 100;
}
return 0;
}
I expect 4 values to be returned, however I only get 1, which is the value at 0.
You print 4 values but without separator, you might add std::cout << std::endl;
while (fahren < 400) {
// convert fahreneheit to celsius
// Subtract 32, then multiply it by 5, then divide by 9
celsius = 5 * (fahren - 32) / 9;
std::cout << celsius << std::endl; // added new line here
fahren += 100;
}
Demo
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
my knowledge of c++ is pitiful. I've been stumped on this simple problem for a long time and would just a point to the right the direction. The basis of the program is to have the user chose a number 1-5 and then based on their decision print out a quote that many times. So if they chose the number 4 it will display the quote 4 times.
Here is what I have so far:
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
int PickNumber()
{
int i;
cout << "Please Enter a Number From 1 to 5:";
cin >> i;
for (int j = 0; j < i; j++)
{
cout << "Congrats!";
}
return i;
}
int main()
{
_getch();
return 0;
}
Just to add an answer to this question already solved by #molbdnilo, #Rietty and #Gox: the PickNumber() function is not called in the main function.
The PickNumber() call just needs to be added to the main function.
int main() {
PickNumber();
return 0;
}
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 a student who just learned to use C++ for a few weeks. I write C++ code on Code:: Block and I am trying to run this code to learn about vector:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<int> intVector;
for(int i = 0; i < 10; i++)
{
intVector.push_back(i+1);
}
cout << "Numbers in vector: ";
for(int i = 0; i < intVector.size(); i++)
{
cout << intVector[i] + " ";
}
}
But the output is really weird:
Numbers in vector: vector::_M_emplace_back_auxector::_M_emplace_back_auxctor::_M_emplace_back_auxtor::_M_emplace_back_auxor::_M_emplace_back_auxr::_M_emplace_back_aux::_M_emplace_back_aux:_M_emplace_back_aux_M_emplace_back_aux
Process returned 0 (0x0) execution time : 0.047 s
Press any key to continue.
Does anyone know how to fix this problem? Do I use vector in the wrong way?
Try changing the cout line like this:
cout << intVector[i] << " ";
What you're trying to do is add an int& and a string literal -- in some compilers it will emit a warning indicating for you that you're not getting what you intended for.
For example with Clang:
warning: adding '__gnu_cxx::__alloc_traits>::value_type' (aka 'int') to a string does not append to the string [-Wstring-plus-int]
cout << intVector[i] + " ";
~~~~~~~~~~~~~^~~~~
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
Here are the instructions:
Write a program that accepts an integer input from the keyboard and computes the sum of all the integers from 1 to that integer. Example, if 7 were input, the sum: 1 + 2 + 3 + 4 + 5 + 6 + 7 would be computed. Use a while or for loop to perform the calculations. Print out the result after the sum has been calculated. NOTE: if you input a large integer, you will not get the correct results.
What I can't figure out is how to ADD all of the integers together. Any help would be greatly appreciated.
//preprocessor directives
int main ()
{
//declare and initialize variables
int n, i;
int total;
//user input
cout << "Enter an integer: ";
cin >> n;
//compute sum of all integers from 1 to n
total=0;
for (i = 1; i <= n; i++)
cout << i;
return 0;
}
You add by using the += operator:
for (i = 1; i <= n; i++)
total += i;
cout << total;
Note this is short for total = total + i.
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
As I understand from
Left shift Float type
one cannot use left shift operator on float values. But when I tried it, it gave the same answer as multiplying by 2n.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
// your code goes here
float a = 1.1234;
int b = (int)(a*(1<<10));
int c = (int)(a*pow(2,10));
cout << "\n a = " << a << " b = " << b << " c = " << c;
return 0;
}
It outputs a = 1.1234 b = 1150 c = 1150
In which case will the two outputs (b and c) differ?
int b = (int)(a*(1<<10));
Here, since both 1 and 10 are integers, you are performing left shift operation on integer instead of on floating-point number.
you are multiplying 1024 with value of a(1.1234) in both case.
it does not mean you are shifting float value.