1.

Solve : I need help with a C++ code?

Answer»

Hello everybody:

I´m a student of first year Networks Systems Management Course and I must to finish a little homework my TEACHER has said.
I have almost all the code written but there is a function I can´t do noting with it, for more and more I have tried to solution.

Please, I need to solve this. The code is that follows:

#include
#include
#include
#define MAX_TAM 1000
class array_c
{                             // (Author)
 int array [MAX_SZ];
 int num_elements;
 public:
 array_c(){num_elements=0;array[num_elements]=0;};
 void insert (int position, int element);
 void print ();
 void DELETE (int position);
 void sorting ();
 int search (int element);
 int operator [] (int position);  /*This is the line I dont know what to do with it . This lie in the fact you can pass the position of an element of the array, then the function give back the element of that position.*/
};

main ()
{
array_c array;
int element, position=0, k;
do
      {
      cout << "Put a number for the array " << "\n";
      cout << "(To finish put 0): ";
   fflush (stdin);
   cin >> element;
   cout << "\n";
   if (element!=0)
         {
            array.insert (position, element);
         position++;
      if (position >= MAX_SZ)
               {
             cout << "There is no room for more elements.";
             elemento=0;
            }
         }
       }
while (element!=0);
cout << "This is the array: " << "\n" ;
array.print();
cout << "\n\n";
cout << "Put the element position you want erase: ";
fflush (stdin);
cin >> position;
cout << "\n";
array.delete (position);
position--;
array.print();
cout << "\n\n";
cout << "Now, the elements of array are going to be sorted. ";
cout << "\n";
array.sorting();
array.print();
cout << "\n\n";
cout << "Put the number of the element you want to look for: ";
fflush (stdin);
cin >> element;
cout << "The position of this number is:" << array.buscar (elemento);
cout << "Put the position of the element ypou wanna look for in the array: ";
fflush (stdin);
cin >> position;
k=[](posicion);       //This is the wrong call to the function and the main problem of this code in C++
cout << "The number of this position is:" << k;
getch ();
return 0;
}

int array_c::operator[](int position)// And this would must be the function.
{
 return array [position-1];
}



void array_c::insert (int position, int element)
{
array[position]=element;
num_elements=position+1;
}

void array_c::delete (int position)
{
int i;
for (i=(position-1); i      {
      array=array[i+1];
   }
num_elements--;
}

void array_c::print ()
{
 int i;
 for (i=0;i<num_elements;i++)
 {
 printf ("%d, ", array);
 }
}

void array_c::sorting()
{
int aux, i, CHANGE;
do
       {
   change=0;
   for (i=0;i<num_elements;i++)
   if (array>array[i+1])
         {
       aux=array[i+1];
       array[i+1]=array;
       array=aux;
       change=1;
      }
       }
while (change==1);
}

int array_c::search (int element)
{
int i;
 for (i=0;i<num_elements;i++)
       {
       if (element==array)
             {
       return (i+1);
         }
      }
 if (i==num_elements)
       {
    cout << "Not exist this number in the array " <<"\n";
    return 0;
   }
}
Nobody can help me a little, please?you need to STATE what problem it is - more info is needed:
which part of the code do u think is faulty or needs replacing?
or rather which function are you not SURE of?Thanks very much for your response!!
Well, the problem is that follows:
I have written all the functions declared in header and the all the main code, but when I have arrived to this 'class array_c' line who say:

 int operator [] (int position);  

And I have written the function call I have understand is good:

k=[](position);

And the function itself:

int array_c::operator[](int position)// (And this would must be the function).
{
 return array [position-1];
}
Then, when I compile, Borland returns me a error message like this:

Call to undefined function what is refered to the 'k=[](posicion); ' line.

I have tried to change it like:

k= operator[](position);

But it doesn´t work neither.

I´m desperated.

Help please.



Sorry for the html code of this web, when I say:
int array_c::operator[](int position)// (And this would must be the function).

I want to say:

 int array_c::    operator[](int position)// (And this would must be the function).  



Discussion

No Comment Found