Do you know what is a char array ?
baby_c commented: nice work +1
Aia commented: For endorsing the use of gets() -2
abhimanipal 91 Master Poster
Do you know what is a char array ?
I am sorry I did not understand what is your problem ?I ran the code that you have posted for 10! and I got the answer. So what is the problem ?
These are the problems that I see with your code
You have not checked for overflow. If you enter as i/p a very large number then the factorial of that number will not be able to fit in the 32 bit (or what ever is the size of int in your system) and so the answer will be negative
You have used the non portable header file conio.h. Also use int main as opposed to just main
Why should system calls be avoided if you know your target environment ?
An easy way would be to execute the ls command in your program...
@Aranarth
Its a C- style approach. When we are program in C, all the variables have to be declared at the start of the function block
WaltP's approach is much much more simpler ....
Print the value of the file descriptor. When I printed the value I got -1.
One reason could be, as the article mentioned that I am unable to open the file due to lack of permissions.
Have you taken care of this issue ?
Did your problem get solved ?
If not where are you stuck now ?
I used this reference for setsockopt
http://msdn.microsoft.com/en-us/library/ms740476%28VS.85%29.aspx
According to this, setsock opt returms 0 for success. Change that part of code for your program.
Did you implement the changes suggested by nbaztec
@nbaztec
Are you actually recommending gets over fgets ?
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 ?
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
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 ?
When you fork for the second time, the parent is not waiting. Also only the parent should wait (See my example above)
Does nbaztec suggestion solve your problem ?
Also instead of using text files, may be that could solve your problem
Dont use getment(). Use malloc instead to allocate memory. Google for the syntax of malloc
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;
}
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
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 ?
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
The only way you can learn these things is by programming. So keep practicing.
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);
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
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
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
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.
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
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 ?
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.
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
NA
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.
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
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;
}
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
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
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 ?
Is this code a function in some program ? Otherwise you really dont need to stract the strings. Just print them in the correct order
Of course. I see my mistake now..... Can never be complacent with pointers.
func(&arr[0][0]);
which is justfunc(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;
}
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]);
}
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
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 ?
}
}
}
In the main function when you call getData, the array m_t is not declared anywhere .Either make this array global or pass a pointer (reference to this array) to the getData function
I guess you best bet would be to use getline
Replace getch() with cin.get()
@choragos ...
Check out the strtok function.... I think that might solve your problem....
You could save the code to a different file and try running it there ....
Is this code all there is or is there more to it