10 Replies - 1503 Views - Last Post: 05 December 2012 - 12:58 AM Rate Topic: -----

#1 Reynolds773  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-October 12

Need help using Arrays in function prototypes and definitions

Posted 04 December 2012 - 03:16 PM

I am doing a lab assignment using arrays. If you read the directions, it says I need to replace both statements with one function. What statements are they referring to? The "if" and "while"? I'm also unsure of what to put in the parameters of the function. It says to put one employeeId in for the parameters, but I am not told to make a variable called employeeId. I only have an array.

/*  TODO (STEP 1):

                Replace both statements with one function call to
                InputEmployeeWage, where the next employee id in
                employeeIds array is passed to the function and
                the return value is assigned to the corresponding
                employee's wage in employeeWages array
        */

        cout << "Enter wages for employee "
             << employeeIds[count] << ": ";

        cin >> employeeWages[count];

        


        if( count < MAX_EMP )
        {
            cout << "\nAnother (y or n): ";
            cin >> response;
            response = toupper(response);
        }

    }while(response == 'Y' && count < MAX_EMP);

    cout << fixed << showpoint << setprecision(2);
    cout << "\n ID     Wage" << endl;



Here is how the function is supposed to work:

/*  TODO (STEP 1):  Define InputEmployeeWage function

    Function to prompt and input wages for one employee.

    Parameters:
        in: empId
    Precondition:
        empId is id for employee whose wages will be input
    Postcondition:
        returns a double value for input wages
*/


I may need help on writing the other functions, but I want to see if I can do it after I get this one down.

Here is the previous part of the program:

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

/*  TODO: Declare function prototypes during each step.

    NOTE: See comments following main() for function specifications
          regarding return types, function names, and parameter lists.
*/
void InputEmployeeWage(???????);
void GetTotalWages(double employeeWages[], int);
void GetLargestValue(double employeeWages[], int);
void GetIndexOfSmallest(double employeeWages[], int);
void PrintEmployeeList(int employeeIds[], double employeeWages[], int);


//------------------------------------------------------------------------------

