Custom Search

Tuesday, November 4, 2008

Use the following template prior to each function definition

// Function: FUNCTION_NAME

//

// Purpose: EXPLAIN WHAT THIS FUNCTION DOES TO SUPPORT THE

     //        CORRECT OPERATION OF THE PROGRAM, AND HOW IT DOES

     //        IT.

//

// Parameters:

     // parameter_name (IN, OUT, or IN/OUT) -- EXPLANATION OF

     //    THE PURPOSE OF THIS PARAMETER TO THE FUNCTION.

//    (REPEAT THIS FOR ALL FORMAL PARAMETERS OF THIS FUNCTION.

//

// Returns: IF THIS FUNCTION SENDS BACK A VALUE VIA THE

     //        RETURN MECHANISM, DESCRIBE THE PURPOSE OF THAT

     //        VALUE HERE.


 

IN = USED TO PASS DATA INTO THIS FUNCTION,

OUT = USED TO PASS DATA OUT OF THIS FUNCTION

IN/OUT = USED FOR BOTH PURPOSES.


 

Example of a Good Functional Comment Block


 

// Function: FIND_BIGGEST_QUAKE

//

// Purpose: Identify the earthquake from the list of earthquake

//    intensities (quake_list) that has the largest magnitude.

//    It is assumed that the given list of quake intensities is

//    an array-based list of unordered Richter Scale

//    magnitudes; this function performs a simple sequential

//    search through the array to locate the position of the

//    largest magnitude (the largest value) in the list.

//

// Parameters:

//    quake_list (IN) -- the array of earthquake magnitudes. This

//        is just an array of real numbers; the first magnitude

//        is assumed to be at index 0.

//    num_entries (IN) -- the quantity of magnitudes in quake_list.

//

// Returns: The index (position) of the largest earthquake

//    magnitude in the quake_list array.


 


 

A programmer reading this comment will have a good understanding of the

operation of the function and this knowledge will make reading the code

easier, should the programmer find that to be necessary. (It's possible

that all they needed to know about this function was found in the comment.)


 

Note that the description of the subprogram includes the algorithm(s)

used to carry out the work of the routine, but includes no details about

the implementation of the algorithm(s). Readers who want that level of

detail will need to read the code.


 

Also note that this comment could have been written before the subprogram

was written! This is not unusual at all; when you write your code,

you should be following a detailed outline that you developed first.

Your block comments can be written based on your outline of the operation

of the routine. In other words, there is no excuse for you to wait until

the last minute to do your commenting!


 

Open a file for output but do it carefully.

#include <fstream> //inclusion for file i/o

#include <iostream> //inclusion for cin/cout


 

using namespace std;


 

int main()

{

string outputfile, response;


 

ofstream out_obj;

ifstream in_obj;


 

cout<<"enter the name of your output file:";

cin>> outputfile;


 

in_obj.open(outputfile.c_str());


 

while(!in_obj.fail())

{in_obj.close();

cout<< outputfile<< " already exists\n";

cout << "do you want to overwrite it?\n"<<"Enter (y)es or (n)o:";

cin >> response;

if (response=="y")

{

break;

}

else

{

     cout<<"enter a new name for your output file:";

     cin>> outputfile;

in_obj.open(outputfile.c_str());

}

}


 


 

out_obj.open(outputfile.c_str());


 

cout<<"ok, so now I am writing into "<<outputfile <<" Check it out!\n";

out_obj<<"ok, so this is what I wrote into "<<outputfile<<endl;


 

return 0;

}

Defining a class and creating a vector of the class, then sorting and searching the vector

#include <vector>

#include<string>

#include <cmath>

#include <fstream>

#include <cstdlib>

#include <iostream>

#include <iomanip>

#include <string>

using namespace std;


 

const long SIZE=6;

const double PI=3.14159;


 

class tankcar {

//********the following attributes representthe object********************

//**********they can only be acessed by the methods***********************


 

private:

    string id;

    double radius;

    double length;

    double weight;

