vidit_X 29 Junior Poster

Use cin.get() twice..

primenum(x);

cin.get();
cin.get();

return 0;

There are other ways, but this one's the easiest.

vidit_X 29 Junior Poster

Thanks alwaysLearning0, I'll check them out.

vidit_X 29 Junior Poster

Did you restart your server after installing the extension?

vidit_X 29 Junior Poster

Its working fine at my end. After entering the first sentence, it asks me whether I want to continue or not. Can you give a little more detail about your problem?

vidit_X 29 Junior Poster

if the upper limit of your two number is 2 ^ 32 which is 4294967296 and sqrt of this 65536.

so 65536 is 5 digit number, for a 100,000 there are only 9592 prime numbers according to this site http://primes.utm.edu/howmany.shtml

so generate these 9592 prime numbers first and save this.
and what ever range you get, you just need to find if your range can be divided by these 9592 numbers.

Yes, there is an upper limit on both i and j.

Calculating and storing all the 9952 prime numbers, and then using them would surely be faster but would that be considered a good solution?

vidit_X 29 Junior Poster

Thanks Rashakil, a better algorithm is what I need.

The program is being evaluated by an online judge(automated) with a fix number of test cases. I think it should have optimizations on.

A good compiler with optimizations will automatically take the invariants out of loop, am I right?

vidit_X 29 Junior Poster

I need to write a program, which prints out all the prime numbers between two numbers. The first input specifies the number of test cases, followed by the limits. Here is an example..

Input:
2
1 10
3 5

Output:
2
3
5
7

3
5

I wrote the following code, but it exceeds the time limit of 6seconds. How can I optimize it to perform faster?

#include<iostream>
#include<cmath>
using namespace std;

bool prime(long);

long primegen(long, long);

int main()
{
 int n;
 long i,j;
 cin>>n;;
 while(n--!=0)
 {
  cin>>i>>j;
  if((j-i) <= 100000)
	  primegen(i,j);
  cout<<"\n";
 }
 return 0;
}

long primegen(long i, long j)
{
 long min=i, max= j;
 
 while(min<=max)
 {
  if(prime(min))
	printf("%ld \n", min);
  if(min==1 || min % 2 == 0)
	  min++;
  else 
	  min=min+2;
 }
 return 0;
}

bool prime(long num)
{
 double j= (double)num;
 if(num == 1)
	 return false;
 if(num == 2)
	 return true;
 if(num % 2 == 0)
     return false;
 for(int i=3; i<= sqrt(j); i=i+2)
	if(num % i == 0)
		return false;
 return true;
}
vidit_X 29 Junior Poster

Use the return value, initialize avgScore with average score...

avgScore = calAvgScore(...)
vidit_X 29 Junior Poster

I think its because you have ORed "R", which is a non-zero value and thus the statement always evaluates to true.

The same problem with your second if statement. And why you using string when you need to store just a character?

vidit_X 29 Junior Poster

As @Taywin suggested, the error is in your sorting...

Seems like you have mixed Bubble Sort and Selection sort algorithms.

vidit_X 29 Junior Poster

You using string without string header...

#include<string>
vidit_X 29 Junior Poster

Tried G++, its working fine at my end. But when trying to submit the above solution to the website, its giving a runtime error with no other information.

vidit_X 29 Junior Poster

Thanks psudeorandom21 for your solution. But can you please answer some of my questions in first post.

vidit_X 29 Junior Poster

Hehe WaltP, you are right that doesn't makes sense.

But this is the only information I'm getting while trying to submit the solution, not any more.

And the solution I posted above is working on my end. Please refer to the first post for more details.

vidit_X 29 Junior Poster

Someone please give a look to this problem.. http://www.codechef.com/problems/PALIN/

I tried submitting the above solution but its giving a runtime error.

vidit_X 29 Junior Poster

Thanks L7Sqr, its working fine using GCC too.

But as I mentioned, when I'm trying to submit it on a site, its giving a runtime error.
The solution is checked automatically based on the language selected, I tried C++(gcc-4.0.0-8), C++(gcc-4.3.2) and even C99strict.

vidit_X 29 Junior Poster

I wrote a program which finds the next palindrome. First, it takes an input specifying the number of test cases. The code runs fine in Visual C++, but am not able to run it in GCC.

I have never used GCC before so unaware with its functioning. But am expecting a standard code should run in all the standard compilers. I'm submitting the solution on a website. What option should I select, C++(gcc-4.0.0-8) or C++(gcc-4.3.2)?

