# Comparing a 2d Array (newbie)

• 12-07-2001
Cockney
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
• 12-07-2001
DeViLs_SouL
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?????
• 12-07-2001
Cockney
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
• 12-07-2001
Cockney
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
• 12-07-2001
Fordy
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
• 12-07-2001
Jeremy G
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.
• 12-07-2001
Cockney
Thanks a lot guys :)

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

thanks
• 12-08-2001
Cockney
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 :)
• 12-08-2001
Cockney
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?
• 12-08-2001
Cockney
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
• 12-08-2001
Jeremy G
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.
• 12-08-2001
Cockney
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?
• 12-08-2001
Prelude
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