    char type;


 

//******************here begin the method prototypes***********************

//******************the methods define the interface**************

//******************between the "outside world" and *****************

//******************an object's attributes*********************************

public:

    tankcar(); //constructor has same name as class

//overloaded constructor

tankcar(string initial_id, double initial_radius,double initial_length,double initial_weight,

            char initial_type);


 

~tankcar(); //destructor


 

//readers - report current values of class attributes

    double read_radius();

double read_length();

double read_weight();

char read_type();

    double read_volume();

    double read_girth();

    double read_area();

    string read_id();

//writers - update the values of attributes

    void write_radius(double);

    void write_length(double);

    void write_weight(double);

    void write_type(char);

    void write_id(string);

};

//************************end class definition**************************


 

//************************begin class method definitions****************

//*** The :: is called a scope resolution operator ********************

//*** The scope resolution operator notifies the compiler where to look for

//*** the declarations of variables not declared in the method blocks**


 

tankcar::tankcar() //constructor has same name as class

{cout<<"+";

     id ="";

radius=0.0;

length=0.0;

weight=0.0;

type='z';

}


 

//overloaded constructor

    tankcar::tankcar(string initial_id, double initial_radius,double initial_length,

            double initial_weight,char initial_type)

    { cout<<"\noutput for this example,note an initialized tankcar has been "<<

        "created\n";

     id = initial_id;

radius=initial_radius;

length=initial_length;

weight=initial_weight;

type=initial_type;

    }


 

//destructor for this application does nothing

//only one destructor allowed


 

tankcar::~tankcar() //destructor

    {cout<<"-";

    }


 

//readers - report current values of class attributes

double tankcar::read_radius()

        {return radius;}

double tankcar::read_length()

        {return length;}

double tankcar::read_weight()

        {return weight;}

char tankcar::read_type()

        {return type;}

double tankcar::read_volume()

        {return PI*pow(radius,2.0)*length;}

double tankcar::read_girth()

        {return 2.0*PI*radius;};

double tankcar::read_area()

        {return 2.0*PI*radius*(radius+length);}

    string tankcar::read_id()

        {return id;}

//writers - update the values of attributes

void tankcar::write_radius(double new_radius)

        {radius=new_radius;}

void tankcar::write_length(double new_length)

{length=new_length;}

void tankcar::write_weight(double new_weight)

        {weight=new_weight;}

void tankcar::write_type(char new_type)

        {type=new_type;}

    void tankcar::write_id(string new_id)

        {id = new_id;}

//***********end of methods for class account***********************


 

//********start general function prototypes for application*******


 

void get_cars(vector <tankcar> &c);    //if the function updates the vector

                    //the vector must be passed by explicit reference

void print_cars(vector <tankcar> c);

void sort_cars_by_id(vector <tankcar> &c);

void use_linear_search(vector <tankcar> c);

void use_binary_search(vector <tankcar> c);

int linear(vector <tankcar> c,string search);

int binary(vector <tankcar> c,string search);


 

int main()

{

vector <tankcar> cars;


 

get_cars(cars);

print_cars(cars);


 

use_linear_search(cars);


 

sort_cars_by_id(cars);

print_cars(cars);


 

use_binary_search(cars);


 

return 0;

}


 

void get_cars(vector<tankcar> &c)

{tankcar next_tanker;

double rad, len,w;

char t;

string id, file_name;

ifstream in;

cout<<"enter name of tank car data file"<<endl;

getline(cin, file_name);

in.open(file_name.c_str());

if(in.fail())

{cout<<"cannot open "<<file_name<<endl;

exit(1);

}


 

in>>id>> rad >>len >>w>>t; //priming read for test of eof

while (!in.eof())

{ next_tanker.write_id(id);

next_tanker.write_radius(rad);

next_tanker.write_length(len);

next_tanker.write_weight(w);

next_tanker.write_type(t);

c.push_back(next_tanker); // add a copy of the next_tanker to the end of the vector

in>>id>> rad >>len >>w>>t; //all subsequent data read here and tested for eof

                //at the top of the loop-read first then test for eof


 

}

in.close();

}


 

void print_cars(vector<tankcar> c)

