Category Archives: Comp Sci
Random Selection without replacement
#include "stdafx.h"
#include
#include
#include
#include
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
clock_t start; /* Line 8 */
clock_t finish; /* Line 9 */
const int arraysize=36;
const int SelectN=6;
int row[SelectN];
int Crows=100;
int g;
int arraycounter=arraysize;
int a[arraysize+1];
start = clock();
srand(60);
for (int k=0;k|"<
A very cool web site
connecting using jdbc and pulling back some data
Connecting to some sql servers with jdbc.
Download the JDBC driver for your databases and add them to the project in eclipse (or whatever) then:
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class DbConnect{
public static void main(String args[]){
String dbtime="";String db_connect_string="";String db_userid="";String db_password="";String query="";
try {
//Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
int choose=2;
switch (choose)
{
case 0:
db_connect_string="jdbc:mysql://yourMySQLServerNameHere/yourMySQLData";
db_userid="ooooohyourusername";
db_password="waaaaaayYourPassword";
query = "Select top 10 * FROM YourTable";
break;
case 1:
db_connect_string="jdbc:sqlserver://yourSQLServerNameHere;databaseName=yourDatabaseName";
db_userid="ooooohyourusername";
db_password="waaaaaayYourPassword";
query = "Select top 10 * FROM YourTable";
break;
}
Connection con = DriverManager.getConnection(db_connect_string,db_userid, db_password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
int rn=0;
while (rs.next()) {
dbtime = rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4)+" "+rs.getString(5);
System.out.println(rn+" : "+dbtime);
rn++;
} con.close();
} //end try
catch(ClassNotFoundException e) {e.printStackTrace();}
catch(SQLException e) {e.printStackTrace();}
}
}
Word count histogram (horizontal)
Take a look at this:
#include
main()
{
int wlen[255]; //why would you have a word with more letters than this?
int i,g,wordcount,lettercount,inner,c,maxVal,maxLet;
wordcount=lettercount=maxVal=maxLet=0;
for(i=0;i<=254;++i) wlen[i]=0; //zero out the array
while ((c=getchar())!=EOF)
{
if ((c=='\n'||c=='\t'|| c==' ' || c=='.' || c==',' || c==';' || c==':')&& lettercount>0)
//don't count double 'spacers' as zero length words
{
++wordcount;
wlen[lettercount]=wlen[lettercount]+1;
if (lettercount>maxLet) maxLet=lettercount;
if (wlen[lettercount]>maxVal) maxVal=wlen[lettercount];
lettercount=0;
}
else ++lettercount;
}
for(i=maxVal;i>0;--i)
{
for (inner=0;inner<=maxLet;++inner)
{
if (wlen[inner]>=i) printf("X");
else printf(" ");
}
printf("\n");
}
for (g=0;g<=maxLet;++g) printf("%d",g);
printf("\n\nHey Finished Man!!\n");
}
one loop and a decision
very very basic for loop and if else if else
#include
using namespace std;
int main()
{
int i;
for(i=0;i<=13;i++)
{cout << "Hello world! "<< i <
..notice that by the time it leaves the
for (){}
loop it is 14 not 13
Want system pause?
system("PAUSE");
then you'll need:
#include
Preprocessor directives
An example of some preprocessor stuff:
#include
#define __thing__ " amazing "
#define __func__(x,y) ((x)>(y)?(x):(y))
#define _FAT_
#define _Cars_
using namespace std;
int main()
{
cout << "Hello world!"<< __thing__ <
C++ MFC
Make sure:
#include "stdafx.h"
comes FIRST otherwise you will get lots of proper errors.
If you see the following error:
error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
Make sure that your linker is set to compile for windows:
Project > Properties > Linker > System > SubSystem
set to
Windows (/SUBSYSTEM:WINDOWS)
Another key hint is to set your
Project > Properties > Configuration Properties > General > Use of MFC
set to:
Use MFC in a Shared DLL
Data visualisation
Android APK file for finding the square root
Android
Warning: written as a “hello world” project until I’m better at it.
From the name it’ll probably end up as an “is it prime” sort of program or something similar.