How to an array to a function

Collapse
X
 
  • Time
  • Show
Clear All
new posts

  • SUNIL TYATA
    replied
    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 . . .

    Leave a comment:


  • SUNIL TYATA
    replied
    the function named floor(.......) used is not identified by c++ complier or is it user defined function?

    Leave a comment:


  • BenFedorko
    replied
    Originally posted by nlal
    Hi i have an array

    Code:
    static int virtual_addr[10]=
    {8196,34893,38583,22883,61843,43532,333,8448,2334,9492};
    that i am trying to pass to a function

    Code:
    int determinePage(int virtual_addr){
      int page_num;
      int page_size = 4096;
    
      page_num = floor(virtual_addr/page_size);
       return page_num;
    }
    however I am getting errors.Any help would be appreciated
    int determinePage(i nt virtual_addr){
    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:


  • weaknessforcats
    replied
    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:


  • Dheeraj Joshi
    replied
    Then array name and the parameter passed to the function can not be same.
    It should be like.

    Code:
    int determinePage(int virtual_addr_i) // Note the name of the variable is not same as array name
    {
       //Some stuff goes here
    }
    And function call must be like

    Code:
    for (int i=0; i<10; i++)
    {
        cout <<determinePage(virtual_addr[i])<< endl;
    }
    Regards
    Dheeraj Joshi

    Leave a comment:


  • nlal
    replied
    yes i am passing one value at a time

    Leave a comment:


  • nlal
    replied
    yes but my immediate concern is how to correctly pass the parameter to the function without getting errors

    Leave a comment:


  • Dheeraj Joshi
    replied
    Are you trying to pass one value at a time to functions from your array?

    Regards
    Dheeraj Joshi

    Leave a comment:


  • Dheeraj Joshi
    replied
    Code:
    cout <<determinePage()<< endl;
    You are calling a function without arguments.

    and you say your function takes one argument.
    Code:
    int determinePage(int virtual_addr[])
    Regards
    Dheeraj Joshi

    Leave a comment:


  • nlal
    replied
    No i don't have it

    Leave a comment:


  • Dheeraj Joshi
    replied
    Do you have any function prototyping?
    If yes. Please post it.

    Regards
    Dheeraj Joshi

    Leave a comment:


  • nlal
    replied
    Code:
    int main()
    {
        cout << "Page Number is: " <<endl;
        for (int i=0; i<10; i++){
        cout <<determinePage()<< endl;
        }
    
    system("pause");
    return 0;
    }
    however the error is generated in the function determinePage

    Leave a comment:


  • Markus
    replied
    That isn't the complete code. You don't show how you're calling the function, etc., nor the updated function.

    Leave a comment:


  • nlal
    replied
    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:


  • Markus
    replied
    Can you show us the full code, please?

    Leave a comment:

Working...