{int i;

cout<<

"\n\n"<<setw(4)<<"id"<<setw(8)<<"radius"<<setw(12)<<"length"<<setw(10)<<"weight"<<setw(8)<<"type";

cout<<setw(15)<<"volume"<<setw(10)<<"girth"<<setw(12)<<"area"<<"\n\n";


 

cout<<fixed<<setprecision(2);


 

for(i=0; i < c.size(); ++i)

{ cout<< setw(4)<<c[i].read_id()

     <<setw(8)<<c[i].read_radius()<<setw(12)

     <<c[i].read_length()<<setw(10)

     << c[i].read_weight()<<setw(8)<< c[i].read_type()

     <<setw(15)<< c[i].read_volume()<<setw(10)<< c[i].read_girth()

     <<setw(12)<< c[i].read_area()<< endl;

}

}


 

/* Bubble sort with early exit when no swaps*/

void sort_cars_by_id(vector<tankcar> &c)

{

tankcar temp;

int i,j,swapsmade=1;


 

for( i=c.size(); i >=2 && swapsmade ; --i)

{    swapsmade = 0;

    for(j=0; j < i-1; ++j)

    {

        if( c[j].read_id() > c[j+1].read_id())

        { temp = c[j];

         c[j]=c[j+1];

         c[j+1] = temp;

         swapsmade =1;

        }

    }

}

}


 


 

void use_linear_search(vector<tankcar> c)

{int k, more;

string search_id;

do{

    cout<< "enter the id of a tank car whose volume you want to know:";

    cin>> search_id;

    k=linear(c,search_id);

    if (k<0)

        cout<<"tankcar "<<search_id<<" not found\n\n";

    else

        cout<< "the volume of car " << search_id <<" is "<<c[k].read_volume()<<"\n\n";


 

    cout<<"enter 1 to continue this query process; otherwise enter a 0:";

    cin>>more;

} while(more);

}


 

void use_binary_search(vector<tankcar> c)

{int k, more;

string search_id;

do{

    cout<< "enter the id of a tank car whose volume you want to know:";

    cin>> search_id;

    k=binary(c,search_id);

    if (k<0)

        cout<<"tankcar "<<search_id<<" not found\n\n";

    else

        cout<< "the volume of car " << search_id <<" is "<<c[k].read_volume()<<"\n\n";


 

    cout<<"enter 1 to continue this query process; otherwise enter a 0:";

    cin>>more;

}while(more);

}


 


 


 

int linear(vector<tankcar> c,string search)

{

int i;

for( i=0; i<c.size(); ++i)

{if(search == c[i].read_id())

return i;

}

return -1;

}


 


 

int binary(vector<tankcar> c,string search)

{

int mid, lo=0, hi= c.size()-1;


 

while (lo <= hi)

{mid = (lo + hi)/2;

if( search < c[mid].read_id())

hi = mid-1;

else

if (search > c[mid].read_id())

lo = mid +1;

else

return mid;

}

return -1;

}

[pt@cs my_prog1]$

[pt@cs my_prog1]$ c++ search_class_vec.C

[pt@cs my_prog1]$ a.out

+enter name of tank car data file

tank_data.txt

--------


 

id radius length weight type volume girth area


 

1234 12.00 40.00 12000.00 a 18095.56 75.40 3920.70

4321 12.00 35.00 11000.00 a 15833.61 75.40 3543.71

5423 10.00 35.00 10000.00 b 10995.56 62.83 2827.43

3333 13.00 40.00 14000.00 c 21237.15 81.68 4329.11

1123 13.00 40.00 14500.00 c 21237.15 81.68 4329.11

9933 10.00 35.00 10000.00 b 10995.56 62.83 2827.43

8865 14.00 20.00 12000.00 d 12315.03 87.96 2990.79

-------enter the id of a tank car whose volume you want to know:1111

-------tankcar 1111 not found


 

enter 1 to continue this query process; otherwise enter a 0:1

enter the id of a tank car whose volume you want to know:9999

-------tankcar 9999 not found


 

