Thread: Comparing a 2d Array (newbie)

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    10

    Smile Comparing a 2d Array (newbie)

    Hi all,
    I am new to this world

    I want to make a simple program that will compare the contents of a 2d array. Let's say array[a][b] have some integers in it. All I want to do is to make a simple program that will read the contents of array compare them and each time an integer in that array is repeated add one to that number.

    Maybe this example might help:
    Let's say i have c=array[a][b] and that I end up for a [2][2] array with
    c=0
    c=0
    c=1
    c=0
    Now i want to be able to check this and say that in that array i have 3 zero's and one 1.

    I have been trying to make somthing like this work but could not achieve it
    All I got through was to read the contents of the array but not to compare it.

    Any ideas of what i should do ?

    Thanks
    Pana8hnaikos The Pride Of Greece

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    21
    mm... couldn't get what ur meaning...
    u want to check, in a 2d array, if u have a number twice???and if so, add 1 to that number??? or to a separate counter?????

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    10
    I just want to count how many times a number of the array occurs. I do not want to add 1 to the number in the array. I just want to o........ the occurences of the contents of the array.

    Thanks
    Pana8hnaikos The Pride Of Greece

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    10

    source code

    This is what i have done so far but does not work i don't get values of counterarray[m]

    #include <iostream.h>
    main()
    {
    int i,m;
    int j;
    int array[4][2]={{1,2},{2,4},{3,6},{4,8}};
    const int n=8;
    int counterarray[n];



    for(i=0;i<4;i++)
    {
    for(j=0;j<2;j++)
    {
    m=array[i][j];
    counterarray[m]+=1;
    cout<<counterarray[m]<<"\n";
    }
    }


    }




    Any ideas ???
    thanks
    Pana8hnaikos The Pride Of Greece

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    #include <iostream.h> 
    int main(void) 
    { 
    int i,x,m = 0; 
    int j; 
    int array[4][2]={{1,2},{2,4},{3,6},{4,8}}; 
    //const int n=8; ??
    int counterarray[10];
    for(x = 0;x<10;x++){counterarray[x] = 0;}
    
    
    for(i=0;i<4;i++) 
    { 
    for(j=0;j<2;j++) 
    { 
    m=array[i][j]; 
    counterarray[m]++; 
    
    } 
    } 
    for(x = 0;x<10;x++){
    cout << "There were " << counterarray[x] << " instances of " << x << endl;
    }
    
    return 0;
    }
    That will count the instances of each number between 0 and 9...you can then play with it to get it to do exactly what you want

  6. #6
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704

    I think

    I THINK its becuase you never assign a value to counterarray.. You may only be incrimention how many spaces are in the array.

    But im not entirely sure.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    10
    Thanks a lot guys

    I think this might work afterall I'll get back to you shortly.

    thanks
    Pana8hnaikos The Pride Of Greece

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    10
    Yeap it did work. All that time i wasn't setting the counterarray to zero in the beggining of the program

    Thanks a lot you guys

    You know how it goes now that i solved that problem a new one came up
    I'll keep you posted. Thanks a lot once again
    Pana8hnaikos The Pride Of Greece

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    10
    hi again

    I want to know how can I make my program to run with flags under windows?
    example: prog [-h] [-run]
    -h help flag
    -run run prog flag

    I know how I can do that to linux systems but don't know how to do it for windows based systems.

    I'm using visual c++ to do my program under windows.

    Any ideas?
    Pana8hnaikos The Pride Of Greece

  10. #10
    Registered User
    Join Date
    Dec 2001
    Posts
    10
    Forget what i asked above i found the solution to that

    New question :
    How can find the summation of the contents of an array ?
    I have a axb array and its contents are numbers ,how can i add them all up and get just a single result?

    thanks
    Pana8hnaikos The Pride Of Greece

  11. #11
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    axb[NUM_ITEMS];
    int total = 0;
    int i = 0;
    while (i <= NUM_ITEMS)
    {
    total += axb[i];
    i++;
    }
    // NOT THE MOST EFFICIENT WAY, but works. Assuming the syntax is correct, sorry ive been up for 23 hours and counting.
    Last edited by dbgt goten; 12-08-2001 at 11:13 AM.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  12. #12
    Registered User
    Join Date
    Dec 2001
    Posts
    10
    How can I implement power and square root to my program? provided that i include math.h?

    I did it like this
    double pow(double x,double y);
    double sqrt(double z);

    but when compiling i got many errors some syntax about () and opother about double (i have int x,y,z) .

    Any ideas?
    Pana8hnaikos The Pride Of Greece

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Those functions are already defined in math.h, so all you have to do is call them.
    result = pow(x, y);
    result = sqrt(z);

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  2. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  3. cannot print out my 2d array correctly! please help
    By dalearyous in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2006, 02:07 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM