Quantcast

Maximum PC

It is currently Wed Aug 20, 2014 3:48 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Dude, C++ help
PostPosted: Mon Dec 13, 2004 2:27 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
Problem
http://cs.mwsu.edu/~tgriffin/student_downloads/CS_1043_Program_6_Bubble_Sort.pdf

Code
Code:
///////////////////////////////////////////////////////////////////////////////
//  Joseph Kocurek
//  CMPS 1043 - Computer Science I
//  Program #6
//  Reads from a file and calculates greatests sales and who should get the bonus
///////////////////////////////////////////////////////////////////////////////

#include <string>               // String header file
#include <iostream>                // Input Output stream header file
#include <fstream>               // Allows you to read and write from a file
#include <iomanip>

using namespace std;            // Standard included statement

struct employee{
        string first_name;
        string last_name;
        double sales;
      double yearly_salary;
      int bonus;
};



int main ()                         // Main function
{                                   // Opens main function

       ofstream outfile;
       ofstream ofile;
       ifstream ifile;
      int x = 0, y, most_sales, least_sales;
      double average, median, total_sales = 0, max, least;
      int hours = 40*50;
      double pay= 12.99;
      double numbers[100];
      double temp;
      int i, j , count =1;
      employee data[100];





      ifile.open("emp_sales.txt",ios::in);
      outfile.open("output.txt",ios::out);


      cout<<"Joseph Kocurek"<<endl;
      cout<<"Program 6"<<endl;
      cout<<"December 12, 2004"<<endl<<endl;

      
      
      
      while(!ifile.eof()&& x< 100){

         ifile>>data[x].first_name;

         ifile>>data[x].last_name;

         ifile>>data[x].sales;

         x++;



      }

      for( y=0 ; y != x; y++){
         total_sales = data[y].sales + total_sales;
         numbers[y] = data[y].sales;

      }

      average = total_sales / x;
         
      
      
      for( y=0 ; y != x; y++){
         data[y].sales;
         if(data[y].sales > max){
            max = data[y].sales;
            most_sales = y;

         }
      }

      least= max;
      for (y = 0; y!=x; y++){
         data[y].sales;
         if(data[y].sales < least){
            least= data[y].sales;
            least_sales=y;
         
         }
      }





    



      for(i = (x-1); i>=0; i--){
         for(j=1; j<=i; j++){
            if(numbers[j-1] >numbers [j]){
               temp = numbers [j-1];
               numbers[j-1] = numbers[j];
               numbers[j]=temp;
            }

         }
      
      }






            
      if(x%2==0){
         median=(numbers[x/2-1]+numbers[x/2])/2.0;
      }
      else{
         median=(numbers[x/2]);
      }



      cout << setiosflags(ios::fixed | ios::showpoint)<<setprecision(2);
      cout  << "Sales Winner:  "<< data[most_sales].first_name<<" "<<data[most_sales].last_name<<endl;
      cout <<"Sales Loser:  "<<data[least_sales].first_name<<" "<<data[least_sales].last_name<<endl;
      cout << "Average Sales:  "<<average<<endl;
      cout << "Median Sales:  "<<median<<endl<<endl;

      cout<<"Person's to receive a 5000 dollar bonus"<<endl;
      cout<<"---------------------------------------"<<endl;
      for(y=0; y!=x; y++){
         if(data[y].sales >= median){
            cout<<count<<".  "<<data[y].first_name<<endl;
            count++;
            data[y].bonus=5000;

         }


      }


      count = 1;

      cout<<endl<<endl;
      
      cout<<"Person's to receive a 1000 dollar bonus"<<endl;
      cout<<"---------------------------------------"<<endl;

      for(y=0; y!=x; y++){
         if(data[y].sales < median){
            cout<<count<<".  "<<data[y].first_name<<endl;
            count++;
            data[y].bonus=1000;
         }
      }
      
      count = 1;
      
      cout<<endl<<endl;

      cout<<"Yearly Salary"<<endl;
      cout<<"---------------------------------------"<<endl;

   

    

    

      //   data[y].yearly_salary = (data[y].sales * pay_percent) + (pay*40*50)+ data[y].bonus;


    

      for(y=0; y!=x; y++){
         data[y].yearly_salary = (data[y].sales *.10) + (pay*40*50) + data[y].bonus;
      }

      
      for(y=0; y!=x; y++){         
         cout<<count<<".  "<<data[y].first_name<<" "<<data[y].last_name<<" - "<<data[y].yearly_salary<<endl;
         count++;            
              
      }







      ifile.close();

      



    
      
      

       

      

      outfile.close();
      system("PAUSE");

      return 0;

}


Last edited by alex911 on Tue Dec 14, 2004 1:59 pm, edited 4 times in total.

Top
  Profile  
 
 Post subject: Re: Dude, C++ help
PostPosted: Mon Dec 13, 2004 7:37 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
What problems are you having? Please tell them.

_alex911 wrote:
Code:
///////////////////////////////////////////////////////////////////////////////
//  Joseph Kocurek
//  CMPS 1043 - Computer Science I
//  Program #6
//  Reads from a file and calculates greatests sales and who should get the bonus
///////////////////////////////////////////////////////////////////////////////

#include <string>               // String header file
#include <iostream>                // Input Output stream header file
#include <fstream>               // Allows you to read and write from a file
#include <iomanip>

