How to pass a Fix array to unknown size array

Discussion in 'C Programming' started by Belinda Lopez, Mar 30, 2008.

  1. Belinda Lopez

    Belinda Lopez

    Joined:
    Mar 30, 2008
    Messages:
    1
    Likes Received:
    0
    Hi,

    I am trying to do a program that has a fix array and I need to pass thsi array to a new array varying from 2 lines to 20 lines (or rows) and seing how many times it hits the same number (this is the LRU algorithm).

    My problem is I do not know how to dynamically set the dynamic array or if it wil be better using vectors and passing it to a function. Please help.


    #include <iostream>
    #include <iomanip>
    using namespace std;


    const int NUM_ROWS = 18,
    NUM_COLS = 20;

    int fillCache_LRU(int [][NUM_COLS], int **array);


    int main()
    {
    int results;
    int **array;
    int param1,param2;

    cout << "Enter the number of cache lines from 2 to 20 to calculate the cache hit? ";
    cin >> param1;

    while (param1 < 2 || param1 > 20)
    cout << "Invalid Line number.\n"
    << "Enter the line Number from 2 to 20 even numbers: ";
    cin >> param1;


    array = new int*[param1]; // create first dimension (of pointers to 2nd dimension)

    for (int i=0; i< param1; i++)
    {
    array = new int[param2]; // create second dimension(s) }

    int table1[NUM_ROWS][NUM_COLS] =
    {{1, 11, 1, 11, 1, 11, 1, 11, 1, 11, 1, 11, 2, 9, 2, 7, 2, 7, 2, 10},
    {2, 11, 2, 11, 2, 10, 2, 10, 6, 10, 6, 11, 6, 9, 6, 9, 6, 7, 6, 7},
    {6, 7, 6, 9, 6, 9, 6, 7, 6, 7, 6, 7, 6, 7, 6, 9, 6, 7, 6, 7},
    {6, 7, 6, 9, 6, 7, 6, 7, 6, 7, 6, 11, 6, 10, 6, 10, 6, 11, 6, 11},
    {6, 7, 6, 7, 6, 7, 6, 10, 6, 7, 6, 11, 6, 10, 6, 10, 6, 11, 6, 11},
    {6, 11, 6, 10, 6, 10, 6, 10, 6, 11, 6, 12, 6, 12, 6, 12, 6, 14, 6, 14},
    {6, 14, 6, 14, 6, 7, 6, 7, 6, 7, 6, 11, 6, 11, 6, 10, 6, 10, 6, 11},
    {3, 10, 3, 10, 3, 11, 3, 7, 3, 7, 3, 7, 3, 7, 3, 10, 3, 10, 3, 10},
    {3, 10, 3, 10, 3, 10, 3, 9, 3, 9, 3, 9, 3, 9, 3, 10, 3, 8, 3, 10},
    {3, 10, 3, 10, 3, 10, 3, 10, 3, 8, 3, 8, 3, 8, 4, 8, 4, 10, 4, 8},
    {4, 9, 4, 9, 4, 9, 4, 9, 4, 9, 4, 7, 4, 7, 4, 7, 4, 15, 4, 15},
    {4, 9, 4, 9, 4, 9, 4, 9, 4, 10, 4, 16, 4, 16, 4, 10, 4, 10, 4, 16},
    {4, 10, 4, 10, 4, 10, 4, 16, 4, 9, 4, 15, 4, 7, 4, 7, 4, 7, 4, 15},
    {4, 7, 4, 7, 4, 7, 4, 7, 4, 15, 4, 7, 4, 7, 4, 7, 4, 9, 4, 9},
    {4, 9, 4, 10, 4, 10, 4, 15, 4, 15, 4, 15, 4, 9, 4, 9, 4, 9, 4, 9},
    {4, 15, 4, 7, 4, 7, 4, 15, 4, 15, 4, 15, 4, 15, 4, 15, 4, 15, 4, 8},
    {4, 10, 4, 10, 4, 10, 4, 8, 4, 8, 4, 10, 4, 10, 4, 10, 4, 10, 4, 10},
    {4, 18, 4, 18, 4, 18, 4, 19, 4, 16, 4, 19, 4, 19, 4, 15, 4, 15, 4, 19}};

    results = fillCache_LRU(table1, array);

    if (results == -1)
    cout << "There were no hits for this line " << endl;

    else
    {
    cout << "The cache hits for the numbers of lines are: " << endl;
    cout << (results +1 ) << ".\n";
    }


    return 0;
    }

    //*************************************************************************
    // Function Definition for fillCache_LRU
    //*************************************************************************

    int fillCache_LRU(int array[][NUM_COLS], int **table1[int numrows][int numcols])
    {
    int count = 0;
    int total = 0;
    bool found = false; //Flag

    for(int row = 0; row < NUM_ROWS; row++)
    {
    for(int col = 0; col < NUM_COLS; col++)
    {
    total += total +1;

    for (int i = numrows)
    {
    for (int j = 0; j < numcols; i++)
    {

    table1[j] = array[row][col];
    if(table1 [j] == array[row][col])
    {
    count = count +1;
    found = true;

    break;
    }

    int cache_hit = count/total;

    if (found == true)

    cout << "Number of hits is = " << cache_hit << endl;
    }
    }
    return count;
    }
    }
    }
     
    Belinda Lopez, Mar 30, 2008
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.