Windows Vista Code Released!

April 6, 2008 by Rapster

Ok. The title was completely misleading. But I found this picture online - a spoofed MS Vista code. I found it extremely funny and also uncannily accurate. Looks like MS entry products are getting as bad as Apple’s. Fortunately Apple fixes the products quickly enough and usually there are only minor quirks. Lets hope MS does the same. In any case, here’s the code :

:D :D

Funny is how I find it and you can be sure that’s what it was made to be.

Rapster

Void Pointer

March 29, 2008 by Rapster

Void pointer is a concept I discovered in one of my accidents. After that question which KVKK plucked from the question papers of the Infosys Entrance Exam to give to us twelfth graders, I tried various ways of solving it and using the concept of a void pointer was one of them. I never knew it existed and never for a moment believed it did. I actually tried it out of frustration and guess what?.. It works!!

KVKK gave some syntax for void pointer to be used during the program. Obviously, he never thought it was going to be any serious issue at school level and didn’t care to detail upon the syntax. But after a lot of attempts and checking the TurboC3 help in frustration ,I saw my mistake.

I had declared a void pointer ‘ptr’ and pointed it to an integer location:

void *ptr=new int;

Then I tried operations on it using this syntax :

*(int*(ptr))=10;

It gave me errors. I checked TurboC3 Help out of sheer frustration and found the correct syntax:

*(int*)ptr;

The difference? : C and C++ typecasting. I should have guessed when KVKK said that this was the only way to handle different data types using one variable name in ‘C’ while there were many other ways in ‘C++’.

Still. Yay TurboC3 Help!

Rapster

Accidents

February 21, 2008 by Rapster

There are some things in C++ that I discover by accident and love the fact that I have done so again. :D

So, as a post after a long time, I’ll post an important discovery of mine.

I knew that in the objects of fstream, the first parameter of the open function was meant for the path of the file and not the file alone. But I seriously had no idea how to enter a path. After many many attempts, I have discovered the way to specify your path even if the file is not in the folder where your program is. This is an example:

"C:\\super crappy folder\\sub crappy folder\\dumbfile.txt"

The use of \\ was the key which took me an eternity to find out. In any case, thats all I wanted to post about and I’m posting from the Windows Live Writer. So, I’m waiting to see how this post turns out to be.

Until next time,

Rapster

Permutations

September 11, 2007 by Rapster

At last, I, Rapster have done it!!! After thinking it out for less than 5 minutes, I came up with this program and guess what!!!… Chrono posted another program with the same logic in that community… Damn!!!

But then, I went a step ahead and also checked for repeating digits. Right now, I am too bored to explain the logic. So, here’s the program:

#include <iostream.h>
#include <stdlib.h>
int permute(float* a, int size, int from);
void swap(float& x, float& y);
int main()
{
float* a;
int size, count;
cout << “Enter size: “;
cin >> size; cout << endl;
a=new float [size];
cout << “Enter the elements: \n”;
for(int i=0; i<size; cin >> a[i], i++);
cout << endl;
count=permute(a,size,size-1);
cout << “Count: ” << count << endl;
system(”PAUSE”);
}
int permute(float* a, int size, int from)
{
int j=0;
if(from>=2)
{
for(int i=from; i>=0; i–)
{
if(a[i]!=a[from] || (a[i]==a[from] && i==from))
{
swap(a[from],a[i]);
j+=permute(a,size,from-1);
swap(a[from],a[i]);
}
}
}
else
{
for(int i=size-1; i>=2; cout << a[i] << ” “, i–);
cout << a[from] << ” ” << a[from-1] << endl;
j++;
if(a[from]!=a[from-1])
{
for(int i=size-1; i>=2; cout << a[i] << ” “, i–);
cout << a[from-1] << ” ” << a[from] << endl;
j++;
}
}
return j;
}
void swap(float& x, float& y)
{
float z=x;
x=y;
y=z;
}

Its kinda short but it sure is swell!!!

Till next time,

Rapster

Equation Solver

September 7, 2007 by Rapster

Rapster is back!!!.. Its been a long time since I blogged in my favourite blog and an even longer time though since I mailed. But right now, this is the program of the moment.

This program is a multiple variable equation solver. And when I say MULTIPLE, i mean MULTIPLE!!! This program also includes inside the transpose program, the inverse program and the finding of value of a determinant program. (Essentially, thats why it took me so long to finish!!!). So, without further ado, I present to you, my most prestigious program yet!

