Help pls C++ function of array

In summary, In order to use these c++ functions, you need to make changes to the function declaration and the variable declaration. You also need to specify the size of the pgrid array.
  • #1
villiami
27
0
Have read a million different sites and threads, but still not getting anywhere (damn 1st year java makes addresses incomprehensible!).

What changes do I need to make to get these c++ functions to work? //---------Functions

//-- A function to increment the (n,m) element
void flipsite(double* pgrid[][10], int n, int m)
{*pgrid[n][m] = *pgrid[n][m] + 1;
}

//-- function to output the array. A form using pointers is fine if that's easier.

double[][10] newgrid(double grid[][10])
{return grid;
}//--------------
void main() {
// I first define an array like:
double grid[5][10];
double grid2[5][10]

flipsite(grid,1,1);
grid2 = newgrid(grid);

std::cout << grid[1][1] << ", " << grid2[1][1] << std::endl; //should output "1, 1"
}

//Thanks!
 
Technology news on Phys.org
  • #2
villiami said:
Have read a million different sites and threads, but still not getting anywhere (damn 1st year java makes addresses incomprehensible!).

What changes do I need to make to get these c++ functions to work?


//---------Functions

//-- A function to increment the (n,m) element
void flipsite(double* pgrid[][10], int n, int m)
{*pgrid[n][m] = *pgrid[n][m] + 1;
}

//-- function to output the array. A form using pointers is fine if that's easier.

double[][10] newgrid(double grid[][10])
{return grid;
}


//--------------
void main() {
// I first define an array like:
double grid[5][10];
double grid2[5][10]

flipsite(grid,1,1);
grid2 = newgrid(grid);

std::cout << grid[1][1] << ", " << grid2[1][1] << std::endl; //should output "1, 1"
}

//Thanks!

lol java. in c/c++ arrays can be a bit tricky as you've discovered. no offense or anything, but that code is wrong in a lot of ways.

here are some hings to remember:

1) array indexing is zero-based:
double arr[5]; // creates an array with 5 elements
arr[0] = 1; // assigns the value 1 to the zeroth element
cout<<arr[5]; // ! there is no 5th element - they are numbered 0-4. the compiler will NOT care about this, you might get an error when you run your program. you might not, and have a "garbage" value 8.03474e-645 so something strange like that.

2) you cannot return an array from a function
double[3] foo(int num1, int num2){...}
is simply not allowed.

3) the name of an array is a pointer
int arr[3] = {16}; // creates an array with three elements and makes them all equal 16
int arr2[3]; // creates an empty array
arr2 = arr1; // not what you think. arr1 and arr2 are both of type int*, so your are writing the address of arr1 to the int* arr2. this is very bad becuse you;ve allocated memory for arr2, but since you overwrote the address, you have no way of accessing that memory anymore!

4) arrays are passed-by-reference to functions by default. that's how you get around the fact you cannot return an array from a function.

I really don't know what your code is trying to do, maybe clarify its purpose and we could help you out some more
 
  • #3
p1ayaone1 said:
int arr[3] = {16}; // creates an array with three elements and makes them all equal 16
Correction: arr[0] is initialized to 16, but the other two elements are initialized with whatever happens to be on the stack or 0, depending on whether arr is a local variable or global/statiic variable.
p1ayaone1 said:
int arr2[3]; // creates an empty array
Creates an array, but it won't be empty. There will be three numbers in it. Incidentally, an int array will never have a garbage value of 8.03474e-645. That's a floating point value. int variables have int-type garbage values.
p1ayaone1 said:
arr2 = arr1; // not what you think. arr1 and arr2 are both of type int*, so your are writing the address of arr1 to the int* arr2. this is very bad becuse you;ve allocated memory for arr2, but since you overwrote the address, you have no way of accessing that memory anymore!
Neither arr1 nor arr2 is an "lvalue" so the compiler flags this assignment as an error. The name of an array is read-only; you can't change its value.
 
  • #4
I stand corrected, thanks.
 
  • #5
Thanks for your help, I'm still unsure how to declare a function to modify a 2D array.

I'm trying to make a function to increment a specific element:

void flipsite(double* pgrid[][], int n, int m)
{*pgrid[n][m] = *pgrid[n][m] + 1.0;
}So then in the main function I can write:

double grid2[2][3] = {{0.0,0.0,0.0},{0.0,0.0,0.0}}; // An all zeros matrix
flipsite(grid,0,2); //grid[0][2] should now be 1.0, so "std::cout << grid[0][2] << std::endl;" outputs 1.Should the function declaration (and how I refer to the array for the increment command) be different? Do I need to specify the pgrid array's size?

Thanks again!
 
  • #6
villiami said:
void flipsite(double* pgrid[][10], int n, int m)
{*pgrid[n][m] = *pgrid[n][m] + 1.0;
}
That should be:
void flipsite(double pgrid[][10], int n, int m)
{pgrid[n][m] = pgrid[n][m] + 1.0;
}

Your function prototype is for a 2D array of pointers to doubles, but your variable is a 2D array of doubles, remove the "*".
 
  • #7
If you would beloeve it, Dev C++ ouputs "1" for the following code:
Code:
void flipsite(double pgrid[][10], int n, int m)
{
     pgrid[n][m] += 1;     
}
int main()
{
    double pgrid[10][10] = {0};
    flipsite(pgrid,0,120);  // giving an out-of-bouds index, expecting run-time error
    cout<<pgrid[0][120]<<endl;
    system("pause");
    return 0;
}
 
  • #8
Out-of-bounds array indices don't automatically generate run-time errors in C or in C++. That's why they're so dangerous.

You get a run-time error only if the index goes so far out of bounds that the program tries to access protected memory, or if the data at that location causes some other kind of run-time error (e.g. division by zero) which can of course also happen with bad data in the array itself.
 
  • #9
Great! Thanks guys. For some reason I was sure that wouldn't work- I'd read something about its value being passed, so modifying it (without referring to its address) wouldn't actually change the object.

Thanks again!
 
  • #10
Parameters in C and C++ are passed by value rather than by reference, but if the thing being passed is a point of some kind, the function can modify what the pointer is pointing to.
 
  • #11
Some C compilers include debug versions that check for out of bounds conditions, but this only works on arrays that include size information. There's no check on generic pointers, other than out of bound references that go beyond the virtual address space setup for a program.
 

1. What is an array in C++?

An array in C++ is a data structure that stores a collection of elements of the same data type in a sequential manner. It can hold a fixed number of elements and is accessed by their index number.

2. How do I declare and initialize an array in C++?

To declare an array in C++, you need to specify its data type and the number of elements it will hold. For example, int myArray[5]; will declare an array of integers with 5 elements. To initialize the elements, you can use curly braces and specify the values in order, such as int myArray[5] = {1, 2, 3, 4, 5};

3. How do I access and modify elements in an array in C++?

To access an element in an array, you need to use its index number. For example, myArray[2] will access the third element in the array. To modify an element, you can simply assign a new value to it, such as myArray[2] = 10; which will change the third element's value to 10.

4. What is the importance of functions in arrays in C++?

Functions in arrays allow you to perform operations on the elements in the array without having to write repetitive code. They can also help with organizing and optimizing your code by separating different tasks into functions.

5. How do I pass an array to a function in C++?

To pass an array to a function in C++, you need to specify the data type of the array in the function's parameters. For example, void printArray(int myArray[]); will create a function that takes in an array of integers as a parameter. When calling the function, you can pass in the array by its name, such as printArray(myArray);

Similar threads

  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
3K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
11
Views
995
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
935
  • Programming and Computer Science
2
Replies
39
Views
3K
Back
Top