Sunteți pe pagina 1din 12

/* DOCTOR V's EEG 206 TUTORIAL II */

//1. Understanding int, float and double

#include <iostream> //for cout and cin


using namespace std;

int main ()
{
int _ax = 20.578, i = 5252436882;
float y = 34.56789123, j = 2.01300006;
double z = 0.00034556, k = 34556777809;
cout << "FOR INTS ==> _ax is " << _ax << " while i is " << i << endl << endl;

cout << "FOR FLOATS ==> y is " << y << " while j is " << j << endl << endl;
cout << "FOR DOUBLES ==> z is " << z << " while k is " << k << endl << endl;
return 0;
}

// 2. Increment & Decrement Operators (rearrange for accumulation operators)

#include <iostream>
using namespace std;

int main ()
{
int x = 20, i = 5;
x = ++i ;
cout << "x is " << x << " while i is " << i << endl << endl;
return 0;
}

// 3. Use of #define

#include <iostream>
using namespace std;
#define elect 3.14159265
int main ()
{
float x = 20, y;
y = x/elect ;
cout << "x is " << x << ", elect is " << elect << " while y is " << y <<
endl << endl;
return 0;
}

// 4. Data Cast

#include <iostream>
using namespace std;
int main ()
{
int x = 20;
float y,z;
y = x/3;
z = (double)x/3;
cout << "uncasted is " << y << " while casted is " << z << endl << endl;
return 0;
}

// 5. if Statement

#include <iostream>
using namespace std;

