IF you dont use the function floor(...) in your code then I made simple solution may it will help you. Thanks
#include<iostre am>
using namespace std;
int determinePage(i nt);
int main()
{
int page_number;
static int virtual_addr[10]=
{8196,34893,385 83,22883,61843, 43532,333,8448, 2334,9492};
cout << "Page Number is: " <<endl;
for (int i=0; i<10; i++)
{
page_number = determinePage(v irtual_addr[i]);
cout <<page_number << endl;
}
system("pause") ;
return 0;
}
int determinePage(i nt virtual_addr)
{
int page_num;
int page_size = 4096;
page_num = virtual_addr/page_size;
return page_num;
}
out put:
Page Number is:
2
8
9
5
15
10
0
2
0
2
Press any key to continue . . .
How to an array to a function
Collapse
X
-
the function named floor(.......) used is not identified by c++ complier or is it user defined function?Leave a comment:
-
int determinePage(i nt virtual_addr){Hi i have an array
that i am trying to pass to a functionCode:static int virtual_addr[10]= {8196,34893,38583,22883,61843,43532,333,8448,2334,9492};
however I am getting errors.Any help would be appreciatedCode:int determinePage(int virtual_addr){ int page_num; int page_size = 4096; page_num = floor(virtual_addr/page_size); return page_num; }
int page_num;
int page_size = 4096;
page_num = floor(virtual_a ddr/page_size);
return page_num;
}
your page_num = floor(virtual_a ddr/page_size)
is setting page_num = floor(array with 10 values divided by page_size);
you can't use an array like this, you have to pick a value from the array, or make a loop to use every value from the array.
Code:int determinePage(int virtual_addr[]) //<-- the [] MUST be in there { int page_num; int page_size = 4096; page_num = floor(virtual_addr[0]/page_size); // must choose one value from the array to use or it has ten values and can't use them all like this. return page_num; }Leave a comment:
-
The array name and the function argument name can certainly be the same since they are in sifferent scopes.
However, the array name is a pointer to element 0. Therefore, the function argument has to be an int* and not an int.Leave a comment:
-
Then array name and the parameter passed to the function can not be same.
It should be like.
And function call must be likeCode:int determinePage(int virtual_addr_i) // Note the name of the variable is not same as array name { //Some stuff goes here }
RegardsCode:for (int i=0; i<10; i++) { cout <<determinePage(virtual_addr[i])<< endl; }
Dheeraj JoshiLeave a comment:
-
yes but my immediate concern is how to correctly pass the parameter to the function without getting errorsLeave a comment:
-
Are you trying to pass one value at a time to functions from your array?
Regards
Dheeraj JoshiLeave a comment:
-
You are calling a function without arguments.Code:cout <<determinePage()<< endl;
and you say your function takes one argument.
RegardsCode:int determinePage(int virtual_addr[])
Dheeraj JoshiLeave a comment:
-
Do you have any function prototyping?
If yes. Please post it.
Regards
Dheeraj JoshiLeave a comment:
-
however the error is generated in the function determinePageCode:int main() { cout << "Page Number is: " <<endl; for (int i=0; i<10; i++){ cout <<determinePage()<< endl; } system("pause"); return 0; }Leave a comment:
-
That isn't the complete code. You don't show how you're calling the function, etc., nor the updated function.Leave a comment:
-
well that is the code.
I have an array of initialized variables and i m trying to pass its values to the function so that i can divide each value by 4096 and return each value to the main function.Leave a comment:
Leave a comment: