for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (a[j] > a[j+1])
swap(a[j], a[j+1]);
Thursday, July 29, 2010
// dfgdfdfg.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
// within iostream
// istream cin;
// ostream cout;
using namespace std;
bool aComposite[1024] = { true, true };
bool isPrime(int n)
{
return ! aComposite[n];
}
void setupLookupTable()
{
for (int i = 2; i < 1024; i++)
{
// is this number i am visiting
// already marked off as composite
if (aComposite[i] == false)
for (int j=i+i; j < 1024; j+=i)
aComposite[j] = true;
}
}
void print(const int a[], int capacity)
{
for (int i = 0; i < capacity; i++)
cout << a[i] << ' ';
cout << endl;
}
void add(const int a[], const int b[], int c[], int capacity)
{
for (int i = 0; i < capacity; i++)
c[i] = a[i] + b[i];
}
void add(const int a[][3], const int b[][3], int c[][3], int rows, int cols)
{
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
c[i][j] = a[i][j] + b[i][j];
}
void print(const int C[][3], int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
cout << "[" << setw(3) << C[i][j] << "]";
cout << endl;
}
}
int dotProduct(const int A[3][3], const int B[3][3], const int row, const int col)
{
int total = 0;
for (int i = 0; i < 3; i++)
total += A[row][i] * B[i][col];
return total;
}
void mmult(const int A[3][3], const int B[3][3], int C[3][3], const int rows, const int cols)
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
C[i][j] = dotProduct(A, B, i, j);
}
int minimumValue(const int a[], const int capacity)
{
int minValue = a[0];
for (int i = 1; i < capacity; i++)
if (a[i] < minValue)
minValue = a[i];
return minValue;
}
int minimumPosition(const int a[], const int startPos, const int capacity)
{
int minPos = startPos;
for (int i = startPos + 1; i < capacity; i++)
if (a[i] < a[minPos])
minPos = i;
return minPos;
}
void subtract(const int a[], const int b[], int c[], int capacity)
{
for (int i = 0; i < capacity; i++)
c[i] = a[i] - b[i];
}
void mult(const int a[], int scalar, int b[], int capacity)
{
for (int i = 0; i < capacity; i++)
b[i] = a[i] * scalar;
}
int foo(int *a)
{
}
int main() {
int v[10];
foo(v);
foo(&v[0]);
cout << v << endl;
cout << &v[0] << endl;
v[i]
*(v+i)
//setupLookupTable();
//for(int k = 0; k < 1024; k++)
// if (isPrime(k))
// cout << k << endl;
//string s, t;
//int u;
//ifstream jin;
//jin.open("myfirstinput.txt");
//jin >> s;
//jin >> t;
//cout << s << t << endl;
//jin.close();
//ofstream jout;
//jout.open("c:/josh/myfirstoutput.txt");
//for(int i = 0; i < 10; i++)
// jout << i << " ";
//jout << endl;
//jout.close();
//system("PAUSE");
int A[3][3] =
{
{ 4, 6, 0},
{ 6, 0, 1},
{ -1,3, 5}
};
int B[3][3] =
{
{ 0, 6, 0},
{ 1, 0, 1},
{ 1, 0, 5}
};
int C[3][3];
cout << "DP: " << dotProduct(A, B, 0, 1) << endl;
add(A, B, C, 3, 3);
print(C, 3, 3);
cout << endl;
cout << endl;
cout << endl;
int x[5] = {3,4,5,2,3};
int y[5] = {1,1,0,2,3};
int z[5];
int a[5];
// a = x * 6
add(x, y, z, 5);
print(z, 5);
mult(y, 6, z, 5);
print(z, 5);
return 0;
}
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
// within iostream
// istream cin;
// ostream cout;
using namespace std;
bool aComposite[1024] = { true, true };
bool isPrime(int n)
{
return ! aComposite[n];
}
void setupLookupTable()
{
for (int i = 2; i < 1024; i++)
{
// is this number i am visiting
// already marked off as composite
if (aComposite[i] == false)
for (int j=i+i; j < 1024; j+=i)
aComposite[j] = true;
}
}
void print(const int a[], int capacity)
{
for (int i = 0; i < capacity; i++)
cout << a[i] << ' ';
cout << endl;
}
void add(const int a[], const int b[], int c[], int capacity)
{
for (int i = 0; i < capacity; i++)
c[i] = a[i] + b[i];
}
void add(const int a[][3], const int b[][3], int c[][3], int rows, int cols)
{
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
c[i][j] = a[i][j] + b[i][j];
}
void print(const int C[][3], int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
cout << "[" << setw(3) << C[i][j] << "]";
cout << endl;
}
}
int dotProduct(const int A[3][3], const int B[3][3], const int row, const int col)
{
int total = 0;
for (int i = 0; i < 3; i++)
total += A[row][i] * B[i][col];
return total;
}
void mmult(const int A[3][3], const int B[3][3], int C[3][3], const int rows, const int cols)
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
C[i][j] = dotProduct(A, B, i, j);
}
int minimumValue(const int a[], const int capacity)
{
int minValue = a[0];
for (int i = 1; i < capacity; i++)
if (a[i] < minValue)
minValue = a[i];
return minValue;
}
int minimumPosition(const int a[], const int startPos, const int capacity)
{
int minPos = startPos;
for (int i = startPos + 1; i < capacity; i++)
if (a[i] < a[minPos])
minPos = i;
return minPos;
}
void subtract(const int a[], const int b[], int c[], int capacity)
{
for (int i = 0; i < capacity; i++)
c[i] = a[i] - b[i];
}
void mult(const int a[], int scalar, int b[], int capacity)
{
for (int i = 0; i < capacity; i++)
b[i] = a[i] * scalar;
}
int foo(int *a)
{
}
int main() {
int v[10];
foo(v);
foo(&v[0]);
cout << v << endl;
cout << &v[0] << endl;
v[i]
*(v+i)
//setupLookupTable();
//for(int k = 0; k < 1024; k++)
// if (isPrime(k))
// cout << k << endl;
//string s, t;
//int u;
//ifstream jin;
//jin.open("myfirstinput.txt");
//jin >> s;
//jin >> t;
//cout << s << t << endl;
//jin.close();
//ofstream jout;
//jout.open("c:/josh/myfirstoutput.txt");
//for(int i = 0; i < 10; i++)
// jout << i << " ";
//jout << endl;
//jout.close();
//system("PAUSE");
int A[3][3] =
{
{ 4, 6, 0},
{ 6, 0, 1},
{ -1,3, 5}
};
int B[3][3] =
{
{ 0, 6, 0},
{ 1, 0, 1},
{ 1, 0, 5}
};
int C[3][3];
cout << "DP: " << dotProduct(A, B, 0, 1) << endl;
add(A, B, C, 3, 3);
print(C, 3, 3);
cout << endl;
cout << endl;
cout << endl;
int x[5] = {3,4,5,2,3};
int y[5] = {1,1,0,2,3};
int z[5];
int a[5];
// a = x * 6
add(x, y, z, 5);
print(z, 5);
mult(y, 6, z, 5);
print(z, 5);
return 0;
}
Wednesday, July 28, 2010
// dfgdfdfg.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
// within iostream
// istream cin;
// ostream cout;
using namespace std;
bool aComposite[1024] = { true, true };
bool isPrime(int n)
{
return ! aComposite[n];
}
void setupLookupTable()
{
for (int i = 2; i < 1024; i++)
{
// is this number i am visiting
// already marked off as composite
if (aComposite[i] == false)
for (int j=i+i; j < 1024; j+=i)
aComposite[j] = true;
}
}
void print(const int a[], int capacity)
{
for (int i = 0; i < capacity; i++)
cout << a[i] << ' ';
cout << endl;
}
void add(const int a[], const int b[], int c[], int capacity)
{
for (int i = 0; i < capacity; i++)
c[i] = a[i] + b[i];
}
void add(const int a[][3], const int b[][3], int c[][3], int rows, int cols)
{
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
c[i][j] = a[i][j] + b[i][j];
}
void print(const int C[][3], int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
cout << "[" << setw(3) << C[i][j] << "]";
cout << endl;
}
}
void subtract(const int a[], const int b[], int c[], int capacity)
{
for (int i = 0; i < capacity; i++)
c[i] = a[i] - b[i];
}
void mult(const int a[], int scalar, int b[], int capacity)
{
for (int i = 0; i < capacity; i++)
b[i] = a[i] * scalar;
}
int main() {
//setupLookupTable();
//for(int k = 0; k < 1024; k++)
// if (isPrime(k))
// cout << k << endl;
//string s, t;
//int u;
//ifstream jin;
//jin.open("myfirstinput.txt");
//jin >> s;
//jin >> t;
//cout << s << t << endl;
//jin.close();
//ofstream jout;
//jout.open("c:/josh/myfirstoutput.txt");
//for(int i = 0; i < 10; i++)
// jout << i << " ";
//jout << endl;
//jout.close();
//system("PAUSE");
int A[3][3] =
{
{ 4, 6, 0},
{ 6, 0, 1},
{ -1,3, 5}
};
int B[3][3] =
{
{ 0, 6, 0},
{ 1, 0, 1},
{ 1, 0, 5}
}; ;
int C[3][3];
add(A, B, C, 3, 3);
print(C, 3, 3);
x = 6, y = 9, i++, p++;
cout << endl;
cout << endl;
cout << endl;
int x[5] = {3,4,5,2,3};
int y[5] = {1,1,0,2,3};
int z[5];
int a[5];
// a = x * 6
add(x, y, z, 5);
print(z, 5);
mult(y, 6, z, 5);
print(z, 5);
return 0;
}
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
// within iostream
// istream cin;
// ostream cout;
using namespace std;
bool aComposite[1024] = { true, true };
bool isPrime(int n)
{
return ! aComposite[n];
}
void setupLookupTable()
{
for (int i = 2; i < 1024; i++)
{
// is this number i am visiting
// already marked off as composite
if (aComposite[i] == false)
for (int j=i+i; j < 1024; j+=i)
aComposite[j] = true;
}
}
void print(const int a[], int capacity)
{
for (int i = 0; i < capacity; i++)
cout << a[i] << ' ';
cout << endl;
}
void add(const int a[], const int b[], int c[], int capacity)
{
for (int i = 0; i < capacity; i++)
c[i] = a[i] + b[i];
}
void add(const int a[][3], const int b[][3], int c[][3], int rows, int cols)
{
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
c[i][j] = a[i][j] + b[i][j];
}
void print(const int C[][3], int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
cout << "[" << setw(3) << C[i][j] << "]";
cout << endl;
}
}
void subtract(const int a[], const int b[], int c[], int capacity)
{
for (int i = 0; i < capacity; i++)
c[i] = a[i] - b[i];
}
void mult(const int a[], int scalar, int b[], int capacity)
{
for (int i = 0; i < capacity; i++)
b[i] = a[i] * scalar;
}
int main() {
//setupLookupTable();
//for(int k = 0; k < 1024; k++)
// if (isPrime(k))
// cout << k << endl;
//string s, t;
//int u;
//ifstream jin;
//jin.open("myfirstinput.txt");
//jin >> s;
//jin >> t;
//cout << s << t << endl;
//jin.close();
//ofstream jout;
//jout.open("c:/josh/myfirstoutput.txt");
//for(int i = 0; i < 10; i++)
// jout << i << " ";
//jout << endl;
//jout.close();
//system("PAUSE");
int A[3][3] =
{
{ 4, 6, 0},
{ 6, 0, 1},
{ -1,3, 5}
};
int B[3][3] =
{
{ 0, 6, 0},
{ 1, 0, 1},
{ 1, 0, 5}
}; ;
int C[3][3];
add(A, B, C, 3, 3);
print(C, 3, 3);
x = 6, y = 9, i++, p++;
cout << endl;
cout << endl;
cout << endl;
int x[5] = {3,4,5,2,3};
int y[5] = {1,1,0,2,3};
int z[5];
int a[5];
// a = x * 6
add(x, y, z, 5);
print(z, 5);
mult(y, 6, z, 5);
print(z, 5);
return 0;
}
// add two matrices
int A[3][3];
int B[3][3];
int C[3][3];
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
cout << C[i][j] << " ";
int i =0;
++i
this means: (i = i+1, new value of i)
i++
this means: (temp = i, i = i+ 1, temp)
while(i = 0, i < 10, ++i)
cout << i << " ";
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
main()
{
int x = 7;
int y = 8;
swap(&x, &y);
cout << x << " " << y << endl;
}
int A[3][3];
int B[3][3];
int C[3][3];
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
cout << C[i][j] << " ";
int i =0;
++i
this means: (i = i+1, new value of i)
i++
this means: (temp = i, i = i+ 1, temp)
while(i = 0, i < 10, ++i)
cout << i << " ";
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
main()
{
int x = 7;
int y = 8;
swap(&x, &y);
cout << x << " " << y << endl;
}
Tuesday, July 27, 2010
// dfgdfdfg.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
// within iostream
// istream cin;
// ostream cout;
using namespace std;
bool aComposite[1024] = { true, true };
bool isPrime(int n)
{
return ! aComposite[n];
}
void setupLookupTable()
{
for (int i = 2; i < 1024; i++)
{
// is this number i am visiting
// already marked off as composite
if (aComposite[i] == false)
for (int j=i+i; j < 1024; j+=i)
aComposite[j] = true;
}
}
void print(const int a[], int capacity)
{
for (int i = 0; i < capacity; i++)
cout << a[i] << ' ';
cout << endl;
}
void add(const int a[], const int b[], int c[], int capacity)
{
for (int i = 0; i < capacity; i++)
c[i] = a[i] + b[i];
}
void subtract(const int a[], const int b[], int c[], int capacity)
{
for (int i = 0; i < capacity; i++)
c[i] = a[i] - b[i];
}
void mult(const int a[], int scalar, int b[], int capacity)
{
for (int i = 0; i < capacity; i++)
b[i] = a[i] * scalar;
}
int main() {
setupLookupTable();
for(int k = 0; k < 1024; k++)
if (isPrime(k))
cout << k << endl;
string s, t;
int u;
ifstream jin;
jin.open("c:/josh/myfirstinput.txt");
jin >> s;
jin >> t;
cout << s << t << endl;
jin.close();
ofstream jout;
jout.open("c:/josh/myfirstoutput.txt");
for(int i = 0; i < 10; i++)
jout << i << " ";
jout << endl;
jout.close();
system("PAUSE");
int A[3][3] =
{
{ 4, 6, 0},
{ 6, 0, 1},
{ -1,3, 5}
};
int B[3][3];
int C[3][3];
int x[5] = {3,4,5,2,3};
int y[5] = {1,1,0,2,3};
int z[5];
int a[5];
// a = x * 6
add(x, y, z, 5);
print(z, 5);
mult(y, 6, z, 5);
print(z, 5);
return 0;
}
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
// within iostream
// istream cin;
// ostream cout;
using namespace std;
bool aComposite[1024] = { true, true };
bool isPrime(int n)
{
return ! aComposite[n];
}
void setupLookupTable()
{
for (int i = 2; i < 1024; i++)
{
// is this number i am visiting
// already marked off as composite
if (aComposite[i] == false)
for (int j=i+i; j < 1024; j+=i)
aComposite[j] = true;
}
}
void print(const int a[], int capacity)
{
for (int i = 0; i < capacity; i++)
cout << a[i] << ' ';
cout << endl;
}
void add(const int a[], const int b[], int c[], int capacity)
{
for (int i = 0; i < capacity; i++)
c[i] = a[i] + b[i];
}
void subtract(const int a[], const int b[], int c[], int capacity)
{
for (int i = 0; i < capacity; i++)
c[i] = a[i] - b[i];
}
void mult(const int a[], int scalar, int b[], int capacity)
{
for (int i = 0; i < capacity; i++)
b[i] = a[i] * scalar;
}
int main() {
setupLookupTable();
for(int k = 0; k < 1024; k++)
if (isPrime(k))
cout << k << endl;
string s, t;
int u;
ifstream jin;
jin.open("c:/josh/myfirstinput.txt");
jin >> s;
jin >> t;
cout << s << t << endl;
jin.close();
ofstream jout;
jout.open("c:/josh/myfirstoutput.txt");
for(int i = 0; i < 10; i++)
jout << i << " ";
jout << endl;
jout.close();
system("PAUSE");
int A[3][3] =
{
{ 4, 6, 0},
{ 6, 0, 1},
{ -1,3, 5}
};
int B[3][3];
int C[3][3];
int x[5] = {3,4,5,2,3};
int y[5] = {1,1,0,2,3};
int z[5];
int a[5];
// a = x * 6
add(x, y, z, 5);
print(z, 5);
mult(y, 6, z, 5);
print(z, 5);
return 0;
}
Monday, July 26, 2010
Code from class today; also recursive definition of division via repeated subtraction
int mult(int x, int y)
{
int result = 0;
for (int i = 0; i < y; i++)
result += x;
return result;
}
int mult(int x, int y)
{
if (y == 0) return 0;
if (y == 1) return x;
return x + mult(x, y-1);
}
x / 1 = x
x / n = (x - n) / (n - 1)
100 / 10 = ( 100 - 10) / (10 - 1)
int sum(int a[], int capacity)
{
int total = 0;
for(int i = 0; i < capacity; i++)
total += a[i];
return total;
}
main()
{
int x[5] = {6, 10, 23, 12, 1};
cout << sum(x, 5);
}
{
int result = 0;
for (int i = 0; i < y; i++)
result += x;
return result;
}
int mult(int x, int y)
{
if (y == 0) return 0;
if (y == 1) return x;
return x + mult(x, y-1);
}
x / 1 = x
x / n = (x - n) / (n - 1)
100 / 10 = ( 100 - 10) / (10 - 1)
int sum(int a[], int capacity)
{
int total = 0;
for(int i = 0; i < capacity; i++)
total += a[i];
return total;
}
main()
{
int x[5] = {6, 10, 23, 12, 1};
cout << sum(x, 5);
}
Thursday, July 22, 2010
http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
using this, write a function to tell me if a number is prime
bool aComposite[1025];
void setupArray()
{
}
bool isPrime(int n)
{
return ! aComposite[n];
}
bool isPrime(int n)
{
if (n== 1 || n == 2)
return true;
for (int i = 2; i <= n-1; i++)
if (n % i == 0)
return false;
return true;
}
using this, write a function to tell me if a number is prime
bool aComposite[1025];
void setupArray()
{
}
bool isPrime(int n)
{
return ! aComposite[n];
}
bool isPrime(int n)
{
if (n== 1 || n == 2)
return true;
for (int i = 2; i <= n-1; i++)
if (n % i == 0)
return false;
return true;
}
Thursday, July 8, 2010
const double hourlyRate = 16.78;
const double ssRate = 0.06;
const double fedRate = 0.14;
const double stateRate = 0.05;
const int unionDues = 10;
const int healthInsurance = 35;
// input: hoursWorked, numDependents
// output: grossPay, withholdingamts, netPay
int main()
{
cout << "....";
cin >> hoursWorked >> numDependents;
double grossPay;
double netPay;
double ssTax, fedTax, stateTax, health = 0;
grossPay = (hoursWorked > 40 ? 40 * hourlyRate + (hoursWorked-40)*
hourlyRate*1.5 : hoursWorked * hourlyRate);
if (hoursWorked > 40)
grossPay = 40 * hourlyRate + (hoursWorked-40)*hourlyRate*1.5;
else
grossPay = hoursWorked * hourlyRate;
ssTax = grossPay * ssRate;
fedTax = grossPay * fedRate;
stateTax = grossPay * stateRate;
if (numDependents >= 3)
health = healthInsurance;
netPay = grossPay - ssTax - fedTax - stateTax - health;
cout <<
}
// input: currentPrice, numYears, inflationRate
// output: futureCost
int main()
{
double currentPrice, inflationRate, futureCost;
int numYears;
cout << ...
cin >> currentPrice >> numYears >> inflationRate;
inflationRate /= 100;
futureCost = currentPrice;
for(int i = 0; i < numYears; i++)
futureCost = futureCost + futureCost * inflationRate;
cout << futureCost;
}
int main()
{
cout << "Please enter 10 numbers";
int n, sumPositive, sumNegative;
sumPositive = sumNegative = 0;
for (int i = 1; i <= 10; i++)
{
cin >> n;
if (n > 0)
sumPositive += n;
else
sumNegative += n;
}
cout << sumPositive << " " << sumNegative << " " << sumPositive + sumNegative << endl;
}
int main()
{
cout << ...;
double n;
cin >> n;
// compute sqrt(n)
double guess = n / 2;
double r;
do
{
r = n / guess;
prevGuess = guess;
guess = (guess + r) / 2;
} while (abs(guess - prevGuess) > 0.01);
}
const double ssRate = 0.06;
const double fedRate = 0.14;
const double stateRate = 0.05;
const int unionDues = 10;
const int healthInsurance = 35;
// input: hoursWorked, numDependents
// output: grossPay, withholdingamts, netPay
int main()
{
cout << "....";
cin >> hoursWorked >> numDependents;
double grossPay;
double netPay;
double ssTax, fedTax, stateTax, health = 0;
grossPay = (hoursWorked > 40 ? 40 * hourlyRate + (hoursWorked-40)*
hourlyRate*1.5 : hoursWorked * hourlyRate);
if (hoursWorked > 40)
grossPay = 40 * hourlyRate + (hoursWorked-40)*hourlyRate*1.5;
else
grossPay = hoursWorked * hourlyRate;
ssTax = grossPay * ssRate;
fedTax = grossPay * fedRate;
stateTax = grossPay * stateRate;
if (numDependents >= 3)
health = healthInsurance;
netPay = grossPay - ssTax - fedTax - stateTax - health;
cout <<
}
// input: currentPrice, numYears, inflationRate
// output: futureCost
int main()
{
double currentPrice, inflationRate, futureCost;
int numYears;
cout << ...
cin >> currentPrice >> numYears >> inflationRate;
inflationRate /= 100;
futureCost = currentPrice;
for(int i = 0; i < numYears; i++)
futureCost = futureCost + futureCost * inflationRate;
cout << futureCost;
}
int main()
{
cout << "Please enter 10 numbers";
int n, sumPositive, sumNegative;
sumPositive = sumNegative = 0;
for (int i = 1; i <= 10; i++)
{
cin >> n;
if (n > 0)
sumPositive += n;
else
sumNegative += n;
}
cout << sumPositive << " " << sumNegative << " " << sumPositive + sumNegative << endl;
}
int main()
{
cout << ...;
double n;
cin >> n;
// compute sqrt(n)
double guess = n / 2;
double r;
do
{
r = n / guess;
prevGuess = guess;
guess = (guess + r) / 2;
} while (abs(guess - prevGuess) > 0.01);
}
Wednesday, July 7, 2010
// dfgdfdfg.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
//DISPLAY 3.4 The Importance of Braces
//Illustrates the importance of using braces in if-else statements.
#include
using namespace std;
enum month { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sept, Oct, Nov, Dec};
//Program to illustrate the switch statement.
#include
using namespace std;
int main( )
{
month myMonth;
myMonth = Jan;
if (myMonth == Mar)
char grade;
cout << "Enter your midterm grade and press Return: ";
cin >> grade;
if (grade == 'A')
{
cout << "Excellent. "
<< "You need not take the final.\n";
}
else if (grade == 'B')
{
cout << "Very good. ";
grade = 'A';
cout << "Your midterm grade is now "
<< grade << endl;
}
else if (grade == 'C')
{
}
else if ()
{
}
else
{
}
switch (grade)
{
case 'A':
cout << "Excellent. "
<< "You need not take the final.\n";
break;
case 'B':
cout << "Very good. ";
grade = 'A';
cout << "Your midterm grade is now "
<< grade << endl;
break;
case 'C':
cout << "Passing.\n";
break;
case 'D':
case 'F':
cout << "Not good. "
<< "Go study.\n";
break;
default:
cout << "That is not a possible grade.\n";
}
cout << "End of program.\n";
return 0;
}
//
#include "stdafx.h"
//DISPLAY 3.4 The Importance of Braces
//Illustrates the importance of using braces in if-else statements.
#include
using namespace std;
enum month { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sept, Oct, Nov, Dec};
//Program to illustrate the switch statement.
#include
using namespace std;
int main( )
{
month myMonth;
myMonth = Jan;
if (myMonth == Mar)
char grade;
cout << "Enter your midterm grade and press Return: ";
cin >> grade;
if (grade == 'A')
{
cout << "Excellent. "
<< "You need not take the final.\n";
}
else if (grade == 'B')
{
cout << "Very good. ";
grade = 'A';
cout << "Your midterm grade is now "
<< grade << endl;
}
else if (grade == 'C')
{
}
else if ()
{
}
else
{
}
switch (grade)
{
case 'A':
cout << "Excellent. "
<< "You need not take the final.\n";
break;
case 'B':
cout << "Very good. ";
grade = 'A';
cout << "Your midterm grade is now "
<< grade << endl;
break;
case 'C':
cout << "Passing.\n";
break;
case 'D':
case 'F':
cout << "Not good. "
<< "Go study.\n";
break;
default:
cout << "That is not a possible grade.\n";
}
cout << "End of program.\n";
return 0;
}
input: amtToKillMouse, mouseWeight, humanWeight
output: amtToKillHuman in oz of sweetener, and in oz of soda
int main()
{
const double percentSweetenerInSoda = 0.001;
cout >> ....
cin >> amtToKillMouse >> mouseWeight >> humanWeight;
amtToKillHuman = (amtToKillMouse * humanWeight) / mouseWeight;
cout << "amt sweetener in oz to kill human is" << amtToKillHuman;
cout << "amt soda in oz to kill human is" << amtToKillHuman / percentSweetenerInSoda;
return 0;
}
output: amtToKillHuman in oz of sweetener, and in oz of soda
int main()
{
const double percentSweetenerInSoda = 0.001;
cout >> ....
cin >> amtToKillMouse >> mouseWeight >> humanWeight;
amtToKillHuman = (amtToKillMouse * humanWeight) / mouseWeight;
cout << "amt sweetener in oz to kill human is" << amtToKillHuman;
cout << "amt soda in oz to kill human is" << amtToKillHuman / percentSweetenerInSoda;
return 0;
}
// input: boxWeight in ounces
// output: boxWeight in metric tons
// output: how many boxes in 1 metric ton
int main()
{
double boxWeight;
double bwInMetricTons;
int numBoxes;
char ch;
do
{
cout << "please enter box weight in oz.\n";
cin >> boxWeight;
// transform boxWeight into bwInMetricTons
bwInMetricTons = boxWeight * (1/35273.962);
numBoxes = 35,273.962 / boxWeight;
cout << "In metric tons, this is "<< endl;
cout << "The total # boxes in a metric ton is " << numBoxes << endl;
cout << "Would you like to calculate again?";
cin >> ch;
} while (ch == 'y' || ch == 'Y');
return 0;
}
// 1 mt = 35,273.962 oz
// 1 oz = 1/
// 35,273.962 / boxWeight
// output: boxWeight in metric tons
// output: how many boxes in 1 metric ton
int main()
{
double boxWeight;
double bwInMetricTons;
int numBoxes;
char ch;
do
{
cout << "please enter box weight in oz.\n";
cin >> boxWeight;
// transform boxWeight into bwInMetricTons
bwInMetricTons = boxWeight * (1/35273.962);
numBoxes = 35,273.962 / boxWeight;
cout << "In metric tons, this is "<< endl;
cout << "The total # boxes in a metric ton is " << numBoxes << endl;
cout << "Would you like to calculate again?";
cin >> ch;
} while (ch == 'y' || ch == 'Y');
return 0;
}
// 1 mt = 35,273.962 oz
// 1 oz = 1/
// 35,273.962 / boxWeight
Tuesday, July 6, 2010
while
for
do-while
syntactic sugar
conditionals
a == b
a != b
!( a == b)
a < b
a > b
a >= b
a <= b
|| OR
&& AND
a = 5;
b = 2;
if (a == b || c == d)
c = 6;
if (a < c < b)
if (a < c && c < b)
{
infinite loop
int i = 0, j =0;
while (i < 10)
{
cout << "Hello" << endl;
j++;
}
sum of the numbers from 1 to 50
FOR LOOP
for (int i = 0; i < 10; i++)
{
cout << "Hello!\n";
}
for
do-while
syntactic sugar
conditionals
a == b
a != b
!( a == b)
a < b
a > b
a >= b
a <= b
|| OR
&& AND
a = 5;
b = 2;
if (a == b || c == d)
c = 6;
if (a < c < b)
if (a < c && c < b)
{
infinite loop
int i = 0, j =0;
while (i < 10)
{
cout << "Hello" << endl;
j++;
}
sum of the numbers from 1 to 50
FOR LOOP
for (int i = 0; i < 10; i++)
{
cout << "Hello!\n";
}
Thursday, July 1, 2010
Subscribe to:
Posts (Atom)