What does that * means before u, what kind of the variable it is, and what will be the output of this function.
Thanks
void inidat (int nx, int ny, float* u)
{
int ix, iy;
for (ix = 0; ix <= nx-1; ix++)
{
for (iy = 0; iy <= ny-1; iy++)
{
*(u+ix*ny+iy) = (float)(ix * (nx - ix - 1) * iy * (ny - iy - 1));
}
}
}
This is a pointer. A pointer is an object whose value refers to another value stored elsewhere in the computer memory using its address. A pointer references a location, and one can get the object stored at that location by "dereferencing" the pointer.
float * x;
cout << *x;
cout << x;
To get the value of the pointer, you do the 2nd line.
To get the location of the pointer you do the 3rd line.
* means that the follow variable is defined or used (to be used, as in this case, it must've been defined before) as a pointer.
As for the output of the function: it's void so it won't return any value but change some in the last line, I would need the context of the function to see what it's actually doing.
Related
I know that if I have a function like:
public int addOne(int a){
return (a+1)
}
The time complexity order will be O(1) since we only do one operation (the sum).
But what if I have a function that doesn't do any operations, just assigns some values to some global variables. Like this:
public void assignValues(){
a = 2;
b = 3;
c = 4;
//maybe more
}
What would the time complexity be for this function? My guess is that it would still O(1). Is that correct?
When you discuss the time complexity of an algorithm, you first have to define the variable parameter(s). For example, it doesn't make any sense to say that something is O(n) without defining what you measure by n (e.g. the length of an array? The size of the contents of an array? The bit-length of a number? The absolute value of an integer?).
In your particular case, you have a function that takes no parameters. Assuming that the operations within the function don't depend on any other external parameters, the complexity of such a function is always trivially O(1), no matter what operations you perform inside. For example, the following function is also O(1):
public static int DoSth() {
int x = 0;
const int n = 1000000;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
x++;
return x;
}
As already mentioned, this assumes that the parameter-less function has no external dependencies. Consider e.g. the following function
public static void DoSth() {
int n = DateTime.Now.Ticks;
for(int i = 0; i < n; i++)
Console.WriteLine(i);
}
This function is O(n log n) if n is the amount of time that has passed since 1.1.0001.
I want to define a constant array of constants at every MPI node using C++03. M_chunk_sizes defines the size of matrix that will be passed to other nodes and won't be changed during the runtime.
int* define_chunk_sizes( int S, int world) {
int out[world];
double quotient = static_cast<double> (S) / world;
int maj = ceil(quotient);
for (int i =0; i < world - 1; i++)
out[i] = maj;
out[world-1] = maj + (S - maj*world);
return out;
}
int main() {
const int M = 999; // rows
int world_size = 4;
const int* const M_chunk_sizes = define_chunk_sizes(M, world_size);
}
But i get a warning: address of stack memory associated with local variable 'out' returned [-Wreturn-stack-address]
return out;.
What is the right way of doing this?
funciton local variables(stack varibales) will go out of scope and life once function returns.
you have use dynamic memory management operators, so allocate memory to out using
new
and relase memory using
delete
once you done with it.
I wanted to check the performance time for different data in array (random, already sorted, sorted in descending order).
void Quicksort(int *T, int Lo, int Hi)
{
if (Lo<Hi){
int x=T[Lo];
int i=Lo, j=Hi;
do
{
while (T[i] < x) ++i;
while (T[j] > x) --j;
if (i<=j)
{
int tmp = T[i];
T[i] = T[j];
T[j] = tmp;
++i; --j;
}
} while(i < j);
if (Lo < j) Quicksort(T, Lo, j);
if (Hi > i) Quicksort(T, i, Hi);
}
}
Here are the functions used to generate and fill the testing array:
int* createArr(int length){
int* Arr= new int[length];
return Arr;
}
void Random(int *A, int length){
for (int i=0;i<length;i++){
A[i]=rand();
}
}
void Order(int *A, int length){
for (int i=0;i<length;i++){
A[i]=i;
}
}
void Backwards(int *A, int length){
for (int i=0;i<length;i++){
A[i]=length-i;
}
}
It works fine for random numbers, but when I try to fill it in ascending order and sort, it crashes with stack overflow. Can anyone give me a hint of why is it happening?
When you choose the [Lo] item as a partioning value (a pivot), and the array is already sorted, then you get a partitioning 1 to (length–1). This causes the next (recursive) call on a longer part of the array to handle just one item less then the previous level. So, as #AlexanderM noted, you will get as deep into the recursion as the length of your array. If the array is big, it will almost certainly cause the stack overflow.
Try using a tail reursion: check which part is shorter and sort it with a resursive call, then continue on the current level with the longer part. To do that replace the very first if with while and replace
if (Lo < j) Quicksort(T, Lo, j);
if (Hi > i) Quicksort(T, i, Hi);
with
if (j-Lo < Hi-i)
{
Quicksort(T, Lo, j);
Lo = i;
}
else
{
Quicksort(T, i, Hi);
Hi = j;
}
This won't make the program any faster – it will still need O(n^2) time on an already-sorted array, but will protect against linear stack memory usage, keeping it at worst O(log n).
For further directions concerning better performance see Wikipedia article Quicksort, part Choice of pivot.
I'm reading a book to learn Objective-C and this program is suppose to show key concepts in dealing with pointers, and I'm really lost.
Is there some kind of conversion happening in the function's arguments that turn p1, p2, &il, and &i2 to the value (*) of a pointer? Like p1 turns into *p1?
I thought a copy of the variable was passed into the function instead of the actual variable, so why was the value of the passed in variable changed after the function?
Also why am I getting a warning on the 3rd line that says: No previous prototype for function 'exchangeValues'?
Thank you!!
#import <Foundation/Foundation.h>
void exchangeValues (int *pint1, int *pint2) {
int temp;
temp = *pint1;
*pint1 = *pint2;
*pint2 = temp;
}
int main (int argc, char *argv[]) {
#autoreleasepool {
void exchangeValues (int *pint1, int *pint2);
int il = -5, i2 = 66, *p1 = &il, *p2 = &i2;
NSLog(#"il = %i, i2 = %i", il, i2);
exchangeValues(p1, p2);
NSLog(#"il = %i, i2 = %i", il, i2);
exchangeValues(&il, &i2);
NSLog(#"il = %i, i2 = %i", il, i2);
}
return 0;
}
Output:
2012-08-02 11:13:38.569 Test[381:707] il = -5, i2 = 66
2012-08-02 11:13:38.571 Test[381:707] il = 66, i2 = -5
2012-08-02 11:13:38.572 Test[381:707] il = -5, i2 = 66
I would say that's a complex example if you are being taught about pointers!
Is there some kind of conversion happening in the function's arguments
that turn p1, p2, &il, and &i2 to the value (*) of a pointer? Like p1
turns into *p1?
p1 and p2 are declared as int * (pointer to int) and are initialised with the address of i1 and i2 (using the & operator).
I thought a copy of the variable was passed into the function instead
of the actual variable, so why was the value of the passed in variable
changed after the function?
A copy of the variable is passed to the function, however in this case the variable of type int * (pointer to int). The reason the value is changing is because the exchangeValues() function is dereferencing those pointers and swapping the values. This is the only way (in C/Objective-C) a function can modify a variable outside of its own scope, other than the variable being assigned as the return value from a function.
Also why am I getting a warning on the 3rd line that says: No previous
prototype for function 'exchangeValues'?
You seem to have typed it in wrong; remove the line below #autoreleasepool:
#autoreleasepool {
void exchangeValues (int *pint1, int *pint2); <-- delete this line
If you pass a pointer into the function, it indeed passes a copy of that pointer- but it still refers to the same address in memory. So de-referencing that pointer will still point to a variable that's outside of the function scope.
I thought a copy of the variable was passed into the function instead of the actual variable, so why was the value of the passed in variable changed after the function?
A copy of the pointer is passed to the function here. So what the function has points to the memory locations the variables l1 and l2 are stored at. So
void exchangeValues (int *pint1, int *pint2) {
int temp;
temp = *pint1; // store the value that pint1 points to in temp
*pint1 = *pint2; // store the value pint2 points to where pint1 points to
*pint2 = temp; // store the value saved in temp where pint2 points to
}
its a little confusing how the variables have been declared and initialised all in a row like that but basically you have:
i1 is an int set to -5
p1 is a pointer to an int set to the address of i1
same goes for i2 and p2
No conversion is taking place. You're effectively 'swapping' the values that those pointers point to in the function.
Pointers are confusing things but stick with it and it will become clear with enough parctice and example code like this...
Look at this example:
int i;
for (i=1;i.......
and this:
for (int i=1;i........
What's the difference between them?
The first one declares the variable in the scope outside of the loop; after the loop has ended, the variable will still exist and be usable. The second one declares the variable such that it belongs to the scope of the loop; after the loop, the variable ceases to exist, preventing the variable from being inadvertantly/mistakenly used.
In C99, C++, Java, and other similar languages, you will find mostly the second syntax as it is safer -- the loop index belongs to the loop and isn't modified / shared elsewhere. However, you will see a lot of the former in older C code, as ANSI C did not allow the loop variable to be declared in the loop like that.
To give an example:
int i;
// ... lots of stuff
for ( i = 0; i < 5; i++ ){
printf("%d\n",i); // can access i; prints value of i
}
printf("%d\n",i); // can access i; prints 5
By contrast:
for (int i = 0; i < 5; i++ ){
std::cout << i << std::endl; // can access i; prints value of i
}
std::cout << i << std::endl; // compiler error... i not in this scope
That would depend on the language, which you haven't specified :-)
In C (and some others), the scope (effectively duration in this case) of the variable is different. In the first, the variable exists after the loop because it's declared outside it.
In the latter, it disappears when the loop ends because its existence is "inside" the loop body.