enter 1 to continue this query process; otherwise enter a 0:1

enter the id of a tank car whose volume you want to know:1123

-------the volume of car 1123 is 21237.15


 

enter 1 to continue this query process; otherwise enter a 0:0

-------+-


 

id radius length weight type volume girth area


 

1123 13.00 40.00 14500.00 c 21237.15 81.68 4329.11

1234 12.00 40.00 12000.00 a 18095.56 75.40 3920.70

3333 13.00 40.00 14000.00 c 21237.15 81.68 4329.11

4321 12.00 35.00 11000.00 a 15833.61 75.40 3543.71

5423 10.00 35.00 10000.00 b 10995.56 62.83 2827.43

8865 14.00 20.00 12000.00 d 12315.03 87.96 2990.79

9933 10.00 35.00 10000.00 b 10995.56 62.83 2827.43

-------enter the id of a tank car whose volume you want to know:1111

-------tankcar 1111 not found


 

enter 1 to continue this query process; otherwise enter a 0:1

enter the id of a tank car whose volume you want to know:9999

-------tankcar 9999 not found


 

enter 1 to continue this query process; otherwise enter a 0:1

enter the id of a tank car whose volume you want to know:3333

-------the volume of car 3333 is 21237.15


 

enter 1 to continue this query process; otherwise enter a 0:1

enter the id of a tank car whose volume you want to know:4321

-------the volume of car 4321 is 15833.61


 

enter 1 to continue this query process; otherwise enter a 0:0


 

The file read by the program


 

1234 12 40 12000 a

4321 12 35 11000 a

5423 10 35 10000 b

3333 13 40 14000 c

1123 13 40 14500 c

9933 10 35 10000 b

8865 14 20 12000 d


 


 


 

Defining a class and creating an array of the class, then sorting and searching the array

#include <vector>

#include<string>

#include <cmath>

#include <fstream>

#include <cstdlib>

#include <iostream>

#include <iomanip>

#include <string>

using namespace std;


 

const long SIZE=6;

const double PI=3.14159;


 

class tankcar {

//********the following attributes representthe object********************

//**********they can only be acessed by the methods***********************


 

private:

    string id;

    double radius;

    double length;

    double weight;

    char type;


 

//******************here begin the method prototypes***********************

//******************the methods define the interface**************

//******************between the "outside world" and *****************

//******************an object's attributes*********************************

public:

    tankcar(); //constructor has same name as class

//overloaded constructor

tankcar(string initial_id, double initial_radius,double initial_length,double initial_weight,

            char initial_type);


 

~tankcar(); //destructor


 

//readers - report current values of class attributes

    double read_radius();

double read_length();

double read_weight();

char read_type();

    double read_volume();

    double read_girth();

    double read_area();

    string read_id();

//writers - update the values of attributes

    void write_radius(double);

    void write_length(double);

    void write_weight(double);

    void write_type(char);

    void write_id(string);

};

//************************end class definition**************************


 

//************************begin class method definitions****************

//*** The :: is called a scope resolution operator ********************

//*** The scope resolution operator notifies the compiler where to look for

//*** the declarations of variables not declared in the method blocks**


 

tankcar::tankcar() //constructor has same name as class

{cout<<"+";

     id ="";

radius=0.0;

length=0.0;

weight=0.0;

type='z';

}


 

//overloaded constructor

    tankcar::tankcar(string initial_id, double initial_radius,double initial_length,

            double initial_weight,char initial_type)

    { cout<<"\noutput for this example,note an initialized tankcar has been "<<

        "created\n";

     id = initial_id;

radius=initial_radius;

length=initial_length;

weight=initial_weight;

type=initial_type;

    }


 

//destructor for this application does nothing

//only one destructor allowed


 

tankcar::~tankcar() //destructor

    {cout<<"-";

    }


 

//readers - report current values of class attributes

double tankcar::read_radius()

        {return radius;}

double tankcar::read_length()

        {return length;}

double tankcar::read_weight()

        {return weight;}

char tankcar::read_type()

        {return type;}

double tankcar::read_volume()

        {return PI*pow(radius,2.0)*length;}

