Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Saturday, May 15, 2010

Learning C by playing with Pointers

The other day one of my C mentors wanted me to do the following;
How about a project where you are to enter the name and favorite (something) of up to twenty people, sort the data, then output the sorted data. Extra points if you do NOT change the actual data table to do the sort.

Well, I thought I could easily carry out this task.....but I later found out that I needed to learn pointers and how to play with it. I have achieved part A of the project, however I am yet to achieve part B. This one entails a lot of pointer manipulations, however, they say that to dare is to win. So, I will try and take on the part B later, hopefully in a week's time. This project has made me to study pointers and to pick some basic skills concerning dynamic array.







#include
#include
#include
#include
#include
int numData;
typedef struct {
char name[15];
char country[15];
}personalDetails ;
personalDetails *nameCountry;
//nameCountry.name = (char *)malloc(15*sizeof(char *)+ 1);
//nameCountry.country = (char *)malloc(15*sizeof(char *) + 1);


void allocateResources(int n){
int i = 0;
nameCountry = (personalDetails *)malloc(n * sizeof(personalDetails *));
//nameCountry[i].name = (char *)malloc(15*sizeof(char *)+ 1);
//nameCountry[i].country = (char *)malloc(15*sizeof(char *) + 1);
//}
}

void swap (personalDetails *b,int i, int k){
personalDetails g = b[i];
personalDetails f = b[k];

//(*b + i)->name = b[k].name;
b[i] = f;
b[k] = g;
}

int choosePivot(int i, int k){
return ((i+k)/2);
}
void quicksort (personalDetails *mycounty, int i , int k){
int m, n;
if (k > i ){
int num;
num = choosePivot(i,k);
swap(mycounty,i,num);
char *key = mycounty[i].name;
m = i + 1;
n = k;

//abort();



while(m <= n){

while((m <= k) && (strcmp(mycounty[m].name,key) <= 0))
m++;
while((n >= i) && (strcmp(mycounty[n].name, key) > 0))
n--;
if( m < n )
swap(mycounty,n,m);

}
swap(mycounty,i,n);

quicksort(mycounty,i , n-1);
quicksort(mycounty,n+1, k);

}
}


void readSortPrint(){

int i= 0;
int k = 0;
printf("Enter the number of data your would like to input");
printf("....\n");
scanf("%d",&numData);
allocateResources(numData);
printf("You will enter names and countries for %d times\n", numData);
while( i < numData){
printf("Enter Name and Country\n" );
scanf("%s%s", nameCountry[i].name,nameCountry[i].country);
i++;
}
printf("\n\n");
quicksort(nameCountry, 0,numData-1);
printf("Prints Name and Countries in alphabetical order\n");

while(k < numData){
printf("%s---%s\n", nameCountry[k].name,nameCountry[k].country);
k++;
}
//free_data();
free(nameCountry);
}


int main(void){

readSortPrint();

return 0;
}


Saturday, May 08, 2010

Learning C by doing string search






I wanted to be able to confirm that all the characters of a word could be found in a string or not. A friend told me to check out fuzzy string algorithm for this kind of problem. I searched the web, and read a couple of links on the algorithm, however, it appears to be a little different from what I have in mind. So I decided to do "something".

char * name = "janus"
char * searchIn = "jankgopuas";
int count = strlen(name);
int * acc;
int ctemp;
acc = (int *)malloc(count * sizeof(int *));
memset(acc,-1,count);
int position;
for(int i = 0; i < count; ++i){
char * temp = strchr(searchIn,name[i])
position = strlen(searchIn) - strlen(temp);
if temp {
if find(position ,acc){
// find function checks if position is a value is the array already
temp = strchr(searchIn + position , name[i]);
if temp {
acc[i] = strlen(searchIn + position) - strlen(temp);
}
else {
return acc[i];
}
}
acc[i] = position;
}
else{
ctemp = acc[i];
free(acc);
return ctemp;
}
}
ctemp = acc[count - 1];
free(acc);
return ctemp;



Hmm, the above is pretty ugly and I pray you will bear with me. Just learning C and trying out any idea that comes my way.

However, a friend handed this to me.

Scan through name, taking counts of characters to an array
Scan through the search string, decreasing the name character counts
(if over 0)
Scan through the counts, and if any is over 0, no match.

char c[0xff];
memset(c,0,0xff);
int i;
char * str1="foobar";
char * str2="foobababoobar";
char * p;
for (p=str1;*p;p++) c[*p]++;
for (p=str2;*p;p++) if (c[*p]) c[*p]--;
for (i = 0; i < 0xff; i++) if (c[i]) return "no match";
return "match";


There's most likely smarter ways of doing this, could you show me some?








Tuesday, May 04, 2010

Learning C







I felt that I needed to learn something new and exciting, then I asked my friends for direction. I was told to try C language. Wow, I was thrilled by that. One of my friends even gave me a great C book to start out with. I was also given a long list of books to read. However, I was not totally ready to dive into C until I met a hacker called Jari Komppa. That is how my life and experience with C changed. He was like an angel, ever willing to help and above all extremely brilliant.

He gave me a little project to start off with C, could you guess what the project was? Hmm, don't worry here is the answer; To traverse a folder and list all the files with their sizes. I figured out that with C activation record I could easily build up a stack. That was what I did, and pow, I got the desired result. It may not be the best, pretty crude, but I was happy with it.Well, I must confess that I was not able to carry out the task until I got my fingers burnt while studying Algorithms.

#include
#include
#include
#include

void processSize(char *name){
FILE * pFile;
pFile = fopen ( name , "rb" );

fseek ( pFile , 0L , SEEK_END );
long sz = ftell(pFile);
//fputs ( " sam" , pFile );
fclose ( pFile );
printf ("size = %ld bytes",sz);
}

void processFile(char gPath[]){
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
char *gCat;
char *gin;
char *cot;
char *cat;
char *fCat;
char *tempChr;
tempChr = (char *)malloc(strlen(gPath) * sizeof(char *));
strcpy(tempChr,gPath);
hFind = FindFirstFile(tempChr, &FindFileData);

while(FindNextFile(hFind, &FindFileData)){

gCat = strchr(tempChr,'*');
int inValue = strlen(tempChr) - strlen(gCat);

cot = (char *)(malloc(inValue * sizeof(char *)));
strncpy(cot,tempChr,inValue);
strcat(cot, FindFileData.cFileName);

if (!((strcmp(FindFileData.cFileName, ".")== 0)||
(strcmp(FindFileData.cFileName, "..")== 0))){

if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
// printf("future started yesterday");
printf("----%s----\n",FindFileData.cFileName);

strcat(cot,"\\*.*");
processFile(cot);
printf("---End-%s---\n",FindFileData.cFileName);
}
else{
printf("\t\t|--%s--",FindFileData.cFileName);
// printf("ooo%s",cot);
processSize(cot);
printf("\n");
}
}
}
}

int _tmain(int argc, TCHAR *argv[])
{


processFile("C:\\Documents and Settings\\idea\\Desktop\\60-h\\*.*");
//Put your the directory of your target here, note it is assumed you are using windows.

return 0 ;
}



I invite your comments

Tags

Arduino (1) C (3) Clojure (3) Perl (1) the other side (8) VBA (1)

micro's shared items