int main()
{
    const int MAX_EMP = 5;

    int employeeIds[MAX_EMP] = { 1001, 1002, 1003, 1004, 1005 };
    double employeeWages[MAX_EMP];

    char response;
    int count = 0;
    do
    {

This post has been edited by Reynolds773: 04 December 2012 - 03:17 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Need help using Arrays in function prototypes and definitions

#2 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 803
  • View blog
  • Posts: 2,372
  • Joined: 20-March 10

Re: Need help using Arrays in function prototypes and definitions

Posted 04 December 2012 - 03:28 PM

No,

You have to create a function called InputEmployeeWage

and it should replace the cout, cin combination

declaration


double InputEmployeeWage(int employeeIds[]);



any clearer ?


Snoopy.
Was This Post Helpful? 1
  • +
  • -

#3 Reynolds773  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-October 12

Re: Need help using Arrays in function prototypes and definitions

Posted 04 December 2012 - 03:38 PM

Okay, that's what I originally did, but was unsure. My function is still wrong though. I am told to get the wage for ONE employee. Is it okay I have a loop here then? I know this will not work, because count will always be 0.

double InputEmployeeWage(int employeeIds[])
	{
		int count = 0;
		double employeeWages;
		cout << "Enter wages for employee "
             << employeeIds[count] << ": ";

        cin >> employeeWages;
		count++;
	}

Was This Post Helpful? 0
  • +
  • -

#4 Reynolds773  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-October 12

Re: Need help using Arrays in function prototypes and definitions

Posted 04 December 2012 - 04:16 PM

How can I get the function to print the employee ID without referencing which position in the parameter? This is where I'm confused.

This post has been edited by Reynolds773: 04 December 2012 - 04:17 PM

Was This Post Helpful? 0
  • +
  • -

#5 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 803
  • View blog
  • Posts: 2,372
  • Joined: 20-March 10

Re: Need help using Arrays in function prototypes and definitions

Posted 04 December 2012 - 04:18 PM

Hi


No dont do that...

here... consider this example

#include <iostream>

using namespace std;
int enter()
{
    int temp;
    cout << "Enter a number for employee" << endl;
    cin >> temp;

    return temp;
}
int main()
{
    int  i =0;

    int people[100]={};
    do {

    people[i] = enter();



     i++;
    }while(people[i-1]!=999);

    for(int x = 0; x <i-1; x++)
    {
      cout << people[x]<<endl;
    }

    return 0;
}

Any clearer..?

Snoopy.


Was This Post Helpful? 0
  • +
  • -

#6 Reynolds773  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-October 12

Re: Need help using Arrays in function prototypes and definitions

Posted 04 December 2012 - 04:28 PM

Yes, I can see how that works, but I'm not sure that helps my situation. I have to make the program display the ID number for each employee when I enter their wage

Like so:

/*Enter wages for employee 1001: 10

Another (y or n): y
Enter wages for employee 1002: 20.50

Another (y or n): y
Enter wages for employee 1003: 15.25

Another (y or n): y
Enter wages for employee 1004: 24.10

Another (y or n): y
Enter wages for employee 1005: 9.95*/

This post has been edited by Reynolds773: 04 December 2012 - 04:28 PM

Was This Post Helpful? 0
  • +
  • -

#7 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 803
  • View blog
  • Posts: 2,372
  • Joined: 20-March 10

Re: Need help using Arrays in function prototypes and definitions

Posted 04 December 2012 - 04:30 PM

Ahh nuts ,

made a mistake myself now your declarartion should be


double InputEmployeeWage(int employeeIds);

and you pass an individual part of eemployeeIds as an int

like so

double result = InputEmployeeeWage(employeeIds[count]);

Sorry


I have a bad head cold


Snoopy.
Was This Post Helpful? 1
  • +
  • -

#8 Reynolds773  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-October 12

Re: Need help using Arrays in function prototypes and definitions

Posted 04 December 2012 - 04:55 PM

It's okay! I'm at work now, so I'll take a look at your suggestion when I get home. Thanks in advance!
Was This Post Helpful? 0
  • +
  • -

#9 Reynolds773  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-October 12

Re: Need help using Arrays in function prototypes and definitions

Posted 04 December 2012 - 09:41 PM

Ehhh, I've run into more problems. I'm stuck on the GetTotalWages function. I have to find the average wage which is (total/count), so either count or total is incorrect. Here is my code for that function.

EDIT: I now see that I have "double totalWages = 0;" How can I initialize that without it being a pass by reference?

Prototype:
double GetTotalWages(double employeeWages[], int);



Function Definition:
/*  TODO (STEP 2):  Define GetTotalWages function

    Function to find the sum of wages for employees.

    Parameters:
        in: empWages    array containing wages type double
        in: count       integer count of how many array elements to sum
    Precondition:
        empWages contains values for employees who have been assigned wages.
        count is >= 0 AND <= size of array
    Postcondition:
        returns a double value for sum of count wages
*/
	double GetTotalWages(double employeeWages[], int count)
	{
		double totalWages = 0;

		for(count = 0; count <= 5; count++)
		{
			totalWages += employeeWages[count];
		}

		return totalWages;
	}



My sad output :(/>

Enter wages for employee 1001: 10

Another (y or n): y
Enter wages for employee 1002: 20.50

Another (y or n): y
Enter wages for employee 1003: 15.25

Another (y or n): y
Enter wages for employee 1004: 24.10

Another (y or n): y
Enter wages for employee 1005: 9.95

ID Wage

Average Wage: 0.00

Largest wage found is $0.00
Smallest wage found is for employee 1001: $-925596313493178300000000000000000000
00000000000000000000000000.00

End Program - Press any key to continue . . .

This post has been edited by Reynolds773: 04 December 2012 - 09:47 PM

Was This Post Helpful? 0
  • +
  • -

#10 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 803
  • View blog
  • Posts: 2,372
  • Joined: 20-March 10

Re: Need help using Arrays in function prototypes and definitions

Posted 04 December 2012 - 10:49 PM

Hi,

No,

You have your variable count passed to the function so
you can count up to count..

like this

for(int temp = 0;temp < count;temp++)
    {



then use variable temp inside your array

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#11 Reynolds773  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-October 12

Re: Need help using Arrays in function prototypes and definitions

Posted 05 December 2012 - 12:58 AM

I had to turn it in unfortunately :/ I will take a look at it tomorrow though so I can say I finished it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1