Solved

Arithmetic inside arrays

Posted on 2008-10-21
2
189 Views
Last Modified: 2012-05-05
Why is is that when I create my array with this line:

      double b[6] = {1/6, 5/24, 11/120, 19/720, 29/5040, 1/840};

and then cout the contents using:

      for (Counter2 = 0; Counter2 < 6; Counter2++)
            cout << "b[" << Counter2 << "]: " << b[Counter2] << endl;

The resulting output is:

b[0]: 0
b[1]: 0
b[2]: 0
b[3]: 0
b[4]: 0
b[5]: 0
0
Comment
Question by:ltdanp22
  • 2
2 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
Comment Utility
you are dividing integers and resulting integer (0) is then cast to double.
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 500 total points
Comment Utility
#include <stdio.h>

int main ()
{
     double b[6] = {1.0/6, 5.0/24, 11.0/120, 19.0/720, 29.0/5040, 1.0/840};
     int i;
     for (i=0; i<6; i++)
    {
        printf("%f\n",b[i]);
    }
}
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now