#include <iostream.h>
#include <math.h>
struct matrix
{
double* x;
matrix()
{
x=NULL;
}
~matrix()
{
delete []x;
}
};
double find_val(matrix* x, int size);
void multiply_matrix(matrix* x, int size, double value);
void transpose(matrix* x, int size);
void find_adjoint(matrix* x, matrix* y, int size);
void multiply_matrices(matrix* x, matrix* y, matrix* z,int size);
int check_for_singularity(matrix* x, int size);
int main()
{
matrix *coeffs, *consts, *inverse, *soln, *temp;
int size;
char* variables=new char [size];
cout << “Enter no of variables: “;
cin >> size; cout << endl;
coeffs=new matrix [size];
inverse=new matrix [size];
variables=new char [size];
consts=new matrix [size];
soln=new matrix [size];
temp=new matrix [size];
for(int j=0; j<size; j++)
{
temp[j].x=new double [1];
soln[j].x=new double [1];
consts[j].x=new double [1];
}
for(int i=0; i<(2*size)-1; i++)
{
coeffs[i].x=new double [size];
inverse[i].x=new double [size];
}
cout << “Enter variable names: “;
for(int i=0; i<size; i++)
cin >> variables[i];
for(int i=0; i<size; i++)
{
cout << “Equation: ” << i+1 << endl;
for(int j=0; j<size; j++)
cin >> coeffs[i].x[j];
cout << endl << “Enter constant: “;
cin >> consts[i].x[0]; cout << endl;
}
find_adjoint(coeffs,inverse,size);
if(!find_val(coeffs,size))
{
multiply_matrices(inverse,consts,temp,size);
if(check_for_singularity(temp,size))
cout << “No solution!!!” << endl;
else
cout << “Infinite solutions!!!” << endl;
goto end;
}
multiply_matrix(inverse,size,1/find_val(coeffs,size));
multiply_matrices(inverse,consts,soln,size);
for(int i=0; i<size; i++)
cout << variables[i] << ” = ” << soln[i].x[0] << endl;
end:
delete []coeffs;
delete []variables;
delete []inverse;
delete []consts;
delete []soln;
delete []temp;
cin.get(); cin.get();
}
double find_val(matrix* x, int size)
{
double value=0;
if(size>2)
{
for(int i=0; i<size; i++)
{
matrix* y=new matrix [size-1];
for(int k=0; k<size-1; k++)
y[k].x=new double [size-1];
for(int j=1; j<size; j++)
{
for(int k=0, l=0; k<size; k++)
{
if(k!=i)
{
y[j-1].x[l]=x[j].x[k];
l++;
}
}
}
value+=(pow(-1,i)*find_val(y,size-1)*x[0].x[i]);
delete []y;
}
}
else if(size==2)
{
value=((x[0].x[0]*x[1].x[1])-(x[0].x[1]*x[1].x[0]));
}
return value;
}
void find_adjoint(matrix* x, matrix* y, int size)
{
matrix* z=new matrix [size-1];
for(int i=0; i<size; i++)
z[i].x=new double [size-1];
for(int a=0; a<size; a++)
{
for(int b=0; b<size; b++)
{
for(int i=0, j=0; i<size; i++)
{
if (i!=a)
{
for(int k=0, l=0; k<size; k++)
{
if(k!=b)
{
z[j].x[l]= x[i].x[k];
l++;
}
}
j++;
}
}
y[a].x[b]=(pow(-1,a+b)*find_val(z,size-1));
}
}
transpose(y,size);
}
void multiply_matrix(matrix* x, int size, double value)
{
for(int i=0; i<size; i++)
{
for(int j=0; j<size; j++)
x[i].x[j]*=value;
}
}
void transpose(matrix* x, int size)
{
for(int i=0; i<size-1; i++)
{
for(int j=i; j<size; j++)
{
float z;
z=x[i].x[j]; x[i].x[j]=x[j].x[i];
x[j].x[i]=z;
}
}
}
void multiply_matrices(matrix* x, matrix* y, matrix* z, int size)
{
for(int i=0; i<size; i++)
{
z[i].x[0]=0;
for(int j=0; j<size; j++)
z[i].x[0]+=(x[i].x[j]*y[j].x[0]);
}
}
int check_for_singularity(matrix* x, int size)
{
for(int i=0; i<size; i++)
{
for(int j=0; j<size; j++)
if(x[i].x[j])
return 0;
}
return 1;
}

Phew!.. That was one HUGE program. At least it works. I also made it recognise infinite and no solution situations. That’s one program finished.

Till next time,

Rapster!!!

File Sorter Program

July 18, 2007 by Rapster

And I’m Back!!!.. Rapster does it again!! And proves that bastard called KVKK wrong!!.. Yay to Rapster!!!

