Month: February 2011

Code

convert integer to any base C++

this function is capable of converting integers to any base format 2-16. this includes binary, octal, and hexadecimal. adding larger bases is as simple as lengthening the alpha string. //this recursive base converter is valid for base 2~16 void toBase(int num, int base) { string alpha="0123456789ABCDEF"; if (n > 0) { toBase(num/base,base); cout << alpha[num%base]; } }

Code

Some Great Examples of recursion C++

This little program demonstrates the recursion technique in three different settings.  It includes recursive algorithms to search an array, reverse a number, and find the greatest common divisor of two numbers. // Boyd Turley #include <iostream> #include <string> #include <fstream> #include <iomanip> //required to use setw() #include <cstdio> using namespace System; using namespace std; ///////////////////////////////////// Globals const int TABLE_ROWS = 30; const int TABLE_COLS = 20; int table[TABLE_COLS][TABLE_ROWS]; int grid[599]; //////////////////////////////////// FXN Defs. bool posWhole(int checkme); void NumReverse(int reverseme); void setOrder(int num1, int num2); int GCD(int num1, int num2); void gridSearch(); ///////////////////////////////////////////////////////////////////////////////////////// MAIN int main(array<System::String ^> ^args) { char thechoice […]

Code

Dynamic Array Example C++

//this is an example of an array of dynamic memory #include <iostream> using namespace std; void main() { typedef int* IntPtr; IntPtr intList; int listSize; cout << "How big is the list? "; cin >> listSize; intList = new int[listSize]; for (int i = 0; i < listSize; i++) { intList[i] = i; cout << intList[i] << ‘ ‘ << &intList[i] << endl; } cout << endl; delete [] intList; //ALLWAYS remember to match new statements with delete statements system("pause"); }

C++ code Code

A realistic C++ line counter

This program asks the user for a c++ source file. It displays the file’s linecount to the screen. Program exits on invalid input. This sourcefile is 90 lines of code(according to itself). Each of the following counts as one line of code: Preprocessor directives if() while() do..while() switch() non-repeating semicolons #include <iostream> #include <fstream> #include <string> using namespace std; // accepts a valid C++ filename, returns a C++ linecount. Prints a error and exits program on invalid input. int loc(string filename); int main() { string filename; cout << "Enter a cpp filename to linecount " << endl; cin >> filename; […]

Code Verilog

16 bit ALU Verilog design

// 4 channel, 1 bit mux // BK Turley `timescale 1ns/100ps module mux_4to1(out, sel, in0, in1, in2, in3); output out; input [1:0] sel; input in0; input in1; input in2; input in3; reg out; always @( sel or in0 or in1 or in2 or in3) case (sel) 2’b00 : out <= in0; 2’b01 : out <= in1; 2’b10 : out <= in2; 2’b11 : out <= in3; default : out <= in0; endcase endmodule

C++ code Code

Can’t use a string in a C++ switch statement? Here is a simple alternative.

Many programmers are surprised to find that switch statements aren’t compatible with strings in C/C++. When attempting to compile, you will see an error: Visual Studio 2010 —–  error C2450: switch expression of type ‘std::string’ is illegal Netbeans 6.9.1 —– error: switch quantity not an integer Here is an example of invalid code #include <iostream> #include <string> using namespace std; int main(){ string dayofweek = "wednesday"; switch(dayofweek){ case "monday": cout << "Watch football" << endl; break; case "tuesday": cout << "steal a car"<< endl; break; case "wednesday": cout << "rob a bank" << endl; break; case "thursday": cout << "count […]