How to write and implement your own function [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I need to write my own sqrt function: double my_sqrt_1(double n)
How would I go about doing this? At first I tried putting this outside of "int main()":
double my_sqrt_1(double n)
{
int x = 1;
x = (x + n / x) / 2;
}
I then put this:
int main()
{
cout << "Please enter a value ";
cin >> my_sqrt_1;
cout << '\n' << x;
}
I also tried:
int main()
{
cout << "Please enter a value ";
cin >> my_sqrt_1;
cout << '\n' << my_sqrt_1;
}
None of this worked though. I'm probably doing this completely wrong, but it made sense in my head.

"I'm probably doing this completely wrong ..."
Sorry to say that, but yes.
You need a variable to receive input, and call your function passing that variable
int main() {
cout << "Please enter a value ";
double myNumber;
cin >> myNumber;
cout << '\n' << my_sqrt1(myNumber) << endl;
}
Also your function is supposed to return the result of the calculation
double my_sqrt_1(double n) {
double x = 1.0;
// ^^^^^^ ^^
x = (x + n / x) / 2.0;
// ^^
return x; // <<<<<<<<<<<<<<
}

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.

I keep getting the same answer, no matter what I put in [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 5 years ago.
Improve this question
We're doing an assignment in my computer science class that requires us to find the future value of an investment after "n" number of years that the user inputs. It's written in C++. This is the code as I have it now:
#include <iostream>
using namespace std;
int main() {
int P=1000;
int i=0.0275;
int FV;
int n;
cout << "enter number of years:"<< endl;
cin >> n;
cout << "the future value is:"<< endl;
FV = P*(1+(i*n));
cout << FV << endl;
return 0;
}
I keep ending up with an answer of 1000 no matter what "n" I input. Can someone tell me what's wrong with the code?
#include <iostream>
using namespace std;
int main() {
int P=1000;
float i=0.0275; //float instead of int
float FV; //FV should also be float as it will be storing decimal values
int n;
cout << "enter number of years:"<< endl;
cin >> n;
cout << "the future value is:"<< endl;
FV = P*(1+(i*n));
cout << FV << endl;
return 0;
}
the mistake you have done is the type you assigned to your variables! as int only handles integer values i becomes 0 and your result becomes 1000! use float instead of int for numbers with decimal points!
Datatype of i is int as a result of which your floating point value of i will be rounded of to 0 and you will end up getting the same output doesn't matter what your n value is. Change the datatype of your i and FV varaiable from int to float then your output changes based on what n value you key in

c++ vector : initializing with cin in a loop [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm new to programming and i have problem with some items
i would appreciate any help
first i started initializing the vector as followed but i couldn't end the loop with Ctrl+Z
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector <double> temps;
double temp;
cout << "Enter a sequence of tempreatures : " << "\n" ;
while (cin >> temp){
temps.push_back(temp);
}
double sum = 0;
for (int i = 0; i< temps.size(); ++i)
sum += temps[i];
cout << "Mean temprature : " << sum / temps.size() << "\n";
sort(temps.begin(), temps.end());
cout << "Median temprature : " << temps[temps.size() / 2];
then i changed the while into this format :
cout << "ENter a sequence of tempreatures ending in 1500 : " << "\n" ;
while (cin >> temp){
if (temp == 1500)
break;
temps.push_back(temp);
}
now i have this error
"vector subscript out of range"
apparently break does not work properly here
what should i do?
Your issue is in the check condition of for loop.
for (int i = 0; i, temps.size(); ++i)
sum += temps[i];
It should be
for (int i = 0; i < temps.size(); ++i)
i, temps.size() will evaluate and then ignore the part before , and are left with temps.size() as check condition which will always be greater than 0 if you push_back at least one element and your loop will never end.You might want to read how ,(comma) works.
If you switch to std::getline into a string instead of std::cin into a double, you can check whether the input is empty:
std::string input;
std::getline(std::cin, input);
while (!input.empty()){
temps.push_back(atof(input.c_str()));
std::getline(std::cin, input);
}
If you also fix the for-loop as mentioned by Gaurav Sehgal, it works fine (Enter all numbers then hit enter without any input).
If you are on windows then you have to do
CTRL + Z
If you are on Unix based(Linux/Mac) then you have to do
CTRL + D
This will give the end of file signal and you will be able to break the loop

Returning values from a separate function in 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 6 years ago.
Improve this question
I am doing a programming assignment in my CS class using C++ that is giving me problems. The assignment is to write a program that asks the user to input a Fahrenheit temperature. The program must then output the Celsius equivalent of that temperature and the Celsius equivalents of the next 20 Fahrenheit temperatures. This must all be done using a separate function to complete the conversion calculation as well as a function prototype.
I got everything to work except my Celsius outputs all show a value of 0. I cannot seem to find any errors or figure out what is wrong...Please Help!
#include <iostream>
#include <iomanip>
using namespace std;
double Celsius(double temp);
int main() {
double fTemp, cTemp;
cout << "Please input a temperature in degrees Fahrenheit: ";
cin >> fTemp;
cout << "F Temp" << " " << "C Temp"<< endl;
cout << "-------------------" << endl;
for(int count=1; count<= 20; count++){
cTemp = Celsius(fTemp);
cout << setw(6) << fTemp << setw(12) << cTemp<< endl;
fTemp++;
}
return 0;
}
//*************Celsius Function****************//
double Celsius(double temp){
double newC;
newC = (temp-32) * (5/9);
return newC;
}
//************************************************
(5/9) is integer-integer division. You might've as well typed 0 there. Replace it with (5.0/9.0) and it should be fine.
One could simply try (5.0/9.0) or use a type cast by doing something like
double Celsius(double temp){
double newC;
newC = (temp-32) * (float)5/9;
return newC;
}
Thereby, you will not be getting a straight 0 in your outputs.

C++ Prime Number not giving the correct answer [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 9 years ago.
Improve this question
#include <iostream>
#include <cmath>
using namespace std;
bool prime(int n);
int main()
{
double i;
while (true)
{
cout << "Enter a number that isn't 0: ";
cin >> i;
if ( i == 0)
break;
if(prime(i))
cout << i << " is prime" << endl;
else
cout << i << " is not prime." << endl;
}
system ("Pause");
return 0;
}
bool prime (int n)
{
int i;
double sqrt_of_n = sqrt(double (n));
for (i = 2; i <= sqrt_of_n; i++)
{
if (int(n) % 1 == 0)
return false;
}
return true;
}
Everytime I run the program, if I input 7, I get that 7 isn't prime. Can someone help me figure out where I messed up?
I have tried changing between double and int for i and n.
If I input 3, it shows prime.
The problem is that it's showing some prime numbers as not prime.
The body of your for loop doesn't use i at all.
In particular, n % 1 is always zero, for any integral n.
Presumably you want to know whether n is divisible by i, but accidentally checked if n is divisible by 1.
You could easily have discovered this mistake yourself by single-stepping in a debugger, and making the various subexpressions into "watch expressions".