Question 1 --------------------------------------------------------- #include #include using namespace std; int main() { string filename; float x, sum=0; int count=0; cout << "Enter input filename: "; cin >> filename; const char* fs = filename.c_str(); ifstream MyReadFile(fs); while (MyReadFile >> x) { count++; sum = sum + x; } // Close the file MyReadFile.close(); cout << "sum = " << sum << endl; cout << "count = " << count << endl; cout << "average = " << sum/count << endl; return 0; } Question 2 --------------------------------------------------------- #include #include using namespace std; int main() { string ifile, ofile; int count=0, nums[100], i; cout << "Input filename: "; cin >> ifile; cout << "Output filename: "; cin >> ofile; const char* ifs = ifile.c_str(); const char* ofs = ofile.c_str(); ifstream MyReadFile(ifs); ofstream MyWriteFile(ofs); while (MyReadFile >> nums[count]) count++; for(i=count-1;i>=0;i--) MyWriteFile << nums[i] << endl; // Close the file MyReadFile.close(); MyWriteFile.close(); cout << "Program completed successfully" << endl; return 0; } Question 3 --------------------------------------------------------- #include #include using namespace std; int main() { int count=0, nums[100], i, key; ifstream MyReadFile("q03.txt"); while (MyReadFile >> nums[count]) count++; while(true){ cout << "Enter the number = "; cin >> key; i = 0; while(key != nums[i] && i < count) i++; if(i==count) cout << "Not found" << endl; else cout << "It is at position " << i << endl; } // Close the file MyReadFile.close(); cout << "Program completed successfully" << endl; return 0; } Question 5(a) ----------------------------------------------------------------------------------------- int main(){ char x; ofstream ofile("text5a.txt"); ifstream ifile("text5.txt"); while(ifile >> x){ ofile << (char)(x-1); } return 0; } Question 5(b) ----------------------------------------------------------------------------------------- int main(){ char x,y; ofstream ofile("text5b.txt"); ifstream ifile("text5.txt"); ifile>>x; ifile>>y; while(!ifile.eof()){ ofile << y; ofile << x; ifile>>x; ifile>>y; } if(x!=EOF) ofile << x; ifile.close(); ofile.close(); return 0; } Question 6 --------------------------------------------------------------------------------------- int main(){ string word, sub="era"; bool found; int j,k; ofstream ofile("q6_out.txt"); ifstream ifile("q6_in.txt"); int i=0; while(ifile >> word){ j=0; found = true; while(j<=(word.length()-3)&&found){ k=0; found = true; while(k<3&&found){ if(word[j+k]!=sub[k]) found=false; k++; } if(found){ ofile << word << endl; found = false; } else found=true; j++; } } ofile.close(); ifile.close(); return 0; } Question 7 ------------------------------------------------------------------------------------ #include #include using namespace std; string names[100]; string tel[100]; int readFile(){ int i=0; ifstream f("q7.txt"); do{ f>>names[i]; f>>tel[i]; i++; }while(!f.eof()); f.close(); return i-1; } void writeFile(int total){ int i; ofstream f("q7.txt"); for(i=0;i> names[total]; cout << "Tel no: "; cin >> tel[total]; return 1; } } int deleteRecord(int total){ int i,j; string n; if(total==0){ cout << "The storage is empty" << endl; return 0; } else{ cout << "Deleting record..." << endl; cout << "Name: "; cin >> n; i=0; while(names[i]!=n&&i> n; i=0; while(names[i]!=n&&i> t; tel[i]=t; } } int getMenuChoice(){ int choice; cout << "Telephone Enquiry System\n1) List Records\n2) Insert Record\n3) Delete Record\n4) Updat$ do{ cout << "Choice :"; cin >> choice; }while(choice<0||choice>5); return choice; } int main(){ int total, choice; total=readFile(); do{ choice=getMenuChoice(); if(choice==1) listRecord(total); else if(choice==2) total=total+insertRecord(total); else if(choice==3) total=total-deleteRecord(total); else if(choice==4) updateRecord(total); else writeFile(total); }while(choice!=5); return 0; }