# array math

Printable View

• 03-09-2010
me77
array math
Hey,

Here is my code:

Code:

```#include <iostream> using namespace std; int main(void){         int x, i, total, perc;         char n[25], ar[20][20];         cin >> x;         cin.ignore();         for(i = 0; i < x; i++){                         cin.getline(n, 25);                 cin.ignore();                 cin.getline(ar[i], 20);                 cin.ignore();                                                 perc = ((ar[0][0] * 100) / ar[0][1]);                 cout << n << " " << perc << "%";                 cin.ignore();                         }         return 0; }```
'

Code:

```Input: 1 (number of inputs) Name (name of song) 48 52 (amount earned out of total) Find the percentage```
Well it doesn't work. I don't know whats going on with the arrays. ar[0][0] isn't 48 like i expected it to be.. same with ar[0][1]...

instead ar[0][0] somehow = 56 and ar[0][1] = 8... confused.

help please :)
• 03-09-2010
anon
Yes, the character that is output as '0' has an ascii value of 48 (and so on for other digits).

Probably you meant to use an array of numeric values for calculations.
• 03-09-2010
me77
How would I make them numeric values... an array can't really be an "int" right?

So I tried:

int val2, val2;
val1 = ar[0][0];
val2 =ar[0][1];

but when i display their contents after entering 48 52 i get 56 32...
• 03-09-2010
anon
Code:

`int array[10]; //ten integers`
• 03-09-2010
laserlight
Quote:

Originally Posted by me77
How would I make them numeric values... an array can't really be an "int" right?

An array cannot be a char either :)
But what you should do is subtract '0' (as in the character), i.e., you will end up subtracting 48 from 48 to get 0.