Solved

Arithmetic inside arrays

Posted on 2008-10-21
2
209 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xamarin c# deserialize Json containing nested object 2 93
PHP question(s) about order of output 9 64
Why isn't object file created? 6 59
Android development question 2 37
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

777 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