siddhant3s 1,429 Practically a Posting Shark

Arrays and pointer are closely related in C. But first tell me what book are you using?
Anyways, one advice: your scripting languages like Python and Perl makes you feel that programming is fun, this is not the case with C. I am sure you will have to do quite low level things in C. Know that C is statically typed.

If you are just beginning to C, don't use the above said solution. It will tend to confuse you.
You must know that you must explicitly state the size of an array which should be a CONSTANT. Thats the most important luxury you miss when coming from perl. In C we have fixed sized array. ( we too have hacks for dynamically sized array but that would be covered later).
So, the solution would seem like this:

#include <stdio.h>
/*The maximum prime that the Program supports*/
#define MAXPRIMES 100
int main() {
	int max;
	int count = 1;
	int value = 1;
	/*note below, here I passed a constant value,100 as size*/
	int primes[MAXPRIMES];
	int composite;
	int i = 1;
	int j;
    /*I warned the user that maximum primes supported by this program*/
	printf ("How many primes?(Maximum %i)",MAXPRIMES);
	scanf ("%d", &max);
	
	
    /*I am too tired to check your progarm logic.
    As you are a programmer, SHould I trust you?
    Anyways, if you can make it run, get us back*/
	printf ("2 is prime\n");
	primes[0] = 2;
	while (count < maxprimes){
		composite = 0;
		value …
siddhant3s 1,429 Practically a Posting Shark

You should know that python supports slicing. If you do not, read the python tutorial.
The find function returns the first occurrence of the string you pass as the argument. [In the previous example, it returns 4].
So, when I say s[:find("<")] I mean to say " slice me s, from the beginning till the occurrence of '<' ".
You should perhaps go and read the manual if you still don't understand the code.

siddhant3s 1,429 Practically a Posting Shark

Depending on your platform, you can time your application for a given set of data.
Or you can use a profiler . But whatever you do, please don't optimize your code without analyzing its performance.
Optimize only and only when you really need it.

siddhant3s 1,429 Practically a Posting Shark

>I know it has to be a loop, but which function?
Why are you so sure?

>>> s="asdf<script"
>>> print s[:s.find('<')]
asdf
>>>
siddhant3s 1,429 Practically a Posting Shark

Oh man, I accidentally gave ^^ him a good reputation. I meant the bad one. Is this reversible?

siddhant3s 1,429 Practically a Posting Shark
#include<stdio.h>
#include<stdlib.h>

int main()
{
    void* buf_ptr = NULL;
    buf_ptr = malloc(1428480);
    if(buf_ptr)
    {

        printf("%p \n",buf_ptr);
        void* p;
        p=buf_ptr+1;
        printf("%p \n",p);
    }

    return 0;
}
siddhant3s@Xion:~$ gcc testerc.c -o testerc.exe -ansi -pedantic  
testerc.c: In function ‘main’:
testerc.c:11: warning: ISO C90 forbids mixed declarations and code
testerc.c:12: warning: pointer of type ‘void *’ used in arithmetic
siddhant3s@Xion:~$ ./testerc.exe
0xb7c97008 
0xb7c97009

You win....... The code compiles though. But you are correct.

siddhant3s 1,429 Practically a Posting Shark

@Tux4life
Your beautiful post has nothing to do with OP's problem. The construct use for allocating space using malloc is absolutely valid in C.

The only problem is on line:
void* ptr = buf_ptr[20];
as Beair.GQ said, you are assigning a void to void*. The type of buf_ptr is not that of a pointer.
You should perhaps try this: void* ptr=but_ptr+20;//this is good

siddhant3s 1,429 Practically a Posting Shark

Your problem, what I am perceiving is, that you want to have an interface for your class which can return the c-string for name but don't let user change it.
Returning a pointer is not a good idea overall. Change your signature of the get function as:

void getName(char* dump);

This way, the user of your class can provide a pointer to char where he wants the content of the name to be transfered to. The function body should look something like this:

void getName(char* dump){
strcpy(dump,name);
}

Now, your user can use this function like this:

Testing t1("Somename");//construct t1
//
//
char name_of_t1[50];//change size to the maximum of what a name can be

t1.getname(name_of_t1);
std::cout<<name_of_t1;

Using C-string( null terminated char array) is never a good idea when you have the standard std::string to handle that.

siddhant3s 1,429 Practically a Posting Shark

>What i said is just polymorphism
No. What you said was overloading (function overloading to be exact). Read http://www.cs.bu.edu/teaching/cpp/polymorphism/intro/ point number 2.(What is polymorphism?).
In short, while discussing in the domain of C++, polymorphism is the term used to describe the dynamic polymorphism (virtual classes and function and related...) while overloading is the term used to describe static polymorphism.

siddhant3s 1,429 Practically a Posting Shark

OMG, the OP is answering his own question..... oh wait..... So you are twins !! That took me a while after noticing that you both share the same avatar.

> as you know for an array
> &array[0] = array,
> so we just pass in the name of the array when ever we need to pass its
> address.
Umm.. I am sensing a little trouble here. If you are cleared up on what is the difference between an array a pointer, it is fine, but if you are not: Arrays and pointers are different things in C (or C++). Read this won't harm.

siddhant3s 1,429 Practically a Posting Shark

>static arrays are ............
You meant to say "fixed-length" arrays, right?

OP>in here i cant set the array size from function receiving argument? and why?
Because, array size should be a compile time constant, that is: the array size should be known at the compile time. You are already been told what is the solution by Dave.

siddhant3s 1,429 Practically a Posting Shark

I was to be with adatapost if he would have added a line such as ``This is what you want but please stop using a pre-historic compiler".

The fact is that the graphic.h header file won't never give OP, the curses look. The only thing it will do is to make the code Huge and un-tangible ( of course making it unportable too). Do think we should create our own rectangles and squares to create windows and buttons?

I know this character here which mostly posts Turbo C++ compatible code. I don't care what you do when you are with you: use turbo C ++ or any other older non standard compiler. But when you are in a community, please (at least try to) obey standards.

The point I want to highlight is ( which has now become difficult to prove) that the suggestion of graphic.h was not given after reading the OP's "I am on turbo C++" but was to be the default answer by him anyways (I told you it is difficult to prove this now.) Furthermore, the use of graphic.h won't do any good on Turbo C++ either.

I know the Green rep to him (after 3 red one) must be a matter of sympathy. If it was not, there was no need to neutralize the red dot.

> Then stop replying posts you have no intention of of adding constructive responses to!
>Adatapost, you do not have to use a …

tux4life commented: There you've got a good point :) You're just the kind of person we need on this forum. +8
jephthah commented: thanks +10
siddhant3s 1,429 Practically a Posting Shark

>Can ne1 tell me how 2 write a c++ program to find out the determinant of a (mxn )
>matrix
Determinants are only defined for square matrix.
A far as writing of the program is concerned: "NO ONE IS GOING TO WRITE IT AND DELIVER IT TO YOU" The ONE in the last sentence stands for any person having sense.

You need you get out of the habit to beg for code. Doesn't it depletes your honor ?
Try out to write at least some code and then ask proudly : "This is what I try, and these are my problems"

A good pointer to determinant : http://local.wasp.uwa.edu.au/~pbourke/miscellaneous/determinant/

siddhant3s 1,429 Practically a Posting Shark

>I dont know which one to download from where and how to use it.
So, STFW..
Salem has already told you twice to use curses( or ncurses or pdcurses) but look at you.... you are not even bothered.

siddhant3s 1,429 Practically a Posting Shark

>Wouldn't this replicate the problem earlier?
No.

std::string sql_backslash_to_underscore(const char * key)
{
  if (!key) {
    //("got NULL key");
    return NULL;
  }
  std::string rep = key;
  std::replace(rep.begin(),rep.end(),'\\','_');
  std::cout<<(void*)rep.c_str();//lets see what is the location of the cstring of rep
  return rep;//note: it is now returning *copy* of rep.
}
int main()
{
const char* a=sql_backslash_to_underscore("acct\\username").c_str();
    std::cout<<std::endl<<(void*)a;//check the location of a
    std::cout<<std::endl<<a ;
    std::cout<<std::endl;
}
siddhant3s 1,429 Practically a Posting Shark

>^ so.... whatever happened to June 6th?
The plan stretched. Didn't bother to change the signature. (I know this is ignorance). Long story!!...

siddhant3s 1,429 Practically a Posting Shark

>The thing is that the functions calling sql_backslash_to_underscore is expect a
>const char * instead of a std::string
If you don't have trouble changing the calling code, you can make a function call as : sql_backslash_to_underscore("Hello").c_str();

C-type functions which dealing with Cstrings usually let the caller code determine the output. Most of them has prototype as this:
void f(const char* input, char* output);

siddhant3s 1,429 Practically a Posting Shark

Use urllib to fetch the ftp directory:

>>> a=urlopen('ftp://ftp.cs.stanford.edu/pub/cweb/')
>>> a=a.read()
>>> print a
drwxr-xr-x   3 ftp      users        4096 Oct 24  2006 .
drwxr-xr-x  49 sybase   sybase       4096 Dec 11  2008 ..
-rw-r--r--   1 ftp      users        4962 Dec 29  2003 Makefile
-rw-r--r--   1 ftp      users        4950 Feb 14  2002 Makefile~
-rw-r--r--   1 ftp      users        3270 Feb 14  2002 README
-rw-r--r--   1 ftp      users       61440 Oct  1  2003 albertus.tar

as you can see , this is giving me date of last modification as well as size. Use it to check which files are new.

siddhant3s 1,429 Practically a Posting Shark

Just add typname before RangeToValidatorMap::const_iterator it; to make it typename RangeToValidatorMap::const_iterator it; Now try to find out using web, why this fixed the problem. Get back here if you don't find anything.

siddhant3s 1,429 Practically a Posting Shark

Recursive functions is almost never a good idea if you are aiming for speed.
Recursive functions however, are easier to conceptualize than iterative functions. They goes with philosophy ``Developers time is far more important than machine time."

However, on languages such as C, C++ recursive functions are not 'so bad' since function calls are relatively cheap.

As far as this case is concerned, STL provide the reverse function do reverse a string, then checking for palindrome is a child's play:

bool isPalin(const std::string& s)
{
   std::string t(s);
   std::reverse(t.begin(),t.end());
   return s==t;
}
siddhant3s 1,429 Practically a Posting Shark

>I find it somewhat humorous that you compare building an operating system to a
>trivial twenty line homework program.
Then my purpose was solved.

>That's a bad thing. Even at the lowest level you still need to keep an abstract
>view of your design.
The program I was interacting with were very trivial ( prime number generator ). It was in a algorithm book. There, the author used a ASM, to demonstrate that what a high-level program assumes to be `a step' is actually a series of steps in machine code.
But you said it right, one should be able to `quantize' a step. It also varies from person to person and to which domain he is working.

>it's impossible to add another level of randomness by random shuffles or other
>tricks while we are using the same (extremely bad ) rand() generator.
She assumed a perfect random generator while making that remark, IMHO. And she was right too. More random calls will tend to dilate the variance. (of course, I am too, making this remark assuming a perfect random generator) :)

siddhant3s 1,429 Practically a Posting Shark

>Step 1: Fill the array using rand.
>Step 2: Check for zero and if not present, add it to the end.
This is already being discussed.
>Step 3: Randomly shuffle the array.
Code it baby, code it. As I can say "Build an operating system." is a single step


>Apparently you have an extremely limited idea of what a step is.
Yeah. My recent interaction with low level languages are responsible for this.


>See? I can play games with terminology too.
I am very glad to know this.:yawn:

siddhant3s 1,429 Practically a Posting Shark

Works for me: g++ on Linux

#include<iostream>
#include<string>

static const char * sql_backslash_to_underscore(const char * key)
{
  if (!key) {
    //ERROR0("got NULL key");
    return NULL;
  }
  
  std::string rep = key;
  std::replace(rep.begin(), rep.end(), '\\', '_');
  return rep.c_str();
}
int main()
{
    std::cout<<sql_backslash_to_underscore("acct\\username");
}
siddhant3s@Xion:~$ g++ tester.cpp -o tester
siddhant3s@Xion:~$ ./tester
acct_username
siddhant3s 1,429 Practically a Posting Shark

>Duh. It is an extra step, after all.
You mean you can do the entire re-shuffling in one step(in two or more statements?) ? Enlighten me how?

siddhant3s 1,429 Practically a Posting Shark

>Can anyone tell me the answer? Or if it can not be done?
ArkM has beautifully answered your question. Open your mind to see this.

I would again like to point out ArkM's last point: If you are a C++ programmer, you should know that Macros are evil. Look at (http://siddhant3s.uuuq.com/how_to_tell_rusted_cpp/#SECTION00051000000000000000)
and (http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.5)

If you are a C programmer, you should not post C related queries in C++ forums.

siddhant3s 1,429 Practically a Posting Shark

Good, but that requires <algorithm>
Anyways. OP never mentioned that he is restricted not to use it.
I was also thinking of sending this as solution. I dropped the plan when I realize that it would kill his purpose. :)

