2-diamension array

This is a discussion on 2-diamension array within the C++ Programming forums, part of the General Programming Boards category; Hi, I was trying to write a program that has a 2-d array, named a-array and b-arry, lets the user ...

  1. #1
    Unregistered
    Guest

    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. #2
    Registered User InFeStEd-ArCh0n's Avatar
    Join Date
    Apr 2002
    Posts
    63

    Post 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.
    -Mike
    {InFeStEd-ArCh0n}

  3. #3
    Unregistered
    Guest
    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. #4
    Registered User InFeStEd-ArCh0n's Avatar
    Join Date
    Apr 2002
    Posts
    63

    Question 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.
    Last edited by InFeStEd-ArCh0n; 05-02-2002 at 09:21 PM.
    -Mike
    {InFeStEd-ArCh0n}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21