double tankcar::read_girth()

        {return 2.0*PI*radius;};

double tankcar::read_area()

        {return 2.0*PI*radius*(radius+length);}

    string tankcar::read_id()

        {return id;}

//writers - update the values of attributes

void tankcar::write_radius(double new_radius)

        {radius=new_radius;}

void tankcar::write_length(double new_length)

{length=new_length;}

void tankcar::write_weight(double new_weight)

        {weight=new_weight;}

void tankcar::write_type(char new_type)

        {type=new_type;}

    void tankcar::write_id(string new_id)

        {id = new_id;}

//***********end of methods for class account***********************


 

//********start general function prototypes for application*******


 

void get_cars(vector <tankcar> &c);    //if the function updates the vector

                    //the vector must be passed by explicit reference

void print_cars(vector <tankcar> c);

void sort_cars_by_id(vector <tankcar> &c);

void use_linear_search(vector <tankcar> c);

void use_binary_search(vector <tankcar> c);

int linear(vector <tankcar> c,string search);

int binary(vector <tankcar> c,string search);


 

int main()

{

vector <tankcar> cars;


 

get_cars(cars);

print_cars(cars);


 

use_linear_search(cars);


 

sort_cars_by_id(cars);

print_cars(cars);


 

use_binary_search(cars);


 

return 0;

}


 

void get_cars(vector<tankcar> &c)

{tankcar next_tanker;

double rad, len,w;

char t;

string id, file_name;

ifstream in;

cout<<"enter name of tank car data file"<<endl;

getline(cin, file_name);

in.open(file_name.c_str());

if(in.fail())

{cout<<"cannot open "<<file_name<<endl;

exit(1);

}


 

in>>id>> rad >>len >>w>>t; //priming read for test of eof

while (!in.eof())

{ next_tanker.write_id(id);

next_tanker.write_radius(rad);

next_tanker.write_length(len);

next_tanker.write_weight(w);

next_tanker.write_type(t);

c.push_back(next_tanker); // add a copy of the next_tanker to the end of the vector

in>>id>> rad >>len >>w>>t; //all subsequent data read here and tested for eof

                //at the top of the loop-read first then test for eof


 

}

in.close();

}


 

void print_cars(vector<tankcar> c)

{int i;

cout<<

"\n\n"<<setw(4)<<"id"<<setw(8)<<"radius"<<setw(12)<<"length"<<setw(10)<<"weight"<<setw(8)<<"type";

cout<<setw(15)<<"volume"<<setw(10)<<"girth"<<setw(12)<<"area"<<"\n\n";


 

cout<<fixed<<setprecision(2);


 

for(i=0; i < c.size(); ++i)

{ cout<< setw(4)<<c[i].read_id()

     <<setw(8)<<c[i].read_radius()<<setw(12)

     <<c[i].read_length()<<setw(10)

     << c[i].read_weight()<<setw(8)<< c[i].read_type()

     <<setw(15)<< c[i].read_volume()<<setw(10)<< c[i].read_girth()

     <<setw(12)<< c[i].read_area()<< endl;

}

}


 

/* Bubble sort with early exit when no swaps*/

void sort_cars_by_id(vector<tankcar> &c)

{

tankcar temp;

int i,j,swapsmade=1;


 

for( i=c.size(); i >=2 && swapsmade ; --i)

{    swapsmade = 0;

    for(j=0; j < i-1; ++j)

    {

        if( c[j].read_id() > c[j+1].read_id())

        { temp = c[j];

         c[j]=c[j+1];

         c[j+1] = temp;

         swapsmade =1;

        }

    }

}

}


 


 

void use_linear_search(vector<tankcar> c)

{int k, more;

string search_id;

do{

    cout<< "enter the id of a tank car whose volume you want to know:";

    cin>> search_id;

    k=linear(c,search_id);

    if (k<0)

        cout<<"tankcar "<<search_id<<" not found\n\n";

    else

        cout<< "the volume of car " << search_id <<" is "<<c[k].read_volume()<<"\n\n";


 

    cout<<"enter 1 to continue this query process; otherwise enter a 0:";

    cin>>more;

} while(more);

}


 

