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