Relationship between variables - c++

I'm facing this problem in programming it says that if jack knows john and john know mark, then jack should know mark
the input is like this
1 - 3
3 - 4
1 - 4
1 knows 3 and 3 knows 4 so 1 knows 4, the answer is yes otherwise if the third line isn't there the answer is false because there's no connection between these three persons.
How can I implement this in c++ to make a connection between these three (on large inputs).

It is pretty simple if you know c++;
you just have to make a friend List of every person, then you can make a function
bool isFriend(person1,person2)
Which will return true if person1 and person2 knows each other, then you can make relationship of 3rd person if he knows any of them he'll know both of them as your requirement.
To make friend list you can use jagged array data structure and adjacency list with link list;

Related

How to analyse the survey to understand how answer to one question impacts rest

I have a survey data with 5 Question
1.) Whom yu are living with : girlfriend, wife
2.) If the person healthy : yes, no, partially
3.) Is the person working : Yes, No
4.) Are you happy with the person : Yes, No, Cant Say
5.) If the person owns a house : yes, no
I want to analyse one answer impacts the rest of the answers. For eg. how many of perople living with wife who has house and are happy. The requirement is to analyse these answers along each other.
The data collected is this format below. Please suggest what would be the best way to analyse the data in such fashion.
UserId Question Answer
1 Whom yu are living with Wife
1 If the person healthy Yes
1 Is the person working No
1 Are you happy with the person Yes
1 If the person owns a house No
2 Whom yu are living with Girlfriend
2 If the person healthy Partial
2 Is the person working Yes
2 Are you happy with the person Yes
2 If the person owns a house No
3 Whom yu are living with Wife
3 If the person healthy Yes
3 Is the person working yes
3 Are you happy with the person No
3 If the person owns a house Yes
I initially thought of to create a tree map with Questions and Answers and use drill through the open second page with tree map filtered on the user id. But later i realized that the drill through could use the filters which are not part of the parent chart.
Than i thought of using parameters like SSRS, but power bi has limitation to have only one value to the parameters.
What i am looking for is a descent way to analyse this data. Or a workaround on the two possible options i tried to remove roadblocks.
I am open to any different way to analyse the data then i am doing if it answers the questions.
Maybe a simple one, but as long as you are not planning on doing statistical analysis and just want to explore the data, maybe create 5 bar charts with the category of the X and a count on the Y and use the build in interactions by clicking on different categories to gain a basic understanding.

fstream deleting specific start to end rows

Im a noob programmer currently making a small family database using cpp but i have trouble deleting a family from the the list...
My list looks something like this
start-of-family 1
jim
joe
bob
sam
end-of-family 1
start-of-family 2
rob
max
end-of-family 2
start-of-family 3
sue
tom
kim
end-of-family 3
If i wanted to delete family 1, I would locate start-of-family 1 and end-of-family 1. Then run a loop but how do i locate it if the user only inputs an int to represent a family number. Also how do i make the succeeding family numbers deduct by 1 so that family 2 will be 1 and family 3 will be 2.
thanks a lot
If I were doing this problem I would start by making each family into a vector of names. Then, I would create a vector containing those family vectors.
The result would look something like:
{
<(jim), (joe), (bob), (sam) >
<(rob), (max) >
<(sue), (tom), (kim) >
}
Then, if the user wants to delete one of the families, you can use vector.remove(n) where n is the index of the family to be removed.
This sounds like a school or text book assignment. Have you gotten to vectors yet? Where are the names coming from? Are you hard coding them into the list? Or reading them from a .txt file? What kind of list structure are you storing them in right now?
i realized that clearing the db and updating it with what ive got is way easier than modifying the db and updating my program

