C Plus Plus Tutorial/2011 Run
From Intra
Contents[hide] |
[edit] Quadratic equations
- Trial 1:
#include <iostream> #include<math.h> #include<stdlib.h> using namespace std; int main() { double a,b,c; cout<< "input values of a:"; cin >> a; cout << "input value of b:"; cin >> b; cout << "input value for c:"; cin >> c; cout << "you have entered a=" << a << " and b=" << b << " and c=" << c<<endl; double root; root=b*b-4*a*c; if (a==0) { double x; if(b==0) { cout << "Not an equation!\n"; }else { x=-c/b; cout<< "the value of x is: "<<x<<endl; } }else { if (root>0) { //cout << "block 1"; double x1,x2; x1=(-b+sqrt(root))/2*a; x2=(-b-sqrt(root))/2*a; cout << "the value of x1 is: "<<x1<<endl; cout << "the value of x2 is: "<<x2<<endl; } else { if (root==0) { //cout << "block 2"; double x; x=-b/(2*a); cout << "the value of x is: "<<x<<endl; } else { cout << "it is complex:"; // cout << "block 3"; } } } }
- Trial 2:
#include <iostream> #include <math.h> using namespace std; int main() { double a , b, c; cout << "input value a:\n"; cin >> a; cout << "input value b:\n"; cin >> b; cout << "input valu c:\n"; cin >> c; cout << "Now you have entered a ="<< a << "and b =" << b << "and c=" << c; double root; root=b*b-4*a*c; if (root>0) { double dividend; dividend=2*a; if (dividend==0) { // fill this with number 1 from flow chart cout << "Block 1:\n"; double x; x=2*a; cout << "Answer is : " << -c/b << endl; } else { //Fill this in with number 2 from FLOW CHAART cout << "Block 2:\n"; double x1 , x2; x1=(-b+sqrt(b*b-4*a*c))/(2*a); x2=(-b-sqrt(b*b-4*a*c))/(2*a); cout << "The Value of x1:"<< x1 << "\n"; cout <<"The valu of x2:"<< x2 << "\n"; } } else { if (root==0) { // Fill with number 3 cout << "Block 3:\n"; double x; x=-b/(2*c); cout << "The value of x:"<< x <<"\n"; } else { //fILL with number 4 cout << "Block 4:\n"; cout << "It is a Com[plex Number:\n"; } } }
[edit] Loops
#include <iostream> using namespace std; int main() { int i=0; while(i<9) { cout << "The value of 'i' is: "<<i << endl; i=i+2; } }
#include <iostream> using namespace std; int main() { int num=0; while(true) { cout << "Enter an odd number:"; cin >> num; if(num%2!=0) { cout << "OK!"; break; } cout << "No! its not an odd number!"; } }
[edit] For loop
#include <iostream> using namespace std; int main() { for(int i=0;i<9;i++) // i+=3 => i=i+3 { cout << "The value of 'i' is: "<<i << endl; } }
[edit] Hard-coded for loop in action
#include <iostream> using namespace std; int main() { /* a silly program that asks the user to enter five values and after that, it will print the five values and their average */ double number[5]; for(int i=0;i<5;i++) { cout << "Enter a number:"; double n; cin >> n; number[i]=n; } // Now we have five numbers double sum; sum=0; for(int i=0;i<5;i++) { sum=sum + number [i]; cout << number[i] << " "; } cout << "The average is: " << sum/5 <<"\n"; }
[edit] Same with constant
#include <iostream> using namespace std; #define NNUMBERS 3 int main() { /* a silly program that asks the user to enter five values and after that, it will print the five values and their average */ double number[NNUMBERS]; for(int i=0;i<NNUMBERS;i++) { cout << "Enter a number:"; double n; cin >> n; number[i]=n; } // Now we have five numbers double sum; sum=0; for(int i=0;i<NNUMBERS;i++) { sum=sum + number [i]; cout << number[i] << " "; } cout << "The average is: " << sum/NNUMBERS <<"\n"; }
[edit] Vectors
#include <iostream> #include <vector> using namespace std; int main() { /* a silly program that asks the user to enter five values and after that, it will print the five values and their average */ vector <double> number; for(int i=0;true;i++) { cout << "Enter a number (or -9999 to stop):"; double n; cin >> n; if(n==-9999) { break; } number.push_back(n); } // Now we have five numbers double sum; sum=0; for(unsigned int i=0;i<number.size();i++) { sum=sum + number [i]; // or number.at(i) cout << number[i] << " "; } cout << "The average is: " << sum/number.size() <<"\n"; }
[edit] Reading from files
0 0 0 0 2.3 0 0 0 0 1.0 1.5 2.0 3.5 2.1 0 0 0 0 0 0 0 0 0 0 0 0 0 .1 1.0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[edit] Reading a file
#include <iostream> #include <fstream> #include <stdlib.h> using namespace std; int main() { //open the file ifstream myfile("../data/rainfall.txt"); // myfile here is the name of the handle if(!myfile.is_open())// if myfil is not properly opened { cout << "File could not be opend!"; exit(5); } double val; for(int i=0;myfile.good() ;i++) { myfile >> val; cout << val << endl; } }
[edit] Functions
[edit] Greet
#include <iostream> using namespace std; void greet(){ // function definition. cout << "Hello, World!" << endl; } int main() { greet(); //function call }
[edit] Adding
#include <iostream> using namespace std; double addtwo(double x, double y) { double z=x+y; return z; } int main() { double a, b; cout <<"Enter a number: "; cin >> a; cout << "\nEnter another number:"; cin >> b; double c; c=addtwo(a,b); cout << endl << "Sum is :" << c << endl; }
[edit] Prime
#include <iostream> using namespace std; /** Takes an integer argument and tests if it is a multiple of any number other than 1 and itself. if yes, return false if no, return true */ bool isPrime(int num) { // now I want to check if ^^ is the case for(int i=2;i<num-1;i++) { if(num%i==0) { return false; } } return true; } /** Non ending, but dumb prime number finder */ int main() { // let's not bother about mathematics // so start with 3 for(int i=3;true;i++) { // now we have i // I want someone else to find if i can be // a multiple of any number other than 1 and i bool prime; prime=isPrime(i); if(prime==true) { cout << i << " "; } /* if(i%200==0) { cin.get(); } */ } }
[edit] Rainfall Processor
- first iteration(Just prints statistics)
#include <iostream> #include <fstream> #include <stdlib.h> #include <math.h> #include <vector> using namespace std; int main() { //open the file ifstream funabashi ("../data/funabashi_hourly_rainfall.txt"); // my file here is the name of the handle if(!funabashi.is_open()) //if my file is not properlly opened { cout << "File could not be opened, sorry!"; exit(5); } vector <double> val; for (int i=0; funabashi.good(); i++) { double num; funabashi>> num; val.push_back(num); /*if((i+1)%24==0) { cout<<val[i]<< "\n"; }else { cout<<val[i]<< "\t"; }*/ } //now we have the vector val with the total dataset double sum; sum=0; double avg; int j; //counter for rainy hours (values greater than zero) j=0; double pec; for (unsigned int i=0; i<val.size(); i++) { sum=sum+val[i]; if (val[i]!=0) { j++; } } cout<<sum<<"\n"; avg=sum/val.size(); cout<<avg<<"\n"; cout<<j<<"\n"; pec=(j/(double)val.size())*100; cout<<pec<<"\n"; cout<<val.size(); }
[edit] Iteration-2 (hourly to daily)
[edit] Iteration-3 (with functions)
[edit] Text handling
[edit] Character arrays
#include <iostream> using namespace std; int main() { char hell[50]; cout << "Enter your name: "; cin >> hell; cout << "Hello, " << hell << "!\n"; }
#include <iostream> #include <string.h> using namespace std; int main() { char secret[]="openme"; char pass[50]; cout << "Enter your password: "; cin >> pass; // compare secret with pass if(strcmp(secret,pass)==0){ cout << "OK!"; }else { cout << "No!!"; } }
#include <iostream> #include <string.h> #include <stdio.h> using namespace std; int main() { char dir[500], fin[50]; cout << "Enter directory:"; cin >> dir; cout << "Enter filename:"; cin >> fin; char path[550]; // add two string together sprintf(path,"%s/%s",dir,fin); cout << path << endl; }
[edit] C++ Strings
#include <iostream> #include <string> #include <string.h> using namespace std; int main() { string fname, lname; // join two strings cout << "Enter your first name: "; cin >> fname; cout << "Enter your last name"; cin >> lname; string fullname; fullname=fname+" "+lname; cout << "hello: " << fullname << endl; char tmp[500]; strcpy(tmp,fullname.c_str()); cout << "hello: " << tmp << endl; }
[edit] Pointers
#include <iostream> using namespace std; int main() { double k; // k is a decimal number. double * n; // dereference of n is a decimal number. // n points to a decimal number // pointee of n is a decimal number. k=5.0; cout << "k=" << k << endl; n=&k; // assign the address of k to n. (*n)=8.0; // find out the location that is referred to by n, and put 8.0 there. cout << "k=" << k << endl; }
[edit] Pass by reference
#include <iostream> using namespace std; /** divides first argument by the second and returns the answer */ double divide(double a, double b) { double result; result=a/b; return result; } /** integer divides the first argument by second and set the value of the address of 3rd and 4th arugments to anwer and modulo */ void idivide(int a, int b, int * ans, int * rem) { int res1, res2; res2=a%b; res1=(a-res2)/b; *ans=res1; *rem=res2; } int main() { double x=5.0; double y=2.0; double z=divide(x,y); cout << z << endl; int l=15; int m=4; int p, q; idivide(l,m,&p,&q); // passing by reference (p, q) cout << "answer: " << p << " with remaining: " << q << endl; }