Solved

Arithmetic inside arrays

Posted on 2008-10-21
2
200 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
ID: 22765373
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
ID: 22765382
#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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Create a path if not exists 7 67
Acrinis True image 2 54
c++ using a LPCOLESTR * and adding a string varaible to the Pointer 2 46
rhino JavaScript import, load 25 66
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

948 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

22 Experts available now in Live!

Get 1:1 Help Now