Assume that the file SCHOOLS.DAT is created
with the help of objects of class SCHOOLS, which is defined below:
class SCHOOLS
class SCHOOLS
{
int SCode; // School Code
char SName[20]; // School Name
int NOT; // Number of Teachers in the school
public:
void Display()
{cout<<SCode<<"#"<<SName<<"#"<<NOT<<endl;}
int RNOT(){return NOT;}
};
Question
Find the output of the following C++ code considering that the binary file
SCHOOLS.DAT exists on the hard disk with the following records of 10 schools of
the class SCHOOLS as declared in the previous .
SCode SName NOT
1001 Brains School 100
1003 Child Life School 115
1002 Care Share School 300
1006 Educate for Life School 50
1005 Guru Shishya Sadan 195
1004 Holy Education School 140
1010 Rahmat E Talim School 95
1008 Innovate Excel School 300
1011 Premier Education School 200
1012 Uplifted Minds School 100
void main()
{
fstream SFIN;
SFIN.open("SCHOOLS.DAT",ios::binary|ios::in);
SCHOOLS S;
SFIN.seekg(5*sizeof(S));
SFIN.read((char*)&S, sizeof(S));
S.Display();
cout<<"Record :"<<SFIN.tellg()/sizeof(S) + 1<<endl;
SFIN.close();
}
Output
1004#Holy Education School#140
Record :7
My Question How did Record is 7 and not 6.
My approach as the value of S will be 24 . Got it after adding the 2 byte of int Scode, 2 bytes of int NOT, 20 bytes of char SName[20].
so value of sizeof(S) will be 24 and value of SFIN.tellg will be 120 dividing them will give us 5 and then we have to add 1. So it will be 6 right?
You don't even need to think about the actual value of sizeof(S); just treat it as a constant K.
You initially seek to the position 5*K. Then you read K bytes from the file, leaving you at the position 6*K. That means the expression SFIN.tellg()/sizeof(S) + 1 is equivilent to 6*K/K + 1. K/K is equal to 1 for any K, so that further simplifies to 6*1 + 1, which equals 7.
Related
I need to find who has in order A-B-C. Please check the table for example;
id term grade subj num
10 2002 D 332 1
10 2002 A 333 2
11 2005 C 232 1
11 2005 A 232 2
11 2005 B 232 3
11 2005 C 232 4
15 2010 A 130 1
15 2010 B 130 2
15 2010 C 130 3
20 2000 B 500 1
20 2000 A 500 2
20 2000 C 500 3
What i need fromthis table is id : 11 AND 15
The output should be like
id term subj
11 2005 232
15 2010 130
So i need list the id's that had Grade of 'A' in it then was changed to 'B' then it was changed to 'C' .
Num could be in order. It dosen't have to start from 1, it could be 1 or 2 or 3, etc. But it should be in order A then B then C
I dont need to see the ID=20 bec for the num order grades' are not in order.
If all you are looking for is a simple 'A'-'B'-'C' sequence, then the LAG() function is sufficient. That is what I show in the example below. If you are looking for more sequences (e.g. 'A'-'B', 'B'-'C', 'A'-'B'-'C'-'D'), a slightly more complex solution is needed. If so, I'll edit the answer accordingly.
Below is a test program showing the implementation:
DATA d1;
INPUT
id :8.
term :8.
grade :$2.
subj :8.
num :8.
;
DATALINES;
10 2002 D 332 1
10 2002 A 333 2
11 2005 C 232 1
11 2005 A 232 2
11 2005 B 232 3
11 2005 C 232 4
15 2010 A 130 1
15 2010 B 130 2
15 2010 C 130 3
;
RUN;
DATA d2 (
KEEP = id term subj
);
SET d1;
grade_previous_1 = LAG1(grade);
grade_previous_2 = LAG2(grade);
IF (grade = 'C' AND grade_previous_1 = 'B' AND grade_previous_2 = 'A');
RUN;
Note that the LAG functions must be evaluated on their own lines and stored in variables, as shown above - don't fold them into the IF conditions or they won't always get executed. That is, don't say:
IF (grade = 'C' AND LAG1(grade) = 'B' AND LAG2(grade) = 'A');
That actually works in this example but in general it's better to get into the habit of calling LAG() outside of IF conditions and storing results in temporary variables.
My text file look like this:
1 52 Hayden Smith 18:16 15 M Berlin
2 54 Mark Puleo 18:25 15 M Berlin
3 97 Peter Warrington 18:26 29 M New haven
4 305 Matt Kasprzak 18:53 33 M Falls Church
5 272 Kevin Solar 19:17 16 M Sterling
6 394 Daniel Sullivan 19:35 26 M Sterling
7 42 Kevan DuPont 19:58 18 M Boylston
8 306 Chris Goethert 20:00 43 M Falls Church
9 262 James Sullivan 20:12 28 M Sterling
10 348 Bill Gaudere 20:17 54 M Hudson
11 13 Travis Wheeler 20:23 31 M Clinton
12 69 Eric Anderson 20:34 54 M Clinton
13 341 Alex Teixeira 20:46 0 M Clinton
14 112 James Long 20:50 38 M 0
15 279 Nate Richards 21:31 17 M Berlin
......................................................
There are eight columns, separated by 'tabs', except the first name and last name is separated by a space.
I must have eight different types of variables.
int a;
int b;
string c;
string d;
string e;
int f;
char g;
string h;
I need to read the file line by line and cout every line's a, b, c, d, e, f.
I also need those variables for later use.
So, I tried this:
std::ifstream infile("text.txt");
int a;
int b;
string c;
string d;
string e;
int f;
char g;
string h;
while(infile>>a>>b>>c>>d>>e>>f>>g>>h)
{
cout <<"C is: "<<c<<endl; // just to see if the loop is working.
}
I don't need arrays and vectors to store those variables, I have a linked structure. Right now, I just need a way to read the file and store those strings and integers into variables.
But it's not working, lol. Don't know why. I also thought about using getline, something like this:
while(getline(infield, s)):
But, isn't this essentially just giving me one big fat line, with all strings and integers smashed together.
Your approach works exactly as you want it to when testing it on my machine, except for the fact that it will stop at the third entry:
3 97 Peter Warrington 18:26 29 M New haven
This is because of the space in New haven, which will fail the while condition as it fails to be copied into the integer field, a on the next iteration. If you want to keep this structure, perhaps put underscores in place of spaces. Otherwise move to parsing it line by line, perhaps with the std::regex library.
For example, changing the location string to be seperated by underscores instead of spaces results in finding all 15 entries. To change the underscores back into spaces we can use std::replace, so the body of your while loop would look lile:
std::cout <<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<" "<<f<<" "<<g<<" ";
std::replace( h.begin(), h.end(), '_', ' ' );
std::cout<<h<<"\n";
(make sure to include algorithm)
We now have all our entires printed!
To directly answer your original question, I'm guessing the file doesn't exist.
std::ifstream infile("text.txt");
if(!infile.is_open()) {
std::cout<<"Couldn't find file";
return 0;
}
// ..
Hi everyone I have the following function :
#define GET_BIT(p, n) ((((unsigned char *)p)[n/8] >> (n%8)) & 0x01)
void extractBit(void const * data, int bitIndex)
{
string result = "";
result.append(std::to_string(GET_BIT(data, bitIndex)));
}
and following link shows my bits which are pointed by void const* data pointer :http://prntscr.com/3znmpz . void const* data points the part of my screenshot which are represented by red box. (I mean first member is "00000000" shown in green box). If this is required information, my file is written and shown using by little endian.
With this function I want to append bit at bitset position into my result string
For example, when extractBit(data,23) I want to add first 1 in the red box into my result string but it gives me 0. Altough I've looked at my code through a couple hours, I could not find my mistake. Is there anyone to help me ?
The first '1' is not the 23th, it's the 16th bit.
Well, it might look as a 23-th if you just count from left to right. But that's not how your function works.
Inside a byte, you enumerate bits from right to left (0th is rightmost bit, 7th is leftmost, which is a common convention and should be fine).
So, bit numbers as seen by your function are:
7 6 5 4 3 2 1 0 | 15 14 13 12 11 10 9 8 | 23 22 21 20 19 18 17 16 | 31 30 ...
I'm using C++. Using sort from STL is allowed.
I have an array of int, like this :
1 4 1 5 145 345 14 4
The numbers are stored in a char* (i read them from a binary file, 4 bytes per numbers)
I want to do two things with this array :
swap each number with the one after that
4 1 5 1 345 145 4 14
sort it by group of 2
4 1 4 14 5 1 345 145
I could code it step by step, but it wouldn't be efficient. What I'm looking for is speed. O(n log n) would be great.
Also, this array can be bigger than 500MB, so memory usage is an issue.
My first idea was to sort the array starting from the end (to swap the numbers 2 by 2) and treating it as a long* (to force the sorting to take 2 int each time). But I couldn't manage to code it, and I'm not even sure it would work.
I hope I was clear enough, thanks for your help : )
This is the most memory efficient layout I could come up with. Obviously the vector I'm using would be replaced by the data blob you're using, assuming endian-ness is all handled well enough. The premise of the code below is simple.
Generate 1024 random values in pairs, each pair consisting of the first number between 1 and 500, the second number between 1 and 50.
Iterate the entire list, flipping all even-index values with their following odd-index brethren.
Send the entire thing to std::qsort with an item width of two (2) int32_t values and a count of half the original vector.
The comparator function simply sorts on the immediate value first, and on the second value if the first is equal.
The sample below does this for 1024 items. I've tested it without output for 134217728 items (exactly 536870912 bytes) and the results were pretty impressive for a measly macbook air laptop, about 15 seconds, only about 10 of that on the actual sort. What is ideally most important is no additional memory allocation is required beyond the data vector. Yes, to the purists, I do use call-stack space, but only because q-sort does.
I hope you get something out of it.
Note: I only show the first part of the output, but I hope it shows what you're looking for.
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>
#include <cstdint>
// a most-wacked-out random generator. every other call will
// pull from a rand modulo either the first, or second template
// parameter, in alternation.
template<int N,int M>
struct randN
{
int i = 0;
int32_t operator ()()
{
i = (i+1)%2;
return (i ? rand() % N : rand() % M) + 1;
}
};
// compare to integer values by address.
int pair_cmp(const void* arg1, const void* arg2)
{
const int32_t *left = (const int32_t*)arg1;
const int32_t *right = (const int32_t *)arg2;
return (left[0] == right[0]) ? left[1] - right[1] : left[0] - right[0];
}
int main(int argc, char *argv[])
{
// a crapload of int values
static const size_t N = 1024;
// seed rand()
srand((unsigned)time(0));
// get a huge array of random crap from 1..50
vector<int32_t> data;
data.reserve(N);
std::generate_n(back_inserter(data), N, randN<500,50>());
// flip all the values
for (size_t i=0;i<data.size();i+=2)
{
int32_t tmp = data[i];
data[i] = data[i+1];
data[i+1] = tmp;
}
// now sort in pairs. using qsort only because it lends itself
// *very* nicely to performing block-based sorting.
std::qsort(&data[0], data.size()/2, sizeof(data[0])*2, pair_cmp);
cout << "After sorting..." << endl;
std::copy(data.begin(), data.end(), ostream_iterator<int32_t>(cout,"\n"));
cout << endl << endl;
return EXIT_SUCCESS;
}
Output
After sorting...
1
69
1
83
1
198
1
343
1
367
2
12
2
30
2
135
2
169
2
185
2
284
2
323
2
325
2
347
2
367
2
373
2
382
2
422
2
492
3
286
3
321
3
364
3
377
3
400
3
418
3
441
4
24
4
97
4
153
4
210
4
224
4
250
4
354
4
356
4
386
4
430
5
14
5
26
5
95
5
145
5
302
5
379
5
435
5
436
5
499
6
67
6
104
6
135
6
164
6
179
6
310
6
321
6
399
6
409
6
425
6
467
6
496
7
18
7
65
7
71
7
84
7
116
7
201
7
242
7
251
7
256
7
324
7
325
7
485
8
52
8
93
8
156
8
193
8
285
8
307
8
410
8
456
8
471
9
27
9
116
9
137
9
143
9
190
9
190
9
293
9
419
9
453
With some additional constraints on both your input and your platform, you can probably use an approach like the one you are thinking of. These constraints would include
Your input contains only positive numbers (i.e. can be treated as unsigned)
Your platform provides uint8_t and uint64_t in <cstdint>
You address a single platform with known endianness.
In that case you can divide your input into groups of 8 bytes, do some byte shuffling to arrange each groups as one uint64_t with the "first" number from the input in the lower-valued half and run std::sort on the resulting array. Depending on endianness you may need to do more byte shuffling to rearrange each sorted 8-byte group as a pair of uint32_t in the expected order.
If you can't code this on your own, I'd strongly advise you not to take this approach.
A better and more portable approach (you have some inherent non-portability by starting from a not clearly specified binary file format), would be:
std::vector<int> swap_and_sort_int_pairs(const unsigned char buffer[], size_t buflen) {
const size_t intsz = sizeof(int);
// We have to assume that the binary format in buffer is compatible with our int representation
// we also require an even number of integers
assert(buflen % (2*intsz) == 0);
// load pairwise
std::vector< std::pair<int,int> > pairs;
pairs.reserve(buflen/(2*intsz));
for (const unsigned char* bufp=buffer; bufp<buffer+buflen; bufp+= 2*intsz) {
// It would be better to have a more portable binary -> int conversion
int first_value = *reinterpret_cast<int*>(bufp);
int second_value = *reinterpret_cast<int*>(bufp + intsz);
// swap each pair here
pairs.emplace_back( second_value, firstvalue );
}
// less<pair<..>> does lexicographical ordering, which is what you are looking ofr
std::sort(pairs.begin(), pairs.end());
// convert back to linear vector
std::vector<int> result;
result.reserve(2*pairs.size());
for (auto& entry : pairs) {
result.push_back(entry.first);
result.push_back(entry.second);
}
return result;
}
Both the inital parse/swap pass (which you need anyway) and the final conversion are O(N), so the total complexity is still (O(N log(N)).
If you can continue to work with pairs, you can save the final conversion. The other way to save that conversion would be to use a hand-coded sort with two-int strides and two-int swap: much more work - and possibly still hard to get as efficient as a well-tuned library sort.
Do one thing at a time. First, give your data some *struct*ure. It seems that each 8 byte form a unit of the
form
struct unit {
int key;
int value;
}
If the endianness is right, you can do this in O(1) with a reinterpret_cast. If it isn't, you'll have to live with a O(n) conversion effort. Both vanish compared to the O(n log n) search effort.
When you have an array of these units, you can use std::sort like:
bool compare_units(const unit& a, const unit& b) {
return a.key < b.key;
}
std::sort(array, length, compare_units);
The key to this solution is that you do the "swapping" and byte-interpretation first and then do the sorting.
I'm trying to implement an ACO for 01MKP. My input values are from the OR-Library mknap1.txt. According to my algorithm, first I choose an item randomly. then i calculate the probabilities for all other items on the construction graph. the probability equation depends on pheremon level and the heuristic information.
p[i]=(tau[i]*n[i]/Σ(tau[i]*n[i]).
my pheremon matrix's cells have a constant value at initial (0.2). for this reason when i try to find the next item to go, pheremon matrix is becomes ineffective because of 0.2. so, my probability function determines the next item to go, checking the heuristic information. As you know, the heuristic information equation is
n[i]=profit[i]/Ravg.
(Ravg is the average of the resource constraints). for this reason my prob. functions chooses the item which has biggest profit value. (Lets say at first iteration my algorithm selected an item randomly which has 600 profit. then at the second iteration, chooses the 2400 profit value. But, in OR-Library, the item which has 2400 profit value causes the resource violation. Whatever I do, the second chosen is being the item which has 2400 profit.
is there anything wrong my algorithm? I hope ppl who know somethings about ACO, should help me. Thanks in advance.
Input values:
6 10 3800//no of items (n) / no of resources (m) // the optimal value
100 600 1200 2400 500 2000//profits of items (6)
8 12 13 64 22 41//resource constraints matrix (m*n)
8 12 13 75 22 41
3 6 4 18 6 4
5 10 8 32 6 12
5 13 8 42 6 20
5 13 8 48 6 20
0 0 0 0 8 0
3 0 4 0 8 0
3 2 4 0 8 4
3 2 4 8 8 4
80 96 20 36 44 48 10 18 22 24//resource capacities.
My algorithm:
for i=0 to max_ant
for j=0; to item_number
if j==0
{
item=rand()%n
ant[i].value+=profit[item]
ant[i].visited[j]=item
}
else
{
calculate probabilities for all the other items in P[0..n]
find the biggest P value.
item=biggest P's item.
check if it is in visited list
check if it causes resource constraint.
if everthing is ok:
ant[i].value+=profit[item]
ant[i].visited[j]=item
}//end of else
}//next j
update pheremon matrix => tau[a][b]=rou*tau[a][b]+deltaTou
}//next i