loops (nested)
arrays (1d, 2d)
functions (pass by ref, by val, arrays)
recursion (tracing recursion)
sorting
Wednesday, August 4, 2010
Tuesday, August 3, 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;
}
}
//template <int rows, int cols>
//int dotProduct(int (&A)[rows][cols],
// int (&B)[rows][cols],
// 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;
//}
//template <int rows, int cols>
//void mmult(int (&A)[rows][cols], int (&B)[rows][cols], int (&C)[rows][cols])
//{
// for (int i = 0; i < rows; i++)
// for (int j = 0; j < cols; 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 selectionSort(int A[], int capacity)
{
for (int i = 0; i < capacity; i++)
{
int minPos = minimumPosition(A, i, capacity);
swap(A[i], A[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)
{
return 0;
}
template <typename T, typename S>
void swap2(T &a, S &b)
{
T temp = a;
a = b;
b = temp;
}
void print(char s[])
{
for (int i = 0; s[i]; i++)
cout << s[i];
}
void print2(char *s)
{
for (; *s; s++)
cout << *s;
}
int main() {
char name[10] = "hello";
char fred[10] = "hello";
strcmp(name, fred)
if (name < fred)
if (strcmp(name, fred) < 0)
if (name > fred)
if (strcmp(name, fred) > 0)
if(name == fred)
if (strcmp(name, fred) == 0)
fred = name;
strcpy(fred, name);
for (int i = 0; name[i] != 0; i++)
cout << name[i];
cout << endl;
print2(name);
cout << endl;
fred = name;
if (name == fred)
{
}
string b = "hi ";
b += "there";
//int A[3][3], B[4][3];
//dotProduct(A, B, 2, 1);
// int first = 2;
// float second = 3;
// swap2<int, int>(first, second);
// cout << first << " " << second << endl;
// int v[10];
// foo(v);
// foo(&v[0]);
// cout << v << endl;
// cout << &v[0] << endl;
//
//
//
// //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;
}
// iterative approach
void insert(int a[], int capacity)
{
// yank out the element
int value = a[capacity-1];
// shift items over
for (int j = capacity - 2; j >= 0 && a[j] > value; j--)
a[j+1] = a[j];
a[j+1] = value;
}
void insertionSort(int a[], int capacity)
{
for (int i = 1; i <= capacity; i++)
insert(a, i);
}
to sort an array of size n,
first: sort the array of size n -1
insert element n into that sorted partial result of array n-1
// using recursion
void insertionSort(int a[], int capacity)
{
if (capacity == 1)
return;
insertionSort(a, capacity - 1);
insert(a, capacity);
}
// strings
c-strings
C++ strings
char name[10] = "hello";
for (int i = 0; name[i] != 0; i++)
cout << name[i];
//
#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;
}
}
//template <int rows, int cols>
//int dotProduct(int (&A)[rows][cols],
// int (&B)[rows][cols],
// 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;
//}
//template <int rows, int cols>
//void mmult(int (&A)[rows][cols], int (&B)[rows][cols], int (&C)[rows][cols])
//{
// for (int i = 0; i < rows; i++)
// for (int j = 0; j < cols; 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 selectionSort(int A[], int capacity)
{
for (int i = 0; i < capacity; i++)
{
int minPos = minimumPosition(A, i, capacity);
swap(A[i], A[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)
{
return 0;
}
template <typename T, typename S>
void swap2(T &a, S &b)
{
T temp = a;
a = b;
b = temp;
}
void print(char s[])
{
for (int i = 0; s[i]; i++)
cout << s[i];
}
void print2(char *s)
{
for (; *s; s++)
cout << *s;
}
int main() {
char name[10] = "hello";
char fred[10] = "hello";
strcmp(name, fred)
if (name < fred)
if (strcmp(name, fred) < 0)
if (name > fred)
if (strcmp(name, fred) > 0)
if(name == fred)
if (strcmp(name, fred) == 0)
fred = name;
strcpy(fred, name);
for (int i = 0; name[i] != 0; i++)
cout << name[i];
cout << endl;
print2(name);
cout << endl;
fred = name;
if (name == fred)
{
}
string b = "hi ";
b += "there";
//int A[3][3], B[4][3];
//dotProduct(A, B, 2, 1);
// int first = 2;
// float second = 3;
// swap2<int, int>(first, second);
// cout << first << " " << second << endl;
// int v[10];
// foo(v);
// foo(&v[0]);
// cout << v << endl;
// cout << &v[0] << endl;
//
//
//
// //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;
}
// iterative approach
void insert(int a[], int capacity)
{
// yank out the element
int value = a[capacity-1];
// shift items over
for (int j = capacity - 2; j >= 0 && a[j] > value; j--)
a[j+1] = a[j];
a[j+1] = value;
}
void insertionSort(int a[], int capacity)
{
for (int i = 1; i <= capacity; i++)
insert(a, i);
}
to sort an array of size n,
first: sort the array of size n -1
insert element n into that sorted partial result of array n-1
// using recursion
void insertionSort(int a[], int capacity)
{
if (capacity == 1)
return;
insertionSort(a, capacity - 1);
insert(a, capacity);
}
// strings
c-strings
C++ strings
char name[10] = "hello";
for (int i = 0; name[i] != 0; i++)
cout << name[i];
Monday, August 2, 2010
int A[4];
A[0] at 100
A[1] at 104
A[2] at 108
A[3] at 112
A[10]
*(A + 10)
memory location 100 + (10 * sizeof int)
void foo(int p[])
{
for (int i = 0; i < 4; i++)
cout << *(p+i);
}
int * bar(int p[])
{
int B[5] = {5, 4, 3, 1, 6};
B[3] = 7;
return p;
}
main()
{
int A[4];
for (int i = 0; i < 4; i++)
cout << A[i] << " ";
for (int i = 0; i < 4; i++)
cout << *(A+i) << " ";
cout << endl;
foo(A);
int *q = bar(A);
}
template <typename T>
void swap2(T &a, T &b)
{
T temp = a;
a = b;
b = temp;
}
// recursive bubblesort
void bubbleSort(int a[], int capacity)
{
if (capacity == 1)
return;
// do one sweep
for(int i = 0; i < capacity; i++)
if (a[i] > a[i+1])
swap(a[i], a[i+1])
bubbleSort(a, capacity - 1);
}
// 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;
}
}
template <int rows, int cols>
int dotProduct(int (&A)[rows][cols],
int (&B)[rows][cols],
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;
}
template <int rows, int cols>
void mmult(int (&A)[rows][cols], int (&B)[rows][cols], int (&C)[rows][cols])
{
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; 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;
}
int selectionSort(int A[], int capacity)
{
for (int i = 0; i < capacity; i++)
{
int minPos = minimumPosition(A, i, capacity);
swap(A[i], A[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)
{
return 0;
}
template <typename T, typename S>
void swap2(T &a, S &b)
{
T temp = a;
a = b;
b = temp;
}
int main() {
int A[3][3], B[4][3];
dotProduct(A, B, 2, 1);
// int first = 2;
// float second = 3;
// swap2<int, int>(first, second);
// cout << first << " " << second << endl;
// int v[10];
// foo(v);
// foo(&v[0]);
// cout << v << endl;
// cout << &v[0] << endl;
//
//
//
// //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;
}
A[0] at 100
A[1] at 104
A[2] at 108
A[3] at 112
A[10]
*(A + 10)
memory location 100 + (10 * sizeof int)
void foo(int p[])
{
for (int i = 0; i < 4; i++)
cout << *(p+i);
}
int * bar(int p[])
{
int B[5] = {5, 4, 3, 1, 6};
B[3] = 7;
return p;
}
main()
{
int A[4];
for (int i = 0; i < 4; i++)
cout << A[i] << " ";
for (int i = 0; i < 4; i++)
cout << *(A+i) << " ";
cout << endl;
foo(A);
int *q = bar(A);
}
template <typename T>
void swap2(T &a, T &b)
{
T temp = a;
a = b;
b = temp;
}
// recursive bubblesort
void bubbleSort(int a[], int capacity)
{
if (capacity == 1)
return;
// do one sweep
for(int i = 0; i < capacity; i++)
if (a[i] > a[i+1])
swap(a[i], a[i+1])
bubbleSort(a, capacity - 1);
}
// 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;
}
}
template <int rows, int cols>
int dotProduct(int (&A)[rows][cols],
int (&B)[rows][cols],
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;
}
template <int rows, int cols>
void mmult(int (&A)[rows][cols], int (&B)[rows][cols], int (&C)[rows][cols])
{
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; 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;
}
int selectionSort(int A[], int capacity)
{
for (int i = 0; i < capacity; i++)
{
int minPos = minimumPosition(A, i, capacity);
swap(A[i], A[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)
{
return 0;
}
template <typename T, typename S>
void swap2(T &a, S &b)
{
T temp = a;
a = b;
b = temp;
}
int main() {
int A[3][3], B[4][3];
dotProduct(A, B, 2, 1);
// int first = 2;
// float second = 3;
// swap2<int, int>(first, second);
// cout << first << " " << second << endl;
// int v[10];
// foo(v);
// foo(&v[0]);
// cout << v << endl;
// cout << &v[0] << endl;
//
//
//
// //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;
}
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
Wednesday, June 30, 2010
// candyweight.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
using namespace std;
int main()
{
cout << setw(4) << 5 << endl;
cout << setw(4) << 25 << endl;
double pay;
pay = 10.50 * 23.333;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "The pay is $" << pay << endl;
// declare and initialize our variable
int numBars;
numBars=0;
double barWeight = 0;
cout << "Please enter # of bars & weight of 1 bar: ";
cin >> numBars >> barWeight;
// this is a comment!
cout << "\a\a\a\"Total weight is " << numBars * barWeight << endl;
cout << "Please enter # of bars & weight of 1 bar for another brand: ";
cin >> numBars >> barWeight;
cout << "Total weight is " << numBars * barWeight << "\n";
return 0;
}
data type
4 int
4 long int
4.6 float
4.6 double
4.6 long double
"Josh" string
true bool
'x' char
unsigned
signed
long
short
pico hello.cpp
g++ hello.cpp
./a.out
int numpods, peasperpod, total;
double temp;
string name;
hw:
write a prog.
variables: one, two, three, four, five
data type double
1.000, 1.414, 1.732, 2.000, 2.236
N Square Root
1 1.000
2 1.414
3 1.732
4 2.000
5 2.236
//
#include "stdafx.h"
#include
#include
using namespace std;
int main()
{
cout << setw(4) << 5 << endl;
cout << setw(4) << 25 << endl;
double pay;
pay = 10.50 * 23.333;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "The pay is $" << pay << endl;
// declare and initialize our variable
int numBars;
numBars=0;
double barWeight = 0;
cout << "Please enter # of bars & weight of 1 bar: ";
cin >> numBars >> barWeight;
// this is a comment!
cout << "\a\a\a\"Total weight is " << numBars * barWeight << endl;
cout << "Please enter # of bars & weight of 1 bar for another brand: ";
cin >> numBars >> barWeight;
cout << "Total weight is " << numBars * barWeight << "\n";
return 0;
}
data type
4 int
4 long int
4.6 float
4.6 double
4.6 long double
"Josh" string
true bool
'x' char
unsigned
signed
long
short
pico hello.cpp
g++ hello.cpp
./a.out
int numpods, peasperpod, total;
double temp;
string name;
hw:
write a prog.
variables: one, two, three, four, five
data type double
1.000, 1.414, 1.732, 2.000, 2.236
N Square Root
1 1.000
2 1.414
3 1.732
4 2.000
5 2.236
Tuesday, June 29, 2010
assignment 1
1) write a prog which asks
for # of quarters, dimes,
and nickels. output total
value in cents
2)
distance = (acceleration X time^2)/2
prompt for: time in seconds
output: how far it would drop
there is constant acceleration of 32ft per second
for # of quarters, dimes,
and nickels. output total
value in cents
2)
distance = (acceleration X time^2)/2
prompt for: time in seconds
output: how far it would drop
there is constant acceleration of 32ft per second
Monday, June 28, 2010
Lecture 1
111 Algorithmic Problem Solving
program in C++
1 midterm, 1 final -- 70%, weighted toward the better score
labs -- 30%
1) C++ without fear
2) * Savitch
3) Schaum's outline programming with C++
von Neumann architecture
sequential access memory vs.
random access memory
scroll vs.
book
vhs tape vs. dvd
ram vs. disk
1. ask for a number
2. read in a #, call it x
3. ask for a number
4. read in a #, call it y
5. add x and y, put it in z
6. print out z
7. go to step 1
fetch-execute cycle
IP: 1
fetch an instruction from RAM, put it into the ALU
the ALU will execute the instruction
update the IP
fetch instruction....
computers only understand binary
0100100
10010010010010001110
machine language
assembly language
MOV AX, 6
ADD AX, 5
SUB BX, AX
MOV is 1001001
AX is 0001
BX is 1100
SUB is 10010
C++. closer to english
interpreted language
compiled language
main()
{
int x, y, z;
cin >> x >> y;
z = x+y;
cout << z << endl;
}
venus
1. edit the source code
2. compile it
3. run it
IDE
integrated development environment
Microsoft Visual Studio
www.microsoft.com/express
program in C++
1 midterm, 1 final -- 70%, weighted toward the better score
labs -- 30%
1) C++ without fear
2) * Savitch
3) Schaum's outline programming with C++
von Neumann architecture
sequential access memory vs.
random access memory
scroll vs.
book
vhs tape vs. dvd
ram vs. disk
1. ask for a number
2. read in a #, call it x
3. ask for a number
4. read in a #, call it y
5. add x and y, put it in z
6. print out z
7. go to step 1
fetch-execute cycle
IP: 1
fetch an instruction from RAM, put it into the ALU
the ALU will execute the instruction
update the IP
fetch instruction....
computers only understand binary
0100100
10010010010010001110
machine language
assembly language
MOV AX, 6
ADD AX, 5
SUB BX, AX
MOV is 1001001
AX is 0001
BX is 1100
SUB is 10010
C++. closer to english
interpreted language
compiled language
main()
{
int x, y, z;
cin >> x >> y;
z = x+y;
cout << z << endl;
}
venus
1. edit the source code
2. compile it
3. run it
IDE
integrated development environment
Microsoft Visual Studio
www.microsoft.com/express
Monday, May 17, 2010
1) what include file do I need?
2) what data types are available
3) how do i declare?
4) how do i use
1)
#include // for cin and cout
#include // for file io
2)
ifstream
ofstream
these are data types, just like int, double, string
3)
ifstream jin;
ofstream jout;
alternatively:
ifstream jin("hello.txt");
ofstream jout("myoutput.txt");
4)
open a file:
jin.open("hello.txt");
jout.open("myoutput.txt");
close a file:
jin.close();
jout.close();
read from a file:
int x;
jin >> x;
write to a file:
jout << x << "hello" << endl;
2) what data types are available
3) how do i declare?
4) how do i use
1)
#include
#include
2)
ifstream
ofstream
these are data types, just like int, double, string
3)
ifstream jin;
ofstream jout;
alternatively:
ifstream jin("hello.txt");
ofstream jout("myoutput.txt");
4)
open a file:
jin.open("hello.txt");
jout.open("myoutput.txt");
close a file:
jin.close();
jout.close();
read from a file:
int x;
jin >> x;
write to a file:
jout << x << "hello" << endl;
Wednesday, May 12, 2010
What you should perhaps be able to do, come CS211
http://venus.cs.qc.cuny.edu/~alayev
http://venus.cs.qc.cuny.edu/~waxman/211
http://venus.cs.qc.edu/~joshwaxman/Spring2009/cs211
Project 1:
Do things with one-dimensional arrays -- the arrays you know and love. Namely, do the following, in separate programs:
a) Set all the elements of the array to the value 5.
b) Print out all the values of the array, separated by the comma character. There should not be a trailing comma character.
c) Create a string containing all the elements of the array, separated by the comma character.
d) Find the product of all the elements in the array.
e) Find the sum of all elements in the array.
f) Find the minimum value in the array.
g) Find the maximum value in the array.
h) Find the position of the minimum element in the array.
i) Here is a Wikipedia page which has pseudocode for the merge algorithm, which will merge two sorted arrays into a third sorted array.
http://en.wikipedia.org/wiki/Merge_algorithm
Implement it in C++.
j) Fill two arrays (A and B) with items typed in from the keyboard, add up the elements in each position, and store the result in the corresponding spot in array C. Thus, C[6] would contain A[6] + B[6].
k) Read in a bunch of numbers into an array from a file, and then Bubble-sort them.
http://en.wikipedia.org/wiki/Bubble_sort
l) Now that you can find the position of the minimum element in an array (see h), implement the Selection sort:
http://en.wikipedia.org/wiki/Selection_sort
m) Write some code which will reverse an array.
o) Write some code which will read a file containing integers in the range of 0 to 100, and then output the frequency of each element in the file.
p) Write a recursive function which will print out every element in an array. It should take in the array, the position to print out, and the capacity.
(optional q) There is a programming language known as C# (pronounced see-sharp), which is quite similar to C++. Teach yourself some C#, and implement items (a) through (p) in C#.
r) Read up about the goto statement, and write a program which will print "this program is making me dizzy" forever.
http://venus.cs.qc.cuny.edu/~waxman/211
Lab 1:
Make teams of 3 people.
Come up with a cool name.
Register your team name together with list of members.
We will learn how to log in to the computers, how to run Visual Studio. Submit with this first project digital photos of your team members so that I can keep track of you. Submission will be by making a Google Document, and sharing it with me. If you do not finish in lab, you can finish it for homework.http://venus.cs.qc.edu/~joshwaxman/Spring2009/cs211
Project 1:
Do things with one-dimensional arrays -- the arrays you know and love. Namely, do the following, in separate programs:
a) Set all the elements of the array to the value 5.
b) Print out all the values of the array, separated by the comma character. There should not be a trailing comma character.
c) Create a string containing all the elements of the array, separated by the comma character.
d) Find the product of all the elements in the array.
e) Find the sum of all elements in the array.
f) Find the minimum value in the array.
g) Find the maximum value in the array.
h) Find the position of the minimum element in the array.
i) Here is a Wikipedia page which has pseudocode for the merge algorithm, which will merge two sorted arrays into a third sorted array.
http://en.wikipedia.org/wiki/Merge_algorithm
Implement it in C++.
j) Fill two arrays (A and B) with items typed in from the keyboard, add up the elements in each position, and store the result in the corresponding spot in array C. Thus, C[6] would contain A[6] + B[6].
k) Read in a bunch of numbers into an array from a file, and then Bubble-sort them.
http://en.wikipedia.org/wiki/Bubble_sort
l) Now that you can find the position of the minimum element in an array (see h), implement the Selection sort:
http://en.wikipedia.org/wiki/Selection_sort
m) Write some code which will reverse an array.
o) Write some code which will read a file containing integers in the range of 0 to 100, and then output the frequency of each element in the file.
p) Write a recursive function which will print out every element in an array. It should take in the array, the position to print out, and the capacity.
(optional q) There is a programming language known as C# (pronounced see-sharp), which is quite similar to C++. Teach yourself some C#, and implement items (a) through (p) in C#.
r) Read up about the goto statement, and write a program which will print "this program is making me dizzy" forever.
Monday, May 3, 2010
Lab and hw #19
1) Implement a Bubble sort
2) Implement a Selection sort
3) Implement a non-recursive Insertion sort
2) Implement a Selection sort
3) Implement a non-recursive Insertion sort
Wednesday, April 28, 2010
Lab and homework #18
1) Write a function that takes an array and will *return* the maximum. (You have code for minimum.)
2) Write a function that takes an array and will *print out* the maximum.
3) Write a function that takes an array and will *print out* all the elements of the array.
4) Write a function that takes an array and will return the sum of all elements of the array.
5) Write a function that takes an array and will return the product of all elements of the array.
2) Write a function that takes an array and will *print out* the maximum.
3) Write a function that takes an array and will *print out* all the elements of the array.
4) Write a function that takes an array and will return the sum of all elements of the array.
5) Write a function that takes an array and will return the product of all elements of the array.
Monday, April 26, 2010
Lab and homework #17
1) Fill a 2-D array with the number 6
2) Print a 2-D array, with each element separated by a comma and rows separated by newlines.
3) Find the sum of all the elements in a 2-D array.
4) Given a 2D array A, and a 2D array B, add each corresponding element and store it in array C.
2) Print a 2-D array, with each element separated by a comma and rows separated by newlines.
3) Find the sum of all the elements in a 2-D array.
4) Given a 2D array A, and a 2D array B, add each corresponding element and store it in array C.
Wednesday, April 21, 2010
Lab and homework #16
1) A C-string, as opposed to a C++ string, is implemented
as a NULL-terminated array of characters. Thus, "hello" is 6 characters long, 'h', 'e', 'l', 'l', 'o', '\0'.
char s[6] = "hello";
Write a function stringlen which takes in a C-string and returns the string length of 5.
Write a function stringcopy which takes in two C-strings, source and destination, and makes destination = to source.
Write a function stringreverse which will reverse a C-string.
2) Write a function intToString which will take in a C-string and an integer, and write the digits of that number to the C-string.
3) Modify the above so that it takes in a 3rd parameter, which is the base. Passing in a 2 will make the string contain the number in binary. Passing in a 3 will make the string contain the number in ternary. Passing in a 10 will make the string contain the number in decimal.
as a NULL-terminated array of characters. Thus, "hello" is 6 characters long, 'h', 'e', 'l', 'l', 'o', '\0'.
char s[6] = "hello";
Write a function stringlen which takes in a C-string and returns the string length of 5.
Write a function stringcopy which takes in two C-strings, source and destination, and makes destination = to source.
Write a function stringreverse which will reverse a C-string.
2) Write a function intToString which will take in a C-string and an integer, and write the digits of that number to the C-string.
3) Modify the above so that it takes in a 3rd parameter, which is the base. Passing in a 2 will make the string contain the number in binary. Passing in a 3 will make the string contain the number in ternary. Passing in a 10 will make the string contain the number in decimal.
Monday, April 19, 2010
int sum(int I, int K)
{
int retval = 0;
for(int i = I; i <=K; i++)
retval = retval + i;
return retval;
}
int sum2(int I, int K)
{
if (I == K)
return I;
else
return I + sum2(I+1, K);
}
int max1(int I, int K)
{
int revI = reverse(I);
int revK = reverse(K);
retval = max(revI, revK);
return reverse(retval);
}
int max(int revI, int revK)
{
if (revI % 10 > revK % 10)
return revI;
else if (revI % 10 < revK % 10)
return revK;
else // they are equal
return revI % 10 + 10 * max(revI/10, revK/10);
}
{
int retval = 0;
for(int i = I; i <=K; i++)
retval = retval + i;
return retval;
}
int sum2(int I, int K)
{
if (I == K)
return I;
else
return I + sum2(I+1, K);
}
int max1(int I, int K)
{
int revI = reverse(I);
int revK = reverse(K);
retval = max(revI, revK);
return reverse(retval);
}
int max(int revI, int revK)
{
if (revI % 10 > revK % 10)
return revI;
else if (revI % 10 < revK % 10)
return revK;
else // they are equal
return revI % 10 + 10 * max(revI/10, revK/10);
}
Lab and homework #15
1) Using a for loop, fill an array with the number 6.
2) Using a for loop, fill an array with numbers pulled from the keyboard.
3) Using a for loop, print out the contents of an array.
4) An array contains midterm scores for a class. Find the average of the scores on the midterm, and print that out. Then, print out every midterm score which was above average.
5) One array has midterm scores and another has final scores. Make a third array for semester average, which will store the average of the midterm and the final for each student.
6) An array has midterm scores. Apply a curve to these midterm scores, so that each new midterm score is square root of the grade times 10.
2) Using a for loop, fill an array with numbers pulled from the keyboard.
3) Using a for loop, print out the contents of an array.
4) An array contains midterm scores for a class. Find the average of the scores on the midterm, and print that out. Then, print out every midterm score which was above average.
5) One array has midterm scores and another has final scores. Make a third array for semester average, which will store the average of the midterm and the final for each student.
6) An array has midterm scores. Apply a curve to these midterm scores, so that each new midterm score is square root of the grade times 10.
Wednesday, April 14, 2010
Lab and homework #14
1) Write a function exp, which returns x to the power of n, using iteration.
2) Write a function exp2, which returns x to the power of n, using recursion.
3) http://en.wikipedia.org/wiki/Pascal's_triangle
n is the row, k is the column.
P(0, 0) is 1
P(1,0) is 1
P(1,1) is 1
P(2,0) is 1
P(2,1) is 2, because it is P(1,0)+P(1,1)
P(2,2) is 1
Write P, recursively
4) Write a function max, which takes in x and y and returns whichever is bigger, by comparing each *digit* in turn. Do this using iteration. Do this using recursion.
2) Write a function exp2, which returns x to the power of n, using recursion.
3) http://en.wikipedia.org/wiki/Pascal's_triangle
n is the row, k is the column.
P(0, 0) is 1
P(1,0) is 1
P(1,1) is 1
P(2,0) is 1
P(2,1) is 2, because it is P(1,0)+P(1,1)
P(2,2) is 1
Write P, recursively
4) Write a function max, which takes in x and y and returns whichever is bigger, by comparing each *digit* in turn. Do this using iteration. Do this using recursion.
Monday, April 12, 2010
Lab and homework #13
1) Using recursion, find the product of the digits of an integer.
2) Using iteration (= a loop), find the product of the digits of an integer.
3) Using iteration and using recursion, write a function which will find the sum of the numbers I through K.
4) You have a recursive reverse function. Write a recursive forward function which will print all the digits in the forward order. (So, 123 would print 123, by printing a 1, then a 2, then a 3.)
2) Using iteration (= a loop), find the product of the digits of an integer.
3) Using iteration and using recursion, write a function which will find the sum of the numbers I through K.
4) You have a recursive reverse function. Write a recursive forward function which will print all the digits in the forward order. (So, 123 would print 123, by printing a 1, then a 2, then a 3.)
Wednesday, April 7, 2010
Lab and homework #12
pass by reference:
1) write a function putInOrder which takes two integers by reference, and puts them in ascending order.
2) write a function putInOrder3 which takes three integers by reference, and puts them in ascending order.
3) Write a function GenerateFullName which takes in three parameters: FirstName, LastName, and FullName. It should generate full names by concatenation of First Name with Last Name. Which should be passed by value, and which by reference?
4) Write a function GenerateFullName2 which takes in two parameters, First Name and Last Name, and *returns* the Full Name.
5) I will demonstrate a trace through a recursive function. Nothing to hand in for this one.
6) Write a recursive function Fib, which will generate the N'th fibonacci number.
See here:
http://en.wikipedia.org/wiki/Fibonacci_number
1) write a function putInOrder which takes two integers by reference, and puts them in ascending order.
2) write a function putInOrder3 which takes three integers by reference, and puts them in ascending order.
3) Write a function GenerateFullName which takes in three parameters: FirstName, LastName, and FullName. It should generate full names by concatenation of First Name with Last Name. Which should be passed by value, and which by reference?
4) Write a function GenerateFullName2 which takes in two parameters, First Name and Last Name, and *returns* the Full Name.
5) I will demonstrate a trace through a recursive function. Nothing to hand in for this one.
6) Write a recursive function Fib, which will generate the N'th fibonacci number.
See here:
http://en.wikipedia.org/wiki/Fibonacci_number
Wednesday, March 24, 2010
Lab and homework #11
1) Using functions you write yourself,
draw an American flag on the screen. Don't worry about setting the colors, although I will update with instructions of how to change the colors.
2) Write a function which will take a number and print out all the factors of that number.
3) Write a function factorial, which will take a number n and *return* n factorial. Hint: use a for loop for this, with an accumulator.
draw an American flag on the screen. Don't worry about setting the colors, although I will update with instructions of how to change the colors.
2) Write a function which will take a number and print out all the factors of that number.
3) Write a function factorial, which will take a number n and *return* n factorial. Hint: use a for loop for this, with an accumulator.
Monday, March 22, 2010
quiz 3
1) What will be the output of the following code? Explain.
for(int i=0; i<10; i++);
cout << "i";
2) What will be the output of the following code? Explain.
for(int i=0; i<10; i++);
cout << i;
3) What will be the output of the following code? Explain.
for(int i=10; i>0; i--)
cout << i;
4) Given the following function:
int foo(int bar) { return bar+bar*2; }
How would you call this function from main?
main() {
___________________________;
}
for(int i=0; i<10; i++);
cout << "i";
2) What will be the output of the following code? Explain.
for(int i=0; i<10; i++);
cout << i;
3) What will be the output of the following code? Explain.
for(int i=10; i>0; i--)
cout << i;
4) Given the following function:
int foo(int bar) { return bar+bar*2; }
How would you call this function from main?
main() {
___________________________;
}
lab 10
1) Write a function which takes in an int and returns an int:
square
cube
absolute
2) Write a function which takes in two integers and returns an int:
power(X, Y) will return X^Y
sum(X, Y) will return X+Y
difference(X, Y) will return X-Y
product(X,Y) will return X*Y
3) Write a function which takes in two integers and returns a bool
isBigger(X,Y) will return true if X is bigger than Y and false otherwise
isSmaller
isEqual
4) Write a function printRange(X, Y) which returns void. It will print the numbers in the range of X through Y.
After writing each of these functions, call them from a main function.
square
cube
absolute
2) Write a function which takes in two integers and returns an int:
power(X, Y) will return X^Y
sum(X, Y) will return X+Y
difference(X, Y) will return X-Y
product(X,Y) will return X*Y
3) Write a function which takes in two integers and returns a bool
isBigger(X,Y) will return true if X is bigger than Y and false otherwise
isSmaller
isEqual
4) Write a function printRange(X, Y) which returns void. It will print the numbers in the range of X through Y.
After writing each of these functions, call them from a main function.
Monday, March 15, 2010
lab 9
1) Google printf function and use it and a loop to print the following chart:
// print sums of cubes
http://venus.cs.qc.edu/~ryba/cs111/Ch3/sumcubes.cpp
2) Research the following functions, and write a program making use of them: printf, scanf, pow, modf, rand, ceil, floor, getch, getche.
3) Research the switch-case construct, and write a program which makes use of it.
4) Implement a two player game of poker dice.
http://en.wikipedia.org/wiki/Poker_dice
// print sums of cubes
http://venus.cs.qc.edu/~ryba/cs111/Ch3/sumcubes.cpp
2) Research the following functions, and write a program making use of them: printf, scanf, pow, modf, rand, ceil, floor, getch, getche.
3) Research the switch-case construct, and write a program which makes use of it.
4) Implement a two player game of poker dice.
http://en.wikipedia.org/wiki/Poker_dice
Wednesday, March 10, 2010
another way
for (r = 1; r <= height; r++)
{
// print spaces
for (i = 1; i <= height - r; i++)
cout << " ";
// print stars
// print spaces
for (i = 1; i <= r * 2 - 1; i++)
cout << "*";
cout << endl;
}
{
// print spaces
for (i = 1; i <= height - r; i++)
cout << " ";
// print stars
// print spaces
for (i = 1; i <= r * 2 - 1; i++)
cout << "*";
cout << endl;
}
this code doesn't work 100%, for even nums but add some rounding in and it will
#include "stdafx.h"
#include
using namespace std;
int main()
{
int height;
cin >> height;
int rows = height;
int cols = height*2 - 1;
for (int r = 1; r <= rows; r++) {
for (int c = 1; c <= cols; c++)
if (c <= cols/2 - (r) || c >= cols/2 + (r)) cout << " ";
else cout << "*";
cout << endl;
}
return 0;
}
the one to print a balanced triangle
#include
using namespace std;
int main()
{
int height;
cin >> height;
int rows = height;
int cols = height*2 - 1;
for (int r = 1; r <= rows; r++) {
for (int c = 1; c <= cols; c++)
if (c <= cols/2 - (r) || c >= cols/2 + (r)) cout << " ";
else cout << "*";
cout << endl;
}
return 0;
}
the one to print a balanced triangle
Wednesday, March 3, 2010
quiz 2
1) The following is NOT a valid loop in C++:
a) for
b) while
c) do-while loop
d) fruit
2) The following is NOT a valid built-in data type:
a) float
b) double
c) triple
d) string
3) When working in Visual Studio, to use a string data type, you must:
a) #include
b) #include
c) #include
d) all of the above
4) The watch window, in Visual Studio, lets you:
a) time things using a stopwatch
b) check the current value of variables
c) declare a variable
d) none of the above
5) Write a loop to print the numbers 1 thru 4, inclusive
a) for
b) while
c) do-while loop
d) fruit
2) The following is NOT a valid built-in data type:
a) float
b) double
c) triple
d) string
3) When working in Visual Studio, to use a string data type, you must:
a) #include
b) #include
c) #include
d) all of the above
4) The watch window, in Visual Studio, lets you:
a) time things using a stopwatch
b) check the current value of variables
c) declare a variable
d) none of the above
5) Write a loop to print the numbers 1 thru 4, inclusive
Monday, March 1, 2010
Lab 7
1) find the sum of the numbers 1 thru 10
2) find the average of the numbers 1 thru 10
3) prompt for n, find the sum of 1 thru n
4) prompt for n, print all the factors of n
5) using the watch window, trace thru the number reversing program
6) on paper or excel, trace thru change in coins
7) prompt for a letter, say if it is vowel or a consonant
8) work on practice problems for midterm
2) find the average of the numbers 1 thru 10
3) prompt for n, find the sum of 1 thru n
4) prompt for n, print all the factors of n
5) using the watch window, trace thru the number reversing program
6) on paper or excel, trace thru change in coins
7) prompt for a letter, say if it is vowel or a consonant
8) work on practice problems for midterm
Wednesday, February 24, 2010
Lab 6
You can put all this (except 7) into a single C++ program.
1) Take in two numbers. Determine the minimum, maximum, and average of these numbers and print them out.
2) Print out your name 100 times, one on each line.
3) Print out the numbers 1 to 100.
4) Print out the even numbers in the range of 2 to 100.
5) Print out the odd numbers in the range of 1 to 99.
6) Loop forever, asking the user for the grades of midterm and final, and print out, each time, the average of these two values.
7) For your own sakes, step through the code in this example:
"Enter even: use while to force correct input 02-22-10"
8) Research the do-while loop. Using a do-while loop, repeatedly ask the user for a password. If it is "swordfish", proceed. Otherwise, loop around again.
1) Take in two numbers. Determine the minimum, maximum, and average of these numbers and print them out.
2) Print out your name 100 times, one on each line.
3) Print out the numbers 1 to 100.
4) Print out the even numbers in the range of 2 to 100.
5) Print out the odd numbers in the range of 1 to 99.
6) Loop forever, asking the user for the grades of midterm and final, and print out, each time, the average of these two values.
7) For your own sakes, step through the code in this example:
"Enter even: use while to force correct input 02-22-10"
8) Research the do-while loop. Using a do-while loop, repeatedly ask the user for a password. If it is "swordfish", proceed. Otherwise, loop around again.
Monday, February 22, 2010
Quiz 1
1) If I want to print something out to the screen, I would use:
a) CIN
b) COUT
c) cin
d) cout
e) more than one of the above
2) At the top of all the programs so far, we have #included
a) iostream
b) istream
c) ostream
d) streamstream
3) If I wanted to store a value such as "hello", I would put it in a variable of type:
a) int
b) string
c) double
d) cout
4) The arrows for cin go in the direction of:
a) >>
b) <<
c) ^^
d) vv
5) Write a program that will print out a double, 0.3333, by first dividing 1 by 3. Take care that you don't end up printing just a 0, because of the rules of integer division.
a) CIN
b) COUT
c) cin
d) cout
e) more than one of the above
2) At the top of all the programs so far, we have #included
a) iostream
b) istream
c) ostream
d) streamstream
3) If I wanted to store a value such as "hello", I would put it in a variable of type:
a) int
b) string
c) double
d) cout
4) The arrows for cin go in the direction of:
a) >>
b) <<
c) ^^
d) vv
5) Write a program that will print out a double, 0.3333, by first dividing 1 by 3. Take care that you don't end up printing just a 0, because of the rules of integer division.
lab 5
Lab exercises
1) Using if statements in the program, ask someone for his or her first and last name. If the total number of letters in both names is more than 20, tell them that their name is too long and that they must shorten it.
Google "C++ string length"
2) Look up the ?: operator, and modify one of Dr. Ryba's if programs to use this operator instead.
3) Using ifs, take in a number and find out if it is a leap year. The rules for
leap year are as follows:
every 4th year is a leap year
unless it is a century
unless it is a 4th century, in which case it is a leap year
1) Using if statements in the program, ask someone for his or her first and last name. If the total number of letters in both names is more than 20, tell them that their name is too long and that they must shorten it.
Google "C++ string length"
2) Look up the ?: operator, and modify one of Dr. Ryba's if programs to use this operator instead.
3) Using ifs, take in a number and find out if it is a leap year. The rules for
leap year are as follows:
every 4th year is a leap year
unless it is a century
unless it is a 4th century, in which case it is a leap year
Thursday, February 18, 2010
Lab 4
1) Convert celsius to Fahrenheit. (the opposite of what you did in class.)
2) Step through the coins program (using the F10). Set watches on all the variables.
3) http://en.wikipedia.org/wiki/Polish_coins_and_banknotes
Take in a number of grosz, and show the minimum number of coins to give this amount in change.
http://en.wikipedia.org/wiki/Polish_coins_and_banknotes
4) Convert zloty to a bunch (5) of other currencies:
http://www.lse.co.uk/currency-converter.asp?Sym=PLN&Currency=polish_zloty
http://venus.cs.qc.edu/~ryba/cs111/
2) Step through the coins program (using the F10). Set watches on all the variables.
3) http://en.wikipedia.org/wiki/Polish_coins_and_banknotes
Take in a number of grosz, and show the minimum number of coins to give this amount in change.
http://en.wikipedia.org/wiki/Polish_coins_and_banknotes
4) Convert zloty to a bunch (5) of other currencies:
http://www.lse.co.uk/currency-converter.asp?Sym=PLN&Currency=polish_zloty
http://venus.cs.qc.edu/~ryba/cs111/
Wednesday, February 17, 2010
Monday, February 8, 2010
Lab 3
1) Run through all the programs up to the end of 2-8-10 on Dr. Ryba's website.
2) Work through all the tutorials up to Tutorial 2 on this website.
3) In Visual Studio, step through this program.
HW:
1) A program which gets in the speed (in mph) and time elapsed (in hours), and calculates and prints out the distance traveled.
2) Work through all the tutorials up to Tutorial 2 on this website.
3) In Visual Studio, step through this program.
HW:
1) A program which gets in the speed (in mph) and time elapsed (in hours), and calculates and prints out the distance traveled.
Wednesday, February 3, 2010
Lab 2 summary
plan:
qccs111.blogspot.com
1) pico our first program on *venus* compile it, execute it.
pine = pine is not elm
pico = pine composer
ls = directory listing
pwd = present working directory
2) Get persons name and greet them.
#include
#include
using namespace std;
int main()
{
cout << "Please enter your name: ";
string theName;
cin >> theName;
cout << "Howdy, " << theName << "!" << endl;
return 0;
}
HW: Modify this to ask for first and last names, and greet the person using the full name.
qccs111.blogspot.com
1) pico our first program on *venus* compile it, execute it.
pine = pine is not elm
pico = pine composer
ls = directory listing
pwd = present working directory
2) Get persons name and greet them.
#include
#include
using namespace std;
int main()
{
cout << "Please enter your name: ";
string theName;
cin >> theName;
cout << "Howdy, " << theName << "!" << endl;
return 0;
}
HW: Modify this to ask for first and last names, and greet the person using the full name.
Subscribe to:
Posts (Atom)