NP-complete 42 Junior Poster

What was the exact problem? Kindly share the scenario and the solution as might help others visitng this post...

NP-complete 42 Junior Poster

Also, you would like to print array[0] to array[4] to get the first 5 values :)

NP-complete 42 Junior Poster

You are NOT using StringTokenizer to merge the 2 arrays into the 3rd. You are using it to read the 2 arrays from a txt file. In that context, your strategy will depend on the way the data is distributed in that txt file.

As far as the merging is concerned, I believe you're looking into a better solution ^^

NP-complete 42 Junior Poster

So you want to echo the command line?

Well, well... consider this simple code snippet for that (from oracle's official tutorial):

public class Echo {
    public static void main (String[] args) {           
        for (String s: args) {
            System.out.println(s);
        }

        // you could have also used 
        // for (int i = 0; i < args.length; i++)
        // then inside, println(args[i])
        // the above is just a handy syntactic sugar
        // to print arrays and collections.
    }
}

Now output depends on how you give the input:

c:\yourCodeDirectory>java ab cd
ab
cd

c:\yourCodeDirectory>java "ab cd"
ab cd

c:\yourCodeDirectory>java "ab    cd  ef       gh            etc"
ab    cd  ef       gh            etc

So, moral of the story, giving inverted commas ("") around your command line input will treat the entire sequence (till you hit enter) as a single entity.

NP-complete 42 Junior Poster

for running an app you do not need a compiler. You can simply take out the .exe and share it.
It'll run, but given the fact that you're using "Turbo C++" it might not run as you would've expected it to, on a few machines.

A few advice:

  • Get rid of that turbo c. Immediately. Use Code::Blocks or Visual Studio 2010 express. You'll thank me later!
  • Read up on static linking of libraries. (it'll help for the issue at hand)
NP-complete 42 Junior Poster

@Taywin: I think you meant your loop to be :
for (int i = 1; i < array[index]; i++

and NOT
for (int i = 1; i <= array[index]; i++)

Why inclusive? Pseudo code syntax?

NP-complete 42 Junior Poster

Define a string somewhere and call it whatever... i called it str...then inside that loop use this:

// for the last word, no comma afterwards...
if (i == (aa5.length - 1)) {
    str = str + aa5[i];
} else {
    str = str + aa5[i] + ", ";
}
NP-complete 42 Junior Poster

Why do i always see an Indian arguing against int main? Is it so hard to accept the truth?
I'm an Indian and it seriously pisses me off.

NP-complete 42 Junior Poster

just to bring to the notice of the OP, Yashwant Kanetkar's book, Let us C (as mentioned by shylesh_kerala above) is NOT a good book. If you find Kernighan and Ritchie a bit difficult, you can follow a book called "C programming, a modern approach (second editon) by K. N. King". Its a very good book and has some serious coding and also teaches some good practices.

NP-complete 42 Junior Poster

okay, firstly, repeat this a 100 times,

"I'll never call main() from other functions"

main() is supposed to be called by the OS, and it will get really angry if you call it instead..!!

so remove all those calls to main() (line no. 31 to 33) and you'll get out of that loop...

oh, btw, you'll have to pass the sides to your calculations() function like this:

void calculations (int side1, int side2, int side3)
{
    // Do calculations
}

and offcourse, change the prototype suitably...


P.S: your main logic for triangle checking is flawed...

NP-complete 42 Junior Poster

may be you'll just need to put a cin.getchar() right before you return from main(), or a suitable breakpoint...


can't tell you more than that without looking at the code...:)

NP-complete 42 Junior Poster

Firstly, the two of you should have explained where the OP went wrong, before giving out your (wrong, unoptimized, non-standard, non-indented) code.

@shikhin:
your code is WRONG

A thought about optimization:
the looping can be done only up to the square root of the number. You need not go up to the half of it.

Finally, to xaop:

You were right on the fact that you have to divide and check for remainders. But you have do that inside the loop and every time you find a remainder you can safely come out of the loop and say that the number is not prime. That's what the two codes did (though the first one was wrong). And if the loop terminates without finding a remainder, the variable which you used for checking (in the examples above they used "prime" though traditionally it has been named "flag") will not be modified, and that's what is being checked n the last else.

Hope this helps.

P.S: few things about the code:
1) you have included math.h but didn't use it. Now that you know that the loop should go up to the square root of the no. put it to some good use. oh, btw calculate the square root outside the loop and store the value in a variable and then use this variable in your loop construct.
2) you need not declare i as long int. A simple int would do. …

NP-complete 42 Junior Poster

my apologies too...

NP-complete 42 Junior Poster

@ pvfloripa:
Do you think that after 4 years the OP is still in search of a "best" solution? Also this thread was marked solved!!
You are new to daniweb, so in future keep this in mind - you should not post in very old threads (and this one was 4 years old) unless its absolutely necessary.
Read the rules for once at least and try to stick to them. It make things a lot easier.
Good luck.

NP-complete 42 Junior Poster

@ parse :

Dont give out codes and that too codes that don't stick to the specs. For eg you were supposed to declare and initialize separately but you did them together.
Also, when the OP was asked to study and then come up with a better solution you should have perhaps waited for him to come up with a solution.
Your third post was far better (those links that you gave). In future try to help the OP with his learning don't just give out codes that he can simply copy paste for his HW. That's not daniweb.
:)

jonsca commented: Yes +4
NP-complete 42 Junior Poster

@tesu:
The algorithm is correct. Its just that the first tw0 terms of the series must be printed separately. fibo(18) is 2584 iff fibo(0) = 0. So what you are printing as your first term should be the 2nd term (ie fibo(2) if we take the first term as fibo(0)).

generally the series is :

fibo(0)------ 0
fibo(1)------ 1
fibo(2)------ 1
fibo(3)------ 2
fibo(4)------ 3

...............
...............

fibo(18)------ 2584


hope it helps.


P.S: @xaop
once you get the logic correct spend some time on coming up with meaningful variable names. Also use proper indentation. As of now, a small advice, never use void main(). Its plain wrong (and disrespect to the creators of C).

NP-complete 42 Junior Poster

okay, taywin already gave you an algorithm to approach the 2nd problem (about sine series). I guess you are having problems following it, so here's a bit more detailed description:

Firstly look at the series carefully. There are three important things to consider.
1) You have to calculate powers...(use library function)
2) You have to calculate factorials...(define a function separately to handle this)
3) You have to be careful about the sign of the terms (its alternate + and -)

There's also another thing to consider, the index of the main loop. Observe that the first term of the series has x^1/factorial(1), then x^3/factorial(3) and so on... so if you want 7 terms, the 7th term should have x^13/factorial(13). Right?? Do you see any relation?? Well, if you want 'n' terms you'll have the last term having x^(2n-1)/factorial(2n-1). So you may have your loop from 1 to (2*n-1) and increment it twice, something like:

for (int i = 1; i <= (2*n - 1); i = i+2;)

Note that this is just a way, not necessarily the best way. Generally its advisable (and sometimes fashionable) to start your loop from 0, but never mind.

Okay, after that you would have to think about the +ve, -ve thing. As taywin mentioned, checking for odd/even indexes can be a solution but in our present indexing system (from 1 to 2n-1 hopping 2 at a time, ie i=i+2) this won't be working (since, all i's are …

NP-complete 42 Junior Poster

i guess this is too easy to fetch him a grade:) ...but still...i'll be more carefull next time...

and thanks for the compliment...

NP-complete 42 Junior Poster

firstly i congratulate you for your attempt on "being the compiler" and checking the code as the compiler would have. That's a nice approach. But unfortunately the compiler is not as logical about logical operators as it should have been. There's something called "operator precedence" which kills the fun. So when you are doing

if (num1 > num2 && num3)

the > operator gets more preference and checks whether num1 is greater than num2. Lets say this is TRUE. Then it does something like TRUE && num3. This is definitely not the way you wanted it to be. right??
So, basically you have to do something like:

if (num1 > num2 && num1 > num3)

Do this for all your IFs.

Your 2nd and 3rd code has been explained by daviddoria. Go through them and do as you have been told and come back if you have any doubts.

Happy to help.
Cheers...

empror9 commented: thanks *_^ +1
NP-complete 42 Junior Poster

like all students protesting the use of antique softwares may be??

On a serious note...its all about spreading awareness. I have seen many students are simply ignorant about the gotchas of using turbo c or about the cons of writing non-standard code. So on your part you can simply spread the awareness amongst your friends and they in their turn will spread it to others and we can just hope that one day things will change. But for that we really need to appreciate the change. People are too relaxed and it seems that they don't want the change. This attitude needs to change. And change fast.

NP-complete 42 Junior Poster

If your looking for OOP design then you can give this book a try: "Head First Design Patterns" from O'Reilly. It's very good. Regarding UML you only need to learn a few basics and the rest will come along as and if you work with it. For that you can start with a book called UML distilled by Martin Fowler.

Hope it helps.

NP-complete 42 Junior Poster

IMHO you must change line no. 9 to something like:

else if (a==b && b==c && c==a)

The advise that finito gave you can be considered but that's not the concern in this case. Your logical checking is not right.

Actually your version will only work if you input all sides as 1.

WHY?

Because in C a 1 also means TRUE. And in that "if" if a=1,b=1 and c=1 what it does is it first checks whether a == b i.e whether true == true....that's TRUE so next it checks whether true == c OR whether true == 1 OR whether true == true (since 1 means true), and obviously true == true is indeed TRUE. So your code will only print "eq triangle" if you enter 1 for all three sides.

Hope that clarifies.

P.S: @ abhimanipal:

In my opinion, scanf() is not that bad for simple inputs of ints, chars or floats. It only gets a bit buggy when it comes to strings. Then fgets() can surely be a big help (and is also safe). Otherwise for simple usage I think scanf() is just fine. But yes, there are hidden gotchas and fgets() is definitely better.
But it all depends on the coder how he uses the functions given to him.

Aia commented: It all depends how the function is used. Correct! +9
NP-complete 42 Junior Poster

@Jwenting:
I apologize for my loose use of the clause "for-profit". I didn't mean it that way.

As far as the second post is concerned, I beg to differ.

A country (or person) doesn't have that potential. It can at most create it.

Well said. In that case i believe India (and all countries in this world) has all the potentials to "create" the potentials to be at the top. After all, we all are human beings and geographical factors do not determine a person's potential.
If a person from country X can, then so can a person from country Y.

And India will never create that potential as long as they remain intent only on showing the world how superior they are by running diploma mills instead of universities...

Firstly, how do you relate "running diploma mills" to "showing the world how superior they are"? Secondly, who told you that India has more diploma mills that universities? There's also a third part to it but lets save that for the end.

You guys have a massive superiority complex which prevents you from ever achieving anything

Well, I don't know how many Indians you have met and if at all that no. is sufficient for you to jump to this conclusion but i have a thought...seeing your rebuttal (or should I say, racist) posts against mine that does not relate to you in particular or any country in general, i think i smell some "inferiority …

nav33n commented: Correct. +0
vaultdweller123 commented: Very well said sir specially on the "showing the world how superior they are" there a bunch of racist and jealous people who likes to be always on top, but for me i admire indian programmers as they are naturally intellegent +0
NP-complete 42 Junior Poster

i think you are slowly going towards a homework assignment....first malloc then linked list now perhaps you would ask how to add nodes or delete them...:)


well, here's a link that might be some good to you....
http://www.cplusplus.com/reference/clibrary/cstdlib/malloc/


P.S: In C++ why would you like to use "malloc" anyway. You have better options like "new"

Ancient Dragon commented: Agree +28
NP-complete 42 Junior Poster

One more thing, you can use a char variable to print your alphabets

char myChar = 'A';

and then inside the 2nd loop you print this (as many times the loop iterates) and then you INCREMENT it so that it now becomes 'B';

myChar++    //will make it 'B' if it was 'A'

Hope it helps.

NP-complete 42 Junior Poster

First things first....I am an Indian and yet with a heavy heart I believe in most of the opinions put forward so far in this thread.

I wish more Indians read this thread and more could THINK before going in blindly for quantity over quality.

As already mentioned these for-profit institutes are all worthless. But then whats a workaround?

In my humble opinion I believe India has all the potential to be the top country (in every aspect) but alas people have stopped thinking and its high time they realize their potentials.
I somehow agree with others when they said that the good IT professionals from India are all trained in the US or UK. That's again a shame for India. Its not that we don't have potential its just that we don't accept change.
And that's the reason you find turbo C++ everywhere in India even in the top most universities !!

Finally as an advice to Chichiro, i would repeat what others have already mentioned DON'T go for these non profit institutes. Except if you are inclined towards programming I guess self studying is far more fruitful. After you have studied well and grasped the concepts clearly go for reputed certificates. For e.g. if you are learning JAVA go for SCJP or other certis from SUN/Oracle. If you are learning Dot Net go for certis from Microsoft.

This way you will not waste your money on something that has no …

maydhyam commented: Nicely put... +0
NP-complete 42 Junior Poster

How many times do you want DANIWEB to remind you of CODE TAGS?? They are so sick and tired that they have also watermarked "please wrap code in code tags..." in every "post reply" message box. Still you guys can't see. Its a shame.

I also found that you have used comments in a foreign language!!!

Now, how about me giving you some help in Bengali, my mother tongue?

NP-complete 42 Junior Poster

well actually <string> includes all the functions and other instances and also overload operators(to enhance functionality) of the string "class". So if you wanna use any of those(which in all probability you will) you HAVE to include <string>.
In the above code as well, you HAVE to #include <string>. Why ?? Because I have used the << and >> operators for string objects and they will not work unless you #include <string>.
So its always a better idea to include <string> as Fbody has already mentioned.

Hope it clarifies
Cheers.