abhimanipal 91 Master Poster

Since you are pretty much using stdin for input you can opt for gets(), Usage:

char st[50];
gets(st);

However fgets has a little advantage since it can limit the number of input characters.

Hope thats clear to you.

OP wanted to input a file name only, gets() should work fine for him
int main()
{
    char str[10];    
    printf("Enter file name\n");
    gets(str);    
    printf("File name is %s\n",str);
    return 0;
}

This is a short stub which accepts the file name from the user. Are you actually saying its fine to use gets here ?

abhimanipal 91 Master Poster

You dont need to go all the way upto n/2. Just check utpo square root of n.
google what are the pros and cons of using goto

abhimanipal 91 Master Poster

Did you implement the changes suggested by nbaztec

abhimanipal 91 Master Poster

@nbaztec

Are you actually recommending gets over fgets ?

abhimanipal 91 Master Poster

In one of my previous posts I told you to google for the syntax of malloc. Did you do that ?Both of your questions will be answered.

I meant where in the main program are you opening and closing the files ?Also is it necessary for you to use processes or can you use threads as well ?

abhimanipal 91 Master Poster

Open file 1
Open file 2
Read a string of n chars from file1 into array1
Read a string of n chars from file 2 into aray2

Compare array1 and array2. You will have to ignore spaces and new lines when you are trying to comapre

abhimanipal 91 Master Poster

What is this 3271. In the current form you are allotting 3271 bytes of memory. Is that what you want ?Also after malloc you have to check to see if the memory has been allotted or not

With regards to the file. Where are you opening and closing the files ?

abhimanipal 91 Master Poster

@aranath

In the code sample that you have given, it is possible that more than 1 enemy may be present in the sample spot. Also you want to use srand to randomize the order in which the random numbers are generated :)

@GamerXXX is it fine if 2 enemies appear at the same time

abhimanipal 91 Master Poster

Logically Incorrect
void main as opposed to int main
Unnecessary use of goto

Aia commented: GOTO jail, don't collect $200. Where's that free jail card when needed? ;) +9
abhimanipal 91 Master Poster

When you fork for the second time, the parent is not waiting. Also only the parent should wait (See my example above)

abhimanipal 91 Master Poster

Does nbaztec suggestion solve your problem ?
Also instead of using text files, may be that could solve your problem

abhimanipal 91 Master Poster

You probably want to say

while(cAns=='y'||cAns=='Y')
{}

Also I think the expansion of while(cAns=='y'||'Y') should be while((cAns=='y')||'Y') which will always be true

abhimanipal 91 Master Poster

Dont use getment(). Use malloc instead to allocate memory. Google for the syntax of malloc

abhimanipal 91 Master Poster

You have to make the parent wait for the child to die before killing itself. A simple solution will be to use wait(NULL) in the parent

Now the parent waits for the child to die before terminating

int main()
{
    int pid;
    pid = fork();
    if (pid)
    {
                printf("I'm the parent %d\n", getpid());
                wait(NULL);
                printf("The child has died\n");
    }
    else
    {
                printf("I am the child\n");
    }
    return 0;
}
abhimanipal 91 Master Poster

Use pointer to pointer for passing the FILE pointer

Also you dont need the if condition on line 92.
After the while loop just use the 2 for loops
for(k=l;k<=mid;k++)
{}
for(k=j;k<=high;k++)
{}

that it. If you cant understand why this is sufficient, work it out on a paper and pencil. Also print the array at the end of the sort function. So you can see how the array is getting sorted. So if there is a bug it will be easier for you.
If you still cant debug, paste the input to the sort function and its final output here

abhimanipal 91 Master Poster

fgets can be used to input integers as well. We convert the string to integer using atoi.

abhimanipal 91 Master Poster

Google for fgets.. Its best if you start using it from an earlier stage as opposed to changing your habit when you have already been using scanf for a while

abhimanipal 91 Master Poster

What is the objective of your program ? Are you supposed to read from a file, do some processing on the string and send the string across ? If yes then I think you are over complicating a simple program ?

abhimanipal 91 Master Poster

What is the objective of your assignment ?

abhimanipal 91 Master Poster

There is a logical error in your code. Check to see what code I have written and you will spot the error.

mult1=1;
for(i=n;i<=m;i++) //loop till the m
{
     mult=1; 
     for(j=1;j<=i;j++)// for factorial
     {
        mult=mult*j;
     }
     mult1=mult1*mult;
}