void use_binary_search(vector<tankcar> c)

{int k, more;

string search_id;

do{

    cout<< "enter the id of a tank car whose volume you want to know:";

    cin>> search_id;

    k=binary(c,search_id);

    if (k<0)

        cout<<"tankcar "<<search_id<<" not found\n\n";

    else

        cout<< "the volume of car " << search_id <<" is "<<c[k].read_volume()<<"\n\n";


 

    cout<<"enter 1 to continue this query process; otherwise enter a 0:";

    cin>>more;

}while(more);

}


 


 


 

int linear(vector<tankcar> c,string search)

{

int i;

for( i=0; i<c.size(); ++i)

{if(search == c[i].read_id())

return i;

}

return -1;

}


 


 

int binary(vector<tankcar> c,string search)

{

int mid, lo=0, hi= c.size()-1;


 

while (lo <= hi)

{mid = (lo + hi)/2;

if( search < c[mid].read_id())

hi = mid-1;

else

if (search > c[mid].read_id())

lo = mid +1;

else

return mid;

}

return -1;

}

[pt@cs my_prog1]$

[pt@cs my_prog1]$ c++ search_class_vec.C

[pt@cs my_prog1]$ a.out

+enter name of tank car data file

tank_data.txt

--------


 

id radius length weight type volume girth area


 

1234 12.00 40.00 12000.00 a 18095.56 75.40 3920.70

4321 12.00 35.00 11000.00 a 15833.61 75.40 3543.71

5423 10.00 35.00 10000.00 b 10995.56 62.83 2827.43

3333 13.00 40.00 14000.00 c 21237.15 81.68 4329.11

1123 13.00 40.00 14500.00 c 21237.15 81.68 4329.11

9933 10.00 35.00 10000.00 b 10995.56 62.83 2827.43

8865 14.00 20.00 12000.00 d 12315.03 87.96 2990.79

-------enter the id of a tank car whose volume you want to know:1111

-------tankcar 1111 not found


 

enter 1 to continue this query process; otherwise enter a 0:1

enter the id of a tank car whose volume you want to know:9999

-------tankcar 9999 not found


 

enter 1 to continue this query process; otherwise enter a 0:1

enter the id of a tank car whose volume you want to know:1123

-------the volume of car 1123 is 21237.15


 

enter 1 to continue this query process; otherwise enter a 0:0

-------+-


 

id radius length weight type volume girth area


 

1123 13.00 40.00 14500.00 c 21237.15 81.68 4329.11

1234 12.00 40.00 12000.00 a 18095.56 75.40 3920.70

3333 13.00 40.00 14000.00 c 21237.15 81.68 4329.11

4321 12.00 35.00 11000.00 a 15833.61 75.40 3543.71

5423 10.00 35.00 10000.00 b 10995.56 62.83 2827.43

8865 14.00 20.00 12000.00 d 12315.03 87.96 2990.79

9933 10.00 35.00 10000.00 b 10995.56 62.83 2827.43

-------enter the id of a tank car whose volume you want to know:1111

-------tankcar 1111 not found


 

enter 1 to continue this query process; otherwise enter a 0:1

enter the id of a tank car whose volume you want to know:9999

-------tankcar 9999 not found


 

enter 1 to continue this query process; otherwise enter a 0:1

enter the id of a tank car whose volume you want to know:3333

-------the volume of car 3333 is 21237.15


 

enter 1 to continue this query process; otherwise enter a 0:1

enter the id of a tank car whose volume you want to know:4321

-------the volume of car 4321 is 15833.61


 

enter 1 to continue this query process; otherwise enter a 0:0


 

The file read by the program.


 


 

1234 12 40 12000 a

4321 12 35 11000 a

5423 10 35 10000 b

3333 13 40 14000 c

1123 13 40 14500 c

9933 10 35 10000 b

8865 14 20 12000 d


 


 

Using an array of structs and Bubble_sort

#include <iostream>

#include <iomanip>

using namespace std;

