implement striping algorithm C++ - c++

Hi I am having trouble implementing a striping algorithm. I am also having a problem loading 30000 records in one vector, I tried this, but it is not working.
The program should declare variables to store ONE RECORD at a time. It should read a record and process it then read another record, and so on. Each process should ignore records that "belong" to another process. This can be done by keeping track of the record count and determining if the current record should be processed or ignored. For example, if there are 4 processes (numProcs = 4) process 0 should work on records 0, 4, 8, 12, ... (assuming we count from 0) and ignore all the other records in between.`
Residence res;
int numProcs = 4;
int linesNum = 0;
int recCount = 0;
int count = 0;
while(count <= numProcs)
{
while(!residenceFile.eof())
{
++recCount;
//distancess.push_back(populate_distancesVector(res,foodbankData));
if(recCount % processIS == linesNum)
{
residenceFile >> res.x >>res.y;
distancess.push_back(populate_distancesVector(res,foodbankData));
}
++linesNum;
}
++count;
}
Update the code
Residence res;
int numProcs = 1;
int recCount = 0;
while(!residenceFile.eof())
{
residenceFile >> res.x >>res.y;
//distancess.push_back(populate_distancesVector(res,foodbankData));
if ( recCount == processId)//process id
{
distancess.push_back(populate_distancesVector(res,foodbankData));
}
++recCount;
if(recCount == processId )
recCount = 0;
}
update sudo code
while(!residenceFile.eof())
{
residenceFile >> res.x >>res.y;
if ( recCount % numProcs == numLines)
{
distancess.push_back(populate_distancesVector(res,foodbankData));
}
else
++numLines
++recCount
}

You have tagged your post with MPI, but I don't see any place where you are checking a processor ID to see which record it should process.
Pseudocode for a solution to what I think you're asking:
While(there are more records){
If record count % numProcs == myID
ProcessRecord
else
Increment file stream pointer forward one record without processing
Increment Record Count
}
If you know the # of records you will be processing beforehand, then you can come up with a cleverer solution to move the filestream pointer ahead by numprocs records until that # is reached or surpassed.

A process that will act on records 0 and 4 must still read records 1, 2 and 3 (in order to get to 4).
Also, while(!residenceFile.eof()) isn't a good way to iterate through a file; it will read one round past the end. Do something like while(residenceFile >> res.x >>res.y) instead.
As for making a vector that contains 30,000 records, it sounds like a memory limitation. Are you sure you need that many in memory at once?
EDIT:
Look carefully at the updated code. If the process ID (numProcs) is zero, the process will act on the first record and no other; if it is something else, it will act on none of them.
EDIT:
Alas, I do not know Arabic. I will try to explain clearly in English.
You must learn a simple technique, before you attempt a difficult technique. If you guess at the algorithm, you will fail.
First, write a loop that iterates {0,1,2,3,...} and prints out all of the numbers:
int i=0;
while(i<10)
{
cout << i << endl;
++i;
}
Understand this before going farther. Then write a loop that iterates the same way, but prints out only {0,4,8,...}:
int i=0;
while(i<10)
{
if(i%4==0)
cout << i << endl;
++i;
}
Understand this before going farther. Then write a loop that prints out only {1,5,9,...}. Then write a loop that reads the file, and reports on every record. Then combine that with the logic from the previous exercise, and report on only one record out of every four.
Start with something small and simple. Add complexity in small measures. Develop new techniques in isolation. Test every step. Never add to code that doesn't work. This is the way to write code that works.

Related

How to compare many booleans to pass through specific outputs? C++ for UE4

I myself am not a programmer, but I have a programmer friend who is trying to help me with a certain task in Unreal Engine 4, and I was hoping to find some advice here to pass on to him.
What we are trying to make is a 'Node' in UE4 that can take in many boolean values (20+), and pass out specific values, or rather to pass through the event chain/line.
For example, I could have 6 booleans coming into the node, and I would want one of the outputs to pass through if boolean 2 and 4 were true, 1 was false, and the rest aren't looked at (essentially N/A). I made a quick image below to showcase what it would look like.
Example Of What Node would look like
My friend says he is not sure how such a node could be accomplished in C++, so I am hoping someone here can help give us a nudge in the right direction. Otherwise, I'll be stuck messing with branch nodes, and nodes, or nodes, and the like till my ears bleed and my project looks like a bowl of spaghetti.
Thanks for the suggestion, but I feel that that implementation is a bit too simple for what I am needing.
Hhhmmm, maybe I can explain my thoughts to the logic of it. Essentially, have the incoming boolean values into the node be made into an array of integers(or floats, dont know the difference between them really), with True = 1, and False = 2. The number of inputs to the array node can be determined by the number of inputs into the main Node.
Then, based on the number of inputs into the main Node, you can that same number of options per each event out pin on the main Node. Each option would have 3 check boxes. Checking the first box would output a 1 for True, second box would output 2 for False, and third box would output 3 for Dont Check.
These outputs could then be made into an array themselves. And then you would just have to compare the two arrays to see if they match, and anywhere there is a 3 value in the second array, that would output an 'is matching' regardless of what it is being compared to.
I just don't know actual coding, so I need a bit of help to try and explain this line of logic to my buddy, in terms of code.
Based on the information:
std::vector<int> input_vector; //the index will represent sequence and value will represent state
for(int i=0; i<node_size; i++)
{
input_vector.push_back(state); //you will push state=1 or state=2 here
}
std::vector<int> node_vector;
bool flag = true;
for(int case_no=0; case_no<cases;case_no++)
{
//checking node_vector with input_vector
for(int i=0; i<node_size; i++)
{
int choice;
std::cin >> choice; //either 1 or 2 or 3
node_vector.push_back(choice);
}
for(int i=0; i<node_size; i++)
{
if(node_vector[i]==3)
continue;
else if(node_vector[i]==input_vector[i])
continue;
else
{
flag = false;
break;
}
}
if(flag==true)
break;
else
continue;
}
if(flag)
std::cout << "Matched";
else
std::cout << "Not Matched";

Need to find duplicates from an array in c++, and then put them in another array

this is my first post here and I would be very happy if you could help me.
The task is - Create an array from 6 input numbers, then put the duplicated numbers in another array and then output the array with the repeated numbers.
Do you have any ideas? I'm still a newbie and need some help. Thanks in advance guys !!
EDIT:
I'm not sure if I'm on the right way, that's why I didn't post what I've done yet. But this is it:
#include <iostream>
using namespace std;
int main()
{
int a[6];
int b[6];
int i,z;
for (i=0; i<6; i++){
cin>>a[i];
}
for (z=0; z<6; z++){
if (a[0]==a[1]) b[z]=a[0];
if (a[0]==a[2]) b[z]=a[0];
if (a[0]==a[3]) b[z]=a[0];
if (a[0]==a[4]) b[z]=a[0];
if (a[0]==a[5]) b[z]=a[0];
if (a[1]==a[2]) b[z]=a[1];
if (a[1]==a[3]) b[z]=a[1];
if (a[1]==a[4]) b[z]=a[1];
if (a[1]==a[5]) b[z]=a[1];
if (a[2]==a[3]) b[z]=a[2];
if (a[2]==a[4]) b[z]=a[2];
if (a[2]==a[5]) b[z]=a[2];
if (a[3]==a[4]) b[z]=a[3];
if (a[3]==a[5]) b[z]=a[3];
if (a[4]==a[5]) b[z]=a[4];
else b[z]=0; cout << b[z];
}
return 0;
}
To give you a better understanding of how to solve this, i'll try to show you what is going on via an example.
Lets say you have just entered the 6 numbers you request via cin, and your a[] variable now looks like this in memory:
a[] = { 5, 2, 6, 2, 1, 6 };
The duplicates here are 2 and the 6. (pretty obvious for us humans) :-)
You start to compare the first 2 values in memory: a[0]==a[1], then the first with the third: a[0]==a[2] and so on. If one of these match, you know the value of a[0] has at least one duplicate in memory.
Whenever that happens, you would like to do something that that information. Store it somewhere (like your b[] array) or just output it directly with cout << a[0].
You are now finished with checking a[0] and can continue with a[1] in the same manor, except you do not have to compare with a[0] because you did that in the previous step. Looking at your code, it seems you already understand that you can skip that.
Lets say you really need to store the duplicates. It would help to keep track of how many duplicates you have found.
Pseudo code:
duplicates = 0;
if (a[0] has a duplicate) { b[duplicates] = a[0]; duplicates++; }
if (a[1] has a duplicate) { b[duplicates] = a[1]; duplicates++; }
// etc...
"has a duplicate" would be like the code you had earlier, like: a[0]==a[1] || a[0]==a[2] || a[0]==[3] and so on.
In your example you have just 6 values, so it is not much work to write all the compare statements yourself. If you needed to do this with many more numbers, it would take you ages to write it, and is prone to little mistakes like typo's. Using a for loop would work for few and many numbers:
Pseude code:
duplicates = 0;
for (z = 0 to 6) {
for (y = z+1 to 6) {
if (a[z]==a[y]) {
b[duplicates] = a[z];
duplicates++;
break; // We know it is a duplicate, continue with the next value
}
}
}
But even this is not perfect. If one number occurs more than 2 times in memory, this will store the same duplicate value multiple times.

C++ simple If statement making the rest of the program not execute

I have an assignment where I must read from a file and perform various calculations on it and write the answer to an output file. Everything was going great until I came to this step:
"Reread the file and compute the sum of the integers in the file as long as the sum does not exceed 1000. Use a flag controlled loop structure."
My code snippet is as follows:
dataFile2.close();
dataFile2.clear();
dataFile2.open("J:\\datafile2.txt");
sum = 0;
while(sum < 1000)
{
dataFile2 >> num;
sum = sum + num;
if(sum > 1000)
sum = sum - num;
}
answers << "The sum of the integers not exceeding 1000 is " << sum << endl;
cout << "The sum of the integers not exceeding 1000 is " << sum << endl;
return 0;
My variables have already been declared. when I take out the if statement the sum adds the last number and the sum then exceeds 1000. When the If statement is left in, the answers and cout statements are not executed and there are no compiler warnings or errors.
Any help on this would be greatly appreciated.
-ThePoloHobo
Since no one seems to want to give you a correct answer... (and
to be fair, it's hard to give a correct answer without actually
doing your work for you).
There are two issues in you code. The first is the requirement
that you use a flag. As I said in my comment, the idiomatic
solution would not use a flag, but there's no problem using one.
A flag is a boolean variable which will be tested in the
while, and will be set in a conditional in the loop, when you
find something that makes you want to leave the loop.
The second issue is that you are using num without checking
that the input has succeeded. You must check after the >>
operator. The idiomatic way of checking (and the only thing
that should ever be used by someone not experienced in the
language) is to treat the stream as if it were a boolean:
dataFile2 >> num;
if ( dataFile2 ) {
// Input succeeded...
} else {
// Input failed for some reason, maybe end of file
}
Since all operations on a stream return a reference to the
stream, it is usual to merge the test and the input:
if ( dataFile2 >> num ) {
// succeeded
} else {
// failed
}
(Personally, I find the idea of modifying state in the condition
of an if or a while horrible. But this idiom is so
ubiquitous that you should probably use it, for the simple
reason that that's what everyone expects.)
In pedagogical environments, it's probably acceptable to
consider any failure to be end of file, and just move the test
up into the while (except, of course, that you've been asked
to use a flag). In other contexts, you'll want to take into
account the fact that the failure could be due to a syntax error
in the input—someone inserted "abc" into the file where
you were expecting a number. There are a number of ways of
handling this, all of which are beyond the scope of what you are
trying to do, but be aware that after you've detected failure,
you can interogate the stream to know why. In particular, if
dataFile2.eof() is true, then the failure was (probably) due
to you having read all of the data, and everything is fine. (In
other words, failure to read a data is not necessarily an error.
It can be simply end of file.)
You don't seem to be using a flag variable, which could help in this case. Something like this should fix it:
sum = 0;
bool sumUnder1000 = true; //Or the C++ equivalent, I'm a bit rusty
while(sumUnder1000)
{
if(!dataFile2.good()){
sumUnder1000 = false; //We've reached end of file or an error has occurred
return;
}
dataFile2 >> num;
sum = sum + num;
else if(sum > 1000){
sum = sum - num;
sumUnder1000 = false;
}
}

C++ fastest cin for reading stdin?

I've profiled a computationally-heavy C++ program on Linux using cachegrind. Surprisingly, it turns out the bottleneck of my program is not in any sorting or computational method ... it's in reading the input.
Here is a screenshot of cachegrind, in case I'm mis-interpreting the profiler results (see scanf()):
I hope I'm right in saying that scanf() is taking 80.92% of my running time.
I read input using cin >> int_variable_here, like so:
std::ios_base::sync_with_stdio (false); // Supposedly makes I/O faster
cin >> NumberOfCities;
cin >> NumberOfOldRoads;
Roads = new Road[NumberOfOldRoads];
for (int i = 0; i < NumberOfOldRoads; i++)
{
int cityA, cityB, length;
cin >> cityA;
//scanf("%d", &cityA); // scanf() and cin are both too slow
cin >> cityB;
//scanf("%d", &cityB);
cin >> length;
//scanf("%d", &length);
Roads[i] = Road(cityA, cityB, length);
}
If you don't spot any issues with this input reading code, could you please recommend a faster way to read input? I'm thinking of trying getline() (working on it while I wait for responses). My guess is getline() may run faster because it has to do less conversion and it parses the stream a less total number of times (just my guess, though I'd have to parse the strings as integers eventually too).
What I mean by "too slow" is, this is part of a larger homework assignment that gets timed out after a certain period of time (I believe it is 90 seconds). I'm pretty confident the bottleneck is here because I purposely commented out a major portion of the rest of my program and it still timed out. I don't know what test cases the instructor runs through my program, but it must be a huge input file. So, what can I use to read input fastest?
The input format is strict: 3 integers separated by one space for each line, for many lines:
Sample Input:
7 8 3
7 9 2
8 9 1
0 1 28
0 5 10
1 2 16
I need to make a Road out of the integers in each line.
Also please not that input is redirected to my program to the standard input (myprogram < whatever_test_case.txt). I'm not reading a specific file. I just read from cin.
Update
Using Slava's method:
Input reading seems to be taking less time, but its still timing out (may not be due to input reading anymore). Slava's method is implemented in the Road() ctor (2 down from main). So now it takes 22% of the time as opposed to 80%. I'm thinking of optimizing SortRoadsComparator() as it's called 26,000,000 times.
Comparator Code:
// The complexity is sort of required for the whole min() max(), based off assignment instructions
bool SortRoadsComparator(const Road& a, const Road& b)
{
if (a.Length > b.Length)
return false;
else if (b.Length > a.Length)
return true;
else
{
// Non-determinism case
return ( (min(a.CityA, a.CityB) < min(b.CityA, b.CityB)) ||
(
(min(a.CityA, a.CityB) == min(b.CityA, b.CityB)) && max(a.CityA, a.CityB) < max(b.CityA, b.CityB)
)
);
}
}
Using enhzflep's method
Considering solved
I'm going to consider this problem solved because the bottleneck is no longer in reading input. Slava's method was the fastest for me.
Streams pretty well know to be very slow. It is not a big surprise though - they need to handle localizations, conditions etc. One possible solution would be to read file line by line by std::getline( std:::cin, str ) and convert string to numbers by something like this:
std::vector<int> getNumbers( const std::string &str )
{
std::vector<int> res;
int value = 0;
bool gotValue = false;
for( int i = 0; i < str.length(); ++i ) {
if( str[i] == ' ' ) {
if( gotValue ) res.push_back( value );
value = 0;
gotValue = false;
continue;
}
value = value * 10 + str[i] - '0';
gotValue = true;
}
if( gotValue ) res.push_back( value );
return res;
}
I did not test this code, wrote it to show the idea. I assume you do not expect to get anything in input but spaces and numbers, so it does not validate the input.
To optimize sorting first of all you should check if you really need to sort whole sequence. For comparator I would write methods getMin() getMax() and store that values in object (not to calculate them all the time):
bool SortRoadsComparator(const Road& a, const Road& b)
{
if( a.Length != b.Length ) return a.Length < b.length;
if( a.getMin() != b.getMin() ) return a.getMin() < b.getMin();
return a.getMax() < b.getMax();
}
if I understood how you current comparator works correctly.
As Slava says, streams (i.e cin) are absolute pigs in terms of performance (and executable file size)
Consider the following two approaches:
start = clock();
std::ios_base::sync_with_stdio (false); // Supposedly makes I/O faster
cin >> NumberOfCities >> NumberOfOldRoads;
Roads = new Road[NumberOfOldRoads];
for (int i = 0; i < NumberOfOldRoads; i++)
{
int cityA, cityB, length;
cin >> cityA >> cityB >> length;
Roads[i] = Road(cityA, cityB, length);
}
stop = clock();
printf ("time: %d\n", stop-start);
and
start = clock();
fp = stdin;
fscanf(fp, "%d\n%d\n", &NumberOfCities, &NumberOfOldRoads);
Roads = new Road[NumberOfOldRoads];
for (int i = 0; i < NumberOfOldRoads; i++)
{
int cityA, cityB, length;
fscanf(fp, "%d %d %d\n", &cityA, &cityB, &length);
Roads[i] = Road(cityA, cityB, length);
}
stop = clock();
printf ("time: %d\n", stop-start);
Running each way 5 times (with an input file of 1,000,000 entries + the first 2 'control' lines) gives us these results:
Using cin without the direction to not sync with stdio
8291, 8501, 8720, 8918, 7164 (avg 8318.3)
Using cin with the direction to not sync with stdio
4875, 4674, 4921, 4782, 5171 (avg 4884.6)
Using fscanf
1681, 1676, 1536, 1644, 1675 (avg 1642.4)
So, clearly, one can see that the sync_with_stdio(false) direction does help. One can also see that fscanf beats the pants off each approach with cin. In fact, the fscanf approach is nearly 3 times faster than the better of the cin approaches and a whopping 5 times faster than cin when not told to avoid syncing with stdio.
inline void S( int x ) {
x=0;
while((ch<'0' || ch>'9') && ch!='-' && ch!=EOF) ch=getchar_unlocked();
if (ch=='-')
sign=-1 , ch=getchar_unlocked();
else
sign=1;
do
x = (x<<3) + (x<<1) + ch-'0';
while((ch=getchar_unlocked())>='0' && ch<='9');
x*=sign;
}
you can use this function for any type of number input, just change the paramater type.
This will run pretty faster than std scanf.
If you want to save more time best thing will be to use fread() and fwrite() but in that case you have to manipulate the input by yourself.
To save time you should use fread() to read a large chunk of data from standard input stream in one call.That will decrease the number of I/O calls hence you will see a large difference in time.

C++ simple array element comparison

Okay I don't know if this is even a valid question but I'm posting here because I don't know where else to turn with this. I just started studying programming this half year at a university and we just had the final exam, which I failed. Basically, there were 4 questions, and while the second one looked easy it was actually tricky and I just can't figure out how it should have been done.
Basically the problem is: There is a bank, and when people log in to do business, you need to write a program that records the time they logged in (0-24h), the minutes (0-59), the type of transaction they choose (1 for logging in with a bank card, -1 for logging out with the same bank card, 2 for money input into the account, -2 for withdrawal) and finally either their bank acc number (if they pressed 1 or -1 previously), or the amount they are withdrawing or putting in (if they chose 2 or -2).
Basically here is how we had to do it:
int n; //size of the array or number of ppl who transacted that day
cin >> n;
int bank[n][4];
for (int i=0; i<n; ++i)
{
cin >> bank[n][0];
cin >> bank[n][1];
cin >> bank[n][2];
cin >> bank[n][3];
}
This fills up all the info and then,
basically a sample input looked like this for 4 customers during the day:
11 40 1 458965
12 20 2 6000
15 40 -1 458965
16 25 -2 18000
Here is the part I could not solve:
Our test asked us: How many people were logged in from 12 o clock to 13:00 oclock?
At first I did
int count=0;
for (int i=0; i<n; ++i)
{
if (bank[i][0]==12)
{
count=count+1;
}
}
cout << count;
The problem with this, is that it does not account for people who log in before 12 with a 1 in the third column, but log out at later than 1 oclock with a -1. Which means they were still logged in from 12 to 1pm.
so then I did
int count=0;
for (int i=0; i<n; ++i)
{
if (bank[i][0]==12)
{
count=count+1;
}
if (bank[i][2]==-1)
{
count=count+1;
}
}
cout << count;
but then I realized that this would count some logins twice, because if they logged in at 12 with a 1 for example, then logged out at 3 o clock with a -1 it would count that one person twice.
It also asked us what is the longest period that any person was logged in, assuming the bank kicks everyone off at 24:00. I honestly am not even sure how to even begin that one.
EDIT: SORRY i edited a bunch of stuff to make it clearer and correct code. I'm not too good at this yet forgive my mistakes
I didn’t know how the bank system works. So I made a minimal example for you.
I also didn't know if you used classes before, so I wrote it without.
I cleaned your code a bit:
//Use these enums
enum action { action_login = 1, action_logout = -1, action_input = 2, action_output = -2 };
enum information {information_time_h, information_time_m, information_action, information_bankNumber};
//Place this in the function you have
int peapelToInput = 0; //size of the array or number of ppl who transacted that day
cin >> peapelToInput;
for (int i=0; i<peapelToInput; ++i)
{
//Maby add error handeling? When some one inputs a 'a', it won't do what you want.
cin bank[i][information_time_h];
cin bank[i][information_time_m];
cin bank[i][information_action];
cin bank[i][information_bankNumber];
}
As you can see, I made the code cleaner by adding enums. This makes developing a lot easier.
The login code:
int count=0;
int bankSize = bank.size(); //I guess it's a vector?
for (int i=0; i < bankSize; ++i)
{
if (bank[i][information_time_h] == 12 && bank[i][information_action] == action_login)
count++;
}
cout << "logins at 12:00 - 12:59:" << count << endl;
You can do 2 checks in 1 if, I increment count when they were logedin from 12:00 - 12:59. Do you need exclude people that were loggedout?
The longest time code:
//A function to search when he is logedout
int findLogoutIndex(int start, int accountNumber, XXX bank)
{
int bankSize = bank.size();
for (int i=start; i < bankSize; ++i)
if( bank[i][information_action] == action_logout && bank[i][information_bankNumber] == accountNumber)
return i;
return -1; //Handle this error
}
//And how it workes
int logenst = 0;
int indexLongest = 0;
int bankSize = bank.size(); //I guess it's a vector?
for (int i=0; i < bankSize; ++i)
{
if( bank[i][information_action] != action_login )
continue;
int logoutIndex = findLogoutIndex(i,bank[i][information_bankNumber],bank);
//check if logoutIndex is not -1, or handle the error on an other way.
int loginTimeHour = bank[logoutIndex][information_time_h] - bank[i][information_time_h];
int loginTimeMinute = bank[logoutIndex][information_time_m] - bank[i][information_time_m];
int loginTime = (loginTimeHour * 100) + loginTimeMinute;
if( logenst < loginTime)
{
logenst = loginTime;
indexLongest = i;
}
}
cout << "longest is: H:" << bank[indexLongest][information_time_h] << " M: " << bank[indexLongest][information_time_m] << endl;
You don't need to keep the time format, this way makes comparing a lot easier. Just save the longest login time and the index number of it. That way you can easily access all the data you want.
I didn't take the time to write "good code". But you asked how it can be done, I guess this is good enough to understand it?
I didn't test the code and wrote it in Notepad. So I don't know if it will compile.
The first thing that you need to know is what the questions are actually asking. In the first case, How many people were logged in from 12 o clock to 1 oclock? can mean multiple things. It could mean how many people were logged in during the whole period or how many people were logged in at any given time between those two hours. The difference is whether someone that logs in at 12:15 and logs out at 12:30 is counted or not. The second question is calculating the longest period someone was logged in, and that can be done at the same time.
One possible approach would be managing a lookup table from user id to login times. You read the input linearly, whenever someone logs in you add an entry (acct, time) into the table. When they log out you lookup the account number and calculate the difference of times. If the difference is greater than the maximum you store the new maximum.
For the first question, at 12 you can create a set of the people that was logged in from that lookup table. Whenever someone logs out between that time and 1 you find whether the person was in the set and you remove it if so. When you find the first operation after 1, the set contains the account numbers of all the people that was logged in for the whole period from 12 to 1.
If the question was getting all people that was logged at any time in the period, instead of removing from the set those users that log out before 1, you need to include new users that log in inside the period. At the end of the period the set contains all users that were logged in at any time within the period.
You only need to perform a single pass over the input data which means that you don't even need to store all of the transactions in memory, only the map/set required above. The overall cost of the operation is O(n log n) on the number of operations. (Disclaimer: I haven't done the math, this is a hunch :))
Haven't tested this. Nevertheless, the process followed should be correct.
I'm sure this can still be optimized in terms of execution speed.
Also, I assume by time 12 you mean 12:00 and by time 1 you mean 13:00.
int main()
{
int answer = 0;
// For each transaction
for ( int i = 0; i < count; i++ ) {
// If logged in before 12:00
// bank[i][2] > 0 tells you user logged in.
if ( bank[i][0] < 12 && bank[i][2] > 0 ) {
// Loop through each following transaction.
for ( int j = i + 1; j < count; j++ ) {
// If logged out after 13:00
if ( bank[j][0] > 13 && bank[j][2] < 0 ) {
// Now to check if it was the same user who logged in earlier - how?:
// Only way to differentiate is by comparing the transaction amounts and types.
if ( (bank[i][3] == bank[j][3]) && (bank[i][2] == -1*bank[j][2]) ) { // log-in code = -1 * log-out code.
answer++; // Number of transactions that spanned from before 12:00 till after 13:00.
// Remember, a single person can't have multiple log-ins at the same time. ( assumption )
}
}
}
}
}
}