vidit_X 29 Junior Poster

I am implementing RSA in C++ and here's my design(code structure).

keygen.h

namespace rsa{
    class keygen{
        public:
        //Constructor
        keygen(size);
        //Generate keys
        void generate();
        //Getters
        string gete(){ return xyz; }
        .. 
        ..
        ..

        private:
        //initializes bignums
        void initall();
        keygen(){}
        //Private Member variables goes here
    }
}

prime.h

namespace rsa{
    //First 100 primes
    unsigned int primes[]={2,3,5,7,11.....541};

    //MillarRabin Primality
    bool isPrime(mpz_t, unsigned short);
    //Get Random Prime
    void getPrime(mpz_t, mpz_t)
}

endec.h

namespace rsa{
    //Encryption
    string encryption(string text, const string& n, const string& e);
    //Decryption
    string decryption(string cipher, const string& n, const string& d);
}

Is this a good design? How can I make it better? I want to improve the structure or the overall design, that's why dint post any implementation specific code. Things like naming standards, using classes wherever applicable, standard function signature and similar is what I'm looking for.

vidit_X 29 Junior Poster

I have another question. When I initialize x to 0123, x prints to the console as 83. Why is this?

When you prefix a number with 0, its treated as octal. I think now you can figure out why 83 is getting printed :)

vidit_X 29 Junior Poster
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	double posNum = 0, posPow = 0, sum = 0, newVal;
	int counter = 0;
	while ((posNum != (-1)) && (posNum >= 0))
	{
		counter++;
		cout << "Enter a positive number (-1 to exit): ";
		cin >> posNum;
		if (posNum < 0)
		{
			cout << "No data";
		}
		posPow = pow (posNum, 2);
		sum = (sum + posPow);
	}
	newVal = sqrt(sum);
	cout << newVal << endl;
}

You forgot dividing by counter. Here are some other changes to your code..

while ((posNum != (-1)) && (posNum >= 0))
	{
		cout << "Enter a positive number (-1 to exit): ";
		cin >> posNum;
		if (posNum < 0)
		{
			cout << "No data";
                        break;              //break out of loop if he enters -1
		}
      		counter++;                  //Moved it down, so that it doesn't increments if user enters -1
		posPow = pow (posNum, 2);
		sum = (sum + posPow);

	}
vidit_X 29 Junior Poster

Did you check my code? Is it correct?

P.S : It works only for integers

vidit_X 29 Junior Poster

Here's my solution, it doesn't need Pre-order traversal just the inorder traversal.

//Will only work if it is a complete and full binary tree
#include<iostream>
using namespace std;
void printBFS(int arr[], int size);

int main()
{
	int arr[100];
	int size;
	cout<<"Enter number of nodes";
	cin>>size;
	cout<<"Enter inorder traversal";
	for(int i=0;i<size;i++)
		cin>>arr[i];
	cout<<"\nBFS-";
	printBFS(arr,size);
	return 0;
}

void printBFS(int arr[], int size)
{
	int i, k, x=0, j =size+1;

	//Find the depth of tree
	while(j != 1)
	{
		j=j/2;
		x++;
	}

	//Can't explain here :P
	for(j = x-1; j >=0; j--)
	{
		int k, x = 1;

                //pow(2, j);
		for(k=0; k< j; k++)
			x*=2;

		k=1;
		i=x*k;
		while(i < size+1)
		{
			if(arr[i-1] != -1)
				cout<<arr[i-1]<<" ";
			arr[i-1] = -1;
			k++;
			i=x*k;
		}
	}
}
vidit_X 29 Junior Poster

Can you please provide some testcases for testing?

vidit_X 29 Junior Poster

Binary Search continuously divides the array and keep on searching for the target value whereas in your code the function doesn't do that. It just checks whether the target value is at mid and then changes first and last and then terminates. You would like it to continue searching rather than just doing it once.

Hint: Use loop, rather than conditional statement.

vidit_X 29 Junior Poster

On the right track!! The program is reading the file and displaying all the names with index correctly but you need to include string header file.

Now for your question, ask the user to enter a string and then start comparing it from the start of vector. Something like..

int i;
for(i=0; i< (int)names.size(); i++)
{
    if(strcmp(usrInput, names[i]) == 0)
         break;
}
vidit_X 29 Junior Poster

First, you are calling the factorial function before taking the input from user i.e. num is not initialized.

Secondly, the loop inside factorial function is infinite. It is supposed to run from 1 to num, but watch carefully you are multiplying num inside it so it'll keep on increasing and the loop will go on forever. Also, you are returning answer which has never been initialized.

vidit_X 29 Junior Poster

I think the reason that there are no Marco/#Difine stuff is because iostream is a library not a header file so it doesn't contain any real code... But from the talk it must be in code somewhere... As some extra information I thin the word 'cout' means "Charecter(s) Out"

I think cout stands for Console Output.

vidit_X 29 Junior Poster

Sorry, You already used new.. I overlooked your code.

vidit_X 29 Junior Poster

What error did you get when you try to compile?

vidit_X 29 Junior Poster

For counting the number of characters, the better way is as NathanOliver suggested or

int length;
for(length=0;charArray[length]!='\0';length++);

And now about your code, it won't work.. because your are creating a new array at compile time when the value of counter is 0.

Write a function named getString that has a local char array of 80 elements. The function should ask the user to enter a sentence, and store the sentence in the array. Then the function should dynamically allocate a char array just large enough to hold the sentence, plus the null terminator.

You need to allocate memory using new.

vidit_X 29 Junior Poster

This will, with a litte modification. You should break the loop as soon as it finds a null. But remember it will also count spaces.

