#include<iostream>
#include<time.h>
#include<list>
#include<stdlib.h>
#include<fstream>
using namespace std;
typedef struct diskBtNode
{
int parent; //-1 if NULL
//int size;
int leaf;
int arr[20];
};
int main()
{
fstream myfile;
srand(time(NULL));
myfile.open("btree.txt",ios::in | ios::out | ios::binary | ios::trunc);
long nodesize=256;
long currentpos=0;
if(myfile.fail())
{
std::cout<<"Error opening the file "<<std::endl;
}
currentpos=0;
for(int i=0;i<10;i++)
{
diskBtNode node;
node.parent=rand()%10;
node.leaf=rand()%1;
int n=rand()%19;
int j;
for(j=0;j<n;j++)
{
node.arr[j]=n;
}
node.arr[j]=-1;
cout<<node.parent<<" "<<node.leaf<<" ";
j=0;
while(node.arr[j]!=-1)
{
cout<<node.arr[j]<<" ";
j++;
}
cout<<node.arr[j]<<std::endl;
myfile.seekp(currentpos*nodesize,ios::beg);
myfile.write(reinterpret_cast<char *>(&node),nodesize);
currentpos++;
// p=p+1;
}
cout<<"******************* "<<std::endl;
currentpos--;
long p=0;
while(currentpos>=0)
{
std::cout<<currentpos<<" &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& "<<p<<" "<<std::endl;
diskBtNode node;
myfile.seekg(currentpos*nodesize,ios::beg);
myfile.read(reinterpret_cast<char *>(&node),nodesize);
currentpos--;
p--; //decrementing p
cout<<node.parent<<" "<<node.leaf<<" ";
int j=0;
while(node.arr[j]!=-1)
{
cout<<node.arr[j]<<" ";
j++;
}
cout<<node.arr[j]<<std::endl;
}
myfile.close();
}
This code simply reads and writes to a binary file. In the first part it writes to a file and in the second part it reads from the same file. While reading I was trying to read any random blocks from a file for a finite number of time. But when I am using p variable as a counter, it doesn't work. It's value is decremented in the first iteration directly to -1. I used debugger to track where it changes. Apparently it changes after the read statement is executed. Can somebody please help me with this? The output of the above program is this
8 0 8 8 8 8 8 8 8 8 -1
5 0 8 8 8 8 8 8 8 8 -1
3 0 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 -1
5 0 1 -1
4 0 -1
9 0 13 13 13 13 13 13 13 13 13 13 13 13 13 -1
4 0 11 11 11 11 11 11 11 11 11 11 11 -1
6 0 6 6 6 6 6 6 -1
6 0 8 8 8 8 8 8 8 8 -1
2 0 2 2 -1
*******************
9 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 0
2 0 2 2 -1
8 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1
6 0 8 8 8 8 8 8 8 8 -1
7 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1
6 0 6 6 6 6 6 6 -1
6 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1
4 0 11 11 11 11 11 11 11 11 11 11 11 -1
5 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1
9 0 13 13 13 13 13 13 13 13 13 13 13 13 13 -1
4 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1
4 0 -1
3 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1
5 0 1 -1
2 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1
3 0 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 -1
1 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1
5 0 8 8 8 8 8 8 8 8 -1
0 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1
8 0 8 8 8 8 8 8 8 8 -1
The problem comes from this line :
myfile.read(reinterpret_cast<char *>(&node),nodesize);
nodesizeequals 256, while you structure's size if 88byte ( 22 * 4 bytes int ).
The read is writing memory over the structure, which happens to be the other stack variables.
Use sizeof( node ) when you both write and read the struct to the file.
Not clear what you are trying to achieve, but in your code you have specified.
long p=0;
while(currentpos>=0)
{
....
p--; // this will make p = -1
}
so the p will print as -1 all through the while statement. Are you forgetting to initialize the p variable?
Related
This is the complete code that I wrote:
( [numsin the function] and [arrin main] is the array that needs to be sorted, sizeis the amount of numbers in the array, minis the smallest number in the unsorted part)
#include <iostream>
#include <vector>
using namespace std;
void sort(vector <int> &nums, int size){
int min = 0;
for(int i=0;i<size;i++){
min = i;
for(int j=i+1;j<size;j++){
if(nums[j]<nums[min]){
min = j; //comparing
}
}
nums[i] = nums[min] + nums[i]; //swaping
nums[min] = nums[i] - nums[min];
nums[i] = nums[i] - nums[min];
}
}
int main(){
cout<<"\nEnter Numbers:\n";
vector <int> arr;
int num;
while(cin>>num){
arr.push_back(num);
}
sort(arr,arr.size());
cout<<"\nSorted:\n";
for(int i=0;i<arr.size();i++){
cout<<arr[i]<<" ";
}
}
I'm writing a code that simply sorts the given array. But after trying to debug and find solutions online, I can't figure out which part is wrong. These are some examples of my results:
Enter Numbers:
9 8 7 6 1 2 3 4 5 ^Z
Sorted:
1 2 3 4 5 6 0 0 0
Enter Numbers:
6 4 8 7 2 3 5 ^Z
Sorted:
2 3 4 5 0 7 0
Enter Numbers:
9 8 7 6 5 4 3 2 1 ^Z
Sorted:
1 2 3 4 0 0 0 0 0
This is the result when I added a for loop under the swapping part to show what every round has done to the array:
Enter Numbers:
9 8 7 6 1 2 3 4 5 ^Z
1 8 7 6 9 2 3 4 5
1 2 7 6 9 8 3 4 5
1 2 3 6 9 8 7 4 5
1 2 3 4 9 8 7 6 5
1 2 3 4 5 8 7 6 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 0 8 9
1 2 3 4 5 6 0 0 9
1 2 3 4 5 6 0 0 0
Sorted:
1 2 3 4 5 6 0 0 0
Enter Numbers:
6 4 8 7 2 3 5 ^Z
2 4 8 7 6 3 5
2 3 8 7 6 4 5
2 3 4 7 6 8 5
2 3 4 5 6 8 7
2 3 4 5 0 8 7
2 3 4 5 0 7 8
2 3 4 5 0 7 0
Sorted:
2 3 4 5 0 7 0
9 8 7 6 5 4 3 2 1 ^Z
1 8 7 6 5 4 3 2 9
1 2 7 6 5 4 3 8 9
1 2 3 6 5 4 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 0 6 7 8 9
1 2 3 4 0 0 7 8 9
1 2 3 4 0 0 0 8 9
1 2 3 4 0 0 0 0 9
1 2 3 4 0 0 0 0 0
Sorted:
1 2 3 4 0 0 0 0 0
Please help, thanks.
Your swapping logic doesn't handle the case where the smallest remaining element is the first unsorted element (i.e., i == min). Consider what each line does in this case:
nums[i] = nums[min] + nums[i]; //nums[i] will be doubled
nums[min] = nums[i] - nums[min]; // nums[min] is subtracted from itself, making it 0
nums[i] = nums[i] - nums[min]; // nums[i] is subtracted from itself gain, but 0-0 is still 0
The goal of avoiding a temporary isn't bad by itself, but you do have this nasty edge case. You'd either have to detect the edge case or just bite the bullet and deal with a temporary. You could also call std::swap, but that likely uses a temporary as well.
The advantage to using a temporary or std::swap is that this code would be easier to make generic for other types (especially via std::swap). In addition, std::swap can be specialized for types to avoid temporaries if possible, and if this is actually a bottleneck.
I have a vector with 2 elements a_destinations that represent the source and destination with two integers. 2 4 or 5 1...
I have a matrix test as follows:
0 15 15 15 13 13 15 15 13 13 15 15 15 13 15 15
2 1 2 2 2 2 2 2 2 2 10 2 2 2 10 2
6 1 2 3 3 3 6 6 6 6 6 6 6 6 6 6
7 2 2 3 4 4 2 7 7 7 7 7 7 7 7 7
8 3 3 3 4 5 3 3 8 8 3 3 3 8 3 3
9 4 4 4 4 5 4 4 4 9 4 4 4 9 4 4
7 2 2 2 2 2 6 7 7 7 11 11 7 7 11 7
12 6 6 3 3 3 6 7 8 8 12 12 12 12 12 12
9 7 7 7 4 4 7 7 8 9 7 7 7 9 7 7
13 8 8 8 8 5 8 8 8 9 13 13 13 13 13 13
14 1 11 11 11 11 11 11 11 11 10 11 11 11 14 14
12 6 6 12 12 12 6 12 12 12 10 11 12 12 10 12
15 7 7 7 7 7 7 7 7 13 11 11 12 13 11 15
0 12 12 12 9 9 12 12 9 9 12 12 12 13 12 12
15 10 10 10 10 10 10 10 10 10 10 10 10 10 14 15
0 12 12 12 12 12 12 12 12 12 14 12 12 12 14 15
where test[i][j] represents the path from i to j
each time test[i][j] != j we do the loop again
Example:
path from 2 to 4 >> test[2][4] = 3, test[3][4] = 4: we output: 2, 3, 4
path from 1 to 7 >> test[1][7] = 2, test[2][7] = 6 , test[6][7] = 7: we output 1, 2 ,6, 7
I tried as follows:
std::vector<vector<int>> test;
test = Graphe->P; // MATRIX IS FILLED like on top
vector< int > a_destinations; // Vector with the destinations: 2 4 or 5 10 or 1 4 ...
for ( unsigned i = 0; i < test.size(); i++){
for (unsigned j = 0; j< test.size(); j++){
for (unsigned k = 0; k < a_destinations->size() - 1 ; k ++){
if ( a_destinations->at(k) == i && a_destinations->at(k+1) == j ){
if (test[i][j] == a_destinations->at(k+1)){
cout << a_destinations->at(k) << ", " <<test[i][j];
} else {
cout << a_destinations->at(k) << ", " << test[a_destinations->at(k)][j];
}
cout << ", " << test[i][j];// << ", " << a_destinations->at(k+1);
}
}
}
}
But i end up with 2 destinations always.
I was solving the puzzles in 2017 Advent of Code. It was necessary to fill in circular buffer using certain algorithm. For the buffer implementation I first used vector, and then I tried with deque. I am getting different results when printing values of the vector and the queue. Here's the code:
#include <iostream>
#include <vector>
void PrintBuffer(std::vector<int> a_CircularBuffer)
{
for (std::vector<int>::iterator it = a_CircularBuffer.begin(); it != a_CircularBuffer.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
}
int main()
{
std::vector<int> circularBuffer;
circularBuffer.reserve(20);
circularBuffer.push_back(0);
circularBuffer.push_back(1);
std::vector<int>::iterator currentPosition = circularBuffer.begin() + 1;
for (int i = 2; i < 20; ++i) {
int steps = 378;
if (steps >= i) {
steps = (steps % i);
}
if ((circularBuffer.end() - currentPosition) <= steps) {
currentPosition = circularBuffer.begin() + (((currentPosition - circularBuffer.begin()) + steps) % i);
circularBuffer.insert(currentPosition, i);
}
else {
currentPosition = currentPosition + steps;
circularBuffer.insert(currentPosition, i);
}
PrintBuffer(circularBuffer);
}
return 0;
}
This is the result when using vector:
0 2 1
0 3 2 1
0 3 2 4 1
0 5 3 2 4 1
0 6 5 3 2 4 1
0 7 6 5 3 2 4 1
0 7 6 8 5 3 2 4 1
0 7 6 9 8 5 3 2 4 1
0 10 7 6 9 8 5 3 2 4 1
0 10 7 6 9 11 8 5 3 2 4 1
0 10 7 6 9 11 8 5 3 2 4 12 1
0 10 7 6 9 11 8 5 3 2 4 12 13 1
0 10 7 6 9 11 8 5 3 2 4 12 14 13 1
15 0 10 7 6 9 11 8 5 3 2 4 12 14 13 1
...
and this is when using deque (just change "vector" to "deque" and comment out circularBuffer.reserve(20) line):
0 2 1
0 3 2 1
0 3 2 4 1
0 5 3 2 4 1
0 5 6 3 2 4 1
0 5 6 7 3 2 4 1
0 5 6 7 3 8 2 4 1
0 5 6 7 3 9 8 2 4 1
0 5 6 10 7 3 9 8 2 4 1
0 5 6 10 7 3 9 8 11 2 4 1
0 5 12 6 10 7 3 9 8 11 2 4 1
0 5 12 6 13 10 7 3 9 8 11 2 4 1
0 5 12 6 13 14 10 7 3 9 8 11 2 4 1
0 5 12 6 13 14 10 7 3 15 9 8 11 2 4 1
...
Why there are different results for vector and deque?
You get undefined behaviour when you insert an element causing reallocation, and then use the old iterator again.
Anything can happen.
Use index to store current position and it'll work the same way.
I am newbie on codechef and i was trying to solve the following question however my code runs fine on my machine, i also tested it with some cases.
Question is as follows :-
In Byteland it is always the military officer's main worry to order his soldiers on parade correctly. Luckily, ordering soldiers is not really such a problem. If a platoon consists of n men, all of them have different rank (from 1 - lowest to n - highest) and on parade they should be lined up from left to right in increasing order of rank.
Sounds simple, doesn't it? Well, Sgt Johnny thought the same, until one day he was faced with a new command. He soon discovered that his elite commandos preferred to do the fighting, and leave the thinking to their superiors. So, when at the first rollcall the soldiers lined up in fairly random order it was not because of their lack of discipline, but simply because they couldn't work out how to form a line in correct order of ranks. Sgt Johnny was not at all amused, particularly as he soon found that none of the soldiers even remembered his own rank. Over the years of service every soldier had only learned which of the other soldiers were his superiors. But Sgt Johnny was not a man to give up easily when faced with a true military challenge. After a moment's thought a solution of brilliant simplicity struck him and he issued the following order: "men, starting from the left, one by one, do: (step forward; go left until there is no superior to the left of you; get back in line).". This did indeed get the men sorted in a few minutes. The problem was solved... for the time being.
The next day, the soldiers came in exactly the same order as the day before, and had to be rearranged using the same method. History repeated. After some weeks, Sgt Johnny managed to force each of his soldiers to remember how many men he passed when going left, and thus make the sorting process even faster.
If you know how many positions each man has to walk to the left, can you try to find out what order of ranks the soldiers initially line up in?
Input
The first line of input contains an integer t<=50, the number of test cases. It is followed by t test cases, each consisting of 2 lines. The first line contains a single integer n (1<=n<=200000). The second line contains n space separated integers wi, denoting how far the i-th soldier in line must walk to the left when applying Sgt Johnny's algorithm.
Output
For each test case, output a single line consisting of n space separated integers - the ranks of the soldiers, given from left to right in their initial arrangement.
Example
Input:
2
3
0 1 0
5
0 1 2 0 1
Output:
2 1 3
3 2 1 5 4
Warning: large Input/Output data, be careful with certain languages
#include <iostream>
#include <string.h>
using namespace std;
int main ()
{
int t,n;
cin >> t;
while(t>0){
cin >> n;
int array[n+1];
int stepsmoved,i;
for(i = 1; i <= n; i++){
array[i] = i;
}
for(i = 1; i <=n; i++){
cin >> stepsmoved;
if(stepsmoved == 0){}
else{
int x;
x = array[i];
for (int j = i; j> i- stepsmoved; j--){
array[j] = array[j-1];
}
array[i-stepsmoved] = x;
}
}
for(i = 1; i <= n; i++){
cout<<array[i]<<" ";
}
cout<<endl;
t--;
}
return 0;
}
So is there something logically or syntactically wrong?
The order of 'unwinding' the sorting is relevant.
Here is the code that demonstrates the statement above (the ranks are 1-based, the 1 - is highest, 10 - is lowest, array indices are 0-based):
#include <stdio.h>
void dump(int *a) {
int i;
for (i = 0; i < 10; i++)
printf("%d ", a[i]);
printf("\n");
}
int main() {
int array[10] = {0}, steps[10] = {0};
int i,j;
srand(0);
// Assign ranks in random order
for (i = 0; i < 10;) {
j = rand() % 10;
if (!array[j])
array[j] = ++i;
}
dump(array);
// Sort according to the Sgt Johnny's initial idea
for (i = 1; i < 10; i++) {
for (j = 0; array[j] < array[i]; j++);
if (j < i) {
int k, temp = array[i];
for (k = i; k > j; k--) {
array[k] = array[k-1];
steps[temp-1]++;
}
array[j] = temp;
dump(array);
}
}
printf("Steps:\n");
dump(steps);
printf("\n");
// reconstruct the origina order
#if 1
for (i = 10-1; i >= 0; i--)
#else
for (i = 0; i < 10; i++)
#endif
{
int s = steps[array[i]-1];
for (j = i; s; s--, j++) {
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
dump(array);
}
}
If the reconstruction is done in reverse order, then we get a sequence that matches original:
8 7 5 1 10 4 2 3 9 6
7 8 5 1 10 4 2 3 9 6
5 7 8 1 10 4 2 3 9 6
1 5 7 8 10 4 2 3 9 6
1 4 5 7 8 10 2 3 9 6
1 2 4 5 7 8 10 3 9 6
1 2 3 4 5 7 8 10 9 6
1 2 3 4 5 7 8 9 10 6
1 2 3 4 5 6 7 8 9 10
Steps:
3 5 5 4 2 4 1 0 1 0
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 10 9
1 2 3 4 5 6 7 8 10 9
1 2 3 4 5 6 8 7 10 9
1 2 3 4 5 8 7 10 9 6
1 2 3 4 8 7 5 10 9 6
1 2 3 8 7 5 10 4 9 6
1 2 8 7 5 10 4 3 9 6
1 8 7 5 10 4 2 3 9 6
8 7 5 1 10 4 2 3 9 6
Otherwise, the reconstructed order does not match the original:
8 7 5 1 10 4 2 3 9 6
7 8 5 1 10 4 2 3 9 6
5 7 8 1 10 4 2 3 9 6
1 5 7 8 10 4 2 3 9 6
1 4 5 7 8 10 2 3 9 6
1 2 4 5 7 8 10 3 9 6
1 2 3 4 5 7 8 10 9 6
1 2 3 4 5 7 8 9 10 6
1 2 3 4 5 6 7 8 9 10
Steps:
3 5 5 4 2 4 1 0 1 0
2 3 4 1 5 6 7 8 9 10
2 4 1 5 6 7 3 8 9 10
2 4 5 6 7 1 3 8 9 10
2 4 5 7 1 3 8 6 9 10
2 4 5 7 3 8 6 1 9 10
2 4 5 7 3 8 6 1 9 10
2 4 5 7 3 8 1 9 10 0
2 4 5 7 3 8 1 10 9 0
2 4 5 7 3 8 1 10 0 9
2 4 5 7 3 8 1 10 0 6
How to pick the best uniformed 1d array from the 2d arrays ?
I have two 2d array of : 11 x 10
Example :
4 8 12 12 12 14 16 18 4 1 0
5 7 11 12 13 11 15 18 3 2 1
8 3 12 14 18 19 20 21 8 5 4 ,
8 2 11 12 17 17 19 20 7 4 3 ,
4 7 11 11 11 15 17 19 5 1 1 ,
3 8 11 13 11 15 14 17 4 1 0 ,
4 7 12 13 13 14 16 19 3 1 1 ,
5 9 11 12 13 15 17 19 5 0 1 ,
9 7 25 22 24 18 23 17 3 3 3 ,
4 8 13 13 13 15 17 17 5 2 0 ,
here we have 2d arrays of size 11x10 - Need to analysis and have to find out the common 1d array which has common like.
find the best closing number and its difference- and keep doing for all the corresponding columns in an array .
below answer should be like - finding the corresponding very column and comparing with the next row column - if it has some difference below ( 5 ) take the both column of two rows are same and process for next column of the same row..process untill finding the 1 row where it has at least nearby matches of 5
4 8 11 12 13 13 15 18 4 1 0
why don't you do something like this
int[] count(int[][] array)
int result[11];
for(int x = o; x<= 11;x++)
{
int temp[10];
for(int y = o; y<= 10;y++)
{
temp[y] = array[y][x];
}
result[x] = getHighest(temp);
}
return result;
}
int getHighest(int[] array)
{
int size = array.length;
int[size][1] temp;
for(int x; x<= size;x++)
{
int z = array[x];
temp[z][0]++;
}
int highest = -1;
for(int z; z<= size;z++)
{
int z = array[x];
int h = temp[z][0];
if(h > highest)
{
highest = h;
}
}
return highest;
}
Something like this, but my C++ has gotten a bit of rusty so sorry if there are any mistakes.