need to pass and return arrays, how?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

need to pass and return arrays, how?

 
0
  #1
Jul 18th, 2006
I'm trying to return a struct array from two functions. I don't know what to say. Here is my code:

#include <iostream>
#include <iomanip>
using namespace std;

struct worker {
    int    idNumber;
    int    hoursWorked;
    double hourlyRate;
    double earned;
};

???  input();
???  calculateEarned(worker []);
void outputWorkers(worker []);
int  overtimeWorkers(worker []);

const double OVERTIME_RATE = 1.5;

int main() {

    outputWorkers(calculateEarned(input()));
    

    return 0;
    
}

??? input() {

    int a;
    cout << "Enter number of workers: ";
    cin >> a;
    cout << endl;
    static worker w[a]; // is this right??
    return w;

}

??? calculateEarned(worker w[]) {


    cout << "enter the ID numbers of the " << sizeof(w) << " workers: ";

    for(int i = 0; i < sizeof(w); i++)
        cin >> w[i].idNumber;
    
    cout << endl << "enter the hours worked for the " << sizeof(w) << " workers: ";
    
    for(int k = 0; k < sizeof(w); k++)
        cin >> w[k].hoursWorked;
    
    cout << endl << "enter the base pay for each of the " << sizeof(w) << " workers: ";
    
    for(int a = 0; a < sizeof(w); a++)
        cin >> w[a].hourlyRate;
    
    return w;
}

void outputWorkers(worker w[]) {

    cout << setw(10) << "worker" << setw(10) << "rate" << setw(10) << "hours" << setw(10) << "pay"
         << endl << endl;
    
    for(int i = 0; i < sizeof(w); i++) {
    
    if(w[i].hoursWorked > 40)
        w[i].earned = static_cast<double>(w[i].hoursWorked - 40) * w[i].hourlyRate * OVERTIME_RATE
                      + static_cast<double>(40) * w[i].hourlyRate;
    else
        w[i].earned = static_cast<double>(w[i].hoursWorked) * w[i].hourlyRate;
    
        cout << fixed << setprecision(2)
             << setw(10) << w[i].idNumber
             << setw(10) << w[i].hourlyRate
             << setw(10) << w[i].hoursWorked
             << setw(10) << w[i].earned
             << endl;
    }
}

