Prabakar 77 Posting Whiz

Well, if dealing with 2-d is your problem, try this. len n = r * c of the matrix.

use buble sorting, on matrix[0][0] to matrix[0][n-1]. It will sort, the matrix, left to right & top to bottom

And also, in the code, you use, NO_OF_ROWS as a matrix, as a function & as an integer. What is this?

Prabakar 77 Posting Whiz

Hey and also u are reading \n along with the IP address, It is evident in the output
12 will do I guess

Prabakar 77 Posting Whiz

>>strcat ( fname,"\\") ;

remove this line, you already have a slash in the name

Prabakar 77 Posting Whiz

Thank you, & Is my inference right, I am asking about the memory.

Prabakar 77 Posting Whiz

ok..as u said, we delete the memory pointed by pointer using 'delete'. but after tht in my prog.. there is a line ----
delete s;
s = NULL;

after this, the pointer becomes NULL, then how it can points fun2()..

The compiler, binds this code. The compiler, belives that there exist an object in the memory NULL (0) & then calls the fun2(), with the 'this' pointer set to NULL(0).

I wrote the following code,

#include<iostream.h>
class sample
{
public :
    sample(){}
    void fun1(){}
    void fun2(){
        cout << this << endl; 
    }
    ~sample(){}
};

main()
{
    sample *s = new sample;
    s->fun1();
    delete s;
    s = NULL;
    s->fun2();
    ((sample*) 12) -> fun2() ;
    ((sample*) main) -> fun2() ;
}

and the out was,
0
12
4190 // address of main

Prabakar 77 Posting Whiz

I am Inferring something, here.

"Mythology"[5] ; is a valid c statment.

The code resides, in the memory & the constant "Mythology", resides with it.

So it will also have, a vaild address. and *("Mythology"+5) will return 'l'?

Am I right or am I worng?. Please Justify.

Prabakar 77 Posting Whiz

When I searched the web, I found an intersting program, I dont think I can explain a bit. But, I would like to share it. It displays my home country thats the most I can say about it!

Here goes,

#include<stdio.h>
main()
{
int a,b,c;
int count = 1;
for (b=c=10;a=
"- LLLLLL?, LMKC,XYZHELLO FOLKS,\
TFy!QJu ROo TNn(ROo)SLq SLq ULo+\
UHs UJq TNn*RPn/QPbEWS_JSWQAIJO^\
NBELPeHBFHT}TnALVlBLOFAkHFOuFETp\
HCStHAUFAgcEAelclcn^r^r\\tZvYxXy\
T|S~Pn SPm SOn TNn ULo0ULo#ULo-W\
Hq!WFs XDt!" [b+++21]; )

for(; a-- > 64 ; )
putchar ( ++c=='Z' ? c = c/ 9:33^b&1);
}
Prabakar 77 Posting Whiz

I guess, the term 'delete s' is misleading.
delete s does not delete the pointer, it deletes the object pointed by it.
It would make sense if delete was named as deletethememorypointedby.
You would be happy to use that big of a keyword, won't you!!!
And in this program, even

((sample*) 12) -> fun2() ; // will work

even
((sample*) main ) -> fun2() ; // will work. dont belive me, try as many times as you like.

but, never refernce any class variable, in that funtion or else the program will crash.

Prabakar 77 Posting Whiz

I dont like to use recursion, unless it is very much necessary. If i were to calcualte GCD between 2 numbers, I would do it somthing like this.

#define min(a, b) (a)<(b)?(a):(b)
int main ( )
{
    int a, b, i, c, d, gcd = 1, lcm ;
    cin >> a >> b ;
    c = a ; d = b ;
    for ( i = min(a, b) ; i > 1 ; i-- )
         if ( a % i == 0 && b % i == 0 )
         {
              gcd = gcd * i ;
              a = a / i ;
              b = b / i ;
              i = min ( i, min ( a, b ) );
          }
    lcm = c / gcd * d;
    cout << gcd << lcm ;
}
Prabakar 77 Posting Whiz
#include <stdio.h>
#include<string.h>
int main ()
{
      char firstipadd[20], fname[30] = "D:\\";
      FILE *pFile;;
      pFile = fopen ("D:\\xyz.txt" , "r");
      fseek(pFile,169,SEEK_SET);
      fgets (firstipadd , 14 , pFile);
      fclose (pFile);
      strcat ( fname, firstipadd) ;
      rename ( "D:\\xyz.txt", fname ) ;
      getchar ( ) ;
}

This code is working for me for your txt file.
However, I dont like the idea of using fseek like this.
I would prefer reading the file char by char.
But since you are in a hurry, I'll give this for now.

Prabakar 77 Posting Whiz

Analytical geomentry, gives variousways to calculate distance. In this case, we can use

sqrt( (x1- x2)*(x1-x2) + (y1- y2)*(y1-y2) )

Prabakar 77 Posting Whiz

Sorry, I went to lunch, & in a haste, I didnt put it the right way.

Lets say you create a class with some variables and functions. Now you create an object to it. like this

class c
{
public: 
    int n ;
    void getn( int n1 ) { n = n1; }
} ;

c cobj ;
would create a fresh copy of the "int n" only, there is no meaning in creating a new copy of the same function, so fn will take there memory while loading. They will live even if there is no object for the class.

The compiler takes the pointer as cobj pointer and calls the fun2 () stored in memory

the fun2() does not use any variable in the class ( ofcourse, you have no variable in the class ) that is why your code worked.

In your class there is no variable. even if you create an object it is not going to take any space( but sizeof ( cobj) will return 1. It makes sense because, c cobj[10] with all having the same address is non-sense)

I guess this shoul explain

Prabakar 77 Posting Whiz

sample *s, creates a near pointer to the class sample. "new sample" creates an object, and calls the constructor with 0 argument. s->fun1() ; calls function 1.

s-> fun2() calls fun2 since functions dont depend on this object because there is no variable used,
when objects are created space is allocated only to the variables not to functions ( virtual fns are exceptions)

if fun2 would have had cout << n ;
Then there will be an run time error, cause there is no such n.

Prabakar 77 Posting Whiz

Thank you, for those details. And then we download softwares during lab hours and take it home with our pen drive. Recently, some viruses and trojan horses spreaded all over the lan & to my pen drive. Thats how it happened. Anyway now the lab incharge is fixing it.

Prabakar 77 Posting Whiz

Search the forum first, many threads are there on bitwise operators

Prabakar 77 Posting Whiz

void list::inslast(int x)
{

nn=new node;
nn->data=x;
p=first;
while(p->ptr!=NULL)
{ p=p->ptr;}

Write a code to handle p == NULL

void list::delelement(int x)
{
node *q;
p=first;
while(p->data==x)
{
q=p;
p=p->ptr;
}
q->ptr=p->ptr;
cout<<"\n\t\t Successfully deleted..\n";

}

while ( p->data != x ) should be used. x is what you dont want right.

while deleting you should use 'delete p ;'

Prabakar 77 Posting Whiz

Why dont you loop it till the user inputs 0 or
till the scanf fails.
i.e., scanf ( "%d", addr ) ;
will scan data as far as it is valid inputs like 'x' when scanf expects integer are invaild & scanf will return 0.

Does this answer your question?

Prabakar 77 Posting Whiz

Yes

Prabakar 77 Posting Whiz

I find the algorithm Interesting, thank you.
I'll soon work with it after my semester exams are over.

Just now I have installed Win XP after the virus attack. I learned my lesson. Ill never download softwares in college. I will install Visual studio soon & learn visual C.

About recover.txt, I was tempted to find answers exhaustively, & I knew, there must be lots & lots of answers, So I used recover.txt to save the progress of the program & then when I run it back, it would continue with next answers.

Prabakar 77 Posting Whiz

sytem() fn doesnt work for me either. My computer always says "Not enough memory" when I use system() & spawn...() functions. but then, many dos commands can be implemented using c. eg, you can manipulate files & folders, etc with some functions. Format a drive, and other dos functions using dos interrupt & ROM-BIOS interrupt if you are using a old C compiler like me.

Prabakar 77 Posting Whiz

I use conio.h because thats what I have learned so far. Help me please, what is the better, how do I learn it & where do I get the compiler for it

Prabakar 77 Posting Whiz

I understand a little. And also, could you help me with your code please

Prabakar 77 Posting Whiz

There are many I would call this one great

Prabakar 77 Posting Whiz

It has nothing to do with your previous question. Why put it hear.
Find its length, with a for loop.

for ( i = 0 ; i < length/2 ; i++ )
{
//swap ith char with length -1- ith char
}

Prabakar 77 Posting Whiz

considering line 80 to 104,
You alredy have the data in line & you know exactly what format the data is. So, why dont you use sscanf().

And for the malloc problem

1) You may follow what dragon said & loop twice over the file
2) use a linked list. That would make you change a lot of code
3) or Use realloc, but be carefull though, cause you are using a pointer to a pointer to a float

The first looks better to me.

Prabakar 77 Posting Whiz

I dont know HTML but I have read the basic few years back.

And I have some questions.

1) Coments in HTML start with <!... right?
So are you restricting the size of comments

2) Some HTML Tags dont have ending tag.
I did'nt find any peice of code that deals with this. Ofcourse I am in a hurry & did'nt examine the code line by line

Prabakar 77 Posting Whiz

Well, use any function that does the job.

besides, rewind() is nothing but

fseek (/* file pointer*/fp ,0L, SEEK_SET ) ;

Prabakar 77 Posting Whiz

I have never used bios.h much but I hope bioskey(1) will work the same way as kbhit()

#include<stdlib.h>
#include <stdio.h>
#include <bios.h>
#include<string.h>

