vidit_X 29 Junior Poster

Function call should be like this

func(array_name); //where array_name is the name of your array.

You should pass the array without any index, because passing with an index makes the value at that index to be passed as argument not the array. And you second question's answer -
Why will the function call define the array length? Array has to be defined before its passed, either dynamically or static by you.

vidit_X 29 Junior Poster

Thanks VernonDozier and Salem. :)

vidit_X 29 Junior Poster

cmath different in visual c++ and dev c++ ? Sorry, I know i ask a lot of questions but i'm Curious.

vidit_X 29 Junior Poster

Thanks Salem for your answers. :) But DEV-CPP compiled the code with no error, whereas with the same code Visual C++ gave the above error.

vidit_X 29 Junior Poster

When i compiled the above code in Visual C++, I got the following error

error C2668: 'sqrt' : ambiguous call to overloaded function

Though I understood the error, but the same code gave no errors when i compiled it in DEV-CPP. So my question is why does different compilers give different errors(and some dont give that too)? Which one's ISO based? Does every compiler have different header files? Previously, i used Turbo C++ v3.0 but it doesn't supports new C++ code. Which compiler replaced Turbo C++?

A lot of questions ;)

vidit_X 29 Junior Poster

system("pause");
return 0;
use that at the end of ur main method should work:)

I think using system("pause"); is not recommended.

vidit_X 29 Junior Poster

Thanks VernonDozier for your concern. :) Actually after the Lerner's post, i realized what was Salem pointing at. "I dont got it" at the time when Salem gave me the hint, but after lerner's post it got clear. Anywayz, Thanks VernonDozier .

vidit_X 29 Junior Poster

Salem gave me the hint, but I dont got it. :( Thanks Lerner for helping.

vidit_X 29 Junior Poster

Any other tips for efficiency?
There is only 1 even prime number i.e 2.

vidit_X 29 Junior Poster

Thanks Salem for help, I think you really hate those who use "void main()" and you love "int main()" as your avtar and line under name says :D

vidit_X 29 Junior Poster

Why void main is deprecated in ISO standards? Where can i get a brief overview of the ISO C++ standards?

vidit_X 29 Junior Poster

I know about namespaces and the new standards i.e #include<iostream> , but ya my compiler "Turbo C++ v3.0" is old and dont understands the new formats like namespaces and the new style header files. An upgraded, according to the new standards, code will be

#include<iostream>
#include<cmath>
using namespace std;
bool isprime(int);
int main()
{
 int i,j,c1,c2,num;
 bool f1,f2;
 cout<<"Enter the number-";
 cin>>num;
 for(i=2,j=num-2;i<=num/2;i++,j--)
 {
  f1=isprime(i);
  if(f1) 
   { 
    f2=isprime(j);
	if(f2)
    cout<<"Pair:"<<i<<" + "<<j<<endl;	
   }
 }  
 return 0;
}	  
bool isprime(int a)
{
 int i;
 for(i=2;i<=sqrt(a);i++) 
  if(a%i==0)
   return false;
  else 
   continue;	
 return true;
}

And thanks Salem and hiraksarkardg for your concern. :) Is conio.h not ISO C++ standard. Which funtion to be used in place of clrscr() and getch()?

vidit_X 29 Junior Poster

But, i think a doc file is different form a simple text file so this should not work.

vidit_X 29 Junior Poster

Q.Write a program which will print all the pairs of prime numbers whose sum equals the number entered by the user. ( suggested by Aniseed ) (Intermediate)

#include<iostream.h>
#include<conio.h>
#include<math.h>

int prime(int);
void main()
{
 int i,j,c1,c2,num,f1,f2;
 clrscr();
 cout<<"Enter the number-";
 cin>>num;
 for(i=2,j=num-2;i<=num/2;i++,j--)
 {
  f1=prime(i);
  if(f1) 
   { 
    f2=prime(j);
	if(f2)
    cout<<"Pair:"<<i<<" + "<<j<<endl;	
   }
 }  
 getch();
}	  
int prime(int a)
{
 int i;
 for(i=2;i<=sqrt(a);i++) 
  if(a%i==0)
   return 0;
  else 
   continue;	
 return 1;
}

Plz verify it. Any suggestions,comments or recommendations are welcomed.

vidit_X 29 Junior Poster

I noticed that recheck , but i did something wrong in the second loop while correcting that. Therefore to avoid further error, i implemented it this way.
Thanks VernonDozier for optimized code. :)

vidit_X 29 Junior Poster

Does that mean i wrote a good code .. Hurray!! :twisted:

vidit_X 29 Junior Poster

Thanks William :) for your suggestions. I liked them. Can you tell me more about size_t (is it the longest unsigned integer?) And are there any suggestions for shorting the code, bcoz i think i coded it a bit lengthier way.

vidit_X 29 Junior Poster

This is strstr() function made by me as given in the practice problem.

char *cstrstr(char s[],char ss[])
{
 int l1,l2,i,j,k,flag=0;
 char *p=NULL;
 l1=strlen(s);
 l2=strlen(ss);
 for(i=0;i<l1;i++)
 {
  //cout<<"i:"<<i;
  p=&s[i];
  if(ss[0]==s[i])
  {
  for(k=i,j=0;j<l2;j++,k++)
  {
   //cout<<"j:"<<j<<"k:"<<k;
   if(ss[j]==s[k])
   {
    flag=1;
    continue;
   } 
   else
   {
    flag=0;
    break;
   }
  }
  }
 if(flag)
 return p;
 }
 return NULL;
}

Plz verify it. Any suggestions,comment or recommendations are welcomed.
[I'm between a beginner and a pro in c++]