#define SIZE 6

struct tankvol {

double radius;

double length;

};

struct tankcar {

tankvol capacity;

double weight;

char type;

};


 

void get_cars(tankcar c[], int n);

void print_cars(tankcar c[], int n);

void sort_cars_by_radius(tankcar c[], int n);


 

int main()

{tankcar cars[SIZE];


 

get_cars(cars, SIZE);


 

print_cars(cars, SIZE);


 

sort_cars_by_radius(cars, SIZE);


 

print_cars(cars, SIZE);


 

return 0;

}


 


 

void get_cars(tankcar c[], int n)

{int i;

for(i=0; i < n; ++i)

{ cout<< "enter radius, length, weight,type\n";

cin>> c[i].capacity.radius >>c[i].capacity.length >>c[i].weight

>>c[i].type;

}

}


 

void print_cars(tankcar c[], int n)

{int i;

cout<< "radius length weight type\n\n";

for(i=0; i < n; ++i)

{

cout<< setw(4)<<c[i].capacity.radius<<setw(10)

<<c[i].capacity.length<<setw(9)

<< c[i].weight<<setw(10)<< c[i].type << endl;

}

}


 


 

/* Bubble sort with early exit when no swaps*/

void sort_cars_by_radius(tankcar c[], int n)

{

tankcar temp;

int i,j,swapsmade=1;


 

for( i=n; i >=2 && swapsmade ; --i)

{ cout<<"i is:"<<i<<endl;

swapsmade = 0;

for(j=0; j < i-1; ++j)

{ cout<<"j is:"<<j<<endl;            //these couts purpose is trace

cout << "comparing:"<<c[j].capacity.radius <<"with:"

<<c[j+1].capacity.radius<<endl;

if(c[j].capacity.radius > c[j+1].capacity.radius)

{ temp = c[j];

c[j]=c[j+1];

c[j+1] = temp;

swapsmade =1;

}

}

}

}

[pt@cs aix]$

[pt@cs aix]$ a.out

enter radius, length, weight,type

4 2 56 a

enter radius, length, weight,type

5 5 60 b

enter radius, length, weight,type

45 20 100 c

enter radius, length, weight,type

3 4 66 a

enter radius, length, weight,type

6 4 77 q

enter radius, length, weight,type

7 8 33 a

radius length weight type


 

4 2 56 a

5 5 60 b

45 20 100 c

3 4 66 a

6 4 77 q

7 8 33 a

i is:6

j is:0

comparing:4with:5

j is:1

comparing:5with:45

j is:2

comparing:45with:3

j is:3

comparing:45with:6

j is:4

comparing:45with:7

i is:5

j is:0

comparing:4with:5

j is:1

comparing:5with:3

j is:2

comparing:5with:6

j is:3

comparing:6with:7

i is:4

j is:0

comparing:4with:3

j is:1

comparing:4with:5

j is:2

comparing:5with:6

i is:3

j is:0

comparing:3with:4

j is:1

comparing:4with:5

radius length weight type


 

3 4 66 a

4 2 56 a

5 5 60 b

6 4 77 q

7 8 33 a

45 20 100 c


 

Simple Bubble Sort of an Integer Array

#include <iostream>

using namespace std;

void read_list(int list[],int n);

 
 

void print_list(int list[],int n);

 
 

void bubble(int list[],int n);

 
 

int main()

{

int list[5];

 
 

read_list(list,5);

print_list(list,5);

cout <<endl;

 
 

bubble(list,5);

 
 

print_list(list,5);

 
 

}

void read_list(int list[],int n)

{cout<<"enter "<<n<<" integers"<<endl;

 for(int i=0;i<n;++i)

    cin>>list[i];

}

void print_list(int list[],int n)

{ for(int i=0;i<n;++i)

    cout<<list[i]<<endl;

}

 
 

void bubble(int c[], int n)

