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 -
#include <fstream>
-
#include <iostream>
-
#include <iomanip>
-
using namespace std;
-
struct Person
-
{
-
double IDNumber,HoursWorked,PayRate,TotalPay[26];
-
char FirstName[10],LastName[10];
-
};
-
void Output (Person Person_Info[])
-
{
-
cout<<"LastName"<<" "<<setw(10)<<"IDNumber"<<" "<<setw(10)<<"TotalPay"<<endl;
-
for (int i=0;i<25;++i)
-
{
-
cout<<setw(6)<<Person_Info[i].LastName<<setw(14)<<Person_Info[i].IDNumber<<setw(12)<<setprecision(5)<<showpoint<<Person_Info[i].TotalPay[0]<<endl;
-
}
-
}
-
double Average (Person Person_Info[])
-
{
-
double sum=0.0,avg=0.0;
-
int j=0;
-
-
for (j=0;j<25;++j)
-
{
-
sum=sum+Person_Info[j].TotalPay[j];
-
}
-
avg=sum/25;
-
cout<<avg<<endl;
-
return (avg);
-
}
-
int main ()
-
{
-
int i=0;
-
Person Person_Info[26]={0};
-
ifstream ifp;
-
ofstream ofp;
-
ifp.open("C:\\Documents and Settings\\Jeff Young\\My Documents\\Visual Studio Projects\\Lab 3 External Files and Structs\\Lab2.in");
-
ofp.open("C:\\Pay.out");
-
// cout<<"FirstName"<<" "<<"LastName"<<" "<<"IDNumber"<<" "<<"HoursWorked"<<" "<<"PayRate"<<" "<<"TotalPay"<<endl;
-
// cout<<"LastName"<<" "<<"IDNumber"<<" "<<"TotalPay"<<endl;
-
ofp<<"LastName"<<" "<<"IDNumber"<<" "<<"TotalPay"<<endl;
-
while (ifp>>Person_Info[i].FirstName>>Person_Info[i].LastName>>Person_Info[i].IDNumber>>Person_Info[i].HoursWorked>>Person_Info[i].PayRate)
-
{
-
if (Person_Info[i].HoursWorked<=40&&Person_Info[i].HoursWorked>=0)
-
{
-
Person_Info[i].TotalPay[i]=(Person_Info[i].HoursWorked*Person_Info[i].PayRate);
-
}
-
else if (Person_Info[i].HoursWorked>40)
-
{
-
Person_Info[i].TotalPay[i]=(40*Person_Info[i].PayRate)+(Person_Info[i].HoursWorked-40)*(Person_Info[i].PayRate*1.5);
-
}
-
// ofp<<Person_Info[i].LastName<<" "<<Person_Info[i].IDNumber<<" "<<Person_Info[i].TotalPay[i]<<endl;
-
// 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;
-
// Average(Person_Info);
-
}
-
Average(Person_Info);
-
// Output (Person_Info);
-
-
}
-
8 2497
Here is your problem.. - double Average (Person Person_Info[])
-
{
-
double sum=0.0,avg=0.0;
-
int j=0;
-
-
for (j=0;j<25;++j)
-
{
-
sum=sum+Person_Info[j].TotalPay[j];//problem is here
-
}
-
avg=sum/25;
-
cout<<avg<<endl;
-
return (avg);
-
}
This function will take from every Person_Info just one TotalPay.
You will need to add another loop inside. - for(j=0;j<25;j++)
-
{
-
for(i=0;i<25;i++) sum+=Person_Info[j].TotalPay[i];//added another loop
-
}
Savage
That still returns the last value of the TotalPay divided by 25. So the same answer as without that loop.
Thanks,
J
- int i=0;
-
-
while (ifp>>Person_Info[i].FirstName>>Person_Info[i].LastName>>Person_Info[i].IDNumber>>Person_Info[i].HoursWorked>>Person_Info[i].PayRate)
-
{
-
if (Person_Info[i].HoursWorked<=40&&Person_Info
-
[i].HoursWorked>=0)
-
{
-
Person_Info[i].TotalPay[i]=(Person_Info[i].HoursWorked*Person_Info
-
[i].PayRate);
-
}
-
else if (Person_Info[i].HoursWorked>40)
-
{
-
Person_Info[i].TotalPay[i]=(40*Person_Info[i].PayRate)+(Person_Info
-
[i].HoursWorked-40)*(Person_Info[i].PayRate*1.5);
-
}
-
// ofp<<Person_Info[i].LastName<<" "<<Person_Info
-
[i].IDNumber<<" "<<Person_Info[i].TotalPay[i]<<endl;
-
// 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;
-
// Average(Person_Info);
-
}
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
Awesome. That was it, I needed to increment i. Thanks a ton, I've been staring at this program all day.
Thanks again,
J
found the answer to this question too.
thanks again.
j
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
Well I'd like to ask a quick question if it's ok. When I had the code: -
-
while (ifp>>Person_Info[i].FirstName>>Person_Info[i].LastName>>Person_Info[i].IDNumber>>Person_Info[i].HoursWorked>>Person_Info[i].PayRate)
-
{
-
if (Person_Info[i].HoursWorked<=40&&Person_Info[i].HoursWorked>=0)
-
{
-
Person_Info[i].TotalPay[i]=(Person_Info[i].HoursWorked*Person_Info[i].PayRate);
-
}
-
else if (Person_Info[i].HoursWorked>40)
-
{
-
Person_Info[i].TotalPay[i]=(40*Person_Info[i].PayRate)+(Person_Info[i].HoursWorked-40)*(Person_Info[i].PayRate*1.5);
-
}
-
ofp<<Person_Info[i].LastName<<" "<<Person_Info[i].IDNumber<<" "<<Person_Info[i].TotalPay[i]<<endl;
-
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;
-
I didn't need to increment i because it was in the while loop???
This is just for my understanding.
Thanks,
J
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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 *...
|
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;
|
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...
|
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...
|
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
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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,...
|
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...
| |