This is a Challenging Question, come and try to solve it together, we need to use std::map, is there anyone to help

Hello Chaps,

This is a Challenging Question, come and try to solve it
We may need std::map ? feel free to help

I have like this in a file1.txt  I have like this in a file2.txt before append file1 to file2


OK Green > start 1                OK Green > start 1
OK Green > start 2                No Green > start 2
No Green > start 3                OK Green > start 3

OK orange > start 1               No orange > start 1
No orange > start 2               OK orange > start 2
No orange > start 3               No orange > start 3

OK red > start 1                  OK red > start 1
OK red > start 2                  OK red > start 2
OK red > start 3                  No red > start 3[/b]


Now I send the file1.txt from pc1 to pc2 where pc2 keeps file2.txt
Now pc2 appended the the content of file1.txt to file2.txt (Append: add file1'content below the content of file2.txt), my question how to make this function check(in the code below) works like this

for ex:

if (2 statements in file2.txt[18 commands] after append) (Green Green) and (OK OK) return true

if both(Green Green) and (OK NO) return True

if both(Green Green) and (No Ok) return False

if both(Green Green) and (No No) return False


note : I called the color type in the code as priority of type string .

my second Question how to make the same function works in pc1 and pc2 as well with only 9 commands in pc1 and 18 commands in pc2 as it works now only in pc1?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const unsigned int FIELDS = 5;
const unsigned int RECORDS = 18;//change here
std::string p2[RECORDS][FIELDS];//2 dim array holds all the commands 
//in file1.txt (9 lines), where as it holds (18 records) in case of file2 loaded  

//readfile(1 ); // for ex: this read file1.txt and put data in p2[ ][ ] or readfile(2) reads file2.txt as explained below in another post to //douos and put in same array as well 


bool check1(int id, string priority) //priority (color type)recv the Red, Green or Orange

{
	std::string idString; // <-- We need to convert `id` to a std::string
	{
		std::stringstream ss;
		ss << id;
		ss >> idString;
	 }
        int counter=0;
	for(unsigned int i = 0; i < RECORDS; ++i)
	{
		if((p2[i][0] == "OK") && (P2[i][1] == priority) && (p2[i][4] == idString))
		{
			counter++;
			}
	}
	
	if((counter==1) || (counter==2) ){//thats work but it is strongly wrong ! as 1 means manythings here !
		return true;	}
	else {
		return false;
					}
	
	
}
Last edited on
It isn't really that challenging. All you need to do is track a little bit of information.

However, your problem statement is a little unclear. Is the following what you want?

Given file1 and file2:
  for each (color + start_id):
    is it "OK" in both file1 and file2?

If that is the case, your associative map should have the (color + start_id) as the key, and the number of "OK" instances as the value. Return true if the number of instances equals two (or more?).

Have you studied standard associative containers like std::map?


I am also at a loss when it comes to your data. How does your p1 array correlate to file1 and file2? Are you reading the files like a CSV, where:

    p1[record_number] == { ok, color, foo, start_id }

?

If that is the case, line 19 of your code should not return true; but instead it should increment a counter -- which tracks the number of times (priority + idString) associate with "OK". Only after either (after looping through all the records and the counter is >= 2) or (bumping the counter to a value of two) should you return true.

Hope this helps.
Thanks Duoas for the reply, I do not know anything about std::map? can be solved by map??
if u know or anyone know about feel free to help.

however lets me explain it more for u
I am reading the files as strings
for ex: OK Green > start 1
p2[0][0]="ok"
p2[0][1]="Green"
p2[0][2]=">"
p2[0][3]="start"
p2[0][4]="1"


to make that clear File2.txt has this
OK Green > start 1
Ok Green > start 2
No Green > start 3
No orange > start 1
Ok orange > start 2
No orange > start 3
OK red > start 1
OK red > start 2
No red > start 3
OK Green > start 1 //file1.txt content start from here
No Green > start 2
Ok Green > start 3
OK orange > start 1
No orange > start 2
No orange > start 3
OK red > start 1
OK red > start 2
OK red > start 3


my question how to make this function check(in the code above) works like this (in case of file2 loaded or readed to the array p2) and Q2 how to fit the same function works with file1.txt where are only 9 records only there .??

if the both the fields in [record1 and record9 ]and (OK OK)[record1 and record9 return true

if both the fields(Green Green) [in record2 and record 11 ] and (OK NO) [in record2 and record 11 ] return True


if both(Green Green) and (No Ok) return False

if both(Green Green) and (No No) return False
Last edited on
Well, that is what I thought. There are a number of ways to solve the problem. I still think the way I suggested in my second post -- using a "count" variable -- is probably your best bet. The answer is pretty explicit.

BTW, it isn't very kosher to go and edit your initial post to be incompatible with the responses given you, such as changing variable names and the like.

Good luck!

I do not think it can works with counter,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
bool check1(int id, string priority) //priority (color type)recv the Red, Green or Orange

{
	std::string idString; // <-- We need to convert `id` to a std::string
	{
		std::stringstream ss;
		ss << id;
		ss >> idString;
        int counter=0;
	 }
	for(unsigned int i = 0; i < RECORDS; ++i)
	{
		if((p2[i][0] == "OK") && (P2[i][1] == priority) && (p2[i][4] == idString))
		{

                     counter++;
			}
	    }
	
	if((counter==1) || (counter==2) ){//thats work but it is strongly wrong ! as 1 means manythings here !
		return true;	}
	else {
		return false;
					}
}
	

and how do you do this if i change this condition to : in the bold line here to return true instead of false

if the both the fields in [record1 and record9 ]and (OK OK)[record1 and record9 return true

if both the fields(Green Green) [in record2 and record 11 ] and (OK NO) [in record2 and record 11 ] return True

if both(Green Green) and (No Ok) return False

if both(Green Green) and (No No) return False
Last edited on
Are you seriously informing me what is professional and what will work?

I've already solved and tested your simple homework assignment. You need to do that now.

As a first step, try compiling your example and see if you can figure out why the compiler complains that it doesn't know what 'counter' is when it gets to line 16. Line 20 has a potential error also: as I indicated above it should check if (counter >= 2).


Also, if you keep changing the conditions by which your code will work then you'll have a hard time finishing the assignment.
sorry mate, no offense I am just telling u that counter does not solve the second problem when I change
the requirement, I am just assuming all the things if it works or not but I think I need the std::map now !
I just chaned it above, but still does not work properly Duoas
BTW how come the counter can be >2 ??



Note: All the above in BOLD has been changed after Duoas replies. I update them to make it easy to understand .

Thanks alot Duoas, I still want ur help big man, do not worry about my data stuff like Ok green 1 can be enough . or anythin u find it easer ;)
John

I just tried to make a new thread for more clarity
http://www.cplusplus.com/forum/general/50337/


Help me plz
Last edited on
Topic archived. No new replies allowed.