Thread: Calculations Issue

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    56

    Calculations Issue

    This is its objective:

    It asks for the name and salary of a specific number of employees... Then it calculates and tells the user the average salary of the company, then it says who's the richest employee in the company;;;

    I cant figure it out how to make it work

    Code:
    #include <iostream>
    #include <string>
    #define COMPANY_SIZE 2
    
    using namespace std;
    
    struct employee
    {
        string name;
        float salary;
    };
    
    float getAverage (employee* e);
    int getRichest (employee* e); // returns richest employee's id
    
    int main()
    {
        employee emp[COMPANY_SIZE];
        for(int x=0; x < COMPANY_SIZE; x++)
        {
            cout<<"Enter Employee's Name: ";
            cin>>emp[x].name;
            cout<<"Enter Employee's Salary: U$ ";
            cin>>emp[x].salary;
            cout<<endl;
        }
    
        cout<<"The Average Salary in this company is U$ "<<getAverage(emp)<<endl;
        cout<<"The Richest Employee in this company is "<<emp[getRichest(emp)].name<<endl;
    
    }
    
    float getAverage(employee* e)
    {
        float accumulator=0;
        for(int x=0; x <= COMPANY_SIZE; x++)
        {
            accumulator = accumulator + e[x].salary;
        }
        return (accumulator / COMPANY_SIZE);
    }
    
    int getRichest(employee* e)
    {
        float highest;
        for(int x=0; x<=COMPANY_SIZE; x++)
        {
            highest = (x==0) ? x : highest;
            highest = (e[x].salary > highest) ? x : highest;
        }
        return highest;
    }
    BTW, any adivice on making the code better is welcome!

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by dhuan View Post
    This is its objective:

    It asks for the name and salary of a specific number of employees... Then it calculates and tells the user the average salary of the company, then it says who's the richest employee in the company;;;

    I cant figure it out how to make it work

    Code:
    #include <iostream>
    #include <string>
    #define COMPANY_SIZE 2
    
    using namespace std;
    
    struct employee
    {
        string name;
        float salary;
    };
    
    float getAverage (employee* e);
    int getRichest (employee* e); // returns richest employee's id
    
    int main()
    {
        employee emp[COMPANY_SIZE];
        for(int x=0; x < COMPANY_SIZE; x++)
        {
            cout<<"Enter Employee's Name: ";
            cin>>emp[x].name;
            cout<<"Enter Employee's Salary: U$ ";
            cin>>emp[x].salary;
            cout<<endl;
        }
    
        cout<<"The Average Salary in this company is U$ "<<getAverage(emp)<<endl;
        cout<<"The Richest Employee in this company is "<<emp[getRichest(emp)].name<<endl;
    
    }
    
    float getAverage(employee* e)
    {
        float accumulator=0;
        for(int x=0; x <= COMPANY_SIZE; x++)
        {
            accumulator = accumulator + e[x].salary;
        }
        return (accumulator / COMPANY_SIZE);
    }
    
    int getRichest(employee* e)
    {
        float highest;
        for(int x=0; x<=COMPANY_SIZE; x++)
        {
            highest = (x==0) ? x : highest;
            highest = (e[x].salary > highest) ? x : highest;
        }
        return highest;
    }
    BTW, any adivice on making the code better is welcome!
    Your "for" loop is incorrect. it should be:
    Code:
       for(int x=0; x < COMPANY_SIZE; x++)
    because emp[2] does not exists. You only have emp[0] and emp[1].

    in "getRichest", you don't need this in the for loop: "highest = (x==0) ? x : highest;" Just initialize "highest" to 0 outside the for loop, then use the for loop to update the value of highest. Otherwise, you'll do that operation at every single iteration; quite wasteful when you have a large array.
    Furthermore, since you want to use "highest" as an index, you should declare it to be "int" instead of "float", and also that function need to return type "int".

    In
    Code:
    accumulator = accumulator + e[x].salary;
    you can just use C++ notation:
    Code:
    accumulator += e[x].salary;
    Last edited by nimitzhunter; 12-02-2010 at 08:52 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calculations with long decimals
    By nik in forum C++ Programming
    Replies: 1
    Last Post: 11-15-2010, 10:15 AM
  2. K&R Learning Issue
    By TimHarrington in forum C Programming
    Replies: 48
    Last Post: 09-06-2010, 04:33 AM
  3. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  4. type safe issue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 09:32 PM
  5. my first issue of GDM
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-12-2002, 04:02 PM