1. 2-diamension array

Hi,
I was trying to write a program that has a 2-d array, named a-array and b-arry, lets the user cin the number from 0 to 9 only, add the values of a-array and b-array and put the answers into c-arry. This works fine, but then I wanted to see how many times the user entered each number in the a-array and how many times each odd number was used in the b-array. The only way I can get that to work is by using if statements, but there has to be an easier and shorter way of doing that. The code is below, rather long and thats why I'm asking for some help to shorten the program.
Thanks#include <iostream.h>
#include <iomanip.h>

int main()

{
int i, j, *pa, *pb, *pc;
int a[3][3];
int b[3][3];
int c[3][3];int atotone = 0, atottwo = 0, atotthree = 0, atotfour = 0, atotfive = 0, atotsix = 0,

atotseven = 0, atoteight = 0, atotnine = 0, atotzero = 0;

int btotone = 0, btotthree = 0, btotfive = 0, btotseven = 0, btotnine = 0;

pa = &a[0][0];
pb = &b[0][0];
pc = &c[0][0];

cout << "Please enter numbers from 0 to 9 randomly: " << endl;

for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
cin >> *(a[i]+j);
}
cout << endl;
}

for ( i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (*(a[i]+j) == 0)
{
atotzero = atotzero + 1;
}
if (*(a[i]+j) == 1)
{
atotone = atotone + 1;
}
if (*(a[i]+j) == 2)
{
atottwo = atottwo + 1;
}
if (*(a[i]+j) == 3)
{
atotthree = atotthree + 1;
}
if (*(a[i]+j) == 4)
{
atotfour = atotfour + 1;
}
if (*(a[i]+j) == 5)
{
atotfive = atotfive + 1;
}
if (*(a[i]+j) == 6)
{
atotsix = atotsix + 1;
}
if (*(a[i]+j) == 7)
{
atotseven = atotseven + 1;
}
if (*(a[i]+j) == 8)
{
atoteight = atoteight + 1;
}
if (*(a[i]+j) == 9)
{
atotnine = atotnine + 1;
}
}
}

cout << "The numbers in a-array are listed below: " <<endl;
cout << endl;

for( i =0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
cout << setw(3) << *(a[i] + j);
}
cout << endl;
}cout << endl;

cout << "The number of times each digit used in the A-array are: " << endl;
cout << endl;
cout << " 0's = " << atotzero << " 1's = " << atotone << endl;
cout << " 2's = " << atottwo << " 3's = " << atotthree << endl;
cout << " 4's = " << atotfour << " 5's = " << atotfive << endl;
cout << " 6's = " << atotsix << " 7's = " << atotseven << endl;
cout << " 8's = " << atoteight << " 9's = " << atotnine << endl;
cout << endl;

cout << endl;

cout << endl;

cout << "Please enter numbers from 0 to 9 randomly: " << endl;

for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
cin >> *(b[i]+j);
}
cout << endl;

} for ( i = 0; i < 3; i++)

{
for (j = 0; j < 3; j++)
{

if (*(b[i]+j) == 1)
{
btotone = btotone + 1;
}

if (*(b[i]+j) == 3)
{
btotthree = btotthree + 1;
}

if (*(b[i]+j) == 5)
{
btotfive = btotfive + 1;
}

if (*(b[i]+j) == 7)
{
btotseven = btotseven + 1;
}

if (*(b[i]+j) == 9)
{
btotnine = btotnine + 1;
}
}
}

cout << "The numbers in b-array are listed below: " <<endl;
cout << endl;

for( i =0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
cout << setw(3) << *(b[i] + j);
}
cout << endl;
} cout << endl;

cout << "The number of times 1, 3, 5, 7, 9 are used in the B-array are: " << endl;
cout << endl;
cout << " 1's = " << btotone << " 3's= " << btotthree << endl;
cout << " 5's = " << btotfive << " 7's= " << btotseven << endl;
cout << " 9's = " << btotnine << endl;
cout << endl;
cout <<endl;

cout << endl;

cout << "The sum of the numbers in a-array and b-arry are listed below: " << endl;
cout << endl;

for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
*(c[i]+j)= *(a[i]+j) + *(b[i]+j);
cout << setw(3) << *(c[i] +j);
}
cout << endl;
}
cout << endl;

cout << endl;
return 0;
}

2. Another way of doing it

Why don't you just make another array of integers to count how many times each number was used. For example:

Code:
```int a_counter[10], x;
for(x=0; x<9; x++)
{
a_counter[x] = 0; //Set all values of the array to zero
}

a_counter[(*(a[ i ]+j)]++;  //Increment the number that corresponds with the numbers typed```
Does that help? Did I clear it up, or shall I go into more detail.

3. I forgot to mention that the user is to randomly enter the numbers 0 to 9. So how do I increment the number when I don't know what numbers the user will enter? I've been trying to get this to work, but so far all I get is negative numbers:

int k, D[10];

for(k = 0; k < 10; k++)
{
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (a[i][j] == k
{
D[k] = D[k] + 1;
}
}
}
}
for (k = 0; k < 10; k++)
{
cout << setw(3) << D[k];
}

4. Perhaps...

Perhaps you are referencing the variable wrong?

Are you sure *(a[ i ]+j) is referencing the correct memory location? I am curious, why are you not using
Code:
`cin>>a[x][y];`
It would do the same thing, so why do you not use that? It could be
Code:
`cin>>a[ i ][ j ];`
You would not have to go through so much trouble yourself. I suggest you try that instead. That is the only other thing I can think of, unless there is some kind of input error.

Popular pages Recent additions