#include<iostream>
using namespace std;
int palnext(char *);
int main()
{
	 char input[100000];
	 int n;
	 cin>>n;
	 while(n!=0)
	 {
		 cin>>input;
		 palnext(input);
		 n--;
		 cout<<input<<"\n";
	 }
	 cin.get();
	 cin.get();
	 return 0;
}
int palnext(char* a)
{
	int half,temp,j=0,i=0;
	while(a[i]!='\0')
		i++;
	half=i/2;
	int flag=1,flag2=1;
	temp=i-1;
	while(j<half)
	{
		flag=1;
		while(a[j]>a[temp])
		{	
			flag2=0;
			a[temp]++;
		}
		while(a[j]<a[temp])
		{
			if(flag)
			{
				a[temp-1]++;
				flag=0;
			}
			flag2=0;
			a[temp]--;
		}
		j++;
		temp--;
	}
	if(flag2)
	{
		if(i%2==0)
		{
			a[half]++;
			a[half+1]++;
		}
		else
			a[half]++;
	}
	return 0;
}

Is it C99strict? Also, any suggestions regarding code optimization is also welcome.

vidit_X 29 Junior Poster

Thanks AD, thread solved :)

vidit_X 29 Junior Poster

What about char**? How it can be used to pass double dimensional character array like in main(int argc, char **argv)?

vidit_X 29 Junior Poster

Ok, but then how it finds that when using a triple pointer? I mean how it calculates the location of a[2][5].

And in the case of char **, it can find the location because char strings have '\0' at the end?

vidit_X 29 Junior Poster

That's where I'm confused AD, a single dimension array can be passed to a single pointer argument but not a double dimension array to a double pointer argument.

I need to know the pointer arithmetic behind a double dimensional array, why is it treated as an array of pointers, rather than a double pointer as in the case of char ** which points to array of strings i.e a double dimensional character array.

vidit_X 29 Junior Poster

Thanks again. Why we need to use a triple pointer as argument while passing a double dimensional array?
Why the following code doesn't works?

int foo(int **a)
{
 return 0;
}
int main()
{
 int a[10][10];
 foo(a); // ERROR
 return 0;
}
vidit_X 29 Junior Poster

Try to program, we'll help if you get stuck.

vidit_X 29 Junior Poster

Thanks AD. We can use double pointer for two dimensional array while dynamic memory allocation.

But while passing a two dimensional array as parameter to a function accepting a double pointer argument, it doesn't works. Why?

vidit_X 29 Junior Poster

I'm a lot confused with multi-dimensional arrays and pointers. I tried searching, found and read some articles. But there are still some doubts.
We can not use a double pointer(int **) for a two dimensional array, right? Instead we need to have an array of pointers with the length equal to the number of columns.

int (*a)[5]=b[][10];

The confusion is, if we can have an array of strings(or double dimensional character array) with double char pointer(char **), why not int ** can point to double dimensional array?

vidit_X 29 Junior Poster

Unfortunately, that didn't work either.

I understand getline, but may I ask how it is more efficient in the case of words? Wouldn't I still have to parse through each character in search of whitespace to determine a word? Could you give me a quick example, because I don't feel like I am envisioning this the way it's actually supposed to work.

But using getline() would atleast make your line count function easier. And I think that's what griswolf meant ...

vidit_X 29 Junior Poster

Where have you defined the overloaded operator << ?

vidit_X 29 Junior Poster
max = num[counter];
min = num[counter];

You are assigning values to max and min from an un-initialized variable and that is the problem. The reason its working with other lines is after the first iteration, the num array gets intialized with the values.

What you can try is .. get a number from the file in the outer for loop, store it in max and min, leave inner for loop as it is and it should work.

vidit_X 29 Junior Poster

In show(), use a loop to traverse through the list till

info->next!=NULL

and increment info every time to point to the next node. In your code, you are not progressing through the list.

vidit_X 29 Junior Poster

There are some errors. The outer while loop ...

