C++/c++

Advertisement


Question
QUESTION: my problem is the makeHeap function,i gt many errors,which i do nt understand.could you pls help mi.i am greatly appreciated for all the help from you.

wen i compile the errors are:
79:error:expected primary-expression before '['token
79:error:expected primary-expression before '['token
82:error:expected primary-expression before '['token
82:error:expected primary-expression before '['token
87:error:expected primary-expression before '['token
88:error:expected unqualified-id before '['token
89:error:expected unqualified-id before '['token
90:error:expected primary-expression before ')'token

code:
void makeHeap(int node, int num, sales *arr)
{    
  int curr, child, temp;
  int numlast = (num-2)/2;
  curr = node;
  if (node <= numlast)// if there's a child
  {
     child = 2*node+1;   // left child
     if (sales[node] < sales[child])//79          node = child;       
     child++; // is there a right child?
     if (child < num && sales[node] < sales[child])//82       node = child;
  }
     if (node != curr) // not a heap      
   {          
      temp = sales[node];        //87         
      sales[node] = sales[curr]; //88
      sales[curr] = temp;        //89         
      makeHeap(node, num, sales);//90     
   }
}

ANSWER: you have a struct, which, IIRC, is this:

struct sales
{
  char id[10];
  float sales;
};

I think much of the confusion that you have is stemming from the name of the struct being the same as that of a member of the struct.

Rewrite the struct as

struct sales_type
{
  char id[10];
  float sales_amount;
};


And now, in the function
void makeHeap( int node, int num, sales_type* arr ) ;

sales_type is a type, arr is a variable of type sales_type* (an array)

sales_type[node] makes no sense, just as int[34] makes no sense.

modify as

// if the sales amount of element at position node
// is less than the sales amount of element at position child

if ( arr[node].sales_amount < arr[child].sales_amount ) // 79

and so on for the rest of your code.






---------- FOLLOW-UP ----------

QUESTION: thankz veri much for all your help.i hv solve all the errors.nw my onli problem left is the function,void printList(sales *arr, int num).could you pls help mi with tis function,hw should i implement it.i hope you could help mi.i am greatly appreciated for all the help from you.  

Answer
To be const-correct,

void printList(sales *arr, int num) ; should be

void printList( const sales* arr, int num ) ;

http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.1
http://www.possibility.com/Cpp/const.html

To print the list, all you need to do is iterate through it, printing out element by element. Something like this, perhaps:

void printList( const sales* arr, int num )
{
   for( int i = 0 ; i < num ; ++i )
   {
       std::cout << std::setw(10) << sales[i].id
         << std::setw(12) << std::fixed << std::set[precision(2)
         << sales[i].sales_value << '\n' ;
   }
}  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.