int main ()
{
int x ;
cout << "Enter a positive integer \n";
cin >> x;

if (x>=100)

{
cout << "\nThis number has more than 2 digits\n" <<endl;

return 0;
}

// 6. if else Statement

#include <iostream>
using namespace std;

int main ()
{
int x ;
cout << "Enter a positive integer \n";
cin >> x;

if (x>=100)

{
cout << "\nThis number has more than 2 digits\n" <<endl;

}
else
{
cout << "\nThis is a number with less than 3 digits\n" << endl;
}
return 0;
}

// 7. if - else if - else Statement

#include <iostream>
using namespace std;

int main ()
{
int x ;
cout << "Enter an integer \n";
cin >> x;

if (x==1)
// change == to = and it gives poor results
{
cout << "\nThis is number 1" <<endl;

}else if(x==5)
{
cout << "\nThis is the fifth number" << endl;

}else
{
cout << "\nI don't know what you're talking about" << endl;
}

cout << endl << endl;

return 0;
}

// 8. Quadratic Equation

#include <iostream>
// #include <math.h> // was necessary before in Visual studio 6.0
using namespace std;

int main ()
{
float a, b, c, p, x1, x2;

cout << "Enter the values of a, b and c in the quadratic equation" << endl;
cout << "a = " ;
cin >> a;
cout << "b = " ;
cin >> b;
cout << "c = " ;
cin >> c;

p = sqrt (b*b - 4*a*c);

cout << "p = " << p << endl;


x1 = (-b + p)/2*a;
x2 = (-b - p)/2*a;

cout << "x1 = "<< x1 << endl;

cout << "x2 = "<< x2 << endl;

return 0;
}

***********************************************************************************
*********************************

// Doctor Vee's tutorial for EEG 206

//9. A program that solves a quadratic equation even if the roots are complex

#include<iostream>
using namespace std;
int main()
{
float a, b, c, p, x1, x2;
cout << " enter the values of a, b and c in the quadratic equation " << endl;
cout << " a = " ;
cin >> a;
cout << " b = ";
cin >> b;
cout << " c = ";
cin >> c;

p = b*b - 4*a*c;

if(p>0)
{
// for real and unequal roots
cout << " p = " << p << endl;
x1 = (- b + sqrt (p))/ (2*a);
x2 = (- b - sqrt(p))/ (2*a);
cout << " x1 = " << x1 << endl;
cout << " x2 = " << x2 << endl;
}
else if (p==0)
{
// for real and equal roots only one result is displayed
cout << " p = " << p << endl;
x1 = (- b + sqrt (p))/ (2*a);
cout << " x1 = " << x1 << endl;
}
else
{
// for complex roots
cout << " p = " << p << endl;
cout << " x1 = " << -b/(2*a) << " + " << (sqrt (-p)) / (2*a) << "j" << endl;
cout << " x2 = " << -b/(2*a) << " - " << (sqrt (-p) )/ (2*a) << "j" << endl;
}
return 0;
}

// 10. switch Statement

#include <iostream>
using namespace std;

int main ()
{
int x ;
cout << "Enter an integer \n";
cin >> x;

switch (x)
{
case 1:
cout << "\nThis is number 1" <<endl;
break;

case 2:
cout << "\nThis is the difference between 3 and 1" <<endl;
break;

case 3:
cout << "\nThis is the square root of 9" <<endl;
break;

case 4:
cout << "\nThis is the square of 2" <<endl;
break;

case 5:
cout << "\nThis is the fifth number" << endl;
break;

default:
cout << "\nI no sabi this one o" << endl;
}

cout << endl << endl;

return 0;
}
// 11a. for Statement

// Powers of 5 from 1 to 1 Million

#include <iostream>
using namespace std;

int main ()
{
int x, i = 1;
for (x=5; x<=1000000; x*=5)
{
cout << "5 raised to power " << i << " is equal to " << x << endl;
i++;
}

return 0;
}

// 11b. while Statement

// Powers of 2 from 1 to 1 Million

#include <iostream>
using namespace std;

int main ()
{
int x=1, i = 0;

while (x<=1000000)
{
cout << "2 raised to power " << i << " is equal to " << x << endl;
i++;
x*=2;
}

return 0;
}

// 12 Ex.1 Barbara Johnston Chapter 3 Question 18


#include <iostream>
using namespace std;

int main ()
{
int x = 10, y = 6, ctr = 0;

cout << "Hi There!" << endl << endl;

while (ctr < 3)


{
cout << x << y;
++ctr;
y--;
}

do
{
cout << "\nI Love C++!";
}while(ctr<3); // change 3 to 4 and see the necessity of the loop
altering statement

return 0;
}

// 12 Ex.2 Barbara Johnston Chapter 3 Question 19

#include <iostream>
using namespace std;

int main ()
{
int a = 5, b = 8;

while (a < b)
{
cout << "\n Red" ;
++a;
}
do
{
cout << "\n Blue";
++a;
}while(a<6);
cout << "\n END!";

return 0;
}
//13. Pointers - accessing and assigning variables - Volume of a cylider question

#include <iostream>
using namespace std;

int main ()
{
float radius, height, volume;
float *radius_ptr = &radius, *height_ptr = &height;
float pi = 3.14159265;
char answer;
do
{

cout << "Enter the radius and height of your cylinder \nRadius = ";
cin >> *radius_ptr;
cout << "Height = ";
cin >> *height_ptr;

volume = pi * (*radius_ptr) * (*radius_ptr) * (*height_ptr);

cout << endl << "The volume of your cylinder is " << volume << endl << endl;
cout << "\n\nDo you still want to try again? \t y = yes and n = no" << endl;
cin >> answer;

} while(answer=='y');
return 0;
}

//4. Functions using pointers - Find Big One Question

#include <iostream>
using namespace std;
void GetTwoNumbers (int *, int *);
int FindBigOne (int , int );
int main ()
{
int num1, num2, x;
char answer;

do
{
GetTwoNumbers (&num1, &num2);
x = FindBigOne (num1, num2);
cout << "\n\nThe Bigger Number is " << x << endl << endl;
cout << "Do you want to try again? y=yes others=no" <<endl;
cin >> answer;
cout << endl << endl << endl;
}
while (answer == 'y');
return 0;
}

void GetTwoNumbers (int *a_ptr, int *b_ptr)


{
int a, b;
cout << "Enter Two Integers" << endl;
cout << "\nInteger 1 = " ;
cin >> a ;
cout << "Integer 2 = ";
cin >> b;

*a_ptr = a;
*b_ptr = b;
}

int FindBigOne (int N1, int N2)


{
if (N1>=N2)
{
return N1;
}else{
return N2;
}
}

5. Pointers - accessing and assigning variables - Volume of a cylider question

#include <iostream>
using namespace std;

int main ()
{
float radius, height, volume;
float *radius_ptr = &radius, *height_ptr = &height;
float pi = 3.14159265;
char answer;
do
{

cout << "Enter the radius and height of your cylinder \nRadius = ";
cin >> *radius_ptr;
cout << "Height = ";
cin >> *height_ptr;

volume = pi * (*radius_ptr) * (*radius_ptr) * (*height_ptr);

cout << endl << "The volume of your cylinder is " << volume << endl << endl;
cout << "\n\nDo you still want to try again? \t y = yes and n = no" << endl;
cin >> answer;

} while(answer=='y');
return 0;
}

//Dr Vee Tuts


//14/11/2014

#include <iostream>
using namespace std;

int main ()
{
int seed;

cout << "Enter a seed value" << endl;


cin >> seed;
cout << "\nYour number is ";

srand (seed);

int x;
x = rand ();
cout << x << endl << endl;

}
#include <iostream>
#include <cstdlib>
using namespace std;

void GetALetter();

int main()
{
int seed,i;
char answer;
cout << "\t WELCOME TO SECRETE CODE PROGRAMME \n\n";
do
{
cout << "enter any seed value for random code \n";
cin >> seed;
srand(seed);
GetALetter();

cout << "\n for another coded message type 'y' or any other key to quit \n";
cin >> answer;
cout << endl;
}
while(answer=='y');
cout << "\n Thank you and GOODBYE \n";
}

void GetALetter()
{
int i,x;
for(i=1;i<=15;i++)
{
x=(rand()%26)+65;
cout << (char)x;
}
cout << endl;
}

#include <iostream>
using namespace std;

int main ()
{
int good = 345;
int *bad = &good;;

*bad = 7566;

cout << good << endl;


}

S-ar putea să vă placă și