Programming

PHP rocks, JAVA sucks…what else can I say?

Merging Data Files

C++, while tedious, is probably one of my favorite classes. I’ve never actually taken a e first line of each file
//Write the higher valuprogramming class before now and I imagine I will be taking more. Anyways, another has gone by and thus another lab is due and you get to see what I’ve done! This week we worked on interacting with outside files. Just simple tasks, like opening, closing, reading, and writing. The program I had to create delt with taking two seperate files and merging them into one while activly sorting the numbers from lowest to highest. I could not merge the data and then sort it. Enjoy!

//Name: Andrew Ferguson
//Date: 2/27/2005
//Purpose: Merge two files while sorting the data

//Open two read files and one write file
//Read and compare the first line of each file
//Write the higher value to the screen and to the write file
//Read the next highest number from the file that containted the previous higher number and compare again
//Repeat until both read files are completly read

#include

#include
#include

int main(){
ifstream file_one, file_two;
ofstream fout;
double next_one, next_two;

file_one.open(“location1.dat”);
file_two.open(“location2.dat”);
fout.open(“output.dat”);

if(file_one.fail()){
cout << "File failed to open." << endl; exit(1); } if(file_two.fail()){ cout << "File failed to open." << endl; exit(1); } file_one >> next_one; //Prime the pump…so to speak
file_two >> next_two; //Prime the pump…so to speak

cout << "Merging Files..." << endl; while1 && (!file_two.eof())){ if(next_one <= next_two){ fout << next_one << endl; cout << next_one << endl; file_one >> next_one;
}
else{
fout << next_two << endl; cout << next_two << endl; file_two >> next_two;
}
while2 && (!file_two.eof())){
fout << next_two << endl; cout << next_two << endl; file_two >> next_two;
}
while3 && (!file_one.eof())){
fout << next_one << endl; cout << next_one << endl; file_one >> next_one;
}

}
file_one.close();
file_two.close();
fout.close();
cout << "...Complete!" << endl; } [/code]

Download the Windows Executable
File “Location1.dat”
File “Location2.dat”
The last two files are the files that are merged. You can download and use them or you can create your own files called location1.dat and location2.dat. If you create your own files, each file can contain as many floating points as you want with one per line. The files must be in the same directory as the program.

