abhimanipal 91 Master Poster


p.i = new getValue();

While I am sure your question must have been answered already, I am interested to know what does new getValue() do.

abhimanipal 91 Master Poster

Then to encrypt you use the formula C = pow(M,e)mod n. Why are you using a for loop for this ?
Similarly in the decrypt use the formula M = pow(C,d). You dont need the for loop for that

abhimanipal 91 Master Poster

Got it.. thanks a lot

abhimanipal 91 Master Poster

Hello Everybody,
I have a small doubt regarding how we can call constructors in a C++ class

class test
{
public:
       
      test()
      {
            cout<<"In test\n";
      }
};


int main() 
{
        test t1;
        test t2();    // This call does not give compile time error but does not call the constructor
	cin.get();
}

So my question is what exactly happens when the statement test t2() is executed. Why does this call not invoke the default constructor.

abhimanipal 91 Master Poster
abhimanipal 91 Master Poster

What do you mean by the prototype of error. Can you explain a little bit more, what is that you want.

abhimanipal 91 Master Poster

In the first look there seems to be a problem in the way you create the linked list. According to what I think, you dont have a head pointer. What is the name of the pointer which points to the head of the linked list ?

abhimanipal 91 Master Poster

Works fine for me. This is the output that I got when I ran the code

please enter value x,y for top left10
please enter value x,y for bottom right20
please enter value x,y for point30
point 30, is outside rectangleplease enter value x,y for top left15
please enter value x,y for bottom right23
please enter value x,y for point45
point 45, is outside rectangleplease enter value x,y for top left10
please enter value x,y for bottom right30
please enter value x,y for point15
point 15, is outside rectangleplease enter value x,y for top left

But there is a logical error in the code. point 15 is inside the rectangle not outside it

abhimanipal 91 Master Poster

Maybe this will help you ...

struct test
{
     int a;
     char b;
     float c;
};

void func(test* t1)
{
    cout<<t1->a<<"  "<<t1->b<<"  "<<t1->c<<endl;
}


int main() 
{
    test t1;
    t1.a= 5;
    t1.b= 'A';
    t1.c= 4.5;
    func(&t1);
    cin.get();
}
abhimanipal 91 Master Poster

Looking mathematically, this approach is clearly wrong, cause factorization involves decomposition of number into PRIMES and not into other numbers (such as 9 etc.).
So checking division with non-prime mathematically doesn't make any sense.
http://mathworld.wolfram.com/PrimeFactorization.html

Mathematically also this approach is not wrong. As I explained in the above post 9 will never be a factor of a number.

2. As Banfa noted - this approach also wastes unnecessary CPU cycles. We (and beginners) must know how to make effective algorithms- by reducing CPU cycles.
So clearly my approach is teaching how to achieve this. Which is good.

Your approach has to make an assumption about the max size of the input number, which will vary from system to system. As programmers our code has to be portables as well. Secondly if you assume 32 bit system as Banfa noted you will store 7,26,800 numbers. 4 bytes per interger makes the total space requirement = 4* 726800. Also you need this much storage irrespective of the fact that input is 1 or 60,000.

About your note How to generate primes. There are plenty of algorithms how to find primes. Or you can just download prime list from internet,- i see no problem here.

Most (if not all the algorithms) to generate primes are inefficient. My problem with your approach is I think determining if a number is prime or not is an important part of the exercise so I were the teacher I would not …

abhimanipal 91 Master Poster

Also this branch is totally wrong-

/* invalid prime factor */
    else {
      if(d == 2) d = 3;
      else d += 2;
    }

this code will fail after prime P=7 -> 7+2 = 9, but 9 is not prime number, instead after 7, next prime factor should be 11.
http://en.wikipedia.org/wiki/Prime_number
So this approach is BAD. Instead you must save prime numbers into some array kind of

int primes[N] = {2, 3, 5, 7, 11, 13, 17,/* until N*/};

then in invalid prime factor branch just move to next prime factor in primes array.

I dont think what you are saying is correct. The algorithm used by westsider is correct.
Yes after 7 the code will indeed produce 9 and 9 is not a prime number. But the input number will never be divisible by 9. So if the input is 27, the code will divide it first by 3 to get 9 and then again by 3 to get 3 and then again by 3. No problems here.
Similarly if the input is 34. The code divides the input first by 2 to get 17. Then it will iterate through 3,5,7,9,11,13,15 none of which divides 17. Only 17 divides 17.No problems here as well.
The method given by you is wrong. You want him to save the prime numbers in an array. So if the input number is 100, you want all the prime numbers from 1-100 to be stored in an array. But how …

tux4life commented: Exactly. +8
abhimanipal 91 Master Poster

find return a pointer to the location of the first =.
Then copy the contents from after the = till end of string into a temporary buffer
Return the temporary buffer

abhimanipal 91 Master Poster

Check the parameters that you are passing when you call the function damagecalculate and then check how you have defined the function
PS: Do you actually need to pass so many parameters into a function. You are bound to make a mistake. Cant you roll them up into a struct or something ?

abhimanipal 91 Master Poster

You are welcome

abhimanipal 91 Master Poster

Also dont run your loop till n. Run it till square root of n

abhimanipal 91 Master Poster

If your code is huge then break it into functions
If you have to pass some parameters to the functions, passing them by pointers has an advantage that you can manipulate the value of the string inside the function

Does this answer your question ?If not phrase it differently. As of now its not making any sense at all

mr.confused! commented: not a smart ass! +1
abhimanipal 91 Master Poster

I mean the simulator::run function ....
Do you know the difference between pass by value and pass by reference ?

abhimanipal 91 Master Poster

Are you still getting the error ?
I compiled it on the compiler that I have and it compiled just fine .....
Maybe you want to try a new complier

abhimanipal 91 Master Poster

In the buy function dont declare the array of bids inside the function.Declare it in the run function and pass it to the buy function. Similarly for the sell function.
Now pass these 2 arrays to the match bids function

abhimanipal 91 Master Poster

It is printing because you are printing the values within the function. Your code is similar to some thing of this sort

void func()
{
    int x=5;
    cout<<x; 
}

int main()
{
    func();
}

This code will print the value of 5, but the value of 5 exists only within func. If you want it outside the function as well you do something of this sort

void func(int *x)
{
    *x=5;    
}

int main()
{
    int x; 
    func(&x);
    cout<<x; 
}

Similarly in you code pass the array of structs(objects) as an argument to the Trader::buy and Trader::sell functions.
Then when you want to match the bids fuction, sends these 2 arrays of objects as arguments into the function

where am i supposed to put that?

If you make an array, are you not supposed to put the size of the array in it as well ?
int arr[10];

abhimanipal 91 Master Poster

I think you are over complicating a simple problem. Is this your problem:
You have a file which may or may not exist. If the file exists you have to append content to it. If the file does not exist, create the file and add content to it ?

This is how you want to open the file

FILE* fp;
fp= fopen("filename","ab");
/*
If in addition to appending to the file you also want to read from the file, you open the file like this      
fp= fopen("filename","ab+");
*/
if(fp==NULL)
{
     printf("Error\n");
}
else
{
     //Do what you want and dont forget to close the file after that
     fclose(fp);

}
abhimanipal 91 Master Poster

When you declare an array, you need to give the size of the array

int main()
{
    int arr[];
           
    cin.get();
	return 0;
}

Also how is the array getting its values. In post 1 the code you posted, the buy and sell bids were stored in objects in the functions. They get lost when the functions ends... You have to figure out a way of more permanent storage

abhimanipal 91 Master Poster

Hello, I am having some strings grammar problems.


Also by the way,

data = fopen("C:\\Program Files (x86)\\CodeBlocks\\mydata.dat","rb");
if (data == NULL)

the data isn't getting NULL, even the file doesn't exist!

Try to read the contents of the file. If you are able to read and print the contents then the file does exist. Also what is the data type of the variable data

abhimanipal 91 Master Poster

In line 21 you say

HashTable<Pair<HashedObj,Object> > items;

But HashTable in not C++ key word and nor have you defined it in your program

abhimanipal 91 Master Poster

In the skeleton that you have given the class Dictionary is similar to the class Dict ....But I do not understand why the class Pair is given.

According to me(Corrections are invited) there is no such thing as associative array in C++ . What you have are associative containers. There are 2 main types of associative containers
1. set/ multiset
2. map/ multimap (aka Hashmap )

abhimanipal 91 Master Poster

What exactly do you mean by hashtable of pairs. Based on what I understood from your description, you need a generic implementation of maps. As I see that you have made efforts (also it may be the case that I have misunderstood your problem and the code is completely useless for you) I am giving out more code than I usually give .....
Hopefully this will solve your problem

template<class T1,class T2>
class Dict
{
      map<T1,T2> m1;
public:
      Dict()
      {}
      
      void insert(const T1& key,const T2& value)
      {
         m1[key]=value;      
      }
      
      T2 lookup(const T1&key)
      {
          return m1[key];
      }
      
};
abhimanipal 91 Master Poster

When the value of the variable letter is 65, the alphabet A is displayed. When the value is 90 the variable Z is displayed. At this time the value of counter is 26. The for the values 91-96 there is no display. Then the value of letter becomes 97. Counter gets incremented to 27, we go to the next line and the letter a is displayed

abhimanipal 91 Master Poster

I think the problem is in the main function .... I notice from the code that you have posted in post 1 you are calling search twice... Can you post an updated version of your main function.

abhimanipal 91 Master Poster

You are welcome. Mark the thread as solved

abhimanipal 91 Master Poster

Is that what would cause these errors for me?

You never know until you try

abhimanipal 91 Master Poster

Can you post how you have declared matchedAsk and ask vectors ?
Also towards the ending in the for[line 48] loop, I notice that you are not changing the value of the index y. Is that what you want ?

abhimanipal 91 Master Poster

Is this code a function in some program ? Otherwise you really dont need to stract the strings. Just print them in the correct order

abhimanipal 91 Master Poster

If you can buy a book, buy "Pointers in C" by Yashwant Kanitkar .

abhimanipal 91 Master Poster

Dude why have you used integer(double) arrays instead of integers and doubles ? Also make a display function in your class. It will help you a lot to debug

abhimanipal 91 Master Poster

What help do you want ?

abhimanipal 91 Master Poster

Why are you just passing a void pointer. Why cant you pass the file pointers to the filecopy function ?

abhimanipal 91 Master Poster
//put meshok1 to gumareli_1, meshok2 to gumareli_2 and then plus it 
//using atoi functions ok?
//could you?
//don't forget to type discription plz
//and if you want add me to google friend(zz follow) list "Mikael Arakelyan"
}

No.....
But tell you what here is an example of how to use strtok function http://www.cplusplus.com/reference/clibrary/cstring/strtok/

So if the user can enter strings which just have 2 operands then the code is quite simple. Use strtok and store each operand in each array ...
But if the user can enter expressions which have n operands then its a bit tricky

tux4life commented: Good :) +8
abhimanipal 91 Master Poster

Its above the post in which you pasted code
PS: Why are post numbers removed from the new interface ?/Or is there a way to enable it ?

abhimanipal 91 Master Poster

Sure it is

TaMsgType taMsgOut

typedef struct
{
UINT16 msgId;
UINT16 wordCount;
UINT32 data[MAX_MESSAGE_DATA_SIZE];
} TaMsgTyp;


Joe

In the code that you have posted, you have made the object of the struct before you have define the struct itself. This is a compile time error.

abhimanipal 91 Master Poster

Should you not be using the dot(.) operator instead of the member by pointer (->) operator to access the member data ?
taMsgOut is an object not a pointer to object

abhimanipal 91 Master Poster

This is what I have understood so far.... When the value is 2 you are doing right circular shift and when the value is 3 you are doing left circular shift. You have some input
How do you determine which values come into the result array ?

abhimanipal 91 Master Poster

Adding further to the what UncleLeroy has stated
If you have some code of this sort

char* str= "ABCDEF";
str[1]='A';

You will get a seg fault. I THINK that is because the string "ABCDEF" is stored in the code section of the process and any attempt to modify it results in a run time erorr

abhimanipal 91 Master Poster

What problem are you facing ?

abhimanipal 91 Master Poster

Can you post how you have declared taMsgOut ?

abhimanipal 91 Master Poster

Maybe something of this sort

int i=0;
long int bin=0;

while(i<8)
{
   bin= bin+ rem[i]*(pow(10,i));
   i++; 
}
abhimanipal 91 Master Poster

Of course. I see my mistake now..... Can never be complacent with pointers.

abhimanipal 91 Master Poster

func(&arr[0][0]); which is just func(arr);

I also assumed the same but to my surprise when I wrote this piece of code

void func(int* arr)
{
     printf("%d\n",*arr);
}
      
int main()
{   
    int arr[2][2]={1,2,3,4};
//    func(arr);      This does not compile. What mistake am I making here?
    func(&arr[0][0]);
  
    printf("\n");              
	return 0;
}
abhimanipal 91 Master Poster

If you make the array global you do not need to pass it to any function. All functions have complete access to all global variables. So if you declare the array as global your approach will be something of this sort

int arr[10][10];

void func()
{
// Set/ get data into the array arr
}
int main()
{
  func();
}

But making a variable global is frowned upon in the industry as the variable can be modified from any where. So the other option is to pass the array by pointer (reference)

void func(int* arr)
{
   // The variable arr contains the address of the first element of the array
}

int main()
{
  int arr[10][10];
  func(&arr[0][0]);
}
abhimanipal 91 Master Poster

Yes this was what I was hinting at ....
Yes using the recursive method leads to huge number of function calls( I take back my earlier comment). But if you want to use recursion to solve your problem you have to accept that penalty. It is very rare that recursive solution is more faster than iterative solution

abhimanipal 91 Master Poster

You are on the right track but your implementation can be made more efficient. I have written down some sample code for you. So if you want to find the prime factorization of 12 the call to the function will be

primeFactors(12,v1,2);

void primeFactors(int num,vector<int>& v1,int start)
{
     int i=0;
     
     if(num<=1)
           return ;
     else
     {
            if(num%start==0)
            {
                // We come here when start is a factor of num. So if num is 12 and start is 2, you include 2 in the vector list and call the function again. What will the new arguments be ? 
            } 
            else
            {
              //We come here when start is not a factor of num. So if the num is 13, 2 is not a factor of 13 so we call the function again. What will the arguments be in this case ? 
            }
     }
}