Monday, February 22, 2010

lab 3, together with homework at the bottom

void swap(int &a, int &b)
{
int t = a;
a = b;
b = t;
}

void bubblesort(int a[][10], int col)
{
for (int n = 0; n < 10; n++)
{
for (int i = 0; i < 9; i++)
if (a[i][col] > a[i+1][col])
swap(a[i][col], a[i+1][col]);
}
}


int a[10][10];
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
cin >> a[i][j];

for (i = 0; i < 10; i++)
bubblesort(a, i);


for (i = 0; i < 10; i++, cout << endl)
for (int j = 0; j < 10; j++)
cout << a[i][j];
cin >> num;
t = num;
char picture[9][5];
for (int i = 0; i < 9; i++)
for (int j = 0; j < 5; j++)
picture[i][j] = ' ';

for (i = 0; i < 5; i++)
{
int digit = num % 10;
num /= 10;
draw(picture, digit, 5 - i);
}


for (int i = 0; i < 9; i++, cout << endl)
for (int j = 0; j < 5; j++)
cout << picture[i][j];


HW: read up on templatized functions
write a templatized bubblesort and selection sort
http://www.cplusplus.com/doc/tutorial/templates/

Thursday, February 18, 2010

assignment:
implement 2 different DFSAs
first using gotos
then using 2d array for state transition table

Wednesday, February 17, 2010

Lab 2

1)  Intro to Visual Studio.

2) Read up on Gotos.

3) Implement a Finite State Machine to recognize a language, using Gotos; then, using a 2D array as a state transition table.

http://www.ccs3.lanl.gov/mega-math/workbk/machine/mabkgd.html

http://en.wikipedia.org/wiki/Deterministic_finite-state_machine

http://www.engr.mun.ca/~theo/Courses/fm/pub/fm21sl.pdf

Monday, February 8, 2010

Lab 1

Fill an array of strings from a file. Sort them using bubble sort.
http://en.wikipedia.org/wiki/Bubble_sort

Lab HW 1:
Fill an array of strings from a file. Print them out backwards, starting from the end of the array.

Wednesday, February 3, 2010

review



1. Write a program that will
input an integer n
calculate the sum of the positive integers from 1 – n
output  the sum

#include
using namespace std;
main()
{
Int n;
cin >> n;
cout << (n+1)*(n/2.0) << endl;
int sum = 0;
for (int I = 0; I < n; i++)
                sum = sum + I;

counter; loop control variable;  accumulator; flag
2. Write a program that will input an integer n, and determine if n is the sum of the positive integers 1 – k for some positive integer k.
 For example if n=6 it is the sum of the first 3 positive integers:  1+2+3, but if n=5, then there is no positive integer k such that n=1+2+ … +k.
Your program will print one of 2 messages:
If there is such a k, output the following message:
The number n is the sum of the first k positive integers.
For example, if your program inputs  n=6, it should output the message:
The number 6 is the sum of the first 3 positive integers.
If there is no such k, output the following message:  “There is no positive integer k such that n is the sum of the integers 1 through k.
For example, if your program inputs n=5 it should output the message:
There is no positive integer k such that 5 is the sum of the integers 1 through k.

Main()
{
Int n, sum = 0; cin >> n;
For(int I = 1; I <= n; i++)
{
Sum = sum + I;
If (sum == n)
{
Cout << “the sum of the nums 1 thru “ << I << “is” << n;
Break;
}
Else if (sum > n)
{
Cout << “there is no such sum “;
Break;
}
}


3. Assume that you have an array of 100 integers:
int a[100];
a.       Write a loop that will add up the values stored at the even index positions in the array (i.e. 0,2,4, … ).
Int sum =0;
For (int I = 0; I < 100; I+=2)
          Sum += A[I];






b.      Write a loop that will add up the values stored at the even index positions in the array (i.e. 0,2,4, …  ) but only if the value of the elements in those positions are even.
Int sum =0;
For (int I = 0; I < 100; I+=2)
                If ( A[I] % 2 == 0)
                                Sum += A[I];






c.       Write a loop that will add up the values stored at the odd index positions in the array (i.e. 1,3,5, …  ) but only if the value of the elements in those positions are even.
Int sum =0;
For (int I = 1; I < 100; I+=2)
                If ( A[I] % 2 == 0)
                                Sum += A[I];




4. Write a program that inputs an integer n, and outputs all the numbers that divide n. For example, if n=6, output : 1,2 3. Don’t output the number n itself.

For (int I = 0; I < n / 2; i++)
        If (n % I == 0)
                        Cout << I << “ “;







5. Given the following 2 dimensional array with half the elements shaded black and half white.
            int b[8][8]

Write a program segment that will sum up all the elements in the black boxes.
Sum = 0 ;
For (int I = 0; I < 8; I++)
     For (int J = I%2; J < 8; J+=2)
          Sum += A[I][J];