Basic C++ Programming Help

Started by ajastru2000, July 05, 2003, 01:49:19 AM

Previous topic - Next topic
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

July 09, 2003, 01:46:23 AM #16 Last Edit: July 09, 2003, 01:46:32 AM by wilnix
s/gov/com   ???


wilnix
alt email address: wilnix@hackphreak.org

hehe,

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

Met.
"My Terminal is my Soul"

Go back to using cobol...it makes you crazy!!

:P wilnix
alt email address: wilnix@hackphreak.org

#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.






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.

"My Terminal is my Soul"

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.

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.
"My Terminal is my Soul"

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;
      }
      }
      }

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;
          } 
       } 
   }
}
"My Terminal is my Soul"

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 :(

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.
"My Terminal is my Soul"

SMF spam blocked by CleanTalk