Basic C++ Programming Help

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

Previous topic - Next topic
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. :(

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

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.






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

did you want the answer or just help with the answer? reply and i'll post it either way...

wilnix
alt email address: wilnix@hackphreak.org

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.

July 06, 2003, 10:11:45 PM #6 Last Edit: July 06, 2003, 10:13:49 PM by Metgod
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.
"My Terminal is my Soul"

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.


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
alt email address: wilnix@hackphreak.org

July 07, 2003, 02:05:19 PM #9 Last Edit: July 07, 2003, 02:12:03 PM by Metgod
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.
"My Terminal is my Soul"

Thanx guys,i'l try out n keep u'l updated,really appreciate both ur guidance.

Metgod hooked you up. You should buy him the drink of his choice...or just send him money....

wilnix
alt email address: wilnix@hackphreak.org

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

Pay up buster, or we're telling Bush you carry weapons of mass destruction!!  :P

wilnix
alt email address: wilnix@hackphreak.org

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

SMF spam blocked by CleanTalk