C :: Sorting Has To Be Based On Int Field

Feb 16, 2013

I have a file which has records with 2 fields--one int and one float..what is the best way to sort it?

The sorting has to be based on the int field(the first field) (each time the program runs the file might end up with some hundreds of records).

View 13 Replies


ADVERTISEMENT

C++ :: Sorting Records By User Field In Ascending Order

Nov 7, 2013

How do i sort records by user specified field in ascending order, without knowing how many columns the file has? its getting the data from a file.

View 6 Replies View Related

C++ :: Sorting (array Based) Database?

Mar 26, 2013

knows if there is a ready made function in C++ to sort (numerically or alphabetically) multiple arrays based on a particular array?

i.e. Imagine you have multiple arrays or a multi-dimentional array containing data about users (name, age, etc.)

Is there a function that for example can sort it by name, and automatically shift the age so they still correspond to the correct user name?

View 2 Replies View Related

C/C++ :: Sorting A File Based On ASCII Values?

Sep 1, 2014

New file_sorter.c:

#include <stdio.h>
#include <stdlib.h>
#include "sort.h"
#include <string.h>

[Code].....

My issue is now a segmentation fault in my sorting algorithm. In my assignment I was suppose to implement quick and insertion sort for the file, and organised strictly by ASCII values. We were allowed to look up generic algorithms for the sorts, but we have to convert them to comparing char arrays.

I haven't started trying to configure quick sort yet, but I'll supply the generic algorithm as well. keep in mind that the first line in the file contains the number of lines in the file. I'll try not to disappear this time!

sort.c :
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>

[Code]....

View 4 Replies View Related

C :: Sorting List Of Students Based On Their Test Scores

Jan 18, 2014

I need to sort these students based on their test scores.

My biggest problem is with my sort function. I am not sure of the syntax to call the items the proper way. I also don't know how to call the function properly as I know that i can't pass a int to a struct. Also not sure about the z malloc. Just threw that one in there.

Code:
struct student{
int id;
int score;
};
void sort(struct student* students, int n){
/*Sort the n students based on their score*/

[Code] ....

View 5 Replies View Related

C++ :: Sorting Vectors Based On Value Of First Integer In Ascending Order

Feb 13, 2013

I have a vector which contains vectors containing 7 integers each. I'd like to sort these vectors based on the value of the first integer (int IOT), in ascending order. I know this type of question can be found everywhere, but I'm lost as to why this doesn't compile.

#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <windows.h>
using namespace std;
class orders {
public:
int IOT; // Incoming Order Time

[Code] ....

View 7 Replies View Related

C++ :: Sorting Some Data Based On Values Of A String Of Bits

Apr 1, 2014

I have a problem I am working on where I need to sort some data based on the values of a string of bits. The strings look like this,

010000001110000000

there are 18 bits, 1 means a feature is present, 0 means the feature is absent.

Each of these string has 4 on bits. I need to sort them such that I have the longest possible runs with 3 of the same on bits. It doesn't matter which 3 bits are on, I am just looking to order them in blocks with the longest possible runs. As a second step, the ordered blocks will be sorted by size large>small.

The following data is ordered like I need it to be.

Code:
// block 1, run of 12, keys 1,2,11 are identical (key 12 is also identical)
011000000001100000
011000000001100000
011000000001100000
011000000001100000

[Code] .....

This is the sort order that I am looking for. I need to be able to take a list of the bit strings in any particular order and sort them into the order above. The algorithm would need to recognize that there are 4 on keys and then look for groupings of three common on keys.

This is more of an algorithm question than one about specific implementation in code. I generally assume that most programming problems have been solved one way or another, so I don't know much about analyzing and manipulating strings of bits.

Is there a standard method for this kind of pattern recognition?

View 14 Replies View Related

C++ :: Sorting Data Based On Some Metric - Function Pointer Syntax

Nov 20, 2014

I have a piece of code that sorts data based on some metric. The some metric is something I now want to make flexible so I can easily switch between and compare metrics. To do this, I want to pass the function to use as a parameter to the method that does the sorting (and other stuff). However, I'm having problems figuring out the syntax. Also, I think my [temporary] organization of code is violating a lot of basic code design principles.

To make the function pointer passable, I defined the "typename" in the header where the function is located (it is part of a struct, "Data"):

// Below the struct definition of Data
typedef double (Data::*CostF)(unsigned l, double W) const;

The two example functions I want to use are defined in that struct ("Data"):

// Inside the struct definition
inline double someExampleCost(unsigned l, double W) const {
// Returns some basic calculation
}

The function that uses it is part of a different class (that holds a reference to the first class, in case that matters; I feel like I'm doing something odd here, because I'm binding a member function in the definition/passing, but never referencing the object). It looks like this:

// Inside another class ("Foo")
inline void DoSomeStuff(double& ECost, double& TCost, CostF cost) {
// Irrelevant stuff here
std::sort(vector.begin(), vector.end(), [&](unsigned a, unsigned b){
return (*cost)(a, W) < (*cost)(b, W);
});
// More irrelevant stuff here
}

The error shown is "operand of "*" must be a pointer". If I remove the '*':
[code]return cost(A, W) < cost(b, W);

the error becomes: "expression must have a (pointer-to-)function type."

The call to this function is, currently, just in the main function, as I'm just testing before I wrap it into real code. It looks like this:

// In main
Foo bar; // Make an object of the struct that has the "sorting" function
CostF costFunction = &Data::someExampleCost;
// Bind to the Cost function bar.DoSomeStuff(varA, varB, costFunction);

This bit shows no errors by itself. So, my questions:

a) Clearly I'm missing the insight into Function Pointers. I'm comfortable with regular pointer stuff, but I can't wrap my head around FPs, partly due to the awkward syntax.

b) I'm very uncomfortable with the fact that I'm binding a member function of a class, but never try to reference an actual object of that class. This is probably a big part of why it's not working, but I can't seem to bind a function belonging to a specific object. I thought of doing