using namespace std;            // Standard included statement

struct employee{
        string first_name;
      string sorted_fname;
        string last_name;
      string sorted_lname;
        double sales;
      double sales_sorted;
      double yearly_salary;
};


int main ()                         // Main function
{                                   // Opens main function

       ofstream outfile;
       ofstream ofile;
       ifstream ifile;
      int x = 0, y, most_sales, least_sales;
      double average, median, total_sales = 0, max, least;
      int hours = 40*50;
      double pay= 12.99;
      #define pay_percent = 1.10;

      employee data[100];

      ifile.open("emp_sales.txt",ios::in);
      outfile.open("output.txt",ios::out);


      cout<<"Joseph Kocure"<<endl;
      cout<<"Program 6"<<endl;
      cout<<"December 12, 2004"<<endl<<endl;

      
      
      
      while(!ifile.eof()&& x< 100){

         ifile>>data[x].first_name;

         ifile>>data[x].last_name;

         ifile>>data[x].sales;

         x++;



      }

      for( y=0 ; y != x; y++){
         total_sales = data[y].sales + total_sales;
         numbers[y] = data[y].sales;

      }

      average = total_sales / x;
         
      
      
      for( y=0 ; y != x; y++){
         data[y].sales;
         if(data[y].sales > max){
            max = data[y].sales;
            most_sales = y;

         }
      }

      least= max;
      for (y = 0; y!=x; y++){
         data[y].sales;
         if(data[y].sales < least){
            least= data[y].sales;
            least_sales=y;
         
         }
      }

   





      
            


      cout  << "Sales Winner:  "<< data[most_sales].first_name<<" "<<data[most_sales].last_name<<endl;
      cout <<"Sales Loser:  "<<data[least_sales].first_name<<" "<<data[least_sales].last_name<<endl;
      cout << "Average Sales:  "<<average<<endl;
      cout << "Median Sales:  "<<median<<endl<<endl;

ifile.close();

      outfile.close();
      system("PAUSE");

      return 0;

}


This has a few errors in it, so I think it would be easiest to rewrite it. However, I won't do it for you. I will be using the STL's vector container, which you can translate if you want.


Code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>

class employee
{
   public:
   std::string fname;
   std::string lname;
   double salary;
   double sales;

   employee();
   employee operator=(const employee &emp);
   bool operator==(const employee &emp);
   bool operator<(const employee &emp);
};

employee::employee()
{
   fname = "";
   lname = "";
   salary = 0.0;
   sales = 0.0;
}


employee employee::operator=(const employee &emp)
{
   this->fname = emp.fname;
   this->lname = emp.lname;
   this->salary = emp.salary;
   this->sales = emp.sales;
   
   return *this;
}


bool employee::operator==(const employee &emp)
{
   return this->sales == emp.sales;
}

bool employee::operator<(const employee &emp)
{
   return (this->sales > emp.sales);//yes, we do the other operator.  This is so we trick std::sort into sorting it the way we want it to
}
   

int main()
{
   std::ifstream fin("emp_sales.txt");
   std::ofstream fout("output.txt");
   std::vector< employee > data;//holds the employees
   std::vector< employee >::iterator data_it;//iterator for the above
   employee buffer;

   std::cout <<"blah blah" << std::endl;

   if(!fin || !fout)
   {
      std::cout << "Error opening or creating files." << std::endl;
      std::cin.get();
      return 0;
   }//handel file errors

   while( !fin.eof() )//this is ok, but you have to do something later in the loop; look for it
   {
      fin >> buffer.fname >> buffer.lname >> buffer.sales;
      data.push_back(buffer);//read in data and add it

      if( fin.eof() )
      {
         break;
      }//this is neccessary for this kind of looping
   }

   std::sort(data.begin(), data.end());//now we sort

   std::cout << "blah" << std::endl;
   std::cin.get();

   return 0;
}


I really don't have any inhibitions about showing you how to do it (the assignment was due already).
However, if you really want to do it again the way your professor asked, you can translate my code to work with arrays and your own bubble sort function (he gave you an example ;)).


Top
  Profile  
 
 Post subject:
PostPosted: Tue Dec 14, 2004 11:02 am 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
Actually, he changed it. It is due tomorrow. I'm just needing to know how to organize them in order to calculate the median basically...


Top
  Profile  
 
 Post subject:
PostPosted: Tue Dec 14, 2004 1:59 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
If you compile and run that it works how I want if you have an input file, except the "Yearly Salary" part is not in greastest to least order...


Top
  Profile  
 
 Post subject:
PostPosted: Tue Dec 14, 2004 5:24 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Well, he gave you a simple bubble sort. For a regular method, just have a loop inside a loop. Loop for the number of elements you have in the first loop, and the number of elements you have in the second loop. In the second loop, you see if array[elem] is less than array[elem+1] (with bounds checking!), and if it is, swap them. The first loop just executes the second loop.


Top
  Profile  
 
 Post subject:
PostPosted: Tue Dec 14, 2004 6:23 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
I got the numbers in order into another array, I need to finger out how the sorted numbers can match upto the proper name.


Top
  Profile  
 
 Post subject:
PostPosted: Tue Dec 14, 2004 7:29 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
If you can't use a std::map, just make sure that when you sort the numbers, you move the corresponding elements from the other arrays.


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group