0
  1. !file_one.eof( 

  2. file_one.eof( 

  3. file_two.eof( 

AFdN Calc v0.1

//Name: Andrew Ferguson
//Date: 2/20/2005
//Purpose: To create a calculator to calculate different things. w00t!

#include
#include

void getOption();

double bmi(double& pounds, double& inches);

void doBMI();

double poundsToKilograms(double& input);

void doPoundsToKilograms();

double feetToMeters(double& input);

void doFeetToMeters();

void quadratic(int& a, int& b, int& c, double& soln_1, double& soln_2);

void doQuadratic();

int gcf(int& a, int& b, int& c);

void doGCF();

double stardate(int& mont, int& day, int& year, int& hour, int& minute, int& second);

void doStardate();

int main()
{

getOption();
return 0;
}

void getOption(){
int choice;
cout << endl << "AFdN Calc v0.1" << endl << endl; do{ cout << "Your options are:" << endl << "1 - Calculate your BMI" << endl << "2 - Convert Pounds to Kilograms" << endl << "3 - Convert Feet to Meters" << endl << "4 - Solve a quadratic equation (no complex numbers)" << endl << "5 - Find the Greatest Common Factor for 3 numbers" << endl << "6 - Convert a date and time into Stardate format" << endl << "0 - Exit" << endl << endl << "Enter your choice: "; cin >> choice;
}while1; switch(choice) { case 1: doBMI(); getOption(); break; case 2: doPoundsToKilograms(); getOption(); break; case 3: doFeetToMeters(); getOption(); break; case 4: doQuadratic(); getOption(); break; case 5: doGCF(); getOption(); break; case 6: doStardate(); getOption(); break; case 0: break; } } double bmi(double& pounds, double& inches){ return (poundsToKilograms(pounds)/(feetToMeters(inches)*feetToMeters(inches))); } void doBMI(){ double pounds, inches; cout << "Enter your weight in pounds:"; cin >> pounds;
cout << "Enter your height in inches: "; cin >> inches;
cout << "You have a BMI of " << bmi(pounds, inches) << endl; } double poundsToKilograms(double& input){ return (input*0.454); } void doPoundsToKilograms(){ double pounds; cout << "Enter pounds: "; cin >> pounds;
cout << pounds << " lbs is " << poundsToKilograms(pounds) << " kg" << endl; } double feetToMeters(double& input){ return (input*0.3048/12); } void doFeetToMeters(){ double inches; cout << "Enter inches: "; cin >> inches;
cout << inches << " in is " << feetToMeters(inches) << " m" << endl; } void quadratic(int& a, int& b, int& c, double& soln_1, double& soln_2){ soln_1 =2)/(2*a); soln_2 =3)/(2*a); } void doQuadratic(){ int a,b,c; double soln_1, soln_2; cout << "Enter term 'a':"; cin >> a;
cout << "Enter term 'b':"; cin >> b;
cout << "Enter term 'c':"; cin >> c;
quadratic(a,b,c,soln_1,soln_2);
cout << "The solution to " << a << "x^2 + " << b << "x + " << c << " = 0 is x=" << soln_1 << " and x=" << soln_2 << endl; } int gcf(int& a, int& b, int& c){ { int remainder = b % a; if (remainder != 0) return gcf(remainder,a,c); remainder = c % a; if (remainder != 0) return gcf(remainder,a,c); return a; } } void doGCF(){ int a,b,c; cout << "Enter first term: "; cin >> a;
cout << "Enter second term: "; cin >> b;
cout << "Enter third term: "; cin >> c;
cout << "The Greatest Common Factor is " << gcf(a,b,c) << endl; } double stardate(int& month, int& day, int& year, int& hour, int& minute, int& second){ double nowTime =4*1000; return starTime; } void doStardate(){ int month, day, year, hour, minute, second; cout << "Enter the month: "; cin >> month;
cout << "Enter the day: "; cin >> day;
cout << "Enter the year: "; cin >> year;
cout << "Enter the hour: "; cin >> hour;
cout << "Enter the minute: "; cin >> minute;
cout << "Enter the second: "; cin >> second;
cout << endl << stardate(month, day, year, hour, minute, second); } [/code]

Download AFdN Calc v0.1

0
  1. choice > 7) || (choice < 0 

  2. -b)+sqrt((b*b)-(4*a*c 

  3. -b)-sqrt((b*b)-(4*a*c 

  4. 2322-1970)*365.2422*24*60*60)+(90*24*60*60); double itTime; int monthDays[] = {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; itTime = ((year-1970)*365.2422*24*60*60)+(monthDays[month]*24*60*60)+(day*24*60*60)+((hour-7)*60*60)+(minute*60)+second; double newTime = itTime - nowTime; double starTime = (newTime / (60*60*24*365.2422 

Guess My Number

This weeks programming activity was actually pretty cool. We had to create a game that would generate a random integer between 1 and 10. The user then guesses a number and the computer tells you if you are high, low, or right on. Give it a whirl!

//Name: Andrew Ferguson
//Date: 2/6/2005
//Purpose: Make a program that makes you guess a number between 1 and 10

#include
#include
#include
#include

using namespace std;

int guessNum;
int userGuess;
char n;

int main(){
srand(time(0));
guessNum = 1+rand()%10;
cout << "Try to guess the number. It's between 1 and 10\n"; do{ cout << "What's your guess?\n"; cin >> userGuess;
if(userGuess < guessNum) cout << "You guessed too low.\n\n";& else if(userGuess > guessNum)
cout << "You guessed to high.\n\n"; } while(userGuess != guessNum); cout << "That's right! My number was " << guessNum << ".\n"; printf("Press any key to continue..."); n = getch(); return 0; } [/code]

Download guessGame

0

bubblegum.cpp

Here’s last weeks project:

//Name: Andrew Ferguson
//Date: 1/19/2005
//Purpose: To split up bubble gum
#include <conio.h>
#include <iostream>
#include <stdio.h>

using namespace std;

int sticks, people, sticks_person, sticks_remain;

char n;

int main()
{
	cout << "How many sticks of gum do you have?n";
	cin >> sticks;
	cout << "How many people are in MACS 261C?n";
	cin >> people;
	sticks_person = sticks/people;
	sticks_remain = sticks%people;
	cout << "Every person can receive " << sticks_person << ".n";
	cout << "There are " << sticks_remain << " left over.n";

    printf("Press any key to continue...");
    n = getch();

	return 0;
}

Download the compiled executible

0

gasBill.cpp

Here’s this week C++ project we had to do. I’m just going to post original work that I made, not files they had us fix.

//Name: Andrew Ferguson
//Date: 1/27/2005
//Purpose: Compute gas costs
#include <conio.h>
#include <iostream>
#include <stdio.h>

using namespace std;

int prevMonth, thisMonth, gasDiff;
double cost = 0;
char n;

int main(){
    cout << "Enter last months gas reading:";
    cin >> prevMonth;
    cout << "Enter this months gas reading:";
    cin >> thisMonth;
    if(thisMonth < prevMonth){
                 thisMonth += 10000;
                 }
    gasDiff = thisMonth - prevMonth;

    if(gasDiff <= 70){
               cost = 14.95;
               }
    else if(gasDiff <= 170){
         cost = gasDiff * 0.095;
         }
    else if(gasDiff <= 400){
         cost = gasDiff * 0.0525;
         }
    else{
         cost = gasDiff * 0.02125;
         }
    cout << "Your total gas bill for this month is: " << cost << endl;
    printf("Press any key to continue...");
    n = getch();

    return 0;
}

Download the compiled executable

0

C++: Lab 1


//Name: Andrew Ferguson
//Date: 1/17/2005
//Purpose: To see if I can get this work!
#include <iostream.h>

void main()
{
	cout << "Hello\n";
	cout << "Goodbye\n";
}
0

Senior Project: (Last Day – 1) && (Last Day)

Thursday we mostly worked on the ME’s Fish Bot. There was a problem with the board they used to program the fish. They could write to RAM and run in debug mode and everything would work fine. They could also flash the ROM, but they couldn’t run out of ROM. After a few hours of testing with the scope, it problem was traced to a malformed 3.3 v wave form. Instead of being a constant 3.3 volts, the fish was getting somewhere between 3 and 3.3 volts. The dip in voltage was attributed to a power converter that could only handle 200mA. The fish nominally uses 300mA. So we added another 200mA power converter and the fish worked! Score one for EE.

Friday was just a massive 7 hour coding spree. The code I wrote is working, sort of. But I have a new idea. So I’m going to pretty much start from scratch next week.

0