PS: Avoid using scanf. It has lots of hidden problems. Shift to fgets instead

abhimanipal 91 Master Poster

Search on google. There is a very good chance some one has already encountered the bug before and would have posted the detailed solution
If you cannot find any answer on google then post your question here

abhimanipal 91 Master Poster

The only way you can learn these things is by programming. So keep practicing.

abhimanipal 91 Master Poster

My algorithm is not wrong. I just showed you a simple scenario. If you want to do 8+9 then have a if condition after the summation and handle it there
Also when you return from the function I think you should write

return z;

instead of

return stack(z);
abhimanipal 91 Master Poster

Check out this link
http://forums.macosxhints.com/showthread.php?t=2023

I think this should solve your problem

abhimanipal 91 Master Poster

Switch can only accept integers and chars not strings.
Get rid of the nested loops, you dont need so much complication for such a simple program
You the program flow suggested by nbaztec

abhimanipal 91 Master Poster

I dont agree with the logic of your + operator. I would do something of this sort

while(A.top!=NULL && B.top!=NULL)
{
     sum= (A.top)->data + (B.top)->data;
     Z.push(sum);
}

while(A.top!=NULL)
Z.push((A.top)->data);

while(B.top!=NULL)
Z.push((B.top)->data);

So if the input is 123+ 50, you will have some thing of this sort
Stack 1: 1,2,3
Stack 2: 5,0
Stack 3: (3+0),(2+5),(1)

Now you can figure out how to display the result

abhimanipal 91 Master Poster

What exactly is the problem ? When you say nothing is happening its really not that descriptive ....
Are you able to open the input/output files ?
Are you getting garbage o/p ?
Are you getting compile time/ run time errors ?

The more detail that you give about the error, the faster it will get solved

abhimanipal 91 Master Poster

When you create a file check the file handler to see if it is not null.
Also I dont like the fact that you have forked thrice in the main program. When you call fork() you create an additional process.So after the first call to fork is executed there will be 2 process and each of them will call a fork giving rise to 5 processes. I dont think this was what you had in mind

Probably you should look at threads.

abhimanipal 91 Master Poster

Over three-hundred and a half posts in this forum and still endorsing the use of gets()?
Makes me wonder at one point you are going to recognize that there's no instance for its use. Specially at the learning stage.

I am not endorsing gets(). I was trying to clear the confusion of baby_c so as to why his code behaves the way it does. But I should have included a line at the end telling him to use fgets instead of gets

abhimanipal 91 Master Poster

no problem

abhimanipal 91 Master Poster

What have you got so far ?
Are you a CS student ?And you are trying to get other people to do your programming assignment

abhimanipal 91 Master Poster

The problem is when you code of the sort

int i;
printf("Enter val ");
scanf("%d",&i);

and you input the value 5, you are actually pressing 2 keys 5 and the enter key. The value 5 gets assigned to i but enter key is still present in the stream. Then in the while loop

while(i<4)
    {
              printf("Enter String: ");
              gets(str);
              printf("String is %s",str);
              i++;
    }

The code will print Enter String. The enter('\0') which was already present it the stream will be assigned to str. The code will print String is and then it will again go to the top of the while loop.
When you use fflush(stdin), it removes any char in the stream, so the extra '\0' char which was present in the stream disappears and the program works as expected

I have explained to you why the program works when you use fflush(stdin), but it is a bad idea to use fflush(stdin).Use the link above to see what the problems are with fflush

baby_c commented: nice work +1
Aia commented: For endorsing the use of gets() -2
abhimanipal 91 Master Poster

Can you be more specific of what kind of help do you need ? Are there compile time errors, run time errors or do you need help to design the algorithm ?

abhimanipal 91 Master Poster

Yes I think you are correct. But I advise you to cement your understanding by using some example.
For instance if I have the data 10,20,15, how will the code handle it ?

abhimanipal 91 Master Poster

The data structure of each node will be some thing of this sort

struct node
{
     int data[5];
     struct node* children[5];
     int count;
};

Create the head node first. The when you insert the next node, check how many children does head have. If it has less than 5 children, make the next node the child of the head. If not check how many children does the first child have then check for the 2nd child and so on
This approach is not complete but you can build on this now

abhimanipal 91 Master Poster

Well how about this : Create an array of size n. Then run the algorithm. Each time you find a factor p, increment arr[p] by 1.

Then run a for loop from 1 to n check to see if arr is 0 or not. If it is 0 then ignore it else o/p i^arr

abhimanipal 91 Master Poster

You have not put a '\0' in your wordchar string.
So you get the correct input and then a bunch of garbage. So you want to put a '\0' in the wordchar string after the correct input ends.

abhimanipal 91 Master Poster

So I take it your confusion got cleared ?

abhimanipal 91 Master Poster

In addition to what has been suggested above, I would advise you to run the for loop from 0.....strlen(word) as opposed to 0....20

abhimanipal 91 Master Poster

So you can insert the node in which ever branch you chose to ?
PS: the level of help that you receive here often depends on how much detail you can provide

abhimanipal 91 Master Poster

If you have to insert data into a binary search tree, they get inserted into the tree depending upon their data values . So if my data is 10,11,12, My tree would be some thing of this sort
10
\
11
\
12
\

In such a scenario many of the O(log n) algorithms like search, insert become O(n). To overcome this problem we would like to make 11 the root and 10 and 12 the children. Enter AVL tree. AVL tree is a self balancing binary search tree. After each insertion, the tree calculates the difference between the left tree and the right tree. If the difference is more the 1 , the tree rotates itself. Play around with this applet
http://www.cs.jhu.edu/~goodrich/dsa/trees/avltree.html

abhimanipal 91 Master Poster

NA

abhimanipal 91 Master Poster

Do you know how to create a binary tree ? It is the same principle, but instead of 2 children now you have 5 children.

Also does the problem state any restrictions on how you have to insert the node ? In the sense does the tree have to be balanced or you can insert the node where ever you want ?

abhimanipal 91 Master Poster

Your main function contains a while loop in which you check for which selection is made. But you do not increment the value of i. So if I were to chose option a 5 times, it will enter data in prson[0] only.

abhimanipal 91 Master Poster

Around a week ago..some one posted a very similar question and it was answered (debated) in much detail. Just search for the question in the C forum

abhimanipal 91 Master Poster

I node a similar question in one of the more recent posts. Did your question get solved there ?

abhimanipal 91 Master Poster

The problem is in the winsock library. When you call functions from that library, you get linker error.
Write a simple 2-3 line code calling a function from winsock library to check if your library is installed correctly

abhimanipal 91 Master Poster

Does this help ?

while(true)
{
    //Generate a permutation of the word
    // Compare it to the dictionary
    //if there is a match then break
    //else go for another iteration
}
abhimanipal 91 Master Poster

There are 2 solutions to your problem. First which I believe is the more simpler one. Here you are dealing with a single pointer which points to the beginning of the array

int* test2 (int arr2[][4],int mul){
	for (int i=0;i<2;i++){
		for(int j=0;j<4;j++){
		arr2[i][j]=arr2[i][j]*mul;
	    }
	}
	return &arr2[0][0];//sending back the address location
}

int main(void)
{
    int arr2d[][4]= {{1,2,1,1},{2,2,3,4}};
    int* ptrarr2d=test2(arr2d,3);
    for(int i=0;i<4;i++)
      cout<<*(ptrarr2d +i)<<" ";
    
    cout<<endl;
    cin.get();
    return 0;
}

Here is the second solution. This is more closer to your approach. Here we use a pointer to array. Notice how the function has been declared.

int (*test2 (int arr2[][4],int mul))[4]
{
	for (int i=0;i<2;i++){
		for(int j=0;j<4;j++){
		arr2[i][j]=arr2[i][j]*mul;
	    }
	}
	return arr2;
}


int main(void)
{
    int arr2d[][4]= {{1,2,1,1},{2,2,3,4}};
    int (*ptrarr2d)[4] =test2(arr2d,3);
    
    int* p;
    p= (int*)ptrarr2d;
    
    for(int i=0;i<4;i++)
       printf ("%d ",*(p+i));
       
    printf("\n");
    
 //   for(int i=0;i<4;i++)
 //     cout<<*(ptrarr2d +i)<<" ";
    
    cout<<endl;
    cin.get();
    return 0;
}
abhimanipal 91 Master Poster

I have worked with Neteeza before . I think you will find the information that you are looking for in the manual