/* Create a class named weather report that holds a daily weather report with data members day_of_month,hightemp,lowtemp,amount_rain and amount_snow. The constructor initializes the fields with default values: 99 for day_of_month, 999 for hightemp,-999 for low emp and 0 for amount_rain and amount_snow. Include a function that prompts the user and sets values for each field so that you can override the default values. Write a C++/Java/Python program that creates a monthly report. d) Menu driven program with options to Enter data and Display report e) Report Format */ #include<iostream> #include<iomanip> using namespace std; class weather_report { public: int day; float hightemp; float lowtemp; float amountrain; float amountsnow; void getdata(); void display(); weather_report() { day=99; hightemp=999; lowtemp=-999; amountrain=0; amountsnow=0; } }; void weather_report :: getdata() { cout<<"\nEnter all fields. "; cout<<"\n\tEnter day : "; cin>>day; cout<<"\n\tEnter High temp : "; cin>>hightemp; cout<<"\n\tEnter low temp : "; cin>>lowtemp; cout<<"\n\tEnter amount Rain : "; cin>>amountrain; cout<<"\n\tEnter amount snow : "; cin>>amountsnow; } void weather_report :: display() { cout<<setw(10)<<day<<setw(10)<<hightemp<<setw(10)<<lowtemp<<setw(10)<<amountrain<<setw(10)<<amountsnow<<"\n"; } int main() { float p; int ch; char ch1; weather_report w[31]; do { cout<<"\n\t1. Prompt data\n\t2. Display."; cout<<"\n\n\tEnter your choice : "; cin>>ch; cout<<"\nFor how many days you want to enter field : "; cin>>p; switch(ch) { case 1: for(int i=0;i<p;i++) { w[i].getdata(); } break; case 2: float avghightemp,avglowtemp,avgrain,avgsnow; int a=0,b=0,c=0,d=0; cout<<setw(10)<<"DAY "<<setw(10)<<"HIGH TEMP"<<setw(10)<<"LOW TEMP"<<setw(10)<<"AMOUNT RAIN"<<setw(10)<<"AMOUNT SNOW"<<"\n"; for(int i=0;i<p;i++) { w[i].display(); a=a+w[i].hightemp; b=b+w[i].lowtemp; c=c+w[i].amountrain; d=d+w[i].amountsnow; } avghightemp=(a/p),avglowtemp=(b/p),avgrain=c/p,avgsnow=(d/p); cout<<setw(10)<<"Avg"<<setw(10)<<avghightemp<<setw(10)<<avglowtemp<<setw(10)<<avgrain<<setw(10)<<avgsnow; break; }cout<<"\ndo u want to continue ?"; cin>>ch1; }while(ch1==’Y’ || ch1==’y’); return 0; }
Pratik Kataria is currently learning Springboot and Hibernate.
Technologies known and worked on: C/C++, Java, Python, JavaScript, HTML, CSS, WordPress, Angular, Ionic, MongoDB, SQL and Android.
Softwares known and worked on: Adobe Photoshop, Adobe Illustrator and Adobe After Effects.
/* A book shop maintains the inventory of books that are being sold at shop. The list includes details such as author,title,price,publisher and stock position.Whwnever customer wants a book the sale person inputs the title and author and the system searches the list and display wheather it is available or not.If it is not appropretae message is displayed.If it is,then system displays book details and request for no. of copies required.If requested copies are available,the total cost of requested copies is displayed, otherwise "requested copies not in stock ." is displayed.Design a system using class called book with suitable member function and contructor. Use new operator in construtor to allocate memory. Implement c++ program for system*/ #include<iostream> #include<string.h> #include<iomanip> using namespace std; class book { public: float price; string title,author,publisher; int stock; void accept(); void display(); book(); }; book :: book() { string *title=new string; title='\0'; string *author=new string; author='\0'; string *publisher=new string; publisher='\0'; price=0; stock=0; } void book :: accept() { cout<<"\n"; cout<<"Enter title : "; getline(cin,title); getline(cin,title); cout<<"Enter author : "; getline(cin,author); cout<<"Enter publisher : "; getline(cin,publisher); cout<<"Price : "; cin>>price; cout<<"Stock : "; cin>>stock; cout<<"\n"; } void book :: display() { cout<<"Book Name : "<<title<<endl; cout<<"Author Name : "<<author<<endl; cout<<"Publisher Name : "<<publisher<<endl; cout<<"Price : "<<price<<endl; } int main() { book b[20]; int ch,n=0,copy,i,no; char ch1; string tit,auth; do { cout<<"\n\t1.Enter Book Information.\n\t2.Shop for a book and display total cost.\nSelect choice : "; cin>>ch; switch(ch) { case 1: cout<<"\nHow many books you want to enter : "; cin>>n; for(i=0;i<n;i++) { b[i].accept(); } break; case 2: cout<<"\nEnter name of book u want to search : "; getline(cin,tit); getline(cin,tit); cout<<"\nEnter name of author : "; getline(cin,auth); i=0; while(i<n && b[i].title!=tit && b[i].author!=auth) { i++; } if(i>n) { cout<<"\nBook is not available."; break; } else { b[i].display(); cout<<"\nHow many copies u want?"; cin>>copy; if(b[i].stock<copy) { cout<<"\nThis much copies are not available."; break; } else { b[i].stock=b[i].stock-copy; cout<<"\n\tTotal amount of "<<tit<<" is "<<(copy*b[i].price)<<" Rs."; } } break; }cout<<"\nDo u want to continue?(y/n)"; cin>>ch1; }while(ch1=='y' or ch1=='Y'); return 0; }
Pratik Kataria is currently learning Springboot and Hibernate.
Technologies known and worked on: C/C++, Java, Python, JavaScript, HTML, CSS, WordPress, Angular, Ionic, MongoDB, SQL and Android.
Softwares known and worked on: Adobe Photoshop, Adobe Illustrator and Adobe After Effects.
/* Develop an object oriented program in C++ to create a database of personal information system containing the following information: 1. Name 2. Date of Birth 3. Blood Group 4. Height 5. Weight 6. Insurance Policy No. 7. Address 8. Telephone Number 9. Driving lincense number. Construct the database with suitable member functions, for initializing and destroying the data using constructor, default constructor, copy constructor, destructor, static member functions, friend class, this pointer, inline code and dynamic memory allocation operators new and delete. */ #include<iostream> #include<string.h> using namespace std; class Account; class Person{ char * name; char dob[20]; char bloodg[20]; int height; int weight; int IPNo; string addr; int ph_no; int drivinglicno; friend class Account; public: static int count; //static automatically makes the initialization 0. Person(); ~Person() { cout<<"\n Destruction has been called \n"; } inline void display(); //For inline stack is not created as it is usally done for a class. //Person(char *name, char dob[], char bloodg[], int, int, int, char addr[], int, int); Person(char *,char [], char [], int, int, int, string, int, int); Person(Person *p2); static void getcount(){ cout<<count; //since count being static is part of the class } }; int Person::count = 0; Person::Person(){ name = new char(); strcpy(dob, "00/00/0000"); strcpy(bloodg, ""); height = 0; weight = 0; IPNo = 0; addr = " "; ph_no = 0; drivinglicno = 0; count++; } Person::Person(char * n1, char db[20], char bg[20], int height, int weight, int IPNo, string addr,int ph_no, int drivinglicno){ strcpy(this->name, n1); strcpy(this->dob, db); strcpy(bloodg, bg); this->height = height; this->weight = weight; this->IPNo = IPNo; this->addr = addr; this->ph_no = ph_no; this->drivinglicno = drivinglicno; count++; } Person::Person(Person *p2){ //It is *p1 and not &p1. Pointer since address is being passed. this->name = new char(); //[Important] This is needed. strcpy(this->name, p2->name); strcpy(this->dob, p2->dob); strcpy(bloodg, p2->bloodg); this->height = p2->height; this->weight = weight; this->IPNo = IPNo; this->addr = addr; this->ph_no = ph_no; this->drivinglicno = drivinglicno; count++; } inline void Person::display(){ cout<<"Name : "<<name<<"\n"; cout<<"Date of Birth : "<<dob<<"\n"; cout<<"Blood Group : "<<bloodg<<"\n"; cout<<"Height : "<<height<<"\n"; cout<<"Weight : "<<weight<<"\n"; cout<<"Insurance Policy No. : "<<IPNo<<"\n"; cout<<"Address : "<<addr<<"\n"; cout<<"Mobile Number : "<<ph_no<<"\n"; cout<<"Driving License : "<<drivinglicno<<"\n"; } class Account{ int Accno; char Acc_Type[20]; public: Account(){ Accno = 0; strcpy(Acc_Type,"Undefined"); } void print(Person *p){ cout<<"Account Number: "<<Accno<<endl; cout<<"Name: "<<p->name<<endl; cout<<"Account Type: "<<Acc_Type<<endl; } }; int main(){ Account obj; Person p1; char name[20]; char dob[20]; char bloodg[20]; string addr; int weight, height, ipno, ph_no, dlc; cout<<"Welcome to personal database\n"; cout<<"Enter Name:"<<endl; cin>>name; cout<<"Enter DOB (dd/mm/yyyy)"<<endl; cin>>dob; cout<<"Enter Blood Group:"<<endl; cin>>bloodg; cout<<"Enter Height (cm):"<<endl; cin>>height; cout<<"Enter Weight (kg):"<<endl; cin>>weight; cout<<"Enter Insurance Policy No.:"<<endl; cin>>ipno; cout<<"Enter Address:"<<endl; cin.ignore(); getline(cin, addr); cout<<"Enter Mobile Number:"<<endl; cin>>ph_no; cout<<"Enter Driving License Number (Only digits):"<<endl; cin>>dlc; cout<<"———————————\n"; cout<<"The details entered are: (Parameterized constructor)\n"; Person p2(name,dob,bloodg,height, weight, ipno,addr,ph_no,dlc); p2.display(); Person p3(&p2); cout<<"———————————\n"; cout<<"The details entered are: (Copy constructor)\n"; p3.display(); cout<<"———————————\n"; cout<<"Usage of friend class: \n"; obj.print(&p2); cout<<"———————————\n"; cout<<"The number of people registered in the database are : "; Person::getcount(); cout<<"\n"; cout<<"———————————\n"; return 0; }
Pratik Kataria is currently learning Springboot and Hibernate.
Technologies known and worked on: C/C++, Java, Python, JavaScript, HTML, CSS, WordPress, Angular, Ionic, MongoDB, SQL and Android.
Softwares known and worked on: Adobe Photoshop, Adobe Illustrator and Adobe After Effects.
/* Design a C++ class complex with data members for real and imaginery parts provide default and parameterized constructors. Write a program to perform arithmetic operations of two complex numbers using operator overloading. Use either member function or friend function. */ /* Theory: Operator overloading through member function: Binary -> 1 formal parameter, Unary -> 0 formal parameter Operator overloading through friend function: Binary -> 2 formal parameter, Unary -> 1 formal parameter */ #include<iostream> using namespace std; class complex { double real; double imaginery; public: static int num; complex(){ real = 0; imaginery = 0; } complex(double a1, double a2){ real = a1; imaginery = a2; } complex operator +(complex c){ complex temp; temp.real = this->real + c.real; temp.imaginery = this->imaginery + c.imaginery; return(temp); } complex operator *(complex c){ complex temp; temp.real = (this->real * c.real) – (this->imaginery * c.imaginery); temp.imaginery = (this->imaginery * c.real) + (this->real * c.imaginery); return(temp); } complex operator /(complex c){ complex temp; //OR we can take common div: int div = (c.real*c.real) + (c.imaginery*c.imaginery); temp.real=((real*c.real)+(imaginery*c.imaginery))/((c.real*c.real)+(c.imaginery*c.imaginery)); temp.imaginery=((imaginery*c.real)-(real*c.imaginery))/((c.real*c.real)+(c.imaginery*c.imaginery)); return(temp); } friend complex operator -(complex c1, complex c2){ complex temp; temp.real = c1.real – c2.real; temp.imaginery = c1.imaginery – c2.imaginery; return(temp); } void display(int flag){ if(flag == 1) cout<<"Addition:"<<endl; else if(flag == 2) cout<<"Subtraction:"<<endl; else if(flag == 3) cout<<"Multiplication:"<<endl; else if(flag == 4) cout<<"Division:"<<endl; else{ cout<<"Operand "<<num<<":"<<endl; } cout<<this->real<<"+i"<<this->imaginery<<endl; } }; int complex::num; int main(){ complex::num = 1; complex obj1(3, 2); obj1.display(0); complex::num++; complex obj2(6, 4); obj2.display(0); complex obj3; obj3 = obj1 + obj2; obj3.display(1); obj3 = obj2 – obj1; //obj2(obj1) i.e. obj2 is invoking object obj3.display(2); obj3 = obj1 * obj2; obj3.display(3); obj3 = obj2/obj1; obj3.display(4); return 0; }
Pratik Kataria is currently learning Springboot and Hibernate.
Technologies known and worked on: C/C++, Java, Python, JavaScript, HTML, CSS, WordPress, Angular, Ionic, MongoDB, SQL and Android.
Softwares known and worked on: Adobe Photoshop, Adobe Illustrator and Adobe After Effects.
/*write a program to perform string operations = equality == string copy + concatenation >> reverse << display palindrome function function to find occurence of substring use operator overloading*/ #include<iostream> #include<string.h> #include<iomanip> using namespace std; class strings { char str[100]; public: void getdata() { cout<<"enter string\t"; cin>>str; } void showdata() {cout<<str<<endl;} int operator ~(); int operator =(strings s2); void operator ==(strings s2); friend void operator +(strings s1,strings s2); friend ostream &operator <<(ostream &output,strings &s1) { output<<s1.str; return output; } void operator >>(strings &s1); friend void operator *(strings s1); friend void operator /(strings s1,strings s2); }; //string length int strings::operator ~() { int i=0; while(str[i]!=’\0’) i++; return i; } //string compare int strings::operator =(strings s2) { int i=0; while(str[i]==s2.str[i] && i<~s2) i++; if(i==~s2) return 1; else return 0; } //copy void strings::operator ==(strings s2) { for(int i=0;i<=~s2;i++) str[i]=s2.str[i]; } //concat void operator +(strings s1,strings s2) { int i,j,k=0; strings s3; for(i=0;i<~s1;i++) { s3.str[k]=s1.str[i]; k++; } for(j=0;j<~s2;j++) { s3.str[k]=s2.str[j]; k++; } cout<<s3.str<<endl; } //reverse void strings:: operator >>(strings &s1) { int len=~s1; for(int i=0;i<=len/2;i++) { char t=s1.str[i]; s1.str[i]=s1.str[len-i-1]; s1.str[len-i-1]=t; } } //palindrome void operator *(strings s1) { strings s3; s3==s1; s3>>s3; if((s3=s1)==1) cout<<"palindrome\n"; else cout<<"not palindrome\n"; } //find substring void operator /(strings s1,strings s2) {//s2=substring int flag=0; for(int i=0;i<~s1;i++) { if(s2.str[0]==s1.str[i]) { flag++; for(int j=1;j<=~s2;j++) if(s2.str[j]==s1.str[i+j]) flag++; } } if(flag==~s2) cout<<"substring exists\n"; else cout<<"not found\n"; } int main() { strings s1,s2; int choice; char c; cout<<setw(20)<<"STRING OPERATIONS"<<endl; cout<<"1.accept and display string \n2.check for equality\n3. copy string\n 4.concatenate strings \n5.reverse string\n6.check if a string is a palindrome\n7.search for a substring occurence\n"; do { cout<<"enter choice\t"; cin>>choice; if(choice==2) { cout<<"enter first string\n"; s1.getdata(); cout<<"enter second string\n"; s2.getdata(); if((s1=s2)==1) cout<<"strings are same\n"; else cout<<"strings are different\n"; } if(choice==3) { cout<<"enter string to copy\n"; s2.getdata(); s1==s2; cout<<"contents copied to another string\n"; cout<<"contents of copy string is :\t"; cout<<s1<<endl; } if(choice==4) { cout<<"enter destination string:\n"; s1.getdata(); cout<<"enter source string:\n"; s2.getdata(); s1+s2; } if(choice==1) { s1.getdata(); cout<<"string is:\t"; cout<<s1<<endl; cout<<~s1; cout<<endl; } if(choice==5) { s1.getdata(); s1>>s1; cout<<"string after reversing is\t"; cout<<endl; cout<<s1<<endl; } if(choice==6) { s1.getdata(); *s1; } if(choice==7) { cout<<"enter main string\n"; s1.getdata(); cout<<"enter substring\n"; s2.getdata(); s1/s2; } cout<<"do you want to continue(y/n)?"; cin>>c; }while(c==’y’); return 0; }
Pratik Kataria is currently learning Springboot and Hibernate.
Technologies known and worked on: C/C++, Java, Python, JavaScript, HTML, CSS, WordPress, Angular, Ionic, MongoDB, SQL and Android.
Softwares known and worked on: Adobe Photoshop, Adobe Illustrator and Adobe After Effects.
/* Write a program in C++ using function template to read two matrices of different data types such as integers and floating point values and perform simple arithmetic operations on these matrices separately and display it. */ #include<iostream> #include<stdlib.h> using namespace std; template <class matrix> void add(matrix mat1[20][20],int r1,int c1,matrix mat2[20][20],int r2,int c2) { if( r1==r2 && c1==c2) { cout<<"\nADDITION:\n"; for( int i=0;i<r1;i++) { for( int j=0;j<c1;j++) { cout<<mat1[i][j]+mat2[i][j]<<"\t"; } cout<<"\n"; } } else cout<<"\nAddition can’t be possible\n"; } template <class matrix> void sub(matrix mat1[20][20],int r1,int c1,matrix mat2[20][20],int r2,int c2) { if( r1==r2 && c1==c2) { cout<<"\nSUBTRACTION:\n"; for( int i=0;i<r1;i++) { for( int j=0;j<c1;j++) { cout<<mat1[i][j]-mat2[i][j]<<"\t"; } cout<<"\n"; } } else cout<<"\nSubtraction can’t be possible\n"; } template <class matrix> void mul(matrix mat1[20][20],int r1,int c1,matrix mat2[20][20],int r2,int c2) { matrix temp; if( r2==c1) { cout<<"\nMULTIPLICATION:\n"; for( int i=0;i<r1;i++) { for( int j=0;j<c2;j++) { temp=0; for( int k=0;k<c1;k++) { temp=temp+(mat1[i][k]*mat2[k][j]); } cout<<temp<<"\t"; } cout<<"\n"; } } else cout<<"\nMultiplication can’t be possible\n"; } template <class t> void print(t mat1[20][20],int r1,int c1,t mat2[20][20],int r2,int c2) { cout<<"\nMATRIX 1:\n"; for( int i=0;i<r1;i++) { for( int j=0;j<c1;j++) { cout<<mat1[i][j]<<"\t"; } cout<<"\n"; } cout<<"\nMATRIX 2:\n"; for( int i=0;i<r2;i++) { for( int j=0;j<c2;j++) { cout<<mat2[i][j]<<"\t"; } cout<<"\n"; } } template <class m> void getdata(m mat1[20][20],int r1,int c1,m mat2[20][20],int r2,int c2) { cout<<"\nMATRIX 1:\n"; for( int i=0;i<r1;i++) { for( int j=0;j<c1;j++) { cout<<"Enter the data for a["<<i<<"]["<<j<<"]\t"; cin>>mat1[i][j]; } } cout<<"\nMATRIX 2:\n"; for( int i=0;i<r2;i++) { for( int j=0;j<c2;j++) { cout<<"Enter the data for a["<<i<<"]["<<j<<"]\t"; cin>>mat2[i][j]; } } } int main() { int choice; int a[20][20],b[20][20]; float c[20][20],d[20][20]; int r1,r2,c1,c2; while(1) { cout<<"\n\n************** WELCOME TO MATRIX CALCULATOR *****************\n\t1)INPUT\n\t2)DISPLAY\n\t3)ADDITION\n\t" <<"4)SUBTRACTION\n\t5)MULTIPLICATION\n\t6)EXIT\n\nEnter your choice\t"; cin>>choice; switch(choice) { case 1: cout<<"\nMATRIX 1:\n Enter number of row\t"; cin>>r1; cout<<"\nEnter number of column\t"; cin>>c1; cout<<"\nMATRIX 2:\n Enter number of row\t"; cin>>r2; cout<<"\nEnter number of column\t"; cin>>c2; getdata(a,r1,c1,b,r2,c2); break; case 2: print(a,r1,c1,b,r2,c2); break; case 3: add(a,r1,c1,b,r2,c2); break; case 4: sub(a,r1,c1,b,r2,c2); break; case 5: mul(a,r1,c1,b,r2,c2); break; case 6: exit(0); break; default: cout<<"\nyou have enter wrong choice plz try again"; } } }
Pratik Kataria is currently learning Springboot and Hibernate.
Technologies known and worked on: C/C++, Java, Python, JavaScript, HTML, CSS, WordPress, Angular, Ionic, MongoDB, SQL and Android.
Softwares known and worked on: Adobe Photoshop, Adobe Illustrator and Adobe After Effects.
/* Design a C++ base class consisting of the data members such as name of the student, rollnumber and subject. The derived class consists of the data members subject code, internal assessment and university examination marks. Construct a virtual base class for the item name of the student and roll number. The program should have the facilities. i) Build a master table ii) List a table iii) Insert a new entry iv) Delete old entry v) Edit an entry vi) Search for a record */ #include<iostream> #include<stdio.h> #include<fstream> #include<cstdlib> using namespace std; class base_student { protected: char stud_name[50],subj[20]; int roll_num; public: virtual void getData(); virtual void putData(); }; class derived_marks : protected base_student { int subj_code; double int_assm, uni_marks; public: void getData(); void putData(); int getrollno(); }; void base_student::getData() { cout<<"Enter Name–Subject–RollNum \n\t"<<endl; cin>>stud_name>>subj>>roll_num; } void base_student::putData() { cout<<"The Student Details Are: "<<endl; cout<<"\n Student Name: "<<stud_name; cout<<"\n Roll Number: "<<roll_num; cout<<"\n Subject: "<<subj; } void derived_marks::getData() { base_student::getData(); cout<<"Enter SubjectCode–Internal Assessment–University Marks \n\t"<<endl; cin>>subj_code>>int_assm>>uni_marks; } void derived_marks::putData() { base_student::putData(); cout<<"\n Subject Code: "<<subj_code; cout<<"\n Internal Assessment: "<<int_assm; cout<<"\n University Marks: "<<uni_marks<<endl; } int derived_marks::getrollno() { return (base_student::roll_num); } int main() { int i,ch,n,r,flag=0; ofstream ofd; ifstream ifd; derived_marks d1,d2; cout<<"Enter number of records: "; cin>>n; derived_marks d[10]; while(1) { cout<<"\nMenu: \n1)Enter Records \n2)Display Records \n3)Insert \n4)Search \n5)Delete \n6)Update \n7)Exit"<<endl; cin>>ch; switch(ch) { case 1: ofd.open("student_db.txt",ios::out|ios::trunc); for(i=0;i<n;i++) { d[i].getData(); ofd.write((char*)&d[i],sizeof(d[i])); } ofd.close(); break; case 2: ifd.open("student_db.txt",ios::in); while(ifd.read((char*)&d[i],sizeof(d[i]))) { d[i].putData(); } ifd.close(); break; case 3: ofd.open("student_db.txt",ios::app); d2.getData(); ofd.write((char*)&d2,sizeof(d2)); ofd.close(); break; case 4: cout<<"\nEnter Roll number you want to search: "; cin>>r; ifd.open("student_db.txt",ios::in); while(ifd.read((char*)&d1,sizeof(d1))) { if(d1.getrollno() == r) { cout<<"\nRecord found!"<<endl; d1.putData(); goto stop; } } cout<<"\nRecord does not exist!"<<endl; stop: ifd.close(); break; case 5: cout<<"\nEnter Roll number you want to delete: "; cin>>r; ofd.open("temp.txt",ios::out|ios::trunc); ifd.open("student_db.txt",ios::in); while(ifd.read((char*)&d1,sizeof(d1))) { if(d1.getrollno() == r) { cout<<"\nRecord has been deleted!"<<endl; flag =1; } else { ofd.write((char*)&d1,sizeof(d1)); } } if(flag==0) { cout<<"Record does not exist!"<<endl; } remove("student_db.txt"); rename("temp.txt", "student_db.txt"); ifd.close(); ofd.close(); break; case 6: cout<<"\nEnter Roll number you want to update: "; cin>>r; ofd.open("temp.txt",ios::out|ios::trunc); ifd.open("student_db.txt",ios::in); while(ifd.read((char*)&d1,sizeof(d1))) { if(d1.getrollno() == r) { d2.getData(); ofd.write((char*)&d2,sizeof(d2)); cout<<"\nRecord has been updated!"<<endl; flag =1; } else { ofd.write((char*)&d1,sizeof(d1)); } } if(flag==0) { cout<<"Record does not exist"<<endl; } remove("student_db.txt"); rename("temp.txt", "student_db.txt"); ifd.close(); ofd.close(); break; default: exit(0); } } return 0; }
Pratik Kataria is currently learning Springboot and Hibernate.
Technologies known and worked on: C/C++, Java, Python, JavaScript, HTML, CSS, WordPress, Angular, Ionic, MongoDB, SQL and Android.
Softwares known and worked on: Adobe Photoshop, Adobe Illustrator and Adobe After Effects.
/* Create a c++ class named Television that has data members to hold the model no., screen size and price. Member functions include overloaded insertion and extraction operators. If more than 4 digits are entered call the model if screen size is smaller than 12 and greater than 70 inches or if price is negative or over $5000 then provide the . Write a main function that instantiates a television object which allows users to enter data and display data members. If an exception is caught then replace all data members with 0. */ #include<iostream> using namespace std; class Television{ int modelno; double cost; double size; public: Television(){ modelno = 0; cost = 0; size = 0; } friend istream& operator >>(istream&input, Television &t){ int flag = 0; cout<<"Enter Model Number:"<<endl; input>>t.modelno; cout<<"Enter Cost of TV:"<<endl; input>>t.cost; cout<<"Enter size of TV:"<<endl; input>>t.size; try{ if(t.modelno > 999) throw(1); } catch(int i){ if(i){ cout<<"Model Number cannot be less than 999"<<endl; flag = 1; } } try{ if(t.size<12 || t.size>70) throw(2); } catch(int i){ if(i == 2){ cout<<"The screen size cannot be smaller than 12 inches or greater than 70 inches."<<endl; flag = 1; } } try{ if(t.cost<0 || t.cost>5000) throw(3); } catch(int i){ if (i==3){ cout<<"The cost cannot be negative or greater than $5000"<<endl; flag = 1; } } if(flag == 1) { t.modelno = 0; t.cost = 0; t.size = 0; } return(input); } friend ostream& operator <<(ostream&output, Television &t){ output<<"Model Number of TV: "<<t.modelno<<endl; output<<"Cost of TV: "<<t.cost<<endl; output<<"Size of TV: "<<t.size<<endl; return(output); } }; int main(){ Television t; cin>>t; cout<<t; return 0; }
Pratik Kataria is currently learning Springboot and Hibernate.
Technologies known and worked on: C/C++, Java, Python, JavaScript, HTML, CSS, WordPress, Angular, Ionic, MongoDB, SQL and Android.
Softwares known and worked on: Adobe Photoshop, Adobe Illustrator and Adobe After Effects.
<span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" class="mce_SELRES_start"></span>/* Implement C++/Java/Python program for bubble sort using function template */ #include<iostream> using namespace std; template <typename X> void show(int n,X arr[]); template <typename T> void bubblesort(int n,T arr[]) { T temp; cout<<"\n Before sorting elements are:"; show(n,arr); for(int i=0;i<n-1;i++) { for(int j=0;j<n-i-1;j++) { if(arr[j]>arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } cout<<"after sorting: \n"; show(n,arr); } template <class X> void show(int n,X arr[]) { cout<<endl; for(int k=0;k<n;k++) { cout<<arr[k]<<"\t"; } } int main() { int iarr[100],ch,n; float farr[100]; do { cout<<"\n \t BUBBLE SORT"; cout<<"\n ———————————"; cout<<"\n 1.Integer elements\n 2.Float element\n 3.Exit"; cout<<"\n ———————————"; cout<<"\n Enter your choice:"; cin>>ch; if(ch==1) { cout<<"\n Enter how many Integer Elements :"; cin>>n; cout<<"\n Enter array elements :"; for(int j=0;j<n;j++) cin>>iarr[j]; bubblesort(n,iarr); } if(ch==2) { cout<<"\n Enter how many Float elements :"; cin>>n; cout<<"\n Enter array element :"; for(int k=0;k<n;k++) cin>>farr[k]; bubblesort(n,farr); } }while(ch!=3); return 0; }
Pratik Kataria is currently learning Springboot and Hibernate.
Technologies known and worked on: C/C++, Java, Python, JavaScript, HTML, CSS, WordPress, Angular, Ionic, MongoDB, SQL and Android.
Softwares known and worked on: Adobe Photoshop, Adobe Illustrator and Adobe After Effects.
/* Google standard template library to use list container and using C++ implement following member functions of list class: empty, insert,merge,reverse,sort */ #include<iostream> #include<list> using namespace std; int main() { list<int> list1,list2; list<int>::iterator p; char ch; int a; do { cout<<"\n***************MENU****************\n"; cout<<"\n1.Create a list\n"; cout<<"\n2.Display a list\n"; cout<<"\n3.Insert an element\n"; cout<<"\n4.Check if list is empty\n"; cout<<"\n5.Reverse the list\n"; cout<<"\n6.Sort the list\n"; cout<<"\n7.Merge two lists\n"; cout<<"\n************************************\n"; cout<<"\n Enter your choice(1-7):\n"; cin>>a; switch(a) { case 1: int n,a; cout<<"\n Enter the number of elements you wish to enter:\n"; cin>>n; cout<<"\nEnter your elements:\n"; for(int i=0;i<n;i++) { cin>>a; list1.push_back(a); } cout<<"\n List created!\n"; break; case 2: for(p=list1.begin();p!=list1.end();++p) { cout<<*p<<" "; } break; case 3: int b; cout<<"Enter the new element:"; cin>>b; list1.push_back(b); cout<"\n Generating new list......\n"; cout<<"\n Displaying new list.......\n"; for(p=list1.begin();p!=list1.end();++p) { cout<<*p<<" "; } break; case 4: if(list1.empty()) { cout<<"\n The list is empty."; } else { cout<<"\n The list is not empty."; } break; case 5: list1.reverse(); for(p=list1.begin();p!=list1.end();++p) { cout<<*p<<" "; } break; case 6: list1.sort(); for(p=list1.begin();p!=list1.end();++p) { cout<<*p<<" "; } break; case 7: int ele; cout<<"\n Enter the number of elements you wish to enter:\n"; cin>>n; cout<<"\nEnter your elements:\n"; for(int i=0;i<n;i++) { cin>>ele; list2.push_back(ele); } list1.merge(list2); for(p=list1.begin();p!=list1.end();++p) { cout<<*p<<" "; } break; } cout<<"\n Do you want to continue?(y/n):\n"; cin>>ch; }while(ch=='y'); }
Pratik Kataria is currently learning Springboot and Hibernate.
Technologies known and worked on: C/C++, Java, Python, JavaScript, HTML, CSS, WordPress, Angular, Ionic, MongoDB, SQL and Android.
Softwares known and worked on: Adobe Photoshop, Adobe Illustrator and Adobe After Effects.
/* Using multicore programming implement posix_spawn() function to create a process. */ /*Execution: oct@CCOMPL08-10:~$ g++ templ.cpp -o mat.o oct@CCOMPL08-10:~$ g++ multicore.cpp -lpthread oct@CCOMPL08-10:~$ ./a.out */ #include<iostream> #include<spawn.h> #include<sys/types.h> #include<sys/wait.h> using namespace std; int main(int argc, char *argv[], char *env[]){ pid_t pid, pid1; int status, status1; status = posix_spawn(&pid, "/bin/ls", NULL, NULL, argv, env); cout<<"------------------------ \n"; if(status == 0) cout<<"Process created successfully... \n"; cout<<"------------------------ \n"; wait(&pid); status1 = posix_spawn(&pid1, "mat.o", NULL, NULL, argv, env); cout<<"------------------------ \n"; if(status1 == 0) cout<<"Process created successfully... \n"; cout<<"------------------------ \n"; wait(&pid1); return 0; }
Pratik Kataria is currently learning Springboot and Hibernate.
Technologies known and worked on: C/C++, Java, Python, JavaScript, HTML, CSS, WordPress, Angular, Ionic, MongoDB, SQL and Android.
Softwares known and worked on: Adobe Photoshop, Adobe Illustrator and Adobe After Effects.
#include <iostream> #include <stdlib.h> #include <pthread.h> #include <semaphore.h> using namespace std; pthread_mutex_t status; sem_t empty,full; int buffer[50],x,item; void initialization() { sem_init(&empty,1,0); sem_init(&full,1,50); pthread_mutex_init(&status,NULL); x=0; } void* producer(void *para) { item=rand()%100; sem_wait(&full); //Producer waits if buffer is full pthread_mutex_lock(&status); //Lock the status, so that only one thread can access buffer at a time buffer[x]=item; //producer enters the no. in the buffer cout<<"\nProducer has producer : "<<buffer[x]; x++; pthread_mutex_unlock(&status); //Unlocks the buffer sem_post(&empty); } void* consumer(void *para) { sem_wait(&empty);//if buffer is empty,consumer waits pthread_mutex_lock(&status);//Lock the status, so that only one thread can access buffer at a time x--; item=buffer[x];//Consumer consumes an element cout<<"\nConsumer has consumed : "<<buffer[x]; pthread_mutex_unlock(&status);//Unlocks the buffer sem_post(&full); } int main() { int n,i,p,c; initialization(); cout<<"\nEnter how many producers do you want to create : "; cin>>p; cout<<"\nEnter how many consumers do you want to create : "; cin>>c; pthread_t pid[p],cid; for(i=0;i<p;i++) { pthread_create(&pid[i],NULL,producer,NULL); } for(i=0;i<c;i++) { pthread_create(&cid[i],NULL,consumer,NULL); } for(i=0;i<p;i++) { pthread_join(pid[i],NULL); } for(i=0;i<c;i++) { pthread_join(cid[i],NULL); } return 0; }
Pratik Kataria is currently learning Springboot and Hibernate.
Technologies known and worked on: C/C++, Java, Python, JavaScript, HTML, CSS, WordPress, Angular, Ionic, MongoDB, SQL and Android.
Softwares known and worked on: Adobe Photoshop, Adobe Illustrator and Adobe After Effects.
/* Write object oriented program using C++ for passing command line arguement to thread functions and using command line arguement to determine number of threads to be created. Use multicore programming. */ /*Execution: oct@CCOMPL08-10:~$ g++ multicore1.cpp -lpthread oct@CCOMPL08-10:~$ ./a.out 5 5 */ #include<iostream> #include<pthread.h> #include<stdlib.h> using namespace std; void * display(void *param){ //Note the void* and parameter's void* cout<<"Thread created...\n"; cout<<(char*) param<<"\n"; } int main(int argc, char * argv[]) { pthread_t tid[5]; for(int i=0; i<atoi(argv[1]);i++){//C function atoi helps convert (char*) to int pthread_create(&tid[i], NULL, display, (void *) argv[2]); } for(int j=0; j<atoi(argv[1]);j++){ pthread_join(tid[j], NULL); //Here NULL is the return code which is given. } return 0; }
Pratik Kataria is currently learning Springboot and Hibernate.
Technologies known and worked on: C/C++, Java, Python, JavaScript, HTML, CSS, WordPress, Angular, Ionic, MongoDB, SQL and Android.
Softwares known and worked on: Adobe Photoshop, Adobe Illustrator and Adobe After Effects.
/* Write a program for Matrix Multiplication using multicore programming. */ #include<iostream> #include<pthread.h> using namespace std; int MAT1[10][10],MAT2[10][10],MAT3[10][10]; int r1=3,c1=3,r2=3,c2=3; void *multi(void *p) { int i=0; i= (int &)p; for(int j=0;j<3;j++) { //MAT3[i][j]=0; for(int k=0;k<3;k++) { MAT3[i][j]=MAT3[i][j]+(MAT1[i][k]*MAT2[k][j]); } } } void read_mat(int MAT[10][10], int r, int c) { cout<<"Enter the elements in matrix...: "<<endl; for(int i=0;i<r;i++) { for(int j=0;j<c;j++) { cout<<"Enter the elements for row and column here: "<<endl; cin>>MAT[i][j]; } } } void print_mat(int MAT[10][10], int r, int c) { cout<<"\n"; for(int i=0;i<r;i++) { cout<<"\n"; for(int j=0;j<c;j++) { cout<<MAT[i][j]<<"\t"; } cout<<"\n"; } } main() { pthread_t tid[3]; read_mat(MAT1,r1,c1); read_mat(MAT2,r2,c2); cout<<"MATRIX 1"<<endl; print_mat(MAT1,r1,c1); cout<<"MATRIX 2"<<endl; print_mat(MAT2,r2,c2); for(int i=0;i<3;i++) { pthread_create(&tid[i],NULL,multi,(void *)i); } for(int i=0;i<3;i++) { pthread_join (tid[i],NULL); } cout<<"FINAL MATRIX"<<endl; print_mat(MAT3,r1,c2); }
Pratik Kataria is currently learning Springboot and Hibernate.
Technologies known and worked on: C/C++, Java, Python, JavaScript, HTML, CSS, WordPress, Angular, Ionic, MongoDB, SQL and Android.
Softwares known and worked on: Adobe Photoshop, Adobe Illustrator and Adobe After Effects.