This forum is in archive mode. You will not be able to post new content.

Author Topic: [C] Algorithm for comparing numbers  (Read 2788 times)

0 Members and 1 Guest are viewing this topic.

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
[C] Algorithm for comparing numbers
« on: May 14, 2013, 10:16:59 AM »
Hello there, I have trouble with comparing group of numbers with another group of numbers. For example first group have this values:
1
1
0
And second group have this numbers
1
2
1
I used this code to solve it until i realised it can't be used like that:
Code: [Select]
int compare(int array1[],int array2[]) {
int i,j,sum = 0;
for(i = 0;i <3 ;i++)
      for(j = 0; j< 3 ;j++) {
      if (array2[i] == array1[j]) {
      sum += 1;
      j +=2 ;
           }
      }
      return sum;
}
This code acctualy whenever find a number from second group he just skip second loop so he can check another number without making an error and find more same numbers. Put then i realised , when loop continue and find for example above one more number 1, that can lead to have 4 same numbers by this code insted of 2 only. What I am asking from you is to make algorithm so I can compare two group of 3 numbers but in that way that whenever some number is same with another one , those numbers are just cut from comparing anymore.
Vae Victis - suffering to the conquered

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: [C] Algorithm for comparing numbers
« Reply #1 on: May 14, 2013, 12:05:25 PM »
Well, you can make a new array that has boolean values that specify if to check a specific index or not. Like this:

Code: (C) [Select]
#include <stdio.h>
#include <string.h>

const int SIZE = 3;

int a[] = {1, 2, 3};
int b[] = {2, 3, 0};

int compare(int array1[], int array2[])
{
    int same = 0;
    // boolean values array specifying to check an index or not
    int tocheck[SIZE];
    // initially we want to check all
    memset(tocheck, 1, SIZE);

    int i, j;
    for (i = 0; i < SIZE; i++)
    {
        for (j = 0; j < SIZE; j++)
        {
            // if we need to check, check!
            if (tocheck[j])
            {
                if (array1[i] == array2[j])
                {
                    same++;
                    // we checked this one, no need to check it anymore
                    tocheck[j] = 0;
                }
            }
        }
    }
    return same;
}

int main(void)
{
    printf("Same: %d\n", compare(a, b));
    return 0;       
}


It does run in O(n^2), but it is correct though.
« Last Edit: May 14, 2013, 12:07:31 PM by s3my0n »
Easter egg in all *nix systems: E(){ E|E& };E

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: [C] Algorithm for comparing numbers
« Reply #2 on: May 14, 2013, 01:53:48 PM »
I am not sure what you want. What means "comparing two groups of numbers"?
When is a group equal to another? Do they have to have the same numbers? Is the order of these numbers important?
« Last Edit: May 14, 2013, 02:02:56 PM by Deque »

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Re: [C] Algorithm for comparing numbers
« Reply #3 on: May 14, 2013, 02:30:07 PM »
@s3my0n you offered quite good solution but my proffesor don't want us to use something we didn't learn in school. Thanks for your effort because I am learning how to solve this problem for myself.
@Deque : I have two groups of numbers. I want to check same numbers from both groups. But, i must be carefull with errors I can get while comparing.

1st. error : If first number  from group one is equal to first number from group two, I don't want to continue checking because that can make me have two same numbers even I have only one!
2nd error : If second number from first group is same as first number from same group then if I compare with numbers from another group , that can lead to error of course because I allready counted that number as equal, so I don't want to repeat counting again.
I hope you understand better now.

EDIT:

I created now this kind of code, which is kind a complicated but it can be used. Problem is when I type all 3 numbers and even they are same as other 3 , I only get message that two of them are same. Here is whole code of application:
http://pastebin.com/1pHQMB3p

Last function komparacija1 is acctualy compare. Change that in code. :)
« Last Edit: May 14, 2013, 03:33:14 PM by Code.Illusionist »
Vae Victis - suffering to the conquered

Offline WirelessDesert

  • Knight
  • **
  • Posts: 356
  • Cookies: 10
  • I think...
    • View Profile
Re: [C] Algorithm for comparing numbers
« Reply #4 on: May 14, 2013, 04:48:11 PM »
@s3my0n you offered quite good solution but my proffesor don't want us to use something we didn't learn in school. Thanks for your effort because I am learning how to solve this problem for myself.
What a douche prof. you are in school to learn and experiment. Not to freaking follow every one step he does.
Check out my arduino project: Moving car - School project!
"I'm like current, I always take the easiest route."

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Re: [C] Algorithm for comparing numbers
« Reply #5 on: May 14, 2013, 05:35:47 PM »
@WirelessDesert : I know, right! I was stuned when he said that we need to use only staff we learn from him so he know if we did the task or someone else did it for us. :D
Vae Victis - suffering to the conquered

Offline rasenove

  • Baron
  • ****
  • Posts: 950
  • Cookies: 53
  • ಠ_ಠ
    • View Profile
Re: [C] Algorithm for comparing numbers
« Reply #6 on: May 14, 2013, 05:37:49 PM »
What a douche prof. you are in school to learn and experiment. Not to freaking follow every one step he does.
thats not his profesors fault. This is considered everywhere. The teacher teaches his students in a meaningfull sequence, and breaking that sequence may result in "falling behind" (e.g how will a student understand  classes if he/she doesnt understand functions?)
My secrets have secrets...

Offline vezzy

  • Royal Highness
  • ****
  • Posts: 771
  • Cookies: 172
    • View Profile
Re: [C] Algorithm for comparing numbers
« Reply #7 on: May 14, 2013, 05:47:27 PM »
Bullshit. How does one person showing advanced knowledge disrupt the learning curve of the rest? This is simply school doing its work: making dependent workers incapable of critical thinking on an assembly line, fed with a systemized stream of facts (not correlated and not too much so that it leads to insight).

I have this fear that one day we will see specially designed compilers that will be deployed across schools and institutes of higher learning, as well as in hypothetical "student tablets", where the compiler will be strictly managed through a cloud, so that it will refuse to compile code using syntax and constructs that the student has not formally learned yet, e.g. structs in C.
Quote from: Dippy hippy
Just brushing though. I will be semi active mainly came to find a HQ botnet, like THOR or just any p2p botnet

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Re: [C] Algorithm for comparing numbers
« Reply #8 on: May 14, 2013, 07:18:46 PM »
I don't want to sound rude and interupt your discusion but I kind a need help. I posted whole application code above. You'll probably see some things that can be writen shortly but I don't know much. But all of you should focus on function compare or how i wrote wrong komparacija1 :)
Vae Victis - suffering to the conquered

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: [C] Algorithm for comparing numbers
« Reply #9 on: May 14, 2013, 07:50:14 PM »
@s3my0n you offered quite good solution but my proffesor don't want us to use something we didn't learn in school. Thanks for your effort because I am learning how to solve this problem for myself.
@Deque : I have two groups of numbers. I want to check same numbers from both groups. But, i must be carefull with errors I can get while comparing.

1st. error : If first number  from group one is equal to first number from group two, I don't want to continue checking because that can make me have two same numbers even I have only one!
2nd error : If second number from first group is same as first number from same group then if I compare with numbers from another group , that can lead to error of course because I allready counted that number as equal, so I don't want to repeat counting again.
I hope you understand better now.

EDIT:

I created now this kind of code, which is kind a complicated but it can be used. Problem is when I type all 3 numbers and even they are same as other 3 , I only get message that two of them are same. Here is whole code of application:
http://pastebin.com/1pHQMB3p

Last function komparacija1 is acctualy compare. Change that in code. :)

You didn't answer my questions. I really don't want to guess what output you expect. The amount of equal numbers at the same index?
The amount of equal numbers regardless the order they are in the "group"?
Or just a boolean that says whether they are the same or not?
« Last Edit: May 14, 2013, 07:51:55 PM by Deque »

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Re: [C] Algorithm for comparing numbers
« Reply #10 on: May 14, 2013, 08:08:46 PM »
 Whenever we start comparing two group of numbers, and logicly start with first from left group of numbers, all we do is compare to each number from right group. FIRST number that is equal to first number from left group is like deleted. He don't exists anymore for comparing. In that way each tiem i score a same number, right group number vanish, he is gone forever, so I don't compare him anymore. Do you understand now?
Vae Victis - suffering to the conquered

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: [C] Algorithm for comparing numbers
« Reply #11 on: May 14, 2013, 08:19:54 PM »
I suggest you look up 'matrix math' since that's what you're essentially trying to do here.  There are numerous articles online for handling matrices.  Perhaps this could be used in the future for generating an identity matrix between two 3D points on map or something.  Look up information on matrices and that should lead you to what you need.

+1 to s3my0n for some decent code right off the bat.
-Xires

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.