i've started learnin OOPS lately...
and i hav a question...
check it out...
i started studying abt friend class and friend functins...
a friend function program...
now, how can i write a progarm 2 add two complex variables...
can i use:
void getdata()
{
cin>>a+i;
cin>>b+i;
}
to input two complex variables
or is there an alternate solution?

Recommended Answers

All 2 Replies

nope.

Forget about the mathematical notation.
Create a class to contain your complex numbers as 2 double precision floating point fields, one real and one imagenary.
Either create operators (one of the few valid reasons to have operator overloading is pure mathematics like this) or regular methods for mathematical operations.

\\im not sure what the current key functions for this program is...can you help me?

// Mr. C Grades All Functions

#include <iostream.h>

#include <fstream.h>


// Functions Called
void namefile (char filename [ ]);

void names (char name1[ ],char name2[ ]);

int load_list (int list[ ]);

void selection_sort (int list[ ],int howmany);

float averageof (int list[ ],int howmany);

char lettergrade (float average);

void screenoutput(char a[ ],char b[ ],int n,int list[ ],int h, int l,float avg, char g);

void fileoutput(char filename[ ], char a[ ],char b[ ],int n,int list[ ],int h, int l,float avg, char g);

char morestudents ( );

void goodbye( );

int main (void)

{

char filename [12]; // Name of file to store data on disk

char lastname[20], firstname[20]; // Student's first and last names

int howmany; // Number of Grades

int gradelist[50]; // List of grades (up to 50)

float average; // Average Grade

int high, low; // Highest and Lowest Grades

char grade; // Grade of A, B, C, D or F

char anotherstudent='y';

// Program steps begin here:======================================
namefile (filename);

while (anotherstudent == 'y' || anotherstudent == 'Y')

{

names(firstname,lastname);

howmany = load_list (gradelist);

selection_sort (gradelist,howmany);

high = gradelist[(howmany-1)];

low = gradelist [0];

average = averageof (gradelist, howmany);

grade = lettergrade (average);

screenoutput(firstname,lastname,howmany,gradelist,high,low,average,grade);

fileoutput(filename,firstname,lastname,howmany,gradelist,high,low,average,grade);

anotherstudent = morestudents ( );

}

goodbye ( );

return (0);

} // End of main program ..... functions follow
//Functions----------------------------------------------------------------------------------------------------
void namefile (char filename [ ] )

{

//Get Name of Datafile

cout<<"\n\n\tEnter a filename as filename.dat: ";

cin>>filename;

}

void names (char name1[ ],char name2[ ])

{

char check1 = 'N';

char check2 = 'N';

while (check1 == 'N' || check1 == 'n')

{

cout <<"\n\n\tWhat is this student's first name? ";

cin >> name1;

cout<<"\n\tYou said "<<name1<<" Is that Correct? Y or N ---> ";

cin>>check1;

}

while (check2 == 'N' || check2 == 'n')

{

cout <<"\n\n\tWhat is this student's last name? ";

cin >> name2;

cout<<"\n\tYou said "<<name2<<" Is that Correct? Y or N ---> ";

cin>>check2;

}

}

int load_list (int list[ ])

{

char checkit1;

char checkit2;

int howmany;

checkit1 = 'N';
while (checkit1 == 'N' || checkit1 == 'n')

{

cout <<"\n\n\tHow many grades do you wish to enter for this student? ";

cin >> howmany;

cout<<"\n\tYou said "<<howmany<<" Is that Correct? Y or N ---> ";

cin>>checkit1;

}

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

{

checkit2 = 'N';

while (checkit2 == 'N' || checkit2 == 'n')

{

cout << "\n\tPlease Enter grade: \a"<<(i+1)<<" ..... ";

cin >>list;

cout <<"\n\n\tCheck the input: \a Grade "<<(i+1);

cout <<" is "<<list<<" Correct? Y or N ";

cin >> checkit2;

}

}

//Echo Check Grade Array to Screen.

cout<<"\n\nHere are the grades in your list:\n"<<endl;

for (int j=0;j<howmany;j++)

{

cout <<"\tGrade:"<<(j+1)<<" is "<<list[j]<<endl;

}

return (howmany);

}
void selection_sort (int list[ ],int howmany)

{

for( int i=0; i < howmany - 1; i++ )

{

// Find the smallest item between index i and index howmany-1.

int indexOfMin = i;

for( int j=i+1; j < howmany; j++ )

{

if( list[indexOfMin] > list[j] )

indexOfMin = j;

}

// Swap the max with list.

int temp = list;

list = list[indexOfMin];

list[indexOfMin] = temp;

}

}

float averageof (int list[ ],int howmany)

{

int sum ; //The sum of the numbers in the list

float average; //The average of the numbers in the list

sum = 0;

for (int index=0;index<howmany;index++)

{

sum += list[index];

}

average = float (sum) / float (howmany);

return (average); //send back the sum

}

char lettergrade (float average)

{

char grade;

if (average >= 89.5) grade = 'A';

if (average < 89.5 && average >= 79.5 ) grade = 'B';

if (average < 79.5 && average >= 69.5 ) grade = 'C';

if (average < 69.5 && average >= 59.5 ) grade = 'D';

if (average < 59.5) grade = 'F';

return (grade);

}

void screenoutput(char a[ ],char b[ ],int n,int list[ ],int h, int l,float avg, char g)

{

cout <<"\n\tGrades for "<<a<<" "<<b<<endl;

cout<<"\n\Here are the grades in your list:\n"<<endl;

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

{

cout <<"\tGrade:"<<(j+1)<<" is "<<list[j]<<endl;

}


//(continued on next page.......)

cout <<"\n\n\tHighest Grade: "<<h<<endl;

cout <<"\tLowest Grade: "<<l<<endl;

cout <<"\tAverage: "<<avg<<endl;

cout <<"\tfor: "<<n<<" grades."<<endl;

cout <<"\tThis is a letter grade of "<<g;

}


void fileoutput(char filename [],char a[],char b[],int n,int list[],int h, int l,float avg, char g)

{

//Open the Datafile

ofstream datafile;

datafile.open(filename,ios::app);

//If Something Goes Wrong

if (datafile.fail())

{

cout<<"\nFile could not be opened.";

}

//Gets info from user to enter into the Datafile

else

{

datafile <<"\n\n\n\n\n\tGrades for "<<a<<" "<<b<<endl;

datafile<<"\n\n\tHere are the grades in your list:\n"<<endl;

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

{

datafile <<"\n\tGrade:"<<(j+1)<<" is "<<list[j]<<endl;

}

datafile <<"\n\n\tHighest Grade: "<<h<<endl;

datafile <<"\tLowest Grade: "<<l<<endl;

datafile <<"\tAverage: "<<avg<<endl;

datafile <<"\tfor: "<<n<<" grades."<<endl;

datafile <<"\tThis is a letter grade of "<<g;

}

// Closes the datafile

datafile.close( );

}


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

char morestudents( )

{

char choice;

cout<<"\n\n\n\tDo You Wish to Enter Grades for Another Student?"<<endl;

cout<<"\tEnter Y for yes or any other key for no. ";

cin >> choice;

return (choice);

}

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

void goodbye ( )

{

cout << "\n\n\tGood Things Happen To People Who don't Give Up!!!!!! Bye.\n\n";

}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.