HFX Forum

Programming => C/C++ => Topic started by: ajastru2000 on July 05, 2003, 01:49:19 AM

Title: Basic C++ Programming Help
Post by: ajastru2000 on July 05, 2003, 01:49:19 AM
Hi All,I'm a beginner in C++, try 2 stil grasp it- 1 week old. My Problem:
a1  b1  c    a2  b2  c
1    2   0     2   4    4
1    3   1     2   5    3  
1    4   3     2   6    2
1    5   6     2   7    0

Above re 2 array data, 1:a1,b1,c 2:a2,b2,c    In both arrays a1 n a2 have fixed values,can be ignored. Wat i wan is how do i get C++, by having d 1st array fixed n 2 compare d 'c' value with 2nd array's 'c' value. when both d value re d same(or +/- 2 d value), i need C++ to record both d 'b1' n 'b2' value for d corresponding 'c' values.
Problems:
1)firstly the fixed arrays 1st 'c' value need to scan thru d 2nd array when its value is d same(+/- 2 d 2nd 'c;c value)
2)After getting d first point, d 2nd point wil b d next value or +/- 2 'c' of d 2nd array.If it doesn't find a corresponding value, it needs 2 proceed to the 3rd value of d 1st array, n repeat d above process.
3)C value ranges from 0 to 32, after 32 it starts with 0 again til 32 n so on.

Sorry 2 all i think this a simple problem, wil appreciate if anyone could shed some light into it. :(
Title: Re:Basic C++ Programming Help
Post by: Metgod on July 05, 2003, 05:39:11 PM
I'm trying to decipher what you're after but I just can't... Can you try to explain again ? I'm so lost in your post I don't even know what you're asking.

(No offense when I say this) to me, your question is not coherent... It's the English I don't understand....

Met.
Title: Re:Basic C++ Programming Help
Post by: ajastru2000 on July 06, 2003, 01:43:59 AM
Hi sorry for the confusion,i'l try again to put my Q in the best possible form.

1)I'l explain my set of data characteristics: Ihave 2 sets of them as shown below(This just apart of it not the complete)

a1  b1  c1    a2  b2  c2
1    2   0       2  4    4
1    3   1       2  5    3  
1    4   3       2  6    2
1    5   6       2  7    0

a1 & a2 have fixed values alwayz. b1 & b2 varies according to their respective c1 & c2 value.

2)I need C++ , firstly to use set1 as an reference, than it needs to scan c2 for the same value as in c1, once it gets c1=c2, it needs to record both the value for b1 & b2.

3) this what it basicaly needs to do. But since the 'c' values vary from 0 to 32(0,1,2......30,31,32,0,1,2.....30,31,32,0...).There is a possibility of a number repeating twice or thrice n so on.

4)So to avoid the problem, the program needs to record the first c1=c2 first. The next c1 + 1 value wil be in the following c2 +1/+2/+3. (Here the + refers to the next row n so on)

5)If it can't detect c1 + 1 in the given range, it needs to move to c1 + 2, and scan at c2 +1/+2/+3. In short if it had detected a corresponding value for c1 + n at let's say at c2 + x, for the following c1 + (n+1), it has to scan from c2 + (x+1/+2/+3). If it doesnt it has to move to c1 + (n+2) and scan at the same c2 + (x+1/+2/+3), if it gets a similar value for c1 + (n+2) = c2 + (x+2), than the next scanning for c1 +(n+3) wil be at c2 + ((x+2) +1/+2/+3)) and so on.

I hope i havent complicated things again, if its stil confusing i'l try again,thanx for the responz, appreciate it.





Title: Re:Basic C++ Programming Help
Post by: Metgod on July 06, 2003, 02:34:50 PM
Okay, a little more clear.

Let me make sure I have it understood somewhat...

What you want to do is scan the first array for characters that exist in the second array. Upon finding this, you set the corresponding value in the non-constant element of the array ?

I'm not 100% sure I understand it but that's what I gathered (roughly). Can you elaborate on what I said or tell me what is wrong or right ? I apologize I'm not understanding this, but I'm willing to try to help as much as possible.

Met.
Title: Re:Basic C++ Programming Help
Post by: wilnix on July 06, 2003, 03:48:41 PM
did you want the answer or just help with the answer? reply and i'll post it either way...

wilnix
Title: Re:Basic C++ Programming Help
Post by: ajastru2000 on July 06, 2003, 06:45:10 PM
Hi thanx Met & wilnix,