// In the main again
Data d; // Construct the object, which contains big lookup tables
Foo F(d); // Construct the object, which only holds a reference to a Data object
CostF costFunction = &d.someExampleCost; // Bind to the Cost function of that object

but that doesn't work ("a pointer to a bound function may only be used to call the function").

View 4 Replies View Related

C# :: Field Initializer Cannot Reference The Non-static Field / Public Var

Apr 12, 2015

So I have this class

class DataBase
{
// Change the connection path here to your own version of the database
public SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)v11.0;AttachDbFilename=|DataDirectory|UberDatabase.mdf;Integrated Security=True;");
public DataBase()
{
}
}

And in the same namespace as this class I have a form that calls it like so:

DataBase dBase = new DataBase();
SqlCommand trythis = new SqlCommand("Register", dBase.con);

However, I'm getting the field initializer error on dBase.con. I'm not sure why, but when I call the database from another file (program.cs) it works fine this way.

View 8 Replies View Related

C :: Structure Field Value Is 0 When It Shouldn't

Sep 11, 2014

I'm trying to write a simple program that extract the FAT information from a FAT32 virtual Hard Disk.I have the following structures regarding the FAT format:

Code:

//BOOT RECORD
typedef struct NF_BOOT_RECORD
{
unsigned char BS_jumpBoot[3]; //EB 58 90 = JUMP 58 NOP (Jumps to boot code). Also E9 is acceptable.
unsigned char BS_OEMName[8]; //Either MSWIN4.1 or mkdosfs
unsigned short BS_BytesPerSec; //Little endian. The size of a sector. 128,256,512,1024...
unsigned char BS_SecPerClus; //The number of sectors per cluster (1 CLUSTER = BPS*SPC BYTES)
unsigned short BS_RsvdSecCnt; //The boot sectors (this) are included. That makes at least 1.
}

[code]...

