Jump to content

create an array and return the address of the cell

- - - - -

  • Please log in to reply
4 replies to this topic

#1
alirezan

alirezan

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
Hi
I have an array of 4x2. I want to write a function that simply writes to a cell in the array (say cell 3) and returns the address of the cell that was just written to.

Here is the application. I have a program that uses arrays to store information and during runtime, I want to iterate through the cells of the array find which cell is empty and not used (one cell is used as flag, when used, it will be set to 1). This function should be able to return the address of the cell used to the calling function so that next time it want to change something in this cell it knows the address and can pass it to another function and modify the value. Here are sample function protos

int Handle;

Handle setVal (int initialVal); //Returns an address to the cell that was just written
Bool modVal (int Handle); //Uses the handle to modify the cell pointed to by it.

thanks

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
What do you have so far?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
alirezan

alirezan

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
I have the code that just returns the cell number (i.e. 1-2-3):

Quote

for (int i=0; i<= 3; i++)
if ( tarr[i][0] == FOREVER ) {
tarr [i][0] = exptime;
tarr [i][1] = startTime;
return i;
}
return NO_ROOM;
}

Thanks

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Let me rephrase: what do you have so far that stands a chance of being compiled?

The code you just posted has 5 undeclared variables, no main() function, etc. Nobody can help you with that. Further, it doesn't have either of the functions you said you want to implement.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
notes

notes

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts
Hi,
I ve read your post and I tried to do this for myself, just out of boredom.
It was preety simple the hardest part for me was to figure out index elements :)
I want to assure, I AM BEGGINIER programmer so this code is not perfect and there are probbably 1000 ways to do it better but having this simple code all in main will help you to change it to a function or whatever u want. If you want you can share opinion about it.

#include <cstdlib>

#include <iostream>


using namespace std;


int main(int argc, char *argv[])

{

    

    int r,c;

    char v;

    cout << "Give size of an aray :\nRows : "; 

    cin >> r; // get how many rows

    cout << "\nColumns : ";

    cin >> c; cout << endl; //get how many columns

    //Create own size MxN array filled with '0' you can also use random() to get random numbers.

    int tab[r][c];

    for (int i=0;i<r;i++)

    {

        for (int j=0;j<c;j++)

        {

            tab[i][j]=0;       

        }

    }

    do //Now we will show array and ask which cell will be changed. 

    {

       cout <<"\nNow your array looks :\n"; //Showing current array;

       for (int i=0;i<r;i++)

      { 

        cout <<"|";

        for (int j=0;j<c;j++)

        {

          cout <<" "<<tab[i][j];    

        }  

        cout <<" |\n"; 

      } 

      cout << "\nDo you want to change any cell ?  <y/n>";

      cin >>v; // Chccking if you want to do some changes

      if ((v=='y')||(v=='Y'))

      {

        cout << "\nWhich one ?\nRow: ";

        int k; //You might want to do some loop to see if given row and column is not out of array range.

        cin >> k; k=k-1; // this is because array index really starts from 0, the first element adress is [0,0] not 1,1 :)

        cout << "\nColumn: ";

        int l;

        cin >> l; l=l-1; // same as above

        cout << "\nWhat element should I assign ? ";

        int el;

        cin >> el;

        tab[k][l]=el;

        

      }

      else if ((v=='n')||(v=='N'))

      {

       system("PAUSE");

       return EXIT_SUCCESS;                  

      }

      else

      {

      cout <<"\n!!Error - Wrong chocie!!\n"; //Case someone hit wrong key.

      

      }

    }

    while(true);


}





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users