Unputting decimals into an array then displaying them.

So i am putting some numbers into an array using a basic calculation and then checking to see if they were inputted correctly by displaying them, but they will only display with the first number, not the decimal places after it, here is my code.


#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <conio.h>

using namespace std;


int main()
{
int hold;
double result, decimal[100];


cout << "Here are the special numbers in 1-100\n";

for(int i = 1; i < 101; i++)
{
decimal[i] = 1/i;
}

for(int k = 1; k < 101; k++)
{

cout << setprecision (10) << decimal[k] << endl;

}

cin >> hold;
return(0);
}


You are doing integer division (1/i); both are integers, so it will only retun the integer result and then convert that result to a double when you assign the result. You need to cast one or both to a double to do floating point calculations. Also, you have a buffer over run. Your array goes from 0 to 99, but you are accessing indecies from 1 to 100.
It wont let me put anything but a Int in the array field, so how else would i get it working?

I have tried changing the loops to floats or double but i get "error C2108: subscript is not of integral type"
Bump
 
decimal[i] = 1./i;


Note the dot ('.').
You gotta be kidding me it was just adding that . lol thank you very much :)
Oh! and jimc is absolutely right in his second point also.
What you want is:

 
for(int i = 0; i < 100; i++)
Topic archived. No new replies allowed.