C++ Compare Two Text Files' Contexts [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I want to read orders from order.txt which is;
Chocolate Chip Cookies 1 2
Orange Jelly Candy 2 7
Chocolate ice cream 3 11
Cake Mix Cookies 1 10
And decide if there is enough product from stock.txt which is;
Name Type ItemCount Price
Chocolate Chip Cookies 1 4 600
Orange Jelly Candy 2 5 150
Cake Mix Cookies 1 12 180
Peanut Butter Chocolate Chip Cookies 1 3 120
Chocolate ice cream 3 2 240
Gummi bears 2 15 300
Raspberry Ripple 3 12 250
Alignment is given like that. What is the best way to do it? Is there anything to read from a file which I can store in a way like product name/ number/ price?
Thank you.
I don't understand what the second and third column signify, I assume one of them is order size.
There are many ways to do this and my method might not be the best in all cases but at least it should work in yours:
Read the stock.txt file, you can look at this example but it doesn't really matter what method you use. The important thing is to get the information as a variable into the program. Hopefully your text files uses a tab to separate the fields (this information is lost in the question).
Put the information you get into a std::map, call it orders, use the name as key and the ItemCount as value.
Now read the information in order.txt, use the key to look up the value in your orders.
Check that ItemCount is at least a large as the order size.
In a real world application you would use a Database to store the data of your stock and would write a class order which gets its values from the databas. However i dont think you want to have that much effort so here's a esier way:
Your stock data gets saved in a simple excel table and the order is a textfile
Order.txt:
Beer 1|Paint 5|Oil 3|.....
Use the | as markers to create substrings of the individual products in the order
Bsp. ProductName quantity id|ProductName2 quantity id2|....
As for the excel table you have to to a little research yourself but editing files with a filereader isn't that hard though.
Hope i can help

Django ORM how to maintain non-automatic counter integrity on a large table?

Lets assume I have the following table 'book' (with tens of millions of rows):
id shelf position (on shelf)
1 2 3
2 1 1
3 1 2
4 2 2
5 2 1
... ... ...
40000000 1 2543355
And I need to move book 2 to another shelf or totally remove it.
Is there a good way to close the gap in positions that will open because of it?
I know there is this way to do it, but it will be really bad for performance, won't it?
Book.objects.filter(shelf=self.shelf, position__gt=self.position).update(position=F('position')-1)
I am thinking about changing maximum position on the shelf to moving/deleting position.
Is there a good way to do it with Django ORM?
What you want to do is very similar to a leaderboard implementation in rdbms. This is a really good read for doing that.
Essentially, what you want to do (in sql) is:
UPDATE book SET position = position - 1 WHERE shelf = X and position > Y
Where X is the shelf you moved the book from, and Y is its former position on that shelf. SQL syntax may vary.
In that article there are also examples of doing an insert of position into an existing shelf where positions must increase to make room for the newly inserted book. The article goes on to explain how to best do these in batches as moving one at a time, and then updating N records per move can be quite costly especially if you are repositioning the same books in the same shelf repeatedly.
For example, if you have 100 books on shelf 1, and you move two of them in positions [22, 56], then the optimal solution would move each book from 23-55 by one position, and each book from position 56-100 by two positions. The naive approach would move everything from 23-100 by one position. Then move everything from 56-100 by one position again.
A good way (performance-wise) to close the gap (book2 moved from shelf=1, position=1) will be this (move the last book on shelf to the opened position):
Book.objects.filter(shelf=1).order_by('-position')[0].update(position=1)
An index on (shelf, position) is also needed to maintain performance.
(Found this answer in a related question)

New to Prolog - challenge with lists

First I apologize for any mistakes I may make since English is not my first language.
So I decided to learn Prolog all by myself and I came across this "challenge."
I have this database about TV Shows. It has the following predicates:
person(Person_id,Name).
show(Show_id,Name).
participates(Person_id,Show_id,Activity).
What I have to find out is the relation between 2 people... I have to write an objective like this:
network(Person1,Person2), that given the names of 2 people (Person1,Person2) gives back the name of 2 other people, Person3 and Person4 - Person1 has worked with Person3 on any show, Person2 has worked with Person4, and Person3 and Person4 have both worked together.
I made a list of all the shows Person1 has worked in and then made a list of all the shows Person2 has worked in.
My problem is how to continue from here. I thought about making a list of all the people who worked in the shows Person1 has worked in, and another list with all the people who worked in the shows Person2 has worked in, and then try to find out if, of all the people Person1 has worked with, if someone has worked with someone on the list of people Person2 has worked with.
Can anyone give me some lights on how to work this out? Thanks!!
in prolog there is no such thing as "returning value"
therefore, you actually have to write a predicate like
network(Person1,Person2,Person3,Person4).
the first step is writing the predicate worked_with(Person1,Person2)
something like:
worked_with(Person1,Person2):-
participates(Person1,X,_),
participates(Person2,X,_),
Person1 \= Person2.
after that the network predicate would be something like
network(P1,P2,P3,P4):-
worked_with(P1,P3),
worked_with(P2,P4),
worked_with(P3,P4).
however, this predicate uses as input the ID's instead of the names;
you simply need to write a wrapper that will do the decoding.
i think that you could try to write it yourself as an exercise:b
by the way, if you are just starting to learn prolog, i dont really think that there is a reason to try something complex like that; try something simpler first to grasp the way prolog behaves