while (!infile.eof()
{
.
.
}

will iterate through all the lines of your file. So the values of max, min and sum after the loop will be from the numbers of last line. You lost all the max, min and sum for the previous lines.

The inner for loop is not correct too. Before the outer while loop, you took a number from infile in num and then without using it, you are taking another number in num from infile at the beginning of for loop. So you are loosing the first number from your calculations.

for (int count = 0; count <=7; count++)
		{	
			infile >> num; //You already have a number from line 1 in num
			min = num; 
			max = num;
		
			while (infile >> num)
			{
				if (num > max) max = num;
				if (num < min) min = num;
			}
		}

Also, your logic for finding max and min is incorrect. The for loop is running 8 times to take 7 numbers from the line. And what is the inner while loop doing?

vidit_X 29 Junior Poster

The problem with the code above is the class GradeBook is getting declared twice. Once in the header file and once in your code. So when you remove the header file "GradeBook.h", the code compiles properly.

And about your query of programing with header files. Dividing programs into files makes your code more manageable and it supports re-usability of code. It doesn't make much difference in small programs like this one but its really useful in big programs.

vidit_X 29 Junior Poster

Is this the complete code? Where are the variable declarations?

vidit_X 29 Junior Poster

The size member of class arrayListType is not getting any value and because of that list is not getting allocated.

vidit_X 29 Junior Poster

Its working at my end ..

Can you specify the error?

And please use CODE tags.

vidit_X 29 Junior Poster

Everytime you call getJudgeData(), only one of the variables score1 or score2 or ...
gets a value, not all and between different calls of the same fucntion, the values are not retained.

Also, the call to calcScore() inside getJudgeData() would never be reached. Hope you getting what I mean.

vidit_X 29 Junior Poster

One of the fix is to declare score1, score2 and so on as global variables and then pass them to calcScore() but using global variables is not advised.

vidit_X 29 Junior Poster

Uncomment the friend function declaration

//friend ostream& operator << <T1,T2>(ostream& os, pair2<T1,T2> const& p);

Then edit the function definition for overloaded operator << outside the class to match the friend function declaration. It should work ..

vidit_X 29 Junior Poster

Hey no need to be sorry. What I meant is look at your function signature in declaration. Line 4.

void calcScore()

and the function signature in definition. Line 44.

void calcScore(double one, double two, double three, double four, double five)

Similarly, for the function getJudgeData(). The function declaration and definition should match.

vidit_X 29 Junior Poster

Seems like you would like to overload << operator for your generic class pair2.
You have not declared the overloaded operator function in your class and then you are defining it outside the class.

Either define it inside class or declare it inside class and then define it outside.

vidit_X 29 Junior Poster

The problem is function declarations, you have declared both functions to take 0 arguments -

void getJudgeData();
void calcScore();

Correct them and it should work. And using system("pause") is not good.

vidit_X 29 Junior Poster

@AD, Csurfer - Same here, when I started writing, your posts were not there :)

vidit_X 29 Junior Poster

You cannot use a void returning function with cout.

Since you are outputting something on screen using displayObject(), why not just call it separately. Something like ...

GradeBook myObject( "You got Owned, g++ won't compile" );
cout << "myObject initialized\n";
myObject.displayObject();
cout<<endl;
vidit_X 29 Junior Poster

The problem is function declaration. You have declared the functions to be accepting a char, instead of a char* though you have defined them properly. Correct the function declarations and it should work.

vidit_X 29 Junior Poster

You need to have a global main too.

::main();

And please use code tags around your code, it helps those who wants to help you :)

vidit_X 29 Junior Poster

In Student.h, function declaration of setStudent takes an integer array or integer pointer as argument.

void setStudent(string idIn, string fNameIn, string lNameIn,int scoreIn[]);

But while calling it on line 39 in your code or line 40 here ...

student[numOfStu].setStudent(id, fName, lName, score[SCORE]);

you are actually passing an integer in place of integer pointer, 4th argument.
You are specifying an index in the array, which passes the integer at that index.
Just pass the array without any index and it should work.

YingKang commented: Thanks vidit_x . The error msg is gone now :) +2
vidit_X 29 Junior Poster

This won't compile because function x is having two overloaded versions with same signature -

void x(char)

It should produce an error "Function Re-Declared" .. something like that.

vidit_X 29 Junior Poster

When the user enters the number wrong 10 times, xxx becomes equal to 0.

And then this code

if (xxx==0) goto son;

causes it to exit. Modify the tekrar function, so that it resets xxx. It asks the user if he would like to play more but doesn't changes xxx variable which causes termination. Or you can manually reset xxx everytime tekrar is called.

vidit_X 29 Junior Poster

Please use code tags.

The line

cin>>next;

takes input from the user for the number of eggs and then adds it to total. The while loop continues taking the number of eggs in each nest till the user enters any negative number.

vidit_X 29 Junior Poster

Where have you declared it globally? Its only local declaration withing main at line 78. For global declaration, declare it outside every function and class. For ex-

warehouse car[2];
void main()
{
.
.
.
}
vidit_X 29 Junior Poster

As per my understanding of your problem, you need to declare car as a global variable and you'll be able to access it from everywhere without redeclaration.

Also, you have not defined "option" variable used on line 16 and 18. On line 16, you are treating as a simple variable whereas on line 18, you are treating it as an array.