{

int temp;

int i,j,swapsmade=1;

 
 

for( i=n; i >=2 && swapsmade ; --i)

{   cout<<"i is:"<<i<<endl;

 
 

        swapsmade = 0;

 
 

        for(j=0; j < i-1; ++j)

 
 

        { cout<<"j is:"<<j<<endl;                                  

                        //these couts purpose is trace

 
 

          cout << "comparing:"<<c[j] <<"with:" <<c[j+1]<<endl;

 
 

                if(c[j] > c[j+1])

 
 

                { temp = c[j];

 
 

                  c[j]=c[j+1];

 
 

                  c[j+1] = temp;

 
 

                  swapsmade =1;

 
 

                }

 
 

        }

 
 

}

 
 

}

 
 

 
 

[pt@cs my_prog1]$ c++ bubble.cpp

[pt@cs my_prog1]$ a.out

enter 5 integers

9 6 4 2 1

9

6

4

2

1

 
 

i is:5

j is:0

comparing:9with:6

j is:1

comparing:9with:4

j is:2

comparing:9with:2

j is:3

comparing:9with:1

i is:4

j is:0

comparing:6with:4

j is:1

comparing:6with:2

j is:2

comparing:6with:1

i is:3

j is:0

comparing:4with:2

j is:1

comparing:4with:1

i is:2

j is:0

comparing:2with:1

1

2

4

6

9

[pt@cs my_prog1]$ a.out

enter 5 integers

1 2 3 4 5

1

2

3

4

5

 
 

i is:5

j is:0

comparing:1with:2

j is:1

comparing:2with:3

j is:2

comparing:3with:4

j is:3

comparing:4with:5

1

2

3

4

5


 

 
 

Reading and Writing External Files Using Auxiliary Function

#include <fstream>        //inclusion for file i/o

#include <iostream>        //inclusion for cin/cout

#include <cstdlib>        //inclusion for exit()

using namespace std;


 

void print_headings(ofstream &out_obj);


 

int main()

{int num1,num2,sum;

    

ofstream out_file_obj;        //declare the file stream objects

ifstream in_file_obj;        //ifstream is for input file,

                //ofstream is for output file


 

out_file_obj.open("res.txt");     //open the named file

                    //by invoking the 'open()' method

                    //associated with an ofstream

                    //object. If the file does not

                    //exist it is created. If the file

                    //does exist, it is truncated!!!

                    //So watch out!!


 

                    //file name should follow usual

                    //Dos/Windows/unix file naming

                    //conventions regarding

                    //extensions, etc


 

in_file_obj.open("nums.txt");         //open the named file

                    //by invoking the 'open()' method

                    //associated with an ifstream

                    //object. If the named file

                    //does not exist, the method

                    //fails.


 

if(in_file_obj.fail())        //so, find out if the open failed by

{ cout<<"nums.txt"        //calling the objects 'fail()' method.

<<" could not be opened"    //fail() returns a non-zero value if the

<<" for input\n";        //open did indeed fail; otherwise, it

                    //returns zero.

exit(1);            //terminate the program, signal an error

}


 

// the fstream objects can now be used instead of cout and cin with file

//stream insertion and extraction operators


 

//fstream objects can be passed by reference to functions


 

print_headings(out_file_obj);


 

in_file_obj >> num1 >> num2;    //priming read

while(!in_file_obj.eof())            

{ sum = num1+num2;        

out_file_obj << num1<<"\t\t" <<num2<<"\t\t" <<sum<<endl;

in_file_obj >> num1 >> num2; //read at the bottom of the loop

}


 


 

in_file_obj.close();        //use the 'close()' method to close a file

                    //when you are through using it

out_file_obj.close();

cout<< "program exiting\n";

return 0;

}


 


 

void print_headings(ofstream &out_obj)

{

out_obj << "first num\tsecond num\tsum\n\n";

}


 


 

[pt@cs aix]$ c++ files2.C

[pt@cs aix]$

[pt@cs aix]$ a.out

program exiting

[pt@cs aix]$

[pt@cs aix]$ cat nums.txt

2 40 15

5 67

57

24 34

[pt@cs aix]$

[pt@cs aix]$ cat res.txt

first num    second num    sum


 

2        40        42

15        5        20

67        57        124

24        34        58