siddhant3s 1,429 Practically a Posting Shark

Tell me something: Can you read words or text from mouse? no.
Well this was very vague conclusion though,
Mouse streams are possible. But, mouse is an optional component of a computer. C++ trace back to those years computer when monitors were new technology!!
So, of course you can read mouse (the location of its pointer) and write mouse (set the position of pointer) but that would be a OS specific issue. Each OS will give different control over mouse. Hence they are not part of standard streams. They are not shipped with every compiler.

Note that when you are well verse in C++, you would be able to create your own mouse stream on WIndows( or perhaps Linux)!!

siddhant3s 1,429 Practically a Posting Shark

If it bothers you, try to be expressive while writing your code: Use iterators.
learn the reverse iterators at http://www.cplusplus.com/reference/string/string/rbegin/
After you read the example there, study the following code:

bool isrev(string s1,string s2)
{
    string::reverse_iterator rit;
    string::iterator it;
    if(s1.size()!=s2.size())
        return false;

    for ( rit=s1.rbegin(),it=s2.begin();
    rit < s1.rend()&&it<s2.end();
    rit++,it++ )
        if (*it!=*rit)
            return false;

return true;

}
siddhant3s 1,429 Practically a Posting Shark

Ok, What you want is a bit informal explanation.
Think of stream as a virtual device (presumably a printer). You are give functions to read/write to that device. You actually don't differentiate if that device is actually your monitor, your printer, the hard disk, or the RAM. What you know is that if you have to write or read any of these devices, you can use the same set of functions to do so.
In C++, there are filestream (which can be assume to be a device as hard disk), memorystream( which can be assume to be a device as your RAM), iostream (which can be assume to be a device as monitor for printing and k/b for reading).

You can use the same >> and << operators to read/write from a file, memory, monitor/keyboard, without knowing the specification of each. Thus you save remembering exclusive function for each of these devices.

siddhant3s 1,429 Practically a Posting Shark

Your not catching the value return by the function.

#include <iostream>
using namespace std;
enum months {jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec};
months nextMonth(months& currentMonth);
int main()
{
    months a = nov;

    a=nextMonth(a);

    cout << a << endl;


    return 0;
}

months nextMonth(months &currentMonth)
{
    return months(currentMonth+jan);
}
siddhant3s 1,429 Practically a Posting Shark

The techniques are:
* RTFM & STFW
* Learn to frame and type english
* Try to make more meaning full thread titles
* And at last but not the least read this

Don't take it personally, but it just don't make sense to keep asking trivial questions.
PS: Hint, use the str.find() method.

siddhant3s 1,429 Practically a Posting Shark

The integers are too big. You will have to explicitly flush the stream as it takes a hell lot of time to generate the next number which makes your condition true.
Also, postfix LL after the long long constants as already told by others.

#include<iostream>
using namespace std;
int main()
{
	unsigned long long i;
	for (i=999999999LL;i<1234567890123LL;++i)
        if (i%100000000==0)
            cout<<i<<flush; //check here
}

Read this post for a possible explanation: http://www.daniweb.com/forums/post873624.html#post873624

siddhant3s 1,429 Practically a Posting Shark

>Sometimes the simplest solution is the best one.
Sometimes simplest solution is not the portable one. Your solution lean on grep to be available.

f=open('file.txt');
lines=f.readlines();
for l in lines:
# remove .lstrip() from the following next line, if you don't want to be strict. 
# Any whitespace wont stripped for detection.
    if l.lstrip().startswith("line2="): 
        print l

f.close()
siddhant3s 1,429 Practically a Posting Shark

>I'd suggest a final step of randomly shuffling the values.
That can complicate things. I would suggest OP that: after assigning values 0 to 9 to all those 20 location, choose a location randomly and assign value zero to it.

siddhant3s 1,429 Practically a Posting Shark

Listen, I am using a compiler which runs C++ on my mobile phone. My mobile phone is 1990s build and does not have registers. It just have Memory.
I am not arguing whether your PC has a register or not. Of course it has. we both knows. But the point here is that C++ language does not have any scope of registers as ASM has.
In any machine language (or assembly language), you deal directly with memory and registers. While, C,C++ are languages which are implemented at a higher level. Compliers actually hide you all the details of the hardware architecture.
If you then too want to know the value of register, I am sure your OS would have some system call to help you get those:http://msdn.microsoft.com/en-us/library/aa394373(VS.85).aspx

siddhant3s 1,429 Practically a Posting Shark

>thanks man, a million thanks. It works
So, what is stopping you to mark the thread as Solved.

siddhant3s 1,429 Practically a Posting Shark

Ok I would like say this:
C++ is not the language which runs on MiniComputers ( IBM PC). It can run on your digital wrist watch too (provided if the wrist watch has that capability). So the language doesn't guarantees the existences of registers.

siddhant3s 1,429 Practically a Posting Shark

Use simple boolean statements.
Here is my python interaction:

>>> a = [1,2,3,4,5,6,7,8,9,10,10,10]
>>> b = list(input ("Enter numbers: "))
Enter numbers: [3,4,10]
>>> a= [x for x in a if x not in b]
>>> a
[1, 2, 5, 6, 7, 8, 9]
>>>
siddhant3s 1,429 Practically a Posting Shark

>As C++ developer - stop finding ready-made code or solution otherwise you will
>mess up your work.
"Good programmers know what to write. Great ones know what to rewrite (and reuse)."
He is not looking for "homework helps". He is trying to exercise what is one of the well known good programming methodology "Code reuse".

To OP:
boost::asio is perhaps the most standard way to implement sockets. It is cross-platform and obviously free. Boost also have many library for IPC methods. Try to find them and use them. Boost libraries are the most expertly designed .

iamthwee commented: yep, gotta love the boost libraries. +21
siddhant3s 1,429 Practically a Posting Shark

>How do we output value in register AX?

How can you be so sure that your computer has a register?
C++, or any compiler doesn't know about registers. C++ also runs on those computer which doesn't have registers.
So, don't go at much lower level with a language as C++.

siddhant3s 1,429 Practically a Posting Shark

Each program on specific operating system is alloted its own memory space which is usually parted into 3 components: stack, heap and data.

When you say that you know the value at location 0x245CEA20, You may be mistaken!!

Suppose I have a variable named a in a program A which is of integer type. I can easily print its address by using the address-of operator.
Now suppose, I build another program which would ask the user to enter a Hex,Decimal value and to print the value pointed by it, it would not be the case that I would find the same value. as a has.
So you can't say "I get a value like 22E75100 which is wrong", you actually can never find the value at a particular location before running the program.

There is more complexity involved than this. The Operating system may continuously change the memory map of your program during its run time !! But you never know the difference as these things are abstracted from you. "Hidden backstage"

You will never need a construct like this though. That is, you would never need to fetch the value of a arbitrary memory location.

siddhant3s 1,429 Practically a Posting Shark

mine is probably the space bar, since I always use it to press space......

Yeah, but we use space bar key to press delete.

Me a Emacs user: What can you expect. The left control key.
There is nothing written on it.
Space has gotten much older but there was nothing written on it so it make no difference.
Labels on S,D,K,L,E,N,M, have completely vanished.
Return key has suffered.

The most interesting condition is of the Shift key:
The last [TEX]t[/TEX] has got completely erased. There is a scratch on [TEX]f[/TEX] which appears as [TEX]t[/TEX]. There is a up arrow sign too. So the key becomes:
[tex]\triangle Shit[/tex]

My keyboard also have a key with windows logo which is perfectly new. Also, th "Wake Up" "Sleep"and "Power" keys are shining bright.

I guess I should change my K/B

siddhant3s 1,429 Practically a Posting Shark

Try not to confuse people. Those who knows python does not necessarily know bash. The same way you know bash but do not know python.
BTW, here is your code:

s="dani_web_needs_better_OP"
print s.split("_")[1]

One more advice, try to read a book. These things are covered in the introductory chapters.

scru commented: yes +6
siddhant3s 1,429 Practically a Posting Shark

About negative subscript in arrays and array notations
Consider the following code:

#include<stdio.h>
int main()
{
    int ia[]={0,1,2,3,4,5,6,7};/*a normal array*/
    int* ip=&ia[4];/*a pointer pointing to 5th element*/

    /*WRONG: will print garbage. Unpredictable value*/
    printf("%i \n",ia[-3]);

    /*OK. Prints 1*/
    printf("%i \n",ip[-3]);

    return 0;
}

The Line 11 will be interpreted as *(ia-3) which is (absolutely) perfect since the location has been declared by the compiler.
While Line 8 is hazardous since it access a element which is out-of-bond.

In no case does negative subscript will produce last elements. (This facility is available in Python)

If you want to design a construct to do so, consider this:

#include<stdio.h>
#define AT(X,Y) ((Y<0)?(*(X+sizeof(X)/sizeof(*X) + Y)):(*X+Y))
int main()
{
    int ia[]={0,1,2,3,4,5,6,7};

    printf("%i \n",AT(ia,-1));/* Prints 7*/
    printf("%i \n",AT(ia,2));/* Prints 2*/
    return 0;
}

Always know the Murphy's Law: "you'll be hit the hardest at the worst possible moment (when the customer is looking, when a high-value transaction is trying to post, etc.)"

siddhant3s 1,429 Practically a Posting Shark

Look at lines
dvd movie;
movie(lista);
in int main()

What do you want movie to be: a function or a dvd structure?. Use a different name for the object or a different name for the function.

Note that it is alright to declare a dvd called movie in main(). But now, the global movie ( which is a funtion) will be hidden by local movie( which is a dvd).
You can call the global movie by using the scope resolution operator ::

int main(int argc, char *argv[])
{
    list<string> lista;
    list<string>::iterator it = lista.begin();
    for (int i=1; i<=2; it++){
    it++;
    }

    dvd movie;


    ::movie(lista); //look here
    lista.insert (it,movie.name);

The correct code tags in C++ forums are:
[code=cplusplus] //your code

[/code]
This helps to highlight syntax.
Note that there will be more (logical)errors when you will fix this one.

[edit]
Tux>/* I've also changed your include directives */
I thank you for that. This was one more point that I wanted to tell him.
Why is the OP buddering the compiler to process extra header files when he can just use 3:

#include <iostream>
#include <string>
#include <list>

Rest are useless!!

siddhant3s 1,429 Practically a Posting Shark

The whole concept of Template is to exploit the fact that interface of the data-type passed is same. Consider std:;sort() it takes the vector of any container which have overloaded > operator.
That is, template are used to perform type-unspecific operations.
If you are needing a way to check if the type of the parameter is a int or a float, your designs have flaws and you should not be using Templates for this purpose.
Instead you should have been using a overloaded function.

Another thing you can consider is the Template specialization. Which can be understood here.

Use typeid(), RTTI is not a good idea in the first place. Avoid it.( Read Scott Mayers Effective C++)

siddhant3s 1,429 Practically a Posting Shark

>the main prob is i m a beignner and i have to submit my assignment soon
Your only problem is that you have NOT read a book.
Go and get one and read it.

You have not impressed us a bit so that we should help you.
also read How to ask question the smart way

siddhant3s 1,429 Practically a Posting Shark

>Congratulations! I'm very glad of the opportunity to read this extremely elegant
>code.
LoL. You're sounding funny.:)

>>. 1st = > . 1St?
fine fine. I didn't write that code for 'utmost' perfection. (same way as you did not)
Anyways, that catch can be solved by using isalnum instead.

>By the way, isalpha(s) is a dangerous construct if s contains non-ascii
>characters...
Surprisingly, my implementation handles it. (GNU g++ on Linux)

You looks like a implementor to me, are you?

siddhant3s 1,429 Practically a Posting Shark

>Why even bother clicking it? You can see what it says just by hovering over it XD
Oh yeah!! But I can bet you did opened it. Try hovering now

siddhant3s 1,429 Practically a Posting Shark

This is for you: Click Here