473,405 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Pass a struct array to a function

Hey everyone. I need to pass a struct array to a function, and calculate the average of one of the members of the struct. My code is posted below.
With the current code, it takes the last value of the TotalPay variable and divides it by 25. I need to add the entire TotalPay column, and then divide by 25.

There is some info commented out, which I have used for my own troubleshooting.

Thanks,
J
Expand|Select|Wrap|Line Numbers
  1. #include <fstream>
  2. #include <iostream>
  3. #include <iomanip>
  4. using namespace std;
  5. struct Person
  6. {
  7.     double IDNumber,HoursWorked,PayRate,TotalPay[26];
  8.     char FirstName[10],LastName[10];
  9. };
  10. void Output (Person Person_Info[])
  11. {
  12.     cout<<"LastName"<<"    "<<setw(10)<<"IDNumber"<<"  "<<setw(10)<<"TotalPay"<<endl;
  13.     for (int i=0;i<25;++i)
  14.     {
  15.         cout<<setw(6)<<Person_Info[i].LastName<<setw(14)<<Person_Info[i].IDNumber<<setw(12)<<setprecision(5)<<showpoint<<Person_Info[i].TotalPay[0]<<endl;
  16.     }
  17. }
  18. double Average (Person Person_Info[])
  19. {
  20.     double sum=0.0,avg=0.0;
  21.     int j=0;
  22.  
  23.     for (j=0;j<25;++j)
  24.     {
  25.         sum=sum+Person_Info[j].TotalPay[j];
  26.     }
  27.     avg=sum/25;
  28.     cout<<avg<<endl;
  29.     return (avg);
  30. }
  31. int main ()
  32. {
  33.     int i=0;
  34.     Person Person_Info[26]={0};
  35.     ifstream ifp;
  36.     ofstream ofp;
  37.     ifp.open("C:\\Documents and Settings\\Jeff Young\\My Documents\\Visual Studio Projects\\Lab 3 External Files and Structs\\Lab2.in");
  38.     ofp.open("C:\\Pay.out");
  39. //    cout<<"FirstName"<<" "<<"LastName"<<" "<<"IDNumber"<<" "<<"HoursWorked"<<" "<<"PayRate"<<" "<<"TotalPay"<<endl;
  40. //    cout<<"LastName"<<"     "<<"IDNumber"<<"     "<<"TotalPay"<<endl;
  41.     ofp<<"LastName"<<"     "<<"IDNumber"<<"     "<<"TotalPay"<<endl;
  42.     while (ifp>>Person_Info[i].FirstName>>Person_Info[i].LastName>>Person_Info[i].IDNumber>>Person_Info[i].HoursWorked>>Person_Info[i].PayRate)
  43.     {
  44.         if (Person_Info[i].HoursWorked<=40&&Person_Info[i].HoursWorked>=0)                    
  45.         {
  46.             Person_Info[i].TotalPay[i]=(Person_Info[i].HoursWorked*Person_Info[i].PayRate);                
  47.         }
  48.         else if (Person_Info[i].HoursWorked>40)                        
  49.         {
  50.             Person_Info[i].TotalPay[i]=(40*Person_Info[i].PayRate)+(Person_Info[i].HoursWorked-40)*(Person_Info[i].PayRate*1.5);
  51.         }
  52.     //    ofp<<Person_Info[i].LastName<<"     "<<Person_Info[i].IDNumber<<"     "<<Person_Info[i].TotalPay[i]<<endl;
  53.     //    cout<<Person_Info[i].FirstName<<"     "<<Person_Info[i].LastName<<"     "<<Person_Info[i].IDNumber<<"    "<<Person_Info[i].HoursWorked<<"     "<<Person_Info[i].PayRate<<"     "<<Person_Info[i].TotalPay[i]<<endl;
  54.     //    Average(Person_Info);
  55.     }
  56.     Average(Person_Info);
  57. //    Output (Person_Info);
  58.  
  59. }
  60.  
Jun 14 '07 #1
8 2497
Savage
1,764 Expert 1GB
Here is your problem..

Expand|Select|Wrap|Line Numbers
  1. double Average (Person Person_Info[])
  2. {
  3.     double sum=0.0,avg=0.0;
  4.     int j=0;
  5.  
  6.     for (j=0;j<25;++j)
  7.     {
  8.         sum=sum+Person_Info[j].TotalPay[j];//problem is here
  9.     }
  10.     avg=sum/25;
  11.     cout<<avg<<endl;
  12.     return (avg);
  13. }
This function will take from every Person_Info just one TotalPay.

You will need to add another loop inside.

Expand|Select|Wrap|Line Numbers
  1. for(j=0;j<25;j++)
  2. {
  3.      for(i=0;i<25;i++) sum+=Person_Info[j].TotalPay[i];//added another loop
  4. }
Savage
Jun 14 '07 #2
That still returns the last value of the TotalPay divided by 25. So the same answer as without that loop.

