Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
cmsc245, study guide for your final test DO and DO NOT do study all the old tests do study all the programming projects do ask TAs if you have doubts do read the book do ask for clarifications during the test do not panic do not be later do not mess up the examine paper Book sections to read: 2.1, 2.2 basic types, variable, C++ program structure. 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, expressions o numerical Expression, +,-,*,/,%, o Logical (Boolean) expression: ==, >, <, >=, <=, !, &, |, o charact exp o all mixed up expression. 4.2, 4.3, 4.4: functions, if, local variables, and for statements 5.4: string and related operations. o [], +, <<, ==, <, > o size(), empty(), substr(), replace(), find() 6.1, 6.2: if statements o nested if statements o = and == 9.2: file IO o ifstream, ofstream o open(), getline(), eof(), close() 10.2: array o using for loop to process array data o index o swap, insert, delete some code reading exercise string and array #include <iostream> #include <fstream> #include<string> using namespace std; void testfun() { cout<<"hello"<<endl; cout<<"world"<<endl; } void testfunc2() { int arrayx[5]; int * ptr; ptr=0; *(ptr)=1; } int main() { char carray[100]="abcdefg"; string s1, s2="row, row, row you boat", s3="row", s4="boat"; cout << s2[3]; cout<< s1.size()+s4.size(); cout<<s1.length(); cout<< (s3 < s4); s1= s3 + s4; cout<<s1; cout<< "xxx"+s1+s2+s4+"end"<<endl; cout<<s2.substr(1,3)<<endl; cout << s2.find("ow",0)<<endl; cout << s2.find("ow", 10)<<endl; s1=s2; cout<<s1<<endl; int ind; ind=s1.find("b",0); s1[ind]='g'; for (int i=0;i<=10;i+=5) { s1[i]='m'; } cout<< s1 << endl; } file IO include <iostream> #include <fstream> #include<string> using namespace std; int main() { int intarray[1000]; int newValue; int totalnum=0; ifstream inStream; //inStream.open(filename); //call the open function with the provided filename path inStream.open("C:\\CMSC245\\p5\\fileio\\fileio\\p6data.txt"); if (inStream==0){ cout<<"\nfile not exist, return\n"; //return 0; } int i; string empname; int length; int firstspace; int secondspace; string oneline; string firstname[100]; string lastname[100]; string agestr; int age[100]; //getline(inStream, empname); cout<<endl; for(;;) { getline(inStream,oneline); if (inStream.eof()) break; length=oneline.size(); cout <<oneline; firstspace=oneline.find(" ",0); firstname[totalnum]=oneline.substr(0,firstspace); secondspace=oneline.find(" ",firstspace+1); lastname[totalnum]=oneline.substr(firstspace+1,secondspacefirstspace); agestr= oneline.substr(secondspace+1,length-secondspace); age[totalnum]=atoi(agestr.c_str()); //two steps con: string->c-str (char array)->int cout<<"First Name:"<<firstname[totalnum]<<"...."<<"Last Name:"; cout<<lastname[totalnum]<<"....Age:"<<age[totalnum]<<endl; //intarray[totalnum]=newValue; totalnum++; } for (i=0;i<totalnum;i++) cout<< intarray[i] <<" "; cout<<endl; inStream.close(); ofstream outStream; //inStream.open(filename); //call the open function with the provided filename path outStream.open("C:\\CMSC245\\p5\\fileio\\fileio\\p6dataout.txt"); if (outStream==0){ cout<<"\nfile not exist, return\n"; //return 0; } for(i=0;i<totalnum;i++) outStream<<firstname[i]<<" "<<age[i]<<" "<<lastname[i]<<endl; outStream.close(); } more on array double xValue[5]; for ( int i =0; i<=4;i++) xValue[i]= double (i)/2.0; i xValue 1 0 {0.00,,,,} 2 1 {0.00, 0.5,,,} 3 2 {0.00, 0.5, 1.0,,,} 4 5 3 4 {0.00,0.5,1.0,1.5,,} {0.00,0.5,1.0,1.5,2.0} 20 for (int i=0; i<5; i++) if (i%2 ==0) number [i] = 2*i; else number [i] = 2*i+1; i i%2 i%2 ==0 number[] 1 0 0 True 1 {0,,,,} 2 1 1 False 0 {0,3,,,} 3 2 0 True 1 {0,3,4,,} 4 3 1 False 0 {0,3,4,7 5 4 0 True 1 {0,3,4,7,8} Do the following exercises! make sure you get it. int number[5]={1}; for (int i =1;i<5;i++) number[i]=2*number[i-1]; number[4]=3; for (int i=3;i>=0;i--) number[i]=2*number[i+1]; int number[8]={1,9,8,4,6,2,3,5} int tmp; for (int i=0;i<5;i++) { if (number[i] > number[8-i-1]) { tmp=number[i]; number[8-i-1]=tmp; number[i]= number[8-i-1] } } prime number * Program to find prime numbers till 'n' !! */ #include <iostream> #include <math.h> using namespace std; void list(int n); bool prime_num(int num); void main() { int num; cout << "Enter a number :"; cin >> num; list(num); } void list(int n) { bool isPrime; cout << "The prime numbers less than " << n << " are :"; cout << "2"; for (int i=3; i<n; i++) { isPrime = prime_num(i); if(isPrime) { cout << " " << i; } } cout << "\n"; } bool prime_num(int num) { bool a = true; for (int j=2; j<=sqrt((double)num); j++) { if (((int)num%j)== 0) { a = false; between 2 - square root of number } } return a; } if statement and expressions void main() { int y=0, x = 5,z = 2,m=0; for (x=5; x >1;x=x-1) { m=x+m; if (x>y) if(x>z) m=x; else m=z; else if (y>z) m=y else m=z; y=y+2; z=z+1; cout<<“x is”<<x; cout<< "m is”<<m ; } cout<<"m is: ” +m ; } }