/* Defination of all moves */
#define TL 0x01 // top left i.e., 2 squares up & 1 square left
#define TR 0x02
#define RT 0x04
#define RB 0x08
#define BR 0x10
#define BL 0x20
#define LB 0x40
#define LT 0x80

// stack which holds the position
typedef struct visited {
  char place ; // varies from  0 to 63 representing a square
  char moves ; // availabe moves -> every bit represents a possible move
  struct visited *next ; // link to previous move
} VISIT ;

VISIT *head ; // contains the latest move made by the horse

#define removMov(mov) head->moves &= ~(mov) // removes an available move
#define isMove(mov) (head->moves&(mov))	   // checks if the move is available

unsigned int number = 0 ; // number of answers written in the current file
unsigned int fno = 0 ;    // file number

int count ; // number of places the horse has covered at any point

FILE *sol ; /* pointer to the file in which the answer is written
	"SOLXXXXX.txt" where XXXXX is an unsigned integer  */

void newPlace    ( char ) ;// pushes a new move to the stack VISIT
void undo        ( void ) ;// pops the stack i.e., undos the last move
char isKnown     ( char ) ;// checks if a place has been visited by the horse
void copystack …
Prabakar 77 Posting Whiz

That's ok Should'nt be much of a problem, I'll Change the code and post agin.

Prabakar 77 Posting Whiz

If you say that you dont know the size of the file, then you can use a linked list. get a data using fscanf(), insert it into your list when you find the position. continue until end of file.

But if the file is very big, then use some external sort algorithm

This would prove to be very simple.

Prabakar 77 Posting Whiz

I agree with you there.

Its always joyful to play with pointers & casting pointers. So larryeis, learn a bit more about casting & pointers. I promise, it would be fun

Prabakar 77 Posting Whiz

I have copied the code below. The problem I face is It never seems to end. Thats why I want to start from scratch with better algorithm.

#include<stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<string.h>

/* Defination of all moves */
#define TL 0x01 // top left i.e., 2 squares up & 1 square left
#define TR 0x02
#define RT 0x04
#define RB 0x08
#define BR 0x10
#define BL 0x20
#define LB 0x40
#define LT 0x80

// stack which holds the position
typedef struct visited {
  char place ; // varies from  0 to 63 representing a square
  char moves ; // availabe moves -> every bit represents a possible move
  struct visited *next ; // link to previous move
} VISIT ;

VISIT *head ; // contains the latest move made by the horse

#define removMov(mov) head->moves &= ~(mov) // removes an available move
#define isMove(mov) (head->moves&(mov))	   // checks if the move is available

unsigned int number = 0 ; // number of answers written in the current file
unsigned int fno = 0 ;    // file number

int count ; // number of places the horse has covered at any point

FILE *sol ; /* pointer to the file in which the answer is written
	"SOLXXXXX.txt" where XXXXX is an unsigned integer  */

void newPlace    ( char ) ;// pushes a new move to the stack VISIT
void undo        ( void ) ;// pops the stack i.e., undos the last move
char isKnown     ( char ) ;// checks if …
Prabakar 77 Posting Whiz

it does choose some path. I doubt weather it will find all paths exhaustively. like
left, right forward in random order

Prabakar 77 Posting Whiz

The second can be solved very easily with a for loop alone

for ( i = 1 ; i < n / 2 ; i++ )
if ( !(n % i )) sum += i ;

While the first, you may use the follwing:

1) Get the unsigned number 'n'( and duplicate it) & the base 'b' that should be used.
2) perform 'n' modulo 'b' & get each digit the same way
3) Use switch case or what ever you want to find the corresponding digit in base 'b'.
( My advice dont use switch case. Use the nature of ascii codes for your advantage )
4) print the characters that you should have stored in step 2 in reverse order

I hope It answers your question.

Prabakar 77 Posting Whiz

On second thoughts, I did not find any logical resons to limit combinations.
Could you help a little bit more.
And thanks too, I am finding even intersting problem in google

Prabakar 77 Posting Whiz

thanks a lot

Prabakar 77 Posting Whiz

You can always get integers using scanf functions.

you can use getw() & putw() functions to get integers from a file opened in binary mode.

Does this answer your question?

And also putw ( n = getw(stdin), stdout ) will seem to work but wont for a good reson. Find it of yourself by analyzing.

Prabakar 77 Posting Whiz

A couple of other notes:

- void main is evil. Don't use it.
- You shouldn't have your function prototype inside main(). Move line 13 to somewhere before main()

I nerver thought void main had so much drawbacks thanks a lot for you advice.

Prabakar 77 Posting Whiz

I know nothing about ASP and I am intrested in learning it.
Please help me find a good means to start.

Prabakar 77 Posting Whiz

I happened to try find the total number of ways a horse can cover all 64 squares in a chess board without visiting a square more than once starting from one corner.

All I was able to do was to use a brute force algorithm which run for a whole day just to find about 400000 answers.

Can any one please help to improve the programs performance.