if (charArray[i] != '\0') {
     counter++;
} else
     break;
vidit_X 29 Junior Poster

A character array is terminated with a null('\0') character and the content after that doesn't matter. It will be some random data.

vidit_X 29 Junior Poster

That is because length is a function, not an array. You cannot have an index for a function. I think you are trying to get the character at that index, for that you can directly use [] with a string.

user[i];

And your logic is flawed, when you'll run the program you'll figure it out.

vidit_X 29 Junior Poster

You can use this instead..

cin.getline(charArray, 80);
vidit_X 29 Junior Poster

What do you mean by removing the necessary elements from an array?

Do you want to output increasing elements in an array and remove the smaller ones that comes after them?

vidit_X 29 Junior Poster

Yes, cout is a variable but of type ostream. To be more precise its an object of class ostream which has << as overloaded operators.

So whenever you use that operator on cout, it performs output to the screen.

vidit_X 29 Junior Poster

Your code compiles fine? Because length is a function and you are trying to use it as an array

user.length[i]
vidit_X 29 Junior Poster

Any code would be greatly appreciated.

vidit_X 29 Junior Poster

Ok, I tried to fix it using the code that vidit x stated but there were too many errors. do I put the code after cin >> years?

How can you use it before taking the number of years from user? It cannot predict.

vidit_X 29 Junior Poster

Problem is not in your code but your logic. The expected output seems to be using compound interest but you implemented simple interest. Try..

float raisep = (raise/100);
for(int i = 0 ; i < (years) ; i++)
{
	cout << (i+1) << " $" << wage << "\n"; 
	raiseMoney = raisep * wage;
	wage = wage + raiseMoney;
}
vidit_X 29 Junior Poster

If you want to display a char[] or pass it to a function just use the identifier, not the index.

And word[50] is a character which doesn't exists. When you declare an array A[50], the index ranges from 0 to 49.

vidit_X 29 Junior Poster

You are passing the wrong second parameter to getline(), it expects a string. You are passing it a char..

getline(file,inf[rows][cols]);
vidit_X 29 Junior Poster

You are allocating new memory every time you call *pVketor() and I think that's the problem.

vidit_X 29 Junior Poster

When you print low to high using loop, you incremented num1 and therefore the second loop never runs.

vidit_X 29 Junior Poster

Sorry, I edited it little late.

getline(cin,str);
vidit_X 29 Junior Poster

Instead of

cin>>str

Use

getline(cin,str);
vidit_X 29 Junior Poster

Flush the input stream after input...

while (cin.get (temp) && temp != '\n');
vidit_X 29 Junior Poster

Yes, this is what I can think. There might be other ways. Post your code after adding the above..

vidit_X 29 Junior Poster

You need not store the question, its already in variables. Just use..

if(op == PLUS)
    cout<<"Question "<<a<<" + "<<b<<"\nAnswer "<<answer;
vidit_X 29 Junior Poster

Yes, that is right. You need to generate another c and d.

Since you need to display the question too if user is wrong. You need to store the operation selected during previous switch. And a little correction in your code..

else
{ 
   cout << "Too bad. You are incorrect!!" << endl;
   cout << "Would you like to see the correct answer (Y or N)?" <<endl;
   cin >> Y;
   if(yes == Y)
     cout << "Question"<< ... <<"\nAnswer "<<answer;
   ...
} //You forgot the blockquotes
vidit_X 29 Junior Poster

You are not updating the mid variable..

vidit_X 29 Junior Poster

The range of multiplication and division operands are different than the addition and subtraction operands.

So you need to generate separate operands for them.

vidit_X 29 Junior Poster

The function main() expects a return value. Other than that your code works fine....

return 0;
vidit_X 29 Junior Poster

You want to find the totalcost for each group?

vidit_X 29 Junior Poster

but wouldn't 'head' be overridden by 'test'?

That's what we want to do..

Head is just a pointer which point to the first element of our list. We make the test->next pointer to contain what's in head and then point head to test.

vidit_X 29 Junior Poster

Read words from a paragraph, compare them with some equation(what type of equation?) and then add it to a string if it matches?

Did I get you right?

vidit_X 29 Junior Poster

Compiler translates your code to native machine code. You cannot have a compiled binary which runs on all the platforms.

True for C and C++. Experts please correct me if I'm wrong.

vidit_X 29 Junior Poster

You need to understand the basics of pointers, before dealing with linked lists.

test->Next=Head;
Head=test;

It adds the newly created test node as the first node in the list. Before adding the list is

Head -> First -> Second -> Third...

After adding, it will be

Head -> Test -> First -> Second -> Third...

vidit_X 29 Junior Poster

You forgot to post the code.

vidit_X 29 Junior Poster

Same question as gerard4143, Which compiler are you using?

vidit_X 29 Junior Poster

Is that what you asking?

char alpha='A';
int x=alpha;
vidit_X 29 Junior Poster

Did you try something? Any codes?

vidit_X 29 Junior Poster

It will quit when you'll enter q, since the condition will be false and the loop won't execute.

while(choice == 'c' || choice == 'f')

And please use code tags when you post code.

vidit_X 29 Junior Poster

Have you defined the function square(int,int,int) somewhere in your code?

vidit_X 29 Junior Poster

Sometimes those Intellisense errors\warnings take a little time to disappear.

vidit_X 29 Junior Poster

haha.. Thanks for sharing :)

I surely can use it somewhere.

vidit_X 29 Junior Poster

You are changing the value of variables(a, b and c) while computing the sum of their digits.

a=a/10;

Then you compare them with the sum.