Brute Force Exercise

minimal use of standard functions
I could have used atoi(), and casts, but I didn’t ;)
written in C

if wordpress screwed up the code, let me know

/*
 *Every character in the character set is interpreted as a numeric value
 *the program increments the value of s using the length of the character set as the base
 *if all lowercase letters in the English alphabet were given as the character set, then it would count in base 26 and store it into s
 *the numeric value stored in s, is then converted to the character and printed
 *such that: 
 *given: abc
 *then: a=0, b=1, c=2
 *if s={1,0,2,2,0}
 *then print: bacca
 */

#include <stdio.h>

void print();
int charint(char *cher);
int charlen(char *cher);
int d,f,i,j,k,l,m,n,o,u;
int *s;
char *p;

int main(int argc, char *argv[])
{
        //if no arguments are given, print usage and exit
	if(argc<2)
	{
		printf("usage:\n./Buck  \n\t<number>\tlength of password\n\t<string>\tcharacter set\n");
		return 1;
	}

        //set variables with their initial values
	d=0;
	j=1;
	k=1;	
	l=1;
	m=charlen(argv[1]);		
	n=charint(argv[2]);
	o=0;
	s=malloc(m*sizeof(int));
	p=malloc(m*sizeof(char));
	u=0;
	p=&argv[1][0];
	k=m-1;

        //using the password length, and number of characters given, determin how many combinations are possible
	while(j<=n)
	{
		f=1;
		for(i=0;i<j;i++) f=(f*m);
		o+=f;
		j++;
	}
	if(o<0) o=o*(-1);

        //print number of combinations, length of character set, and max password length
	printf("o: %d m: %d n: %d\n",o,m,n);

        //makes sure initial values for S are all 0
	for(i=0;i<=m;i++) *(s+i)=0;

	d=l-1;

        /*
         *Explanation of the goto's (cause people hate spaghetti code)
         *being that this was just an exercise, I decided to experiment with gotos a bit.
         *forloop: the ones place is a special case, which is why a seperate for loop was used to increment it, afterwards, I could either have added a identical for loop, or just jump to the bottom, and use the existing code
         *end: jumps out of the do loop, an alternative would have been to use a break
         *whileloop: used in place of a while loop
         */
	for(i=0;i<m;i++)
	{
		*(s+d)=i;
		print();
	}
	*(s+d)=0;
	l++;
	d=l-1;
	goto forloop;

	do
	{
		whyloop:;
		*(s+d)=0;
		i++;
		d--;
		*(s+d)=*(s+d)+1;
                if(d<=0 && *(s+d)>k && l<n)
                {
                     *(s+d)=0;
                     l++;
                     goto end;
                }
        }while(d>0 && *(s+d)>k);
 	end: d=l-1;           
	forloop:;
	for(i=0; i<m; i++)
	{
		*(s+d)=i;
		print();
	}
	i=0;
	if(u<o) goto whyloop;
	return 0;
}
void print()
{
	char buff[l+1];
	for(j=0;j<l;j++)
	{
		buff[j]=*(p+*(s+j));
	}
	buff[j+1]=0;
	printf("%s\n",buff);
}
int charlen(char *cher)
{
	i=0;
	while(*(cher+i)!=0)
	{
		i++;
	}
	return i; 
}
int charint(char *cher)
{
	i = charlen(cher);
	i--;
	int j=1;
	int integer=0;

	for(i=i;i>=0;i--)
	{
		switch(*(cher+i))
		{
			case '1': integer+=(1*j); break;
			case '2': integer+=(2*j); break;
			case '3': integer+=(3*j); break;
			case '4': integer+=(4*j); break;
			case '5': integer+=(5*j); break;
			case '6': integer+=(6*j); break;
			case '7': integer+=(7*j); break;
			case '8': integer+=(8*j); break;
			case '9': integer+=(9*j); break;
		}
		j=j*10;
	}
	return integer;
}

Juggling Sequence Permutation Test

This is a basic program that uses the permutation test to display all possible juggling sequences of a certain period and with a certain number of balls. The program can be easily modified to ask the user for the period and number of balls required. Right now, it is hard coded in. The number of balls is 2 and the period is 4. Thus the sum of all of the beats in the sequence must be 8 and the average 2.

I worked on this in class, so please excuse the messy coding. I did not think that I would be showing the source code to anyone else.

 

//Justin Yang
//9-17-2013
//2:02 PM

#include
#include

using namespace std;

int main()
{
int x = 0;
int y = 0;
int z = 0;
int limit_one = 0;
int limit_two = 0;
int limit_three = 0;
int limit_four = 0;
int a_one[2];
int a_two[2];
int a_three[2];
int a_four[2];

int a, b, c, d, n;

cout << "Please input a, b, c, d: "; cin >> a >> b >> c >> d;

cout << "a: " << endl;
n = 0;
int lol = 0;
for (int i = 0; i < 10; i++)
{
if (((n + 0) % 4) == a){
cout << "\t" << n << endl;
a_one[lol] = n;
lol++;

limit_one++;
}
n++;
}

cout << "b: " << endl;
n = 0;
lol = 0;
for (int i = 0; i < 10; i++)
{
if ((n + 1) % 4 == b){
cout << "\t" << n << endl;
a_two[lol] = n;
lol++;
limit_two++;
}
n++;
}
cout << "c: " << endl;
n = 0;
lol = 0;
for (int i = 0; i < 10; i++)
{
if ((n + 2) % 4 == c){
cout << "\t" << n << endl;
a_three[lol] = n;
lol++;
limit_three++;
}
n++;
}
cout << "d: " << endl;
n = 0;
lol = 0;
for (int i = 0; i < 10; i++)
{
if ((n + 3) % 4 == d){
cout << "\t" << n << endl;
a_four[lol] = n;
lol++;
limit_four++;
}
n++;
}

/** for (int i = 0; i < limit_one; i++) cin >> a_one[i];

for (int i = 0; i < limit_two; i++) cin >> a_two[i];

for (int i = 0; i < limit_three; i++) cin >> a_three[i];

for (int i = 0; i < limit_four; i++) cin >> a_four[i];
**/

for (x; x < 3; x++)
{
// int n = a_one[x];
// cout << "A_One: " << a_one[x] << endl;
for (int y = 0; y < 3; y++)
{
// n += a_two[y];
// cout << "A_Two: " << a_two[y] << endl;
for (int z = 0; z < 3; z++)
{
// n += a_three[z];
// cout << "A_Three: " << a_three[z] << endl;
for (int i = 0; i < 3; i++)
{

// cout << "A_Four: " << a_four[i] << endl;
if ((a_one[x] + a_two[y] + a_three[z] + a_four[i]) == 8)
cout << a_one[x] << " " << a_two[y] << " " << a_three[z] << " " << a_four[i] << endl;
}
}
}
}

cout << "This is before System Pause." << endl;
system("PAUSE");
return 0;

}

G2 update; how C determines length of string

G2 is still being developed, I am currently working on modeling it in Inventor, so I can more easily happybirthdaytome work out the structural layout being that it is a much simpler design than the G1

also, if you wanted to know how C determines how long a char array is (which is just a char pointer with offsets in C)

int charlen(char *foobar)
{
     i=0;
     while(*(foobar+i)!=0)
     {
        i++;
     }
     return i;
}

*edit: updated post format