int overtimeWorkers(worker w[]) {

    int count;    
    for(int i = 0; i < sizeof(w); i++)
        if(w[i].hoursWorked > 40)
            count = i + 1;
    
    return count;
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,596
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1488
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: need to pass and return arrays, how?

 
0
  #2
Jul 18th, 2006
there are two ways to declare a one-dimentional array

(1)??? calculateEarned(worker array[]);

and
(2)??? calculateEarned(worker * array);

but there is only one way to declare the return value, using a pointer to the beginning of the array.
worker* calculateEarned(worker []);
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Re: need to pass and return arrays, how?

 
0
  #3
Jul 18th, 2006
ok, thanks, i will work on it
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Re: need to pass and return arrays, how?

 
0
  #4
Jul 18th, 2006
to simplify:
  1. worker* input() {
  2.  
  3. int a;
  4. cout << "Enter number of workers: ";
  5. cin >> a;
  6. cout << endl;
  7. worker w[a]; // is this right??
  8. return w;
  9.  
  10. }
  11.  
  12. and then, say: <span class="ad_notxt"><code class="inlinecode">nextCall(input());</code></span>
how does this work?

I'm getting "returning a local variable" errors, and "size of w[] is not constant," and other, more inscrutable errors.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Re: need to pass and return arrays, how?

 
0
  #5
Jul 18th, 2006
here are the errors of the following code..
workerlist.cpp: In function ‘int main()’:
workerlist.cpp:24: error: ‘w’ was not declared in this scope
jess@linux:~/cpp/labs10+11/Lab#10$ g++ workerlist.cpp
workerlist.cpp: In function ‘int main()’:
workerlist.cpp:24: error: ‘w’ was not declared in this scope
workerlist.cpp: In function ‘void input()’:
workerlist.cpp:39: error: storage size of ‘w’ isn't constant
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. struct worker {
  6. int idNumber;
  7. int hoursWorked;
  8. double hourlyRate;
  9. double earned;
  10. };
  11.  
  12. void input();
  13. void calculateEarned(worker []);
  14. void outputWorkers(worker []);
  15. int overtimeWorkers(worker []);
  16.  
  17. const double OVERTIME_RATE = 1.5;
  18.  
  19. int main() {
  20.  
  21. input();
  22. calculateEarned(w);
  23. outputWorkers(w);
  24. cout << overtimeWorkers(w) << " workers with overtime hours" << endl;
  25.  
  26.  
  27. return 0;
  28.  
  29. }
  30.  
  31. void input() {
  32.  
  33. int a;
  34. cout << "Enter number of workers: ";
  35. cin >> a;
  36. cout << endl;
  37. static worker w[a]; // is this right??
  38.  
  39. }
  40.  
  41. void calculateEarned(worker w[]) {
  42.  
  43.  
  44. cout << "enter the ID numbers of the " << sizeof(w) << " workers: ";
  45.  
  46. for(int i = 0; i < sizeof(w); i++)
  47. cin >> w[i].idNumber;
  48.  
  49. cout << endl << "enter the hours worked for the " << sizeof(w) << " workers: ";
  50.  
  51. for(int k = 0; k < sizeof(w); k++)
  52. cin >> w[k].hoursWorked;
  53.  
  54. cout << endl << "enter the base pay for each of the " << sizeof(w) << " workers: ";
  55.  
  56. for(int a = 0; a < sizeof(w); a++)
  57. cin >> w[a].hourlyRate;
  58.  
  59. }
  60.  
  61. void outputWorkers(worker w[]) {
  62.  
  63. cout << setw(10) << "worker" << setw(10) << "rate" << setw(10) << "hours" << setw(10) << "pay"
  64. << endl << endl;
  65.  
  66. for(int i = 0; i < sizeof(w); i++) {
  67.  
  68. if(w[i].hoursWorked > 40)
  69. w[i].earned = static_cast<double>(w[i].hoursWorked - 40) * w[i].hourlyRate * OVERTIME_RATE
  70. + static_cast<double>(40) * w[i].hourlyRate;
  71. else
  72. w[i].earned = static_cast<double>(w[i].hoursWorked) * w[i].hourlyRate;
  73.  
  74. cout << fixed << setprecision(2)
  75. << setw(10) << w[i].idNumber
  76. << setw(10) << w[i].hourlyRate
  77. << setw(10) << w[i].hoursWorked
  78. << setw(10) << w[i].earned
  79. << endl;
  80. }
  81. }
  82.  
  83. int overtimeWorkers(worker w[]) {
  84.  
  85. int count;
  86. for(int i = 0; i < sizeof(w); i++)
  87. if(w[i].hoursWorked > 40)
  88. count = i + 1;
  89.  
  90. return count;
  91. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,642
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 472
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: need to pass and return arrays, how?

 
0
  #6
Jul 18th, 2006
Error Returning a local variable

You are gettin this error since you are trying to return worker type array "w" which is local to the function and hence is destroyed when the function exits. Said simply the scope or the effect of the local variable is in the function itself since its memory is allocated in the stack of local variables which is destroyed when the function exits.

Errror size of w[] not constant

In C or C++ u cant declare an array like this i.e. an array whose size the compiler has to calculate at run time. C expects that you explicitly declare the size of the array during compile time.

Hence the acceptable options are

worker w [10];

OR

const int numberOfItems = 10;
worker w [numberOfItems];

Bye.
Last edited by ~s.o.s~; Jul 18th, 2006 at 4:38 pm.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Re: need to pass and return arrays, how?

 
0
  #7
Jul 18th, 2006
alright.. i had to rearrange all the functions to accommodate that fact, but it's done now. forget about returning pointers, for now..
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC