But here's where my code fails, it keeps on switching between two sounds quickly.
This is because the sound() function was not meant to be used this way. It will only allow you to play one tone at a time.
To do this, you'll have to generate the waveform yourself. As in.. the raw samples.
Without getting too far into it, the reason why this technique will never work is because computers are discrete. That means that the precision of a computer can only be represented down to a certain level, and then you find that, unlike with a pure mathematical representation of the number line, there are empty spaces between numbers. With the kind of dual wave interaction you are describing, you can't have those empty spaces.
So, because of that, you must actually do something that will combine output frequencies before reaching the discrete level.
once we have written the samples to our .wav file can a standard .wav encoder or library be able to play it, coz it is missing some things like headers and other chunks as you had said
Yes. Infact, it's more likely that you'll have less trouble playing a "canonical" wave file, stripped of miscellaneous chunks. Whenever I work with audio which I plan to load into memory, I always make sure it's stripped of additional chunks and PADs because I'm too lazy to write code that does this for me. Might be a viable idea down the road to do so.
htirwin once we have written the samples to our .wav file can a standard .wav encoder
Yes. libsndfile is a very prolific library used in many open source and commercial programs, as well as by other libraries. It will also read and write many other formats.
It's a cross platform library. The code will work on other operating systems. In order to do this, it uses platform specific code (depending on which operating system) to read and write the files. So in the background, in windows, it's using code similar to what xismn wrote.