Thanks,
J
Jun 14 '07 #3
Savage
1,764 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. int i=0;
  2.  
  3. while (ifp>>Person_Info[i].FirstName>>Person_Info[i].LastName>>Person_Info[i].IDNumber>>Person_Info[i].HoursWorked>>Person_Info[i].PayRate)
  4.     {
  5.         if (Person_Info[i].HoursWorked<=40&&Person_Info 
  6.         [i].HoursWorked>=0)          
  7.         {
  8.             Person_Info[i].TotalPay[i]=(Person_Info[i].HoursWorked*Person_Info
  9.             [i].PayRate);    
  10.         }
  11.         else if (Person_Info[i].HoursWorked>40)      
  12.         {
  13.             Person_Info[i].TotalPay[i]=(40*Person_Info[i].PayRate)+(Person_Info
  14.             [i].HoursWorked-40)*(Person_Info[i].PayRate*1.5);
  15.         }
  16.        //  ofp<<Person_Info[i].LastName<<"     "<<Person_Info
  17.        [i].IDNumber<<"     "<<Person_Info[i].TotalPay[i]<<endl;
  18.        //  cout<<Person_Info[i].FirstName<<"     "<<Person_Info
  19.        [i].LastName<<"     "<<Person_Info[i].IDNumber<<"    "<<Person_Info
  20.        [i].HoursWorked<<"     "<<Person_Info[i].PayRate<<"     "<<Person_Info
  21.        [i].TotalPay[i]<<endl;
  22.     //  Average(Person_Info);
  23.     }
I don't see that u are increasing counter i here.If u don't increase him data will be owerwriten and only last TotalPay will remain.Also whay is Totalpay array,when every time u are writing over secound person.



Savage
Jun 14 '07 #4
Awesome. That was it, I needed to increment i. Thanks a ton, I've been staring at this program all day.


Thanks again,

J
Jun 14 '07 #5
found the answer to this question too.


thanks again.
j
Jun 14 '07 #6
Savage
1,764 Expert 1GB
Awesome. That was it, I needed to increment i. Thanks a ton, I've been staring at this program all day.


Thanks again,

J
I'm more than happy to help you..

Savage
Jun 14 '07 #7
Well I'd like to ask a quick question if it's ok. When I had the code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. while (ifp>>Person_Info[i].FirstName>>Person_Info[i].LastName>>Person_Info[i].IDNumber>>Person_Info[i].HoursWorked>>Person_Info[i].PayRate)
  3.     {
  4.         if (Person_Info[i].HoursWorked<=40&&Person_Info[i].HoursWorked>=0)                    
  5.         {
  6.             Person_Info[i].TotalPay[i]=(Person_Info[i].HoursWorked*Person_Info[i].PayRate);                
  7.         }
  8.         else if (Person_Info[i].HoursWorked>40)                        
  9.         {
  10.             Person_Info[i].TotalPay[i]=(40*Person_Info[i].PayRate)+(Person_Info[i].HoursWorked-40)*(Person_Info[i].PayRate*1.5);
  11.         }
  12.         ofp<<Person_Info[i].LastName<<"     "<<Person_Info[i].IDNumber<<"     "<<Person_Info[i].TotalPay[i]<<endl;
  13.         cout<<Person_Info[i].FirstName<<"     "<<Person_Info[i].LastName<<"     "<<Person_Info[i].IDNumber<<"    "<<Person_Info[i].HoursWorked<<"     "<<Person_Info[i].PayRate<<"     "<<Person_Info[i].TotalPay[i]<<endl;
  14.  

I didn't need to increment i because it was in the while loop???

This is just for my understanding.

Thanks,
J
Jun 14 '07 #8
Savage
1,764 Expert 1GB
No,you don't need to worry about counter incrementation only in for loop(look at for loop syntax),in do-while and while you must increase it manualy..

Savage
Jun 14 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
8
by: Blue Ocean | last post by:
I know this is somewhat dependent on the circumstances, but let me ask anyway. Suppose I have a 100 byte struct or array or something like that. Which would be more efficient? void...
3
by: J | last post by:
I'm at a loss on how to accomplish one item with C# entirely in managed code -- I'd like to make a call to a Win32 function, one of its parameters is a structure that contains a pointer to one or...
2
by: 知易行难 | last post by:
sorry i am poor in English. i am trying to call functions from a "dll"(maybe developed in C++), there is a struct in this function,which is defined like this: typedef struct{ .......... BYTE *...
17
by: Jellicle | last post by:
There are some sturct arrays (or struct vectors), such as: struct S1_t { double keyField ; ..// other fields } S1_t s1;
3
by: bhejafry.mmk | last post by:
hey .. i've been tryin this for a long time now .. jow the bloody hell do i pass an array to the function .. also .. how do i get the function to return an array ... all in C language ... please...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
28
by: Bill | last post by:
Hello All, I am trying to pass a struct to a function. How would that best be accomplished? Thanks, Bill
29
by: Why Tea | last post by:
Suppose you have a 2-dimensional array (matrix) in main() and you want to pass it to a function to do some processing, you usually pass it as a pointer to the first element. But, from the function,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.