csurfer 422 Posting Pro

I may be wrong but what I feel is the pid_t data type within switch() is causing the error with -1. As negativity of the choice element is not correctly defined in some cases its better if you use an integer variable to hold the ForkPid value.

as
int pid;
.
.
pid=fork();
.
.
switch(pid)
{
.
.
}

This will satisfy your needs as the returned value of pid from a child process shall always come within the limits of signed integers.

csurfer 422 Posting Pro

If you just want to extract the index (part after the decimal pointer) you can use this method also (in case your index part is in integer bounds)
If you have scanned it as a floating point number:

1>Convert it into a STRING.
2>Then use standard function strtok() with the delimiter as "." and get the second token.
3>This is your index but in string format.
4>Convert it into an integer number.

If you have taken the input as string itself and want the index to be in string format only then disregard step 1 and 4.

csurfer 422 Posting Pro

18-2-21
was just an example the user can give any input..

"18-2-21" represents the format of input you need to take.You can generalize it as "<years>-<moths>-<days>" separated by just by "-" so that you can use "-" as the delimiter in the strtok( ,<delimiter>) function for your purpose. Other information are already in the earlier post.

csurfer 422 Posting Pro

And ya the ":" beside n indicates you need to assign that value to n
Ex:
It can be read as "Assign the length of array A to variable n" other statements also can be read in the same way...Its an algorithm.

csurfer 422 Posting Pro

The thing you have got here is an algorithm its not the actual code you need to look at it and code it on your own.

Here is a simple algorithm:

for i runs from 1 to ArrayLimit-1
{
    for j runs form 0 to ArrayLimit-i
    {
        compare Array[j] and Array[j+1]
        if Array[j] is greater  // Control Statement
        swap Array[j] and Array[j+1]
     }
}

I hope you can construct your code now.Above is for Sorting array in ascending order. Just by changing the control statement as

if Array[j+1] is greater

you can arrange it in descending order.

csurfer 422 Posting Pro

ok i need a little guide about my program.. How i take my start?
taking the current year,current month,current date,birth year, birth month, birth date input and giving the age in the form of "years-months-days" suppose "18 years-2months-21days"...

You can take "18-2-21" as a string input into string str and then use the
strtok(<str address>,"-") function to tokenise this and get it converted into integer by atoi() function to extract the year month and date values into integer variables.

Then just by using struct tm with clock() and using inbuilt int variables as tm_year,tm_mon,tm_mday you can continue.

csurfer 422 Posting Pro

In my view the usage 'echo helloworld > newfile.txt' within a 'C' program is limiting the redirection operator and as a result it is also being treated as as a simple '>' text character, which on echo prints the output 'helloworld > newfile.txt' to the stdout.

You need to look through this...

csurfer 422 Posting Pro

Firstly...

for(b=0,b<=a,b--)
{
          printf("The Numbers in Descending order:%d\n",b[a]);
}

is wrong.As you can see you are starting b's count from 0 and doing a b-- operation.

Secondly...
You need some sorting mechanism to sort your arrray.The simplest to understand may be selection sort to start with.

csurfer 422 Posting Pro

I just said that loops are the best way to achieve the problem.

And if the person wants to continue in the format she has coded then she can use "goto" as mentioned above.

csurfer 422 Posting Pro

CLK_TCK is limited to turbo c compiler and if I am not worng even to gcc compiler.

I would like to add up a question over here pertaining to this topic.

In the above mentioned codes we are using the clock functions to measure the time taken for a part of the source code we write. Is it possible to calculate the time required "from the call of the main()" till the end.

csurfer 422 Posting Pro

Loops are always better than goto but with goto I think this can be done:

You may also use another variable by the name cnt which would hold the number of times you want to re-run the program in case of a wrong input symbol and alter the code as:

cnt=<number of times you want to run loop>;
----program block----
.
.
.
else // The last else
cnt--;

if(cnt>0)
goto wrong;

----------------------------------------------
And your ' / ' operator will generate an error if num2 is 0 which is not taken care of in the code.

csurfer 422 Posting Pro

Hi

I want to know how we can measure C execution time by seeing the clock cycles generated from the assembly code.

Thanks...

I cannot assure you of this method but it had worked for me.The time.h header file has some variables as CLK_TCK and a data type which you can use to store these values into you can try your purpose by calling clock() function at two instances and the difference in time may serve what you expect.

csurfer 422 Posting Pro

scanf("%[^\n]",str); Do you want to bet I can overflow the 15 characters that str[] can hold?

Wat u said regarding the overflow of string array is perfectly right...but my question is can such overflow of array's at such subtle level cause major damages.? if yes how.? and what to refer to avoid such minute but important details...? I mean to brush up the coding skills...

csurfer 422 Posting Pro

All these cant be done in scanf itself so you can make some changes in ur prog to implement them n then move forward which would give the image as if the input is so....

#include<stdio.h>
#include<ctype.h>
int main(void)
{
char str[15];
scanf("%[^\n]",str);
str[0]=toupper(str[0]);
str[15]='\0';
printf("%s",str);
return 0;
}