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).

4
  • do you need the values? or just output them? Commented Oct 14, 2016 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. Commented Oct 14, 2016 at 21:04
  • This Might Be Of Help Commented Oct 14, 2016 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. Commented Oct 14, 2016 at 21:16

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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).

2 Comments

int votesarr[rows][cols];. -- This is not standard C++ syntax as it uses VLA's.
Didn't need line 4 but otherwise that did it. Thank you!
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)

Comments

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.