Everything seems to work fine. Mostly. The only problem, is that the program gives me the following output:BS_NumFATs shouldn't be 0. In fact, I've checked inside the structure memory, and the information seems correct. BS_NumFATs is 0x02, not 0x00 (It's the byte at offset 0x10, starting at 0x00).

I've checked the order of the structure fields, and their types, comparing them to the FAT specification given by Microsoft (File fatgen103.pdf), and it seems fine, unless I'm missing something. So I don't know why it gives 0 instead of 2, if I'm missing something.It's a Win32 program compiled with GCC version 4.4.0

View 3 Replies View Related

C++ :: Rotating Pixel Field

Apr 21, 2014

I wrote a script that generates n random pixel positions and draws them to the screen. Works well. Now i tried to rotate them. Rotating does work too. But it does not work as i planned it.

paramters 'angle' and 'timestep' work somehow, but not as they should do. the function 'move' is supposed to rotate the pixelfield 'angle' degrees in a given direction, addicted to the 'timestep' parameter. 'timestep' is needed time for drawing in one single game loop.

angle_step = timestep * angle
// x
ppdPoint[i]->x =
pRotationPoint->x + cos(angel_step) * (ppdPoint[i]->x - pRotationPoint->x) - sin(angel_step) * (ppdPoint[i]->y - pRotationPoint->y);
// y
ppdPoint[i]->y =
pRotationPoint->y + sin(angel_step) * (ppdPoint[i]->x - pRotationPoint->x) + cos(angel_step) * (ppdPoint[i]->y - pRotationPoint->y);

rotation point is the middle of the screen. when i set angle to 10 it should rotate 10 degrees / second. Instead it's rotating very very fast and all stars are moving nearer to the center of the screen, so after x rounds there is just 1 pixel left in the middle of the screen. there is a kind of gravition.I'm working with SDL2. What I did find out:

FPS is <= 60, 'cause of the 'SDL_RENDERER_PRESENTVSYNC' flag. When i skip that flag, for any reason the 'gravition' would take more time. FPS is <= 1400 then, 'though i got a natural game loop (i hope):

while(pBuild->getExecuteFlag())
{
pBuild->draw();
pBuild->update(time_dif, pTimeWindow->TimeStampB);
time_dif = (pTimeWindow->TimeStampB - pTimeWindow->TimeStampA);
time_dif *= 0.001;
pTimeWindow->TimeStampA = pTimeWindow->TimeStampB;
pTimeWindow->TimeStampB = SDL_GetTicks();

pBuild->count_fps(pTimeWindow->TimeStampB);
}

So maybe (timestep * angle) isn't the right way?

View 13 Replies View Related

C++ :: How To Output One Particular Field In A Form

Aug 8, 2014

I need writing a program in c++ that will get the input of 50 different users for the following the fields (surname, other names, sex, status, date of birth) and after entering the data for these 50 users, it will then output only the list of all FEMALE users who were registered...

#include <iostream>
#include <string>
using namespace std;
int main () {
string surname, other_names1, other_names2, status, sex, dob;

[Code] ....

I don't really know what to do next - all my attempts have not really given me the result I want.

View 19 Replies View Related

C# :: Using DateTime Field From SQL Database

Oct 30, 2014

I have a DateTime field on a database it looks like this :

2014-10-21 14:34:37.697

I want to use it, as is, in an UPDATE statement something like:

UPDATE PayrollRuns SET IsProcessed = 'True' WHERE Date_Time_Triggered = '2014-10-21 14:34:37.697'

I must have the exact date & time as that uniquely identifies the record.

The problem I'm having is getting it off the database and into an C# variable without it being changed.

I can get it into a string but it changes it becomes:

21/10/2014 14:34:37

I've got it in an object. I've tried:

string date_Time_Triggered = databasesRow["Date_Time_Triggered"].ToString();
//
//and
//
var date_Time_Triggered = databasesRow["Date_Time_Triggered"];

[Code] ....

View 3 Replies View Related

C/C++ :: How To Access Field Of A Class By Name

May 2, 2012

class myCls{
int x;
};

and then I create:

myCls c1;

and now:

is there way that I go like: cout << c1;
not overloading << operator;
and it shows me a value of c1.x?
just wanna replace c1 with that value;
I need sth similar to indexer from c#.

View 1 Replies View Related

C++ :: How To Exchange Strings In Field

Sep 29, 2013

I need exchange strings in field, but how to do that. For example:

{"Hi", "Hello", "Goodbye", "Morning"};
to
{"Hello", "Hi", "Goodbye", "Morning"};

I tried this, but it isn't working:

Code:
for(unsigned int k=0; k<slova[i].size();k++){
t = slova[i][k];
slova[i][k] = slova[i+1][k];
slova[i+1][k] = t;
}

View 7 Replies View Related

C++ :: Pre-Compile Check If Field Does Already Exists

Nov 27, 2014

I have a dynamic storage class based on a vector. The elements of this class shall be addressable via a string.

I have something like the following:

StorageClass* test = new StorageClass();
while(notEnd) {
test->setValue("name3", 3);
test->setValue("name2", 4);

[Code] ....

The Storage class is implement with a vector having to attributes per entry, the first one is name the second one is value. If the setValue function is called, Igo through the whole vector to find the correct item via the name and than i set the new value.

At the end of the loop i want to write out the data into a comma separated value file (*.csv). This shall be done generic by the writeLineToFile() function of the StorageClass.

As a want the columns in csv file to be sorted in a specific way a thought about a preInitalisation routine at the beginning of the program which builds a the vector with all possible entries as something like this in order to define a specific output order.

test->assign("name1");
test->assign("name2");
test->assign("name3");

So my outputfile should look like this:

name1;name2;name3;
5;4;3;

I now have 2 places in the code where i write down the "fieldnames".

I was wondering if there is a possibility to check if the fields called by the setValue function were first aranged in the vector by the assigned() function before or bettwer while compiling the programm.

I thought about MAKROS with #define XYZ and #ifndef XYZ #error to do though, but currently i reached a dead end and find no solution for my problem.

View 4 Replies View Related

C++ :: Input Date Field In A Comment

Mar 11, 2014

I am building a template for CodeBlocks, which pre-write some includes, the main skull and a header.

This header looks like that :
/**
* @Author Me
* @Description Brief description
* @Date ??
*/

I would like to put the current Date, just after the line @Date, is-it possible ? And if yes, it-is possible to put a field @FileName which copy the filename into the comment ?

View 2 Replies View Related

C++ :: Use Object In Header File Field

Mar 26, 2013

I need to use an instance of an object in a header file. The object requires a lot of pre-computation to be created.

I want to assign it to a "static const *NAMEobject*" field in the .h file, once it has been created. I assume that it is better to create it in another file and somehow pass it into the .h file.

View 14 Replies View Related

C++ :: How To Produce Password Field With Asterisk

Sep 2, 2014

void Log_In() {
system("cls");
gotoxy(30, 30);
time_t now;
time(&now);

[Code] .....

How to produce a password field with asterisk ****** .....

View 1 Replies View Related

C++ :: Variable Or Field Entered Void

Jan 16, 2012

I am experiencing some errors with my first c++ program. im getting some compilation errors that i cannot figure out.

In file included from w2.cpp:7:
Fraction.h:17: error: variable or field 'enter' declared void
Fraction.h:17: error: expected primary-expression before 'struct'
Fraction.h:18: error: variable or field 'display' declared void
and so on ..........

Code:
#include "fraction.h"
//using namespace std;
/*
int main() {
struct Fraction fraction;
cout << "Fraction Simplifier" << endl;
cout << "===================" << endl;
enter(&fraction);

[Code] .....

View 1 Replies View Related

C# :: Why Can't A Constant Field Be Of Non-built-in Struct Type

Dec 9, 2011

Code:

class T
{
enum E { }
struct S { }
interface I { }
delegate void D();
class C { }
}

[code]....

All of the above are possible except the constant field of a type that is a struct. I can see why the non-string reference types would default to the only literal expression they can represent - null - and if I'm not mistaken the default value of a struct is an instance of the struct with all its fields set to their default value, but why is it that a const struct field is not possible? is there a reason why or is it just the way c# was written? oh and by the way, what's the idea of allowing enum types to have a default constructor?

View 4 Replies View Related

C :: Variable Or Field Declared Void And Other Errors

Mar 29, 2014

I'm creating a payroll program and here is my code

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define REPORTHEADING1 " Employee Pay Hours Gross Tax Net
"
#define REPORTHEADING2 " Name Rate Worked Pay Due Pay
}

[code]....

The errors I get are: variable or field PrintSummaryReport declared void in function voidPrintSummaryReport... cannot convert FILE to const car for argument to int printf(const char*...) which is for the following lines

Code:

printf(reportFile,REPORTLINEFORMAT1,fullName,payrate,regHours,gross,fedtax,
ssitax,netpay);
printf(reportFile,REPORTLINEFORMAT2,ovtHours,statetax,deferred);

View 7 Replies View Related

C++ :: Initializing Static Field As A Template Object

Aug 26, 2014

I need a static hash table to keep track of all objects of a particular type that are instantiated in a Qt application but I have never used a template class as a static member object before and I can't seem to figure out how to initialize it. QHash is the hash table class that follows the template convetion:

template<class key, class data>

QString is probably self explanatory.

Example Header:

class MyClass {
...
private:
static QHash<QString, MyClass*> instanceTable;
}

Here is my source that doesn't compile.

Example Source

#include header.h
// using default constructor for table...
QHash<QString key, MyClass* instance> MyClass::instanceTable(); // gives Error below.
// Error in above line is "Declaration is incompatible with QHash<QString, Myclass*>"

I have tried doing it a number of different ways and none of them work. How do you initialize a static template object?

View 2 Replies View Related

C++ :: Error - Variable Or Field Declared Void

Sep 17, 2014

Why I am getting this error

error: variable or field createBinaryFile declared void

void createBinaryFile(std::fstream&, std::ifstream&);

View 2 Replies View Related

C++ :: Control Ofstream Output Field Width?

Apr 25, 2013

I have to output a series of data, I have to control the width of parts of my data. like following:

double a[] = {1.0, 2.0, 3.0};
double b[] = {1.5, 2.5, 3.5};
ofsteam myfile("result.txt");
for(int i=0; i<3; i++) {
myfile.width(5);
myfile << i+1 << " : " << a[i] << " - " << b[i] << end;
}

I only want to fix the width of a[i], b[i], but let the rest to be output normally.

how I can make it?

View 5 Replies View Related

C++ :: Creating (Composite) Vector Array Field?

Jan 3, 2013

In a numerically intensive code, I have a Cartesian vector class Vector3d which has the normal operator overloading and basic functions such as dot product, magnitude, etc. For simplicity, assume that it is not a templated class and its components are of type double.

I frequently need large 1-d arrays (e.g. stl vectors) of Vector3d. Two use-cases must be satisfied:

1) The trivial case in which the data are stored as stl vectors of Vector3d;

2) The more difficult case where the individual components are stored as stl vectors of double, and are not guaranteed to be contiguous in memory (so one cannot rely on "stride").

Assuming the array lengths are all identical, I'd like to be able to access both types in a single loop. The straightforward way for case 2) is to construct a temporary Vector3d from the three components (indexed with the loop index). However, I would prefer not to incur the overhead of the constructor.

Is it possible using template metaprogramming. Ideally I'd like a CompositeVector3d that inherits from Vector3d and is constructed with the component vectors, but can be dereferenced using the loop index in the same way as one would do with case 1.

I am not looking for the typical template metaprogramming utility of being able to operate on the entire array without explicit loops. I just want the syntactic "sugar" whereby CompositeVector3d and Vector3d act the same, plus the avoidance of the cost of the constructor. I am not averse to using an additional templated class (perhaps a Field or a View class) to access the basic storage in both case.

Is it possible to do this without using a full template metaprogramming utility?

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved