It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I want to make an application with which you can reserve ticket for your travel. In fact, I'm designing the system for an airline. When I want to create a database (a 2D matrix that saves the number of seats in flights), it gives me errors.
The number of flights set in different place and the number is changing this is my code:
QString** matrix = new QString*[numberofFlights];
for (int i = 0; i < numberofFlight; i++)
{
matrix[i] = new QString[numberofSeats];
}
What class in Qt should i use?
A must-read: Qt container classes.
You could use QVectors or QLists or another container class. For example, to build a vector of vectors:
QVector< QVector<QString> > matrix(numberOfFlights);
for (int i=0; i<numberOfFlights; i++)
matrix[i].fill("", numberOfSeats);
This will create numberOfFlights vectors, that each contain numberOfSeats empty strings.
To set a specific seat:
matrix[flight][seat] = "whatever";
You can iterate over the vectors with the usual Qt foreach, or iterators, or plain for.
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I tried to find some explanation in internet about what the line numbers in hexdump means and why they are ordered 00000000, 00000010, 00000020 and not 1,2..a..a2 etc. but it was unsuccessful. Can some one help me understand this one?
The "line numbers" are actually byte offsets at the beginning of each line, represented in hex notation.
Since there are 16 bytes per line, and the first line starts with byte #0, the offsets are:
00000000
00000010 (which means 16 in decimal)
00000020 (which means 32 in decimal)
... etc
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Write a recursive function that computes the value of the recursively defined function
F(n)= -F(n-2), F(0)=1 and F(1) = -2. Iv been staring at this for hours i don't understand. Thanks for help.
Like any programming problem, you can first express the solution using pseudo code, and then move on to implementing it in your language of choice, e.g.
FUNCTION F(n)
IF n == 0 -- recursion terminates when n = 0, with result 0
RETURN 0
ELSE IF n == 1 -- recursion can also terminate when n = 1, with result -2
RETURN -2
ELSE -- otherwise recursion contiunes with F(n - 2), F(n - 4), ...
RETURN -F(n - 2) -- until one of the terminating conditions is reached
END
From here it should be quite straightforward to implement this function in C++ or whatever language you choose. Be sure to also implement a "test harness", i.e. a function which calls F with a range of different input values and then prints the result, so that you can verify whether the function is behaving correctly and debug it if necessary.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have implemented a code that uses Dijktra's Algorithm. Thanks to Dijkstra I can print the shortest path from the desired source to desired destination. However, what I want to do is to add a feature that tells us directions with turn left , turn right commands.
Examples:
From A to D:
Let's say A is located in street 1 , B is located at street 2 and D is located 60 meters left of the street 2.
From A to D:
Go to Street 2 . Turn left . Go about 60 meters .It will be on your
left.
I need your ideas. Thank you!
To generate driving instructions from a path, you need to store additional information with the graph:
For each road, its length. This is straightforward
For each crossroad, the relation between each pair of incident roads.
You could store the azimuth of each road around the crossroad (road 1-2 goes west from intersection 1), then generate the driving instructions from the relative angle between two roads, type of the crossroad (normal / roundabout) and the topological ordering and relative angles of all other roads.
This approach has the benefit of more compact representation, but it needs more programming.
Alternatively, you could store the relation between each pair separately. This is more reliable (only a human could truly comprehend the complexities of each possible crossroad type in the world), but it's more manual to update (after all, a little AI could in theory defer the crossroad type, even if with errors).
If you have a huge map, you'll want to stick to the first approach. If you are building the map manually, you may prefer the second one - just be sure to not actually store strings with each road pair (unless they're interned by the language), or your memory demands might skyrocket. This needs extra attention when serializing the map to a file (again, a ZIP compression might alleviate that to a great extend, if you opt for that).
If your map is only concerned about simple 4-way crossroads, then the information stored with each pair is simply a left/straight/right enum (approach #2), or just an ordering of the edges around the crossroad (approach #1) where some roads may be null.
For example, your crossroad could (simplest case, approach #1) look like
private Road[] roads = new Road[4];
public enum Direction{
Left, Straight, Right, Back;
// utility methods
}
public Direction getDir (Road from, Road to){
// input checking stripped for clarity
int iFrom = roads.indexOf(from);
int iTo = roads.indexOf(to);
// more input checking
int iDiff = (iFrom - iTo) % 4;
if(iDiff < 0) iDiff +=4 ;
return Direction.getRelative90(iDiff);
//Direction.getRelative90 is just a switch statement.
}
For generating the directions, use the information stored with the map. Remember to concatenate roads (sum up their lengths) that logically follow (no instructions at that intersection = implicit "go straight" - several roads might follow into one, but only one should follow from each), the rest is straightforward.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have following number in SQL Server 2008 : 5.4564556
I just want to get only 4564556 from 5.4564556
it would be better if i can get only 4 among 4564556 that is first digit of fractional part..
How can i do it?
Thanks
You can try in this way :
SELECT (5.4564556 % 1)
You can also use FLOOR():
SELECT Value - FLOOR(Value)
In your case:
5.4564556 - 5 = 0.4564556
in case of negative value you can use ABS
SELECT ABS(Value) - ABS(FLOOR(Value))
To get all the numbers behind the '.', you can (ab)use PARSENAME
select PARSENAME(5.4564556,1)
returns 4564556
Refer also to SQL - How do I get only the numbers after the decimal?
you can get first four decimal parts by :
select SUBSTRING (PARSENAME(5.4564556,1), 1, 4)
Try this
select cast(5.4564556 % 1 * 10 as int)
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
A simplified description of the problem:
There are exactly maxSize people shopping in a store. Each of them has a shopping list, containing the price of items (as integers). Using Fortran arrays, how can I represent all the shopping lists. The shopping lists may contain any number of items (1, 10, 1000000000).
(NOTE: The actual problem is far more complicated. It is not even about shopping.)
The lazy approach would be:
integer :: array(maxSize, A_REALLY_BIG_NUMBER)
However, this is very wasteful, I basically want the second dimension to be variable, and then allocate it for each person seperately.
The obvious attempt, doomed to failure:
integer, allocatable :: array(:,:)
allocate(array(maxSize, :)) ! Compiler error
Fortran seems to require that arrays have a fixed size in each dimension.
This is wierd, since most languages treat a multidimensional array as an "array of arrays", so you can set the size of each array in the "array of arrays" seperately.
Here is something that does work:
type array1D
integer, allocatable :: elements(:) ! The compiler is fine with this!
endtype array1D
type(array1D) :: array2D(10)
integer :: i
do i=1, size(array2D)
allocate(array2D(i)%elements(sizeAt(i))
enddo
If this is the only solution, I guess I will use it. But I was kind of hoping there would be a way to do this using intrinsic functions. Having to define a custom type for such a simple thing is a bit annoying.
In C, since an array is basically a pointer with fancy syntax, you can do this with an array of pointers:
int sizeAt(int x); //Function that gets the size in the 2nd dimension
int * array[maxSize];
for (int x = 0; x < maxSize; ++x)
array[x] = (int*)(calloc(sizeAt(x) , sizeof(int)));
Fortran seems to have pointers too. But the only tutorials I have found all say "NEVER USE THESE EVER" or something similar.
You seem to be complaining that Fortran isn't C. That's true. There are probably a near infinite number of reasons why the standards committees chose to do things differently, but here are some thoughts:
One of the powerful things about fortran arrays is that they can be sliced.
a(:,:,3) = b(:,:,3)
is a perfectly valid statement. This could not be achieved if arrays were "arrays of pointers to arrays" since the dimensions along each axis would not necessarily be consistent (the very case you're trying to implement).
In C, there really is no such thing as a multidimensional array. You can implement something that looks similar using arrays of pointers to arrays, but that isn't really a multidimensional array since it doesn't share a common block of memory. This can have performance implications. In fact, in HPC (where many Fortran users spend their time), a multi-dimensional C array is often a 1D array wrapped in a macro to calculate the stride based on the size of the dimensions. Also, dereferencing a 7D array like this:
a[i][j][k][l][m][n][o]
is a good bit more difficult to type than:
a(i,j,k,l,m,n,o)
Finally, the solution that you've posted is closest to the C code that you're trying to emulate -- what's wrong with it? Note that for your problem statement, a more complex data-structure (like a linked-list) might be in order (which can be implemented in C or Fortran). Of course, linked-lists are the worst as far as performance goes, but if that's not a concern, it's probably the correct data structure to use as a "shopper" can decide to add more things into their "cart", even if it wasn't on the shopping list they took to the store.