Met, wat u understood is rite, i need the first array of column c1 to scan thru d second array for the same values in the column c2, and set the corresponding value of the both arrays of the column b1 & b2. Dat wil b al, thanx.

Wilnix, I want the answer, would appreciate if u could post it.

Thanx guyz for d help being rendered.
Title: Re:Basic C++ Programming Help
Post by: Metgod on July 06, 2003, 10:11:45 PM
Okay what this example (in C) does is:

defines MAX to be of size 5.
Then it makes two integers (i and j) and two arrays (array1 and array2).

Next it fills the array up.

It then checks every number in array2 and compares it to the CURRENT array1 elements. If there is a match it prints out the element of BOTH arrays and that it matched.

I think it should give you a start. I'd do more but still not sure 100%. You can from here fill in the necessary parts (I hope).

If nothing else, it gives you the skeleton code to scan more than one array at a time to find matches.

Code (cpp) Select


#define MAX 5
int main(void)
{
int i, j, array1[5], array2[5];

array1[0] = '1';
array1[1] = '0';
array1[2] = '0';
array1[3] = '7';

array2[0] = '1';
array2[1] = '5';
array2[2] = '0';
array2[3] = '2';


for (i=0;i<MAX-1;i++) {
  for(j=0;j<MAX-1;j++) {
      if (array1[i] == array2[j]) {
        printf("we found a match at array1 element %d and array2 element %d \n", i, j);

          }
       }
   }

return 0;

}


This example code would print out:

we found a match at array1 element 0 and array2 element 0
we found a match at array1 element 1 and array2 element 2
we found a match at array1 element 2 and array2 element 2

That's about all I can offer right now. Maybe Will can offer more since he seems to undersand it a bit more. Just play around with it.

Met.
Title: Re:Basic C++ Programming Help
Post by: ajastru2000 on July 07, 2003, 08:20:48 AM
Hi Met,thanx for ur suggested solution, its nearly fills my requirements. I've got one question here,

Below is the answer for ur program, how do i make the scanning done once when a match is found, wat i mean is, in the second line array1 element 1 = array2 element 2. Once if the elements are matched they musn't be scanned again, here element 2 is again matched element 2 in the 3rd line.
In short once a match is found between both elements in both the arrays, the scanning musn't backtrack to this matched elements.
Hope u could shed some light here too.

we found a match at array1 element 0 and array2 element 0
we found a match at array1 element 1 and array2 element 2
we found a match at array1 element 2 and array2 element 2

Anyway thanx man, i'm using ur method as my building blocks, tryin to modify here and there and learn.thanx.

Title: Re:Basic C++ Programming Help
Post by: wilnix on July 07, 2003, 02:02:19 PM
all you need to do is exit out of the loop when you've found what you need and keep track of where you left off...keywords: more variables. Then you could go back to that point in the array...

wilnix
Title: Re:Basic C++ Programming Help
Post by: Metgod on July 07, 2003, 02:05:19 PM
Okay

here's an idea:

You make a function that actually does the checking and then returns a value. If no match is found, then you return NULL.

Example:

Code (cpp) Select



#define MAX 5
int find_value(int array1[], int array2[]);

int main(void)
{
int x, array1[MAX], array2[MAX];
array1[0] = 1;
array1[1] = 0;
array1[2] = 0;
array1[3] = 7;

array2[0] = 5;
array2[1] = 5;
array2[2] = 0;
array2[3] = 2;


x = find_value(array1, array2);

printf("found match: %d", x);



return 0;
}

int find_value(int array1[], int array2[]) {

int i, j;

for (i=0;i<MAX-1;i++) {
  for(j=0;j<MAX-1;j++) {
      if (array1[i] == array2[j]) {
        printf("we found a match at array1 element %d and array2 element %d \n", i, j);
        return (array1[i]);

      }

  }


}

return (NULL);
}









Now what you do is you call the function and if it returns NULL then there is no match (so do error checking). If it finds a match it'll return the actual character found.

Just play around with this and let me know if you need anymore help. If you want to set the value to another part of another array then just grab the value returned and then set it to the value in the array.

Met.
Title: Re:Basic C++ Programming Help
Post by: ajastru2000 on July 07, 2003, 07:25:56 PM
Thanx guys,i'l try out n keep u'l updated,really appreciate both ur guidance.
Title: Re:Basic C++ Programming Help
Post by: wilnix on July 08, 2003, 01:02:33 PM
Metgod hooked you up. You should buy him the drink of his choice...or just send him money....

wilnix
Title: Re:Basic C++ Programming Help
Post by: Metgod on July 08, 2003, 04:55:41 PM
Hrm... I like that idea.. Money sounds really good.. I could sure use some for more computer stuff, so I could help you out more...

Thanks !

Met.
Title: Re:Basic C++ Programming Help
Post by: wilnix on July 08, 2003, 07:13:29 PM
Pay up buster, or we're telling Bush you carry weapons of mass destruction!!  :P

wilnix
Title: Re:Basic C++ Programming Help
Post by: Metgod on July 08, 2003, 09:02:10 PM
hahaha.. yeah, we mean business mister ! pay me now !!


mail from: someone@someone.com
rcpt to: president@whitehouse.gov
data

This guy has WMD ! Take him down now !

Now you have one final chance.. if you don't pay up I'll enter a period and hit enter ! I mean it !!

hehe, something like that....

Met.
Title: Re:Basic C++ Programming Help
Post by: ajastru2000 on July 08, 2003, 10:34:33 PM
sorry guyz....was down with SARS :-[ .......actually flu....stil hvnt got time 2 solve my problem.....btw rgding dis WMD....i hv given al details on it 2 d UN..... ;D
Title: Re:Basic C++ Programming Help
Post by: wilnix on July 09, 2003, 01:46:23 AM
s/gov/com   ???


wilnix
Title: Re:Basic C++ Programming Help
Post by: Metgod on July 09, 2003, 02:41:21 PM
hehe,

that's cool.. hope you solve the problem when you're well. If not, feel free to ask more.

Met.
Title: Re:Basic C++ Programming Help
Post by: wilnix on July 10, 2003, 08:04:38 PM
Go back to using cobol...it makes you crazy!!

:P wilnix
Title: Re:Basic C++ Programming Help
Post by: ajastru2000 on July 14, 2003, 07:55:58 AM
#define MAX 5
int main(void)
{
int i, j, array1[5], array2[5];

array1[0] = '1';
array1[1] = '0';
array1[2] = '0';
array1[3] = '7';

array2[0] = '1';
array2[1] = '5';
array2[2] = '0';
array2[3] = '2';


for (i=0;i<MAX-1;i++) {
 for(j=0;j<MAX-1;j++) {
     if (array1 == array2[j]) {
       printf("we found a match at array1 element %d and array2 element %d \n", i, j);

         }
       }
   }

return 0;

}


Guyz...srry just recovered frm d flu bout was bad. I tried to put in more loops and variables to get:

1)the program to search once only for element 1(array1) with the first 3 rows of element(array2).

2)If match is found for element1(array1) proceed to element 2(array1) and search for the consequent 3 rows after the matched element 'n'(array2).

3)If a match is not found for element 1(array1) proceed to element 2(array1) and search for the next consequent 3 rows
of element after element 1(array2).

and only show the matched value like the result before. Srry to trouble u guyz but i cant solve it still.Thanx again for both ur patience.





Title: Re:Basic C++ Programming Help
Post by: Metgod on July 14, 2003, 04:02:50 PM
Okay, first, I see the code did not get entered correctly. so let me try again:

Code (cpp) Select


#define MAX 5
int main(void)
{
int i, j, array1[5], array2[5];

array1[0] = '1';
array1[1] = '0';
array1[2] = '0';
array1[3] = '7';


array2[0] = '1';
array2[1] = '5';
array2[2] = '0';
array2[3] = '2';


for (i=0;i<MAX-1;i++) {
 for(j=0;j<MAX-1;j++) {
     if (array1[i] == array2[j]) {
       printf("we found a match at array1 element %d and array2 element %d \n", i, j);

         }
       }
   }

return 0;

}


Now, you should know that if you want the array bigger, you can change the MAX. In fact, there is another way to do it.. but just stick with the define for now. To be honest, there are other things that could be changed, but this is what I came up real quickly.

Also, check an earlier post where I made a function and called it from main so that it returns the value. What you could do as well, is set a flag or an integer and then continue to the next iteration..  Now, I'm not sure what you're after but hopefully what I posted will help some. Do let me know if you need more help.

Met.

Title: Re:Basic C++ Programming Help
Post by: ajastru2000 on July 17, 2003, 12:10:12 AM
Code:


#define MAX 5
int main(void)
{
int i, j, array1[5], array2[5];

array1[0] = '1';
array1[1] = '0';
array1[2] = '0';
array1[3] = '7';

array2[0] = '1';
array2[1] = '5';
array2[2] = '0';
array2[3] = '2';


for (i=0;i<MAX-1;i++) {
 for(j=0;j<MAX-1;j++) {
     if (array1 == array2[j]) {
       printf("we found a match at array1 element %d and array2 element %d \n", i, j);

         }
       }
   }

return 0;

}




This example code would print out:

we found a match at array1 element 0 and array2 element 0
we found a match at array1 element 1 and array2 element 2
we found a match at array1 element 2 and array2 element 2

Hi Met. above is your original codes with the output, how do I limit the match recorded to only once? Refering 2 your 2nd line output it found a match at array1 element 1 and array2 element2.  But it finds the same match again in the 3rd line output. Thanx Met.
Title: Re:Basic C++ Programming Help
Post by: Metgod on July 17, 2003, 01:38:15 PM
If you look back in this thread, I made it into a function. Upon a match it returns a value... thus stopping it.

Try that out and play with it a little and if you still have trouble, feel free to post here again.

And I'm glad I can be a help.

Met.
Title: Re:Basic C++ Programming Help
Post by: ajastru2000 on July 19, 2003, 02:10:10 AM
Hi Met, below is my original source code for my problem, Managed to get it print the match for each only once no repetition..thanx man.
Got one more problem, this are my arrays from a image buffer:
pixelv(i,j) and pixelv(x,y), fro every individual i,j or x,y there is a specific value(pixelv):

As per below how do i get it to scan line by line to find a match, as shown below both i & x are fixed, so only j & y changes, so starting with the first match j=254 & y=144 it must check if the pixelv is  same or not if not it should proceed to the next j+1 & y+1 and so on, if same it'l print both the values for j & i and the similar pixelv values.
Sorry Met, for the problem posing again, hope you could shed some light here. Thanx.

MbufGet2d(MilImage, 0L, 0L, IMAGE_WIDTH, IMAGE_HEIGHT, &pixelv )

for(j=254;j<401;j++)

{
for(y=144;y<291;y++)

{

x=200; i=270;

if(pixelv[j]==pixelv
  • [y])


    {
    fprintf(fptr,"\t%d %d %d %d\n",j ,y ,pixelv[j] ,pixelv
    • [y]);
      break;
      }
      }
      }
Title: Re:Basic C++ Programming Help
Post by: Metgod on July 20, 2003, 12:25:57 AM
Firstly, you should know that I've never really played with 2 dimensional arrays. I'm really not sure what you're after either.. To me, it sounds like you're after the exact same thing as before, except it's two dimensional arrays and one of the dimensions is a constant ? If that's the case, just modify my original function and play with it some. If that's not the case, please try to explain again...


Here is your code (I cleaned it up):

Code (cpp) Select

MbufGet2d(MilImage, 0L, 0L, IMAGE_WIDTH, IMAGE_HEIGHT, &pixelv ) {

x = 200;
i =  270;

  for(j=254;j<401;j++) {
      for(y=144;y<291;y++) {
         if(pixelv[i][j]==pixelv[y])
         {
         fprintf(fptr,"\t%d %d %d %d\n",j ,y ,pixelv[i][j] ,pixelv[y]);
break;
         }
      }
  }
}
Title: Re:Basic C++ Programming Help
Post by: ajastru2000 on July 20, 2003, 08:40:13 PM
Hi sorry Met, actually my codes were mixed up the other day.
Ok, I'l try explaining again using a different example.

1)Lets say we have a Topography map with its total length(j) and width(i) 100 x 200.
2)Each pairing of i & j has a height value of the map.
3)Now we'l take 2 horizontal straight lines, this means fixed j and a constant change of i: i.e 1st line-(50,50) - (50,100) and 2nd line-(75,60)-(75,110), can be seen j(length) for both the lines are fixed and i(width) changes.
4)The height value is contained for each j & i in the topography map buffer.
5)Now what i want the C++ to do is to find the same height value for each adjacent j's in both the horizontal lines. i.e: from the above example if (50,50)-1st line and (75,60)-2nd line has both the same height value it'l record, than it'l go to the next consecutive point of each line (50,51) and (75,61) and so on......

Hope this wil give u a clearer picture Met, thanx alot for ur patience with a beginner, and sorry for the trouble :(
Title: Re:Basic C++ Programming Help
Post by: Metgod on July 22, 2003, 12:34:58 PM
I didn't seem to follow this. I am very tired and not well in many ways.

Can you do me a favor and rename some of your variables ? Change the variable that specifies length to 'length', and width to 'width', etc. It really makes things easier to follow and it is actually good programming practice.

And then would you try to explain again, making it as clear as possible ?

Oh, and glad I can help this far.

Met.