1

I have a two-dimensional templated array of integers that I need to perform division on and convert to doubles (in order to create a percentage). It is being passed to my function in the function definition as

    int votesarr[4][2]

For each int in the array, I need to run a for loop (I assume) to divide the number by 10,000 and cout the resulting double value.

I'm unsure how to work this with the conversion as well as what I need to pass to the function that I haven't already (if anything).

njwoodard
  • 37
  • 1
  • 5
  • do you need the values? or just output them? – Charlie Oct 14 '16 at 21:02
  • @Charlie the resulting percentage value is only used in the function. The original ints are inputted to the array in a different function, and the array is passed into the function as I showed in the OP. So they only need to be outputted and not returned because they aren't used anywhere else. – njwoodard Oct 14 '16 at 21:04
  • [**This Might Be Of Help**](http://stackoverflow.com/questions/554063/how-do-i-print-a-double-value-with-full-precision-using-cout) – amanuel2 Oct 14 '16 at 21:08
  • @njwoodard *For each int in the array, I need to run a for loop* -- But you didn't even show an attempt to write a loop at all, regardless of the reasons you need to loop. – PaulMcKenzie Oct 14 '16 at 21:16

4 Answers4

2

This should do:

int arint[4][2] { {1,2},{ 2,3 },{0,1},{0,2} }; //example intarray arint[x][y];

for (auto &x : arint)for (auto &y : x)std::cout << y / 10000.0 << std::endl;

This will iterate each of arint[x] and for them each of arint[y], and output those with a line seperating. I just left the formatting as basic as possible. The .0 after 10.000 will output the result with decimals.

Charlie
  • 585
  • 9
  • 17
1

Based on the extra information you provided in the comments, here is a simple way to just iterate through the int matrix and output the values as floating-point values.

const std::size_t rows = 4;
const std::size_t cols = 2;
double divisor = 10000.0;
int votesarr[rows][cols]; // fill this somewhere...
for (std::size_t i = 0; i < rows; ++i) {
    for (std::size_t j = 0; j < cols; ++j)
        std::cout << static_cast<double>(votesarr[i][j])/divisor << ' ';
    std::cout << '\n';
}

That said, if you are passing votesarr around to different functions then I'd advise to use either:

std::array<std::array<int, 2>, 4> votesarr; // compile time dimensions known

or

std::vector<std::vector<int>> votesarr(4, std::vector<int>(2));

to make it simpler, instead of using C-style arrays which decay to pointers when passing to methods (preventing proper use of sizeof to determine dimensions, forcing you to pass the rows, cols to the functions).

sjrowlinson
  • 3,297
  • 1
  • 18
  • 35
0

so you need something like:

double percentage = (double)votesarr[i][j]/10000.0;

std::cout >> percentage >> std::endl;

the (double) tells the compiler that you want to cast this to a double. You can do this with (char), (int), (customType) etc...

However, division is a special case--because my 10000.0 has that ".0" at the end, the compiler treats it as a double, and (int) / (double) is treated like (double)/(double)

bwall
  • 984
  • 8
  • 22
0
#include <iostream>

using namespace std;

int main()
{
        int votesarr[4][2] = {{1,1},{1,1},{1,1},{1,1}};
        double result[4][2];
        double temp;
        for (int i = 0; i <= 3; i++) {
                for (int j = 0; j <= 1; j++) {
                        temp = votesarr[i][j];
                        temp = temp/10000;
                        result[i][j] = temp;
                }
        }
        // I just filled out the arrays by i+j
        //then you need to divide each one by 10,000
        //

        return 0;
}
Dendi Suhubdy
  • 2,877
  • 3
  • 19
  • 20