-4
#include <iostream>
#include <time.h>
using namespace std;

void my_func();
int main()
{
    float start_time = clock();
    cout << "Starting time of clock: " << start_time;
    cout << endl << endl;

    for (int i = 0; i < 100000; i++)
    {
        my_func();
    }

    float end_time = clock();
    cout << "Ending time of clock: " << end_time;
    cout << endl << endl;
}

void my_func()
{
    int my_array[5][5];
}

I need to write a program that does a large number of references to elements of a two-dimensional array, using only subscripting. This is really a two-part project, but I'm only concerned with getting the first part right. The second part allows the use of pointers but for now, I am only subject to "subscripting" (indices?). Any advice on how to proceed?

I have successfully completed the first part thanks to Volkan İlbeyli. I am now moving on to the second part:

I need to write a program that does a large number of references to elements of a two-dimensional array, using pointers and pointer arithmetic. Here's what I have so far:

#include <iostream>
#include <time.h>
using namespace std;

void my_func();

int main()
{

    float start = clock();

    for (int i = 0; i < 100000; i ++)
    {   
        my_func();
    }

    float end = clock();
    cout << "Ending time of clock: " << (end - start) / ((double)CLOCKS_PER_SEC);
}

void my_func()
{
    int my_array[10][10];

    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            *(my_array+i+j);
        }
    }
}

I have done the first part and now I am working on the next part. I just want to know if I've missed anything. The code works fine and so does the program. Pointers are not my strongpoint and it took me a lot of time on the Internet to find my answers. Asking for a technical standpoint now on pointers and "pointer arithmetic".

tshepang
  • 12,111
  • 21
  • 91
  • 136
  • 2
    I don't get it. Can you please rephrase? – user703016 Apr 19 '13 at 23:25
  • Is your program going to be doing anything with these "large number of references"? Why must you use "only subscripting"? What's wrong with using pointers? – Dour High Arch Apr 19 '13 at 23:26
  • Can you clarify what you mean by, "only come up with using pointers"? You should be able to access the array elements by array indices: `my_array[i][j] = x;` – Mark Wilkins Apr 19 '13 at 23:26
  • Here is what I must do: Write a C (C++) program that does a large number of references to elements of two-dimensional arrays, using only subscripting. Write a second program that does the same operations but uses pointers and pointer arithmetic for the storage-mapping function to do the array references. Compare the time efficiency of the two programs. Which of the two programs is likely to be more reliable? Why? Explain the results. – user2203636 Apr 19 '13 at 23:38
  • I'm concerned with getting the first part correct before moving onto the other. I'm concerned with "writing a program that does a large number of references to elements of two-dimensional arrays, using only subscripting" at the moment. I will worry about the other part after I get the first part right. – user2203636 Apr 19 '13 at 23:40
  • Does the program have to do anything else? – OGH Apr 19 '13 at 23:42
  • Define a large amount of references? :) Both methods will invloce walking through your 2-dim array, probably with nested for-loops. One will use the indexes of the for-loops and array subscripts, the other will involve ++'ing a pointer along in the loop and reading the value with *. – Michael Dorgan Apr 19 '13 at 23:48
  • You need to ask your teacher or teaching assistant for help. The difference between accessing by subscript and pointer should be clear to you if you got this homework, and if it isn't you need additional tutoring. Your tuition money is paying for just that education and additional help; you should make them earn it - it seems they haven't so far. – Ken White Apr 20 '13 at 00:29

1 Answers1

1

Well, even though I could not understand the purpose or the meaning of the task you are attempting, if I didn't get it wrong, you are asked to reach to the elements of the 2D array by using indices.

Considering your code, in your my_func() you do not access to the elements. You only declare that you will use a 2D array with size 5x5. To access the elements, (i think) in your case to reference the arrays, you should use for loops and indices to access the elements of the array.

I should go like this in your case:

void my_func()
{                                   //assuming that the usage of
    int my_array[1500][500] = {0};  //square matrice is not mandatory

    for(int i=0; i<1500 ; i++){
        for(int j=0; j<500 ; j++){
            my_array[i][j]; //no operation is done, only accessing the element
        }
    }

    return;
}

You could also swap the for loops to access the array in a vertical manner, i.e. going through first column, then 2nd, then 3rd... That will make your referencing a 2D array by indices slower. See this post on SO why it slows down to swap the for loops, i.e. changing the row-column order into column-row.

You should also note that if you are concerned with critical time measuring of a certain event, you should include only that event when starting the clock and ending the clock. In your code, you also include the time to call cout, endl, and the calling time of my_func() which has nothing to do with the array referencing. Also I'd rather measure the time in the main() function since the accessing the 2D array doesn't require too much code. If it did require too much time, I'd call the function, declare the timing variables inside, definitely not print the starting time, start the time before the repeat-the-operations loop and stop after the loop terminates. You can see this post to have an idea about how time is measured and printed (when you need to consider seconds, or milliseconds etc.).

Community
  • 1
  • 1
Varaquilex
  • 3,447
  • 7
  • 40
  • 60