// Robert A. Simpson

// CS132

// Nov. 2,1997

// unit 5

// This progam will use ARRAYS and is practice for using functions.

// This program has three main tasks. I will explain each task at

// The functions in which they are solved.

 

#include <iostream.h>

#include <iomanip.h>

#include <math.h>

// declare all functions. none of my functions will return a value

// notice the void statement.

void fibonacci();

void eratosthenes();

void pascals();

void Continue();

// no problem solving is used in the main program

// MAIN is for function calls only.

main(){

fibonacci();

Continue();

eratosthenes();

Continue();

pascals();

Continue();

}

// this function is used for a break in the program

// I used the row of new line characters to 'clear the screen'

// it's not very effecent but it makes the output look nicer

// (I hope you don't object)

void Continue (){

char dummy;

cout<< "\n\n\nPress any key then ENTER to Continue...";

cin >> dummy;

cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" ;

}

// this function is used to find the Fibonacci numbers(hence the name)

// the first two Fibocacci numbers are 1 and 1 by definition, so I set the

// first two numbers in the array to 1.

void fibonacci(){

const int max=46; // I could declared 48 but it looks better with 46.

unsigned long number[max]={1,1};

// could have avoided this next line by not using the first part of the array

// but it works about the same way. I didn't see the need in changing it.

cout <<setw(12)<<number[1];

for (int count=2;count < max;count++){

number[count]=number[count-1]+number[count-2];

cout << setw(12)<<number[count];

if (count%5==0)

cout<<endl;

}}

// the function is (to me) the hard way to find prime numbers but it does run

// faster than my last program. 1500 is the max (it locks up the computer

// if you go any higher).

void eratosthenes(){

const int max=1500;

int prime[max];

int count4=1;

for(int count=1;count<max-1;count++)

prime[count]=1;

// I know this looks very sloppy but it works and tried to make it easier

// to read. I wasn't sure if I could go into a FOR loop using an IF

// statment but it work.

for (int count1=2;count1< sqrt(max);count1++){

if (prime[count1]==1)

for (int count2=count1+1;count2<max-1;count2++){

if (count2 % count1 ==0)

prime[count2]=0;

} }

for (int count3=1;count3<max-1;count3++){

if (prime[count3]==1){

cout<< setw(7) <<count3;

if (count4 %11 ==0)

cout <<endl;

count4++;

}}}

// and finally we have PASCALS triangle

// I thought this was going to be the hard part of the program

// but after writing it down on paper it looked a lot easier.

// I prints out a real nice looking triangle. I could have mad it MUCH

// larger but I would have to sacrifice large numbers for space and vice-versa

// I think I found a happy medium.

void pascals(){

const int row=13,colum=13;

int pascals[row][colum]={{0},{0}};

int display=35;

for (int reset=0;reset<row;reset++)

pascals[reset][0]=1;

for (int count=1;count<row;count++){

for(int count1=1;count1<colum;count1++)

pascals[count][count1]=pascals[count-1][count1-1]+pascals[count-1][count1];

}

for (count=0;count<row;count++){

display=display-2;

cout <<endl<<setw(display)<<" ";

for(int count1=0;count1<colum;count1++){

if (pascals[count][count1] !=0)

cout<<setw(4)<<pascals[count][count1];

}}

}

back

Home Page