Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9274291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:19:12+00:00 2026-06-18T16:19:12+00:00

I am having the hardest time figuring out what is wrong here: #include <iostream>

  • 0

I am having the hardest time figuring out what is wrong here:

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

double fact(double);
double sinTaylor(double);
double cosTaylor(double);

int main()
{
    double number, sineOfnumber, cosineOfnumber;

    cout << "Enter a number, then I will calculate the sine and cosine of this number" << endl;

    cin >> number;

    sineOfnumber = sinTaylor(number);
    cosineOfnumber = cosTaylor(number);

    cout << fixed << endl;
    cout << cosineOfnumber << endl;
    cout << sineOfnumber << endl;

    return 0;
}

double fact(double n)
{
    double product = 1;
    while(n > 1)
     product *= n--;
    return product;
}

double sinTaylor(double x)
{
    double currentIteration, sumSine;

    for(double n = 0; n < 5; n++)
    {
        currentIteration = pow(-1, n)*pow(x, 2*n+1) / fact(2*n+1);
        sumSine += currentIteration;
    }
    return sumSine;
}

double cosTaylor(double y)
{
    double currentIteration, sumCosine;

    for(double n = 0; n < 5; n++)
    {
        double currentIteration = pow(-1, n)*pow(y, 2*n) / fact(2*n);
        sumCosine += currentIteration;
    }
    return sumCosine;
}

Ok, so here’s my code. I’m pretty content with it. Except for one thing:
sineOfnumber and cosOfnumber, after the calling of sinTaylor and cosTaylor, will add each other in the following cout line that will print each other.
In other words, if number is equal to lets say, .7853, 1.14 will be printed in the line that is intended to print cosineOfnumber, and sineOfnumber will print the result normally.
Can anyone help me identify why this is? Thank you so much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T16:19:13+00:00Added an answer on June 18, 2026 at 4:19 pm

    Are you ever initializing the variables sumSine and sumCosine in your functions? They’re not guaranteed to start at zero, so when you call += inside your loop you could be adding computed values to garbage.

    Try initializing those two variables to zero and see what happens, as other than that the code seems okay.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having the hardest time figuring this out, even though it is one
I'm having the hardest time figuring out why Firefox3.1 returns the value of 'content'
I'm having the hardest time figuring out how to remove home screen AppWidget's programmatically
I am having the hardest time figuring out something that I think should be
So I'm having the hardest time figuring out what this thing is called, or
I'm having the hardest time figuring out something that seems like it should be
I am having the hardest time figuring out how to move my Subversion repository
I am having the hardest time figuring out a way to solve a problem
Check this link! I am having the hardest time trying to figure out why
I am having the hardest time trying to figure out this (should be) simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.