c++ - Part 1: Referencing an Array Without Using Pointers and only Subscripting | Part 2: "..." with Pointers -


#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;      (int = 0; < 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 write program large number of references elements of two-dimensional array, using subscripting. two-part project, i'm concerned getting first part right. second part allows use of pointers now, subject "subscripting" (indices?). advice on how proceed?

i have completed first part volkan İlbeyli. moving on second part:

i need write program large number of references elements of two-dimensional array, using pointers , pointer arithmetic. here's have far:

#include <iostream> #include <time.h> using namespace std;  void my_func();  int main() {      float start = clock();      (int = 0; < 100000; ++)     {            my_func();     }      float end = clock();     cout << "ending time of clock: " << (end - start) / ((double)clocks_per_sec); }  void my_func() {     int my_array[10][10];      (int = 0; < 10; i++)     {         (int j = 0; j < 10; j++)         {             *(my_array+i+j);         }     } } 

i have done first part , working on next part. want know if i've missed anything. code works fine , program. pointers not strongpoint , took me lot of time on internet find answers. asking technical standpoint on pointers , "pointer arithmetic".

well, though not understand purpose or meaning of task attempting, if didn't wrong, asked reach elements of 2d array using indices.

considering code, in my_func() not access elements. declare use 2d array size 5x5. access elements, (i think) in case reference arrays, should use for loops , indices access elements of array.

i should go in case:

void my_func() {                                   //assuming usage of     int my_array[1500][500] = {0};  //square matrice not mandatory      for(int i=0; i<1500 ; i++){         for(int j=0; j<500 ; j++){             my_array[i][j]; //no operation done, accessing element         }     }      return; } 

you swap loops access array in vertical manner, i.e. going through first column, 2nd, 3rd... make referencing 2d array indices slower. see this post on so why slows down swap loops, i.e. changing row-column order column-row.

you should note if concerned critical time measuring of event, should include event when starting clock , ending clock. in code, include time call cout, endl, , calling time of my_func() has nothing array referencing. i'd rather measure time in main() function since accessing 2d array doesn't require code. if did require time, i'd call function, declare timing variables inside, not print starting time, start time before repeat-the-operations loop , stop after loop terminates. can see this post have idea how time measured , printed (when need consider seconds, or milliseconds etc.).


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -