csurfer 422 Posting Pro

>I have another problem I'm using two linked lists and I opened two files for each one but when I write list to file It writes both lists in the two files .

You are using c and m as two pointers to mark the two lists right? Thats the reason you are passing them.But why this?

c=head;
m=head;

by this you are making both c and m point to head so whats the use of passing them to the function ???

Well you have opened your files fpta and fptb in mode "w" write mode and so it would delete all the contents of it next time you open it and write fresh so have a look at it.

csurfer 422 Posting Pro

Well two different linked list just means you are holding two different head pointers to those lists what else? You can use the same structure for constructing their nodes unless you want the information to be held in s1 and s2 are different.Post in your code if you still have a problem.

csurfer 422 Posting Pro

Both of you are right.

Copy constructor is a constructor used only in situations where we come across situations two objects A and B of same class type assigned as

A=B;

I wanted to go with example

X a=X(X const& copyFromMe);

example but it ended up this way.(In making sure I don't give him the thing he seeks in a golden platter ;))

@ramthegreatcv : Ya its return value optimization and about the copy constructor I didn't find any other way to make you understand,so used this bizzare approach.

@tux4life :I was trying to explaining op the purposes of using a copy constructor with that,thats it,I know the way I took is hopeless.

csurfer 422 Posting Pro

Well how have you written the records in the file from which you are taking the input ?
Extras :
>Try not using .eof() function its not good to use it like this several times discussed already in this forum.
>And within the read looping you can always include a counter right to know how much you have read.

csurfer 422 Posting Pro

Answers:

1>Assume that there is a class as:

class check{

int a;

public:

check()
{ 
cout<<"Constructor"; 
}

check(int i)
{ 
//copy constructor code 
}

void setvalue()
{ 
//sets value for a 
}
};

//Inside main way 1 normal
check c;
c.setvalue();

//Inside main way 2 copy constructor
check c(5);

Above shown are the two types of initializations for initialising integer a within the class object.
First method is to call the setvalue function as we normally do to set value to that integer.But you can directly initialize the static variables present within the class object as

check c(5);

if you have written code for it properly.

2>Address of bA and aA are same I have answered already why.

3>Value of aInt and bInt cannot be same because you are returning the value of aInt so aInt's value gets assigned to bInt so their values are same but their addresses differ because they are separate variables.

4>I have already explained why the destructor is called at end of main only,its because aA is not destroyed at the end of fun2 its returned and assigned to bA so now after main destructor for this object is called and that is seen by you.

csurfer 422 Posting Pro

You must have messed up something in other parts of the program... go through it step by step as if you were compiling it...k be the compiler for a while and you'll see the mistakes on your own...;)

csurfer 422 Posting Pro

>Why doesn't this happen for primitive data types like int ,char or float ?
I really didn't get your question.But this will give you your answers I suppose. and also this and this.

To be frank I think you don't know the use of copy constructor at all so please read about it first and then ask your doubts if you don't mind.You need to brush up a lot.

csurfer 422 Posting Pro

You cannot swap two nodes of linked list just by holding two pointers you need at least three for totally swapping two nodes so instead why not switch the contents instead of nodes ???

void sortbyID()
{ 

     struct school *pre;
     struct school *move;
     struct school *temp;
    /*And allocate memory for temp here because we need to use it ahead*/

    /*I hope head is declared globally or else you need to pass it to this function*/
     move=head;
     pre=head;
     move=move->next;

    /*How can pre be equal to NULL??? Its before move right???*/
    while (move!=NULL)
    {
          if ((pre->id)>(move->id))/*Why to move if they are equal?*/
         {
            /*Just move the contents*/
            temp->id=pre->id;
            pre->id=move->id;
            move->id=temp->id;
         }
          pre=pre->next;
          move=move->next;
   }


   /*Free temp we don't need it anymore*/
   free(temp);
   getch(); /*You don't really need it*/
}
csurfer 422 Posting Pro

@athlon32
Check your answers before posting.

1>

They are not, i ran it on my computer and i get 2 different addresses

The address should be same because the OP is returning the object b address and assigning it to a.
2>

I don't think so, I removed the function fun() and left only a, and it gave me both destructor/constructor messages...i think the des/con aren't being called on B :?

How can you ever get destructor and constructor after removing fun() object b is getting declared within fun().
3>

Because it should be like this:
class A{
int i;
public:
A(){
cout<<"default constructor\n";
}
~A(){
cout<<"default destrutor\n";
}


A(A& obj);
};

and this:
A::A(A& obj)
{
i=obj.i;
cout<<"copy constructor\n";
};

You don't need to just a const suffices look at my post.

@ramthegreatcv
Answers :

>1. Why only 1 destructor is getting called.
Because even though you think you are dealing with two objects you are actually dealing with one,because when you used return b in fun(),what you are actually doing is returning the address of that object and assigning it to object a by the statement a=fun(); so only one object is under observation.Here destructor is called only for object a before returning from main.

>2. Why address of a and b are same.
Look at answer for 1.

>3. If I use copy …

athlon32 commented: Thanks Man, you showed me I was wrong and I appreciate it :D +1
csurfer 422 Posting Pro

Well I looked at your code and it has a bunch of problems :

Error 1:

If this itself is your final code then you don't need NextDay() and PreviousDay() functions at all.

Error 2:

weekdayT IncrementDay(weekdayT day,int pass){
int p=0;
int counter=0;
p-=pass;
if(pass<0){
if(day>p)
{
return weekdayT(day-p);
}
else
{
counter = day;
for(int i=0;i<p;i++)
{
counter--;
if(counter<1)
{
counter = 7;
}
}
return weekdayT(counter);
}
}
return weekdayT((day%7)+pass);
}

This return should be:

return weekdayT((day+pass)%7);

Error 3:

Your code gives correct result for

cout << "Increment " << IncrementDay(Tuesday,0);

as Increment 0 but for

cout << "Increment " << IncrementDay(Tuesday,-7);

it gives Increment 7 instead of Increment 0 which even though correct is logically wrong.

And ya a general instruction about functions like below if you really use them :

weekdayT NextDay(weekdayT day){
	return weekdayT((day%7)+1);
}

Don't use a function call just for a single statement processing. As you know the processor needs to do a whole lot of stuff before calling a small function like this one too and in cases like this its nothing but waste of processor time.If you can incorporate functions as small as these in macros then it speeds up your program to a good extent.
Though not an error it helps you better your program so …

yun commented: Thanks for helping brother +1
csurfer 422 Posting Pro

Well where are you exactly using template functionalities in this code segment ??? I don't see its usage anywhere in the code given by you.

csurfer 422 Posting Pro

restrictions need to be placed on the computer so it is not invincible...

Concentrate on this part because it is very easy to make computer win everytime or to make it loose everytime,but randomizing the win or loss is difficult.

csurfer 422 Posting Pro

Every language is beautiful in its own way...C++ is modern object oriented language which is the trend now (and ya better one too out of C and C++ ).But C kind of pushes you into a place where you need to struggle a bit but will strengthen your coding skills and flexes your brain a lot,so finally its your choice but ya to learn C++ you don't need C but its better to know C++ before JAVA.

csurfer 422 Posting Pro

>Where does myvalue come from?
Well that was the reason I said its a starter.myValue is a variable in the class you would define which is defined in such a way that it points to next class object,in kind of linked list fashion.Some what similar to your "next" variable. Here's another thread which might help.

csurfer 422 Posting Pro

Hey MosaicFuneral you seem to love spam msgs....thats funny dude !!! :)

csurfer 422 Posting Pro

No what i meant was a note on on section about the newest threat someone came across would help others get aware of it right ?

csurfer 422 Posting Pro

C.S.U.R.F.E.R.: Cybernetic Synthetic Unit Responsible for Fighting and Efficient Repair ;)

csurfer 422 Posting Pro

I guess I wasn't any help then. :(

OP learnt a new concept,thats good,but he/she learnt it too early thats the problem ;)

csurfer 422 Posting Pro

Every thing but this makes sense to me :

return weekdayT((day-1)%7);

Assuming that the other user function which you have written is going to send values from 0 to 6 only for a weekday(if you have written that properly;)) then (day-1)%7 makes absolutely no sense and is redundant after what you have checked in your if statement.Only day-1 is enough as %7 there checks a thing you already know(that the value is between 0 and 6).

csurfer 422 Posting Pro

Well this is what I got to say,

@cbaechle:
Your thought flow is in the right direction. Looking at the code and assuming you are still at the initial stages(that you haven't yet come across complex stuff) ya n=n->link becomes more or less equal to n++ when n is allocated memory statically that is through the array as ++ would simply mean move to the next block which can be assumed to be correct even though it can cause many problems.But ya as in the code if n is allocated memory randomly then there is now way to say n=n->link => n++.

About overloading ++ here is something you can start with (this is just a starter) :) :

const ClassType& ClassType::operator++()
 {
     myValue=myValue->link;
     return *this;
 }

@Tom Gunn :
I really don't think the instructor thought of all the things you mentioned in basic classes on overloading.

csurfer 422 Posting Pro

Well a weird idea but you can even split the number into digits and store in a linked list or an array or something else... flex your brain and you will do great !!!

csurfer 422 Posting Pro

Many Many Happy Returns Of The Day !!! And Welcome....

>Should I learn how to do C++ in windows and then learn.. Linux shell scripting (did I say that right)?
Well if you ask me thats the easy way but I would suggest you to learn and practice according to standards so install the Ubuntu you have got download a standard version of gcc (for c) and g++ (for C++) and go on coding...

csurfer 422 Posting Pro

Hey I have been using Daniweb for quite some time now and I really feel that it should include a section on Latest Attacks say virus attacks,hackers tweak and other things which can harm the computer and putting light on how to overcome those attacks in the best possible way.
As this community involves a lot of "highly qualified"(which I am proud to say) professionals,this section can help a lot of people in protecting themselves from those attacks.What do you guys feel???

csurfer 422 Posting Pro

Hey William Hemsworth what you are saying is absolutely right,but being one of the reputed posters on Daniweb and knowing the intentions of this forum and its posters,a little support here would be great on this matter,because even though giving away codes is not "banned",its unethical to do so,even you know it.

csurfer 422 Posting Pro

Well I'd like to tell two things :

1> I really didn't get how is your algorithm going to test if there is the thirteenth friday in that month or not.
Your program doesn't have any reference for the year and without that there is no way to check the total number of days from the years start and the computations you have done is something totally out of the way.

2>Here I am not correcting your algorithm but shortening it as you had asked.You can device a far better algorithm so try ;)

//calendar
#include<stdio.h>
#define s scanf
#define p printf
int main ()
{
    int dw,dm,m;
    p("plz enter dw, dm, m\n");
    s("%d%d%d",&dw,&dm,&m);
    //for Alldays taken together

    while (!(dm>=1&&dm<=7)) dm=dm-7;
    
    if(dm==dw) p("in this month there is a 13th friday\n");
    else p("in this month there is no 13th friday\n");

    return 0;
}

You are doing nothing but the above code.And to be frank above code does nothing ;). And ya never use void main,use int main get used to the standards.

csurfer 422 Posting Pro

Why do you want to do it ? And what have you done ? ask this to yourself always before asking for help here !!!

csurfer 422 Posting Pro

Well try it... Its quite simple.Your first check is good.If they are not of same length then they can't be anagrams.

ALGORITHM 1:
Fine now if they are then just initiate two loops and match every letter of the first string with every letter of second string and when you find one such character flag them both as checked and move on with your next computations.

At some point of time before the end of first string(that is before you reach '\0' of first string) you come across a mismatch (that you don't get a pair for one of the characters ) then return false straight away.

ALGORITHM 2:

Assuming your string to consist of only one cased of characters (say lower case or upper case) initialize an array say int counter[26] and initialize all values to 0 assume a variable "index_help" which is 65 (for upper cased) and 97(for lower cased).

Traverse through the string1 as:

for i in loop from 0 to string length of str1
count[ string1 - index_help ]++;

After this you'll have exact count of characters in string1.Now through string2 as

for i in loop from 0 to string length of str2
count[ string2 - index_help ]--;

Finally if they are anagrams the count array after the above loops should hold 0's throughout else can return false.

And so on ....... till ALGORITHM n.

Coding is just about creativity.Think and you'll …

csurfer 422 Posting Pro

Well there are a whole lot of symbols which are quite interesting !!! Like the third letter of English alphabet . When used as "c" it is the fastest speed attained ever in the world.( of course only by photons ;) ) and when treated as "C" it becomes jephtahs number of solved threads "100" lol :)

csurfer 422 Posting Pro

And with a lot more bugs to fix :)

Fixing bugs is fun isn't it ??? ;) Look at your rep points you'll know I am true... :)

csurfer 422 Posting Pro

Well have patience,a fruit tree needs years of care before it gives us tasty fruits.Same is with programming.
Well if you are just trying out to create some windowed interface for your game then IDE's like Dev-Cpp,CODE-Blocks etc directly make it for you on their own without much effort.But if you want to learn something then read about WIN-API.
And ya if you are just concerned about graphics for your game and not a windowed interface then you can start off with C-Graphics for learning, where you can do a lot of stuff then switch over to something like Open-GL or something which is completely graphics oriented.

csurfer 422 Posting Pro

I just hope I could key in the same number of characters from keyword in 30 sec.At least that would help me to code in hell lot of code in minutes ;)

csurfer 422 Posting Pro

@AncientDragon : I don't think the OP mentioned the need for reading the integers present in the file on the first hand.The problem he/she had was the reading of the character into buffer.So here I suppose the conversion to integer would be obsolete.

@nmaillet : Ya it is always read as ASCII values only as you said i,e 48 49 and all for 0 1 etc even when you open the file in normal text mode also because the values are stored in that way.But its the way in which we represent is important,when you read 00110000 into a char variable its nothing but character '0'.

csurfer 422 Posting Pro

Well as I see it only thing I would comment on is this :

infile.open("test.bdb", ios::in | ios::binary);

is not necessary because you have declared infile as ifstream pointer.
So this would be sufficient :

infile.open("test.bdb",ios::binary);

And ya apart from this I don't see any problem in the code frankly.But I would think it would be better if you checked the BDB file format and if your compiler has the bindings to read from such a file.

csurfer 422 Posting Pro

Are you asking a question or answering your own question ???

csurfer 422 Posting Pro

Such a thing would never happen in DANI because here on DANIWEB if there are 100s to tell you where you are right there are 1000s to tell you where you are wrong...!!!

Daniweb ROCKS !!!

csurfer 422 Posting Pro

Well here is what you need to do :
1>Make use of an efficient hash function which will return the appropriate position of the node to be inserted in linked list.(This needs some research and work).
2>Make every key pass through the hash function and then you get the appropriate position of the node,now traverse the linked list n-1 times(if returned number is n) and add your node there properly.

You have finished your assignment ... Enjoy :) !!!

csurfer 422 Posting Pro

Mistakes :

1> We are not here to do your work here. If you are in such a hurry to submit it on 18th go do it yourself !!! Next time you say you need it this is our reply GO DO IT YOURSELF !!!,

(Since you have put in some effort...Have this...)

2> You better know what you are trying to do over here :

main()
char student_info, stname, clas;
int rno;
{

3> You are missing out on the double quotes.The file name is a string...

char inputFileName[]=student_info.txt;

It should be

char inputFileName[]="student_info.txt";

4>

inFile.open(inputFileName);

Better open this specifically in input mode mode as

inFile.open(inputFileName , ios::in);

5>

while(!inFile.eof())

Wrong style of coding read this.

6>

inFile>>rno>>stname>>clas;

You cannot just read in like this.Firstly you haven't allocated memory for stname clas rno and other strings to hold the values and you need to work on the delimiter you have used between the fields and other stuff to read the values properly.

Salem commented: Lots of good points +35
csurfer 422 Posting Pro

Well I just didn't mean the format when i said to recheck it.

As I know it void * pointer is used in order to typecast one type of pointer to another type. As a matter of fact dereferencing a void * pointer is also an error. Its just used as a mediator for type casting of one pointer type to another.

And here you are trying to assign memory to a void * pointer and trying to use it as a normal array but with type void elements stored within it which as I know is wrong. So I told you to recheck it !!!
Check out here and here for more information.
Same problem faceed here by some other person too.

csurfer 422 Posting Pro
buffer=malloc(N);

Check the malloc format.How it is to be used.

Read the community rules...Post the topic of your query,not something as help or urgent!!!

csurfer 422 Posting Pro
#include <iostream.h> 

main() 
{ 
int n, k = 5; 
n = (100 % k ? k + 1 : k - 1);
cout << "n = " << n << " k = " << k << endl; 
return 0; 
}

How the value of n become "4" ?
n=4
k=5

Hey your code line 1 says

n = (100 % k ? k + 1 : k - 1);

here the right hand side is a conditional statement which has format
(<condition>) ? (<if result is not 0 execute this>) : (<else execute this>);

So here it checks what is 100 % k,which is 100 % 5 here which is 0 so it needs to execute the second statement, and second statement is k -1 so here 5 -1 that is 4 gets assigned to n.

Hey sorry adatapost !!! Slow loading problem.

csurfer 422 Posting Pro

Thank you so much for giving us an INSANE assignment...but We Are Not In A Mood To Do It ...!!!:angry:

csurfer 422 Posting Pro

Several things are wrong with your code starting with this:

ptr = malloc(numRows*numCols);

Errors :
What are you exactly trying to do here. This initialization is wrong not only for memory allocation of a 2dimensional array but also for 1dimensional array.What you have done is nothing but

ptr = malloc (100);

Which has no meaning !!!

csurfer 422 Posting Pro

Well one of the ways for the first questions as I thought is this.

1> Take an input number n from the user.
2> "x" is an empty variable.
3> You have "y" as a number under concern.

What you need to do is pick the rightmost n bits of y and make a decimal number from it and feed it into x.
So you can do this.
* Assume an integer variable temp=1;
* Run a loop n-1 times and within it do
temp<<1;
temp | 1;
* After this loop you just need to do y & temp.
* The above algorithm will fetch you the right most n bits of y.

So your answer for 2-6 is x = y & temp;

In the same way try to answer 2-7. ;) The above code will make your other bits of "y" as 0. So you take another variable say z = y for your computations so that you ll get the results and also y will remain unchanged.

csurfer 422 Posting Pro

C runs on some rules,you need to learn them before trying out something,and ya you cannot just translate a code to C and expect it to work.

int maxprimes;
int count = 1;
int value = 1;
int primes[maxprimes];
int composite;
int i = 1;
int j;
printf ("How many primes? ");
scanf ("%d", &maxprimes);

This is wrong. You are trying to take the value of maxprimes from the user and trying to allocate that much memory for the array primes. But as you can see compiler would have already allocated memory for that array and at that time

int primes[maxprimes];

doesn't have any meaning,so the segmentation fault.

What you can do is to take the value of maxprimes and then allocate memory for array,but in C

int maxprimes;
int count = 1;
int value = 1;
int composite;
int i = 1;
int j;
printf ("How many primes? ");
scanf ("%d", &maxprimes);
int primes[maxprimes];

something like this is not allowed.Because in C all data variable's should be declared and initialized before the first operational statement(say an assignment or a check.).Its memory also needs to be allocated priorly if you are allocating it statically.

Now coming to the solution : You need to use dynamic allocation of memory Do this :

//Within main function
int maxprimes;
int count = 1;
int value = 1;
int *primes;
int composite;
int i = 1;
int j;

printf ("How many primes? ");
scanf ("%d", &maxprimes);

//your solution …
csurfer 422 Posting Pro

Too many loop holes as I see it :

1>You need to work on your hash function and also your hashing concept. If you are calculating the hash value of the given word and calculating the hash value of all the words in a file and then matching it,don't you think you are making hash work just as efficiently as linear search ??? Hash should have a complexity of O(1) and you have reduced it to O(n).So work on it.

2>I don't see you freeing the space allocated for the test vector,nor can I see memory being allocated for the data members of the structure testvector.

3>Within the while loop why are you using the function free(d) ??? You are directly allocating memory for d as :

unsigned char d[bytes];

You are not allocating it memory from the heap(dynamic memory) so you cannot use free here like this.

4>And I hope you know that you are trying to work in C and not in C++ and even this forum is for C problems.This code of yours is just out of boundaries,initializing variables wherever whenever required.C will never work like this.

csurfer 422 Posting Pro

> oh thanks man but coudlnt i change direct to buffer?
What directly to buffer ? You mean to say directly to str array right ? I wont say you can't . First you need to write it as you were writing earlier and then after it you need to clear all the memory blocks ahead of it.
That is if your squeezing leaves your str array at index 4 then you need to find the total memory blocks allocated for this array in main function(because thats where you are allocating it memory)and clear all the memory blocks allocated for this block ahead. (Which is the right "FORMAL" procedure...).

Shortcut :
After your str array is over written just move the index 1 block ahead and place '\0' character in it. Placing the '\0' character symbolises the end of string and compiler won't print ahead.

But be warned this method is sure to bring one or the other problem because you won't know where exactly to place the '\0' character.

And ya your program has whole lot of limitations :
1> Your program doesn't take every letter in string2 and delete it in string1.Because its not looping its just matching the indexes. If instead of NAM as second string i give MAN then even with these letters in string1 your code wont delete it because its just matching indexes.

In total :
Try to write a better code for your requisite because …

csurfer 422 Posting Pro

Well in that case say you have 6 characters in str array,then assuming that you overwrite first three characters in the exact way in which you want even then you are doing nothing with the left over's.
Well change your code as this :

#include <stdio.h>
void Squeeze2(char *str,char *save,char *required)
{
     int i,x;
     int buffer=0;//used to save stuff
// And ya make an habit of using null character rather than simple 0
     for( i=0 , x=0 ; str[i]!='\0' && save[x]!='\0' ; i++, x++ ) {
          if( str[i]!=save[x] ) {
              required[buffer]=str[i];
              buffer++;
              }
          }
}
int main(void)
{
    char name[]="NAME";
    char req[10];
    Squeeze2(name,"NAM",req);
    puts(req);
    getchar();
    return 0;
}
csurfer 422 Posting Pro

Guys relax.....lets help this person out .You just need to do this !!!

Here is a link for third grade "Addition of Fractions" lessons.Go through this first.

And now for some C++ lessons by the designer and original implementor of C++ Bjarne Stroustrup himself. You are really lucky !!!

Now you are ready.You can yourself write some code. Happy ??? And I even did this for free....!!! ;)

csurfer 422 Posting Pro

Well I really don't get it. Here are few mistakes in your question itself :

1>The class definition should end with a semicolon.You haven't done it.
2>Where are you trying to return the array to ? I mean if you want the array within the function setName then why all this fuss ? All member functions of a class can directly access the member variables even if they are private.
The term private says that those members can't be accessed by other functions,but the class member functions have no restrictions imposed on them,they can easily access any of the member variables.

>Also, can I directly assign a character array to a pointer?
If you are speaking of something like this :

char array[20];
char *p;
p=array;

Then its valid.Not a problem!!!

csurfer 422 Posting Pro

@siddhant3s : Thanks for the wonderful article but I too have read the same :) !!!

I was just explaining the OP that in a condition such as

char name[20];

A compiler would treat "name" as nothing but "&name[0]" and would proceed.

Even though the printf example gives a warning when compiled in gcc 4.3 prints the string,and this in someway is helpful to get to the bottom of the concept.So this was mentioned.

And ya as a proof ;) I have mentioned a para from Same Article Section 16.Pointers down here :

The most frequent use of pointers in C is for walking efficiently along arrays. In fact, in the implementation of an array, the array name represents the address of the zeroth element of the array, so you can't use it on the left side of an expression. (You can't change the address of something by assigning to it.) If we say

char *y;
char x[100];

y is of type pointer to character (although it doesn't yet point anywhere). We can make y point to an element of x by either of

y = &x[0];
y = x;

Since x is the address of x[0] this is legal and consistent.

Hope now its ok with you :) and ya don't worry about the avtar !!! ;)