Ok, Rewind:

When I had finished the first sessions program and we had come back to copy another demo program, Ganapathy asked some stupid doubt. I don’t know how the discussion went but atlas, I heard KVKK say that it was not possible to sort data in an already created file.

I argued and he said that the algorithm I told him was kinda good but not feasible in real time. And now, I can kick his butt OFFICIALLY!!! This is the easiest program ever.

Ok, explaining the program takes some time so I ain’t gonna do it.  Here, I create a file specifically for this purpose. This could also be used as a function in another program to sort some other file!!!

Here’s the program:

#include <iostream.h>
#include <fstream.h>
struct set_of_variables
{
int rno;
char name[32];
float m1, m2, m3;
};
int chk_no_of_records(char filename[26]);
void copy_file(char dest_filename[26], char src_filename[26]);
void file_skip_copy(char filename[26], int rno);
int main()
{
set_of_variables set1, set2, set3;
char condition;
int i=0;
fstream f, f1;
f.open(”Temp1.txt”, ios::out);
do{
cout << “Enter the Roll No : “;
cin >> set1.rno; cout << endl;
cout << “Enter the name : “;
cin >> set1.name; cout << endl;
cout << “Enter the marks : “;
cin >> set1.m1 >> set1.m2 >> set1.m3; cout << endl;
f << set1.rno << endl << set1.name << endl << set1.m1 << endl << set1.m2 << endl << set1.m3 << endl;
cout << “Do you want to continue? : “;
cin >> condition; cout << endl;
i++;
} while (condition==’y’ || condition==’Y');
f.close();
f.clear();
copy_file(”Temp2.txt”, “Temp1.txt”);
f.open(”Temp1.txt”, ios::out);
while (chk_no_of_records(”Temp1.txt”)!=i)
{
f1.open(”Temp2.txt”, ios::in);
f1 >> set1.rno; f1 >> set1.name;
f1 >> set1.m1; f1 >> set1.m2; f1 >> set1.m3;
set2=set1;
while (!f1.eof())
{
f1 >> set1.rno; f1 >> set1.name;
f1 >> set1.m1; f1 >> set1.m2; f1 >> set1.m3;
if (set1.rno < set2.rno)
set2=set1;
}
f << set2.rno << endl << set2.name << endl << set2.m1 << endl << set2.m2 << endl << set2.m3 << endl;
f1.close();
f1.clear();
file_skip_copy(”Temp2.txt”, set2.rno);
}
f.close();
f.clear();
return 0;
}
void file_skip_copy(char filename[26], int rno)
{
fstream f, f1;
set_of_variables set1;
copy_file(”Temporary.txt”, filename);
f.open(”Temporary.txt”, ios::in);
f1.open(filename, ios::out);
f >> set1.rno;
f >> set1.name;
f >> set1.m1; f >> set1.m2; f >> set1.m3;
while (set1.rno!=rno && !f.eof())
{
f1 << set1.rno << endl << set1.name << endl << set1.m1 << endl << set1.m2 << endl << set1.m3 << endl;
f >> set1.rno;
f >> set1.name;
f >> set1.m1; f >> set1.m2; f >> set1.m3;
}
f >> set1.rno;
f >> set1.name;
f >> set1.m1; f >> set1.m2; f >> set1.m3;
while (!f.eof())
{
f1 << set1.rno << endl << set1.name << endl << set1.m1 << endl << set1.m2 << endl << set1.m3 << endl;
f >> set1.rno;
f >> set1.name;
f >> set1.m1; f >> set1.m2; f >> set1.m3;
}
f.close(); f1.close();
f.clear(); f1.clear();
}
void copy_file(char dest_filename[26], char src_filename[26])
{
set_of_variables set1;
fstream f, f1;
f.open(src_filename, ios::in);
f1.open(dest_filename, ios::out);
f >> set1.rno; f >> set1.name;
f >> set1.m1; f >> set1.m2; f >> set1.m3;
while (!f.eof())
{
f1 << set1.rno << endl << set1.name << endl << set1.m1 << endl << set1.m2 << endl << set1.m3 << endl;
f >> set1.rno; f >> set1.name;
f >> set1.m1; f >> set1.m2; f >> set1.m3;
}
f.close(); f1.close();
f.clear(); f1.clear();
}
int chk_no_of_records(char filename[26])
{
fstream f;
f.open(filename, ios::in);
set_of_variables set1;
f >> set1.rno; f >> set1.name;
f >> set1.m1; f >> set1.m2; f >> set1.m3;
int i=0;
while (!f.eof())
{
f >> set1.rno; f >> set1.name;
f >> set1.m1; f >> set1.m2; f >> set1.m3;
i++;
}
f.close();
f.clear();
return i;
}

Extremely long program, I swear!! But I couldn’t make it any shorter!!

Edit on Previous Post:

You can use fstream objects as well. The only thing to check is that you put a <fstream object>.clear() function at the end of it after closing the object. I didn’t know that at all.

School Program 2

July 17, 2007 by Rapster

Another stupid program that KVKK gave us!

Problem : Accept student data from user and then ask him if he wants to delete any particular roll number’s details completely. Delete the detail of the roll number that the user enters after verifying whether it exists.

Here’s the program:

#include <iostream.h>
#include <fstream.h>
struct set_of_variables
{
int rno;
char name[32];
float mark;
};
int main()
{
set_of_variables set;
int del_rno;
char again;
ofstream f;
f.open(”Student.txt”, ios::out);
do{
cout << “Roll No: “;
cin >> set.rno; cout << endl;
cout << “Name: “;
cin >> set.name; cout << endl;
cout << “Mark: “;
cin >> set.mark; cout << endl;
f << set.rno << endl << set.name << endl << set.mark << endl;
cout << “Continue? : “;
cin >> again; cout << endl;
}while (again==’y’ || again==’Y');
f.close();
cout << “Delete? : “;
cin >> again; cout << endl;
while (again==’y’ || again==’Y')
{
cout << “Roll No? : “;
cin >> del_rno; cout << endl;
ifstream f(”Student.txt”);
ofstream f1(”Temporary.txt”);
f >> set.rno;
f >> set.name;
f >> set.mark;
while (set.rno!=del_rno && !f.eof())
{
f1 << set.rno << endl << set.name << endl << set.mark << endl;
f >> set.rno;
f >> set.name;
f >> set.mark;
}
if(set.rno!=del_rno)
cout << “Doesn’t Exist!!!” << endl;
f >> set.rno;
f >> set.name;
f >> set.mark;
while (!f.eof())
{
f1 << set.rno << endl << set.name << endl << set.mark << endl;
f >> set.rno;
f >> set.name;
f >> set.mark;
}
f.close(); f1.close();
ifstream f_1(”Temporary.txt”);
ofstream f1_1(”Student.txt”);
f_1 >> set.rno;
f_1 >> set.name;
f_1 >> set.mark;
while (!f_1.eof())
{
f1_1 << set.rno << endl << set.name << endl << set.mark << endl;
f_1 >> set.rno;
f_1 >> set.name;
f_1 >> set.mark;
}
f_1.close(); f1_1.close();
cout << “Delete? : “;
cin >> again; cout << endl;
}
return 0;
}

I noticed something extremely peculiar here. The program did not work if I created only 2 fstream objects instead of  5 ifstream and ofstream ones. I reused these 2 objects again and again but the program did not work though there was no error!

Aw well! At least I finished!!..

School Program 1

July 7, 2007 by Rapster

AH!!!.. Its been a long time since my last post here. But Rapster will ALWAYS be back in time.

Rewind : KVKK, the bastard, has set rules in order to make our computer practicals as unenjoyable as possible. We now have some routine called sessions in lab period. We are given a “tough” program in the first session which we have to complete by 3 more sessions. If we don’t, marks will be cut in some exam (I have no idea which!) in the near future.

The first program seemed tough at first but is STUPID in the end as always. I MUST thank Shre or Lord Chronoz here as he was the one who told me an algorithm from which i derived my own and completed the program.

As my hard disk is prone to crashes and as I am completely vetti now and as my blog has to have SOMETHING in it, I am posting the programs that i do in practical “sessions” over here. This is the first program that was given to us.

Problem : You have accept student data from the user and insert it in sorted order into the file - “Student.txt”.

I struggled a lot in the beginning since the bastard never mentioned that extra files could be used but once i was clear on that point, I was able to carry on on my own.

Here’s the program:

#include <iostream.h>
#include <fstream.h>
int main()
{
int rno, rno1;
char name[32], condition, name1[32];
float m1, m2, m3, m_1, m_2, m_3;
cout << “Enter the roll no: “;
cin >> rno; cout << endl;
cout << “Enter the name: “;
cin >> name; cout << endl;
cout << “Enter the marks in the 3 subjects: “;
cin >> m1 >> m2 >> m3; cout << endl;
ofstream fout(”Student.txt”);
fout << rno << endl << name << endl << m1 << endl << m2 << endl << m3 << endl;
fout.close();
do {
cout << “Enter the roll no: “;
cin >> rno; cout << endl;
cout << “Enter the name: “;
cin >> name; cout << endl;
cout << “Enter the marks in the 3 subjects: “;
cin >> m1 >> m2 >> m3; cout << endl;
ifstream fin(”Student.txt”);
ofstream fout(”Temp.txt”);
fin >> rno1;
fin >> name1;
fin >> m_1;
fin >> m_2;
fin >> m_3;
do {
fout << rno1 << endl << name1 << endl << m_1 << endl << m_2 << endl << m_3 << endl;
fin >> rno1;
fin >> name1;
fin >> m_1;
fin >> m_2;
fin >> m_3;
} while (!fin.eof());
fout.close();
fin.close();
ifstream fin1(”Temp.txt”);
ofstream fout1(”Student.txt”);
fin1 >> rno1;
fin1 >> name1;
fin1 >> m_1;
fin1 >> m_2;
fin1 >> m_3;
while (!fin1.eof() && rno1<rno)
{
if (rno1<rno)
{
fout1 << rno1 << endl << name1 << endl << m_1 << endl << m_2 << endl << m_3 << endl;
}
fin1 >> rno1;
fin1 >> name1;
fin1 >> m_1;
fin1 >> m_2;
fin1 >> m_3;
}
fout1 << rno << endl << name << endl << m1 << endl << m2 << endl << m3 << endl;
if (rno1>rno)
{
fout1 << rno1 << endl << name1 << endl << m_1 << endl << m_2 << endl << m_3 << endl;
fin1 >> rno1;
fin1 >> name1;
fin1 >> m_1;
fin1 >> m_2;
fin1 >> m_3;
while (!fin1.eof())
{
fout1 << rno1 << endl << name1 << endl << m_1 << endl << m_2 << endl << m_3 << endl;
fin1 >> rno1;
fin1 >> name1;
fin1 >> m_1;
fin1 >> m_2;
fin1 >> m_3;
}
}
fin1.close();
fout1.close();
cout << “Do you want to continue? : “;
cin >> condition; cout << endl;
} while (condition==’Y’ || condition==’y');
cin.get();cin.get();
return 0;
}

Ofcourse as usual, I have adopted the longest route possible to this program. For instance, I could have used functions to open the files and used functions to copy and other things as well. Ah well! At least I was one of the first ones to finish this program!

Site

April 17, 2007 by Rapster

This is a site which zombie told me just a few moments ago over phone. Its the site of the British Informatics Olympiad and its AWESOME to say the least!!!…

http://www.olympiad.org.uk/

It also contains problems from the International Informatics Olympiad. Check it out!!… Thanks a lot, zombie. You have given me one site to satisfy all my desires….

Rapster

Pascal’s Triangle

April 17, 2007 by Rapster

This is another of my programs which i created very very long ago. It is actually very very inefficient (almost as inefficient as it is old). I was stymied by how to create this program as i had no idea what a pascal’s triangle was used for. It was only when chappli and later shre elaborated to me about its use in binomial theorem that i was able to design this program. Others later designed this program with much advancement like giving functions for calculating factorial. But i still retain this program as i created it by using basic functions which may be useful if i ever forget them. So, here it goes:#include <iostream.h>
int main()
{
long n, counter=1, counter1=1, a=1, b, c, d=1, e=1, f=1, h=1, g, j, ncr, i=1, k;
cout << “Enter the value for n: “;
cin >> n; cout << endl;
while (counter!=(n+1))
{
cout << ” “;
counter++;
}
cout << a << endl;
counter=1;
while (counter!=n)
{
i=0;
k=n-counter;
if (k==0) k=1;
while (i!=k)
{
cout << ” “;
i++;
}
if (counter!=n)
cout << a << ” “;
while (counter1<=counter)
{
g= counter;
b=counter1;
c=counter-counter1;
j=c;
e=counter1;
while (b>1)
{
e=(counter1-d)*e;
b=counter1-d;
d++;
}
d=1;
f=counter;
while (g>1)
{
f=(counter-d)*f;
g=counter-d;
d++;
}
d=1;
h=j;
while (c>1)
{
h=(j-d)*h;
c=j-d;
d++;
}
if (h==0)
h=1;
ncr=f/(e*h);
counter1++;
cout << ncr << ” “;
d=1;
e=1;
h=1;
f=1;
}
counter++;
cout << endl;
counter1=1;
}
return 0;
}

Pretty long eh?… But i still love it. The efficiency ofcourse can always be improved hundredfold but i dont like to do so.

Rapster