Aia 1,977 Nearly a Posting Maven

>That gives me the following error
Yeap, expr doesn't support floating points
In bash you'll need to use bc, calc, awk, or perl
But ksh-93 supports floating points

result=$(((($obyte2 - $obyte1) / ($snapt2 - $snapt1)) * 1000000))
Aia 1,977 Nearly a Posting Maven

Variables in the shell are strings. Another utility is necessary to make the evaluation.
expr can do it, but has many "gotchas" that you need to be aware of, e.i. spaces, (), *, which need to be stripped of its especial meaning.

result=$(expr  \( \( $obyte2 - $obyte1 \) / \( $snapt2 -  $snapt1 \) \) \* 1000000)
Aia 1,977 Nearly a Posting Maven

Whenever you log-in or open a new terminal window that starts the shell to run, a series of scripts are executed to establish the environment in which it is to run. These scripts contains commands just like if you were to execute them in the command line.

~/.bash_profile and ~/.bashrc are two of theses scripts that will allow a user to set up a personal environment, if they are present. If not present, not big deal, the global settings kick in. Those are found in /etc, which a regular user may or may not have access to view.
/etc/profile is the first script run by the shell.

e.g.
I like to interact with the shell using keystroke like the editor vi behaves and not like emacs, which is the default setting. Therefore I have this line in my ~/.bashrc file

set -o vi
Aia 1,977 Nearly a Posting Maven

depends on the pass if its the first pass then 0, if second then 1, e.c.t...

Nope. That i has the value of "segmentation fault".
It never gets initialized.
If you want to pass it a value you need to do it as an argument. void enter(int *p_arr, int i); Make sure the passed int contains a meaningful value.

Aia 1,977 Nearly a Posting Maven

And the point would be?

To make better highways for air planes.
They should patent the technology, you'll never know when the idea might "sky-rocket".

Aia 1,977 Nearly a Posting Maven

Now imagine a future well within your lifetime, where what was considered the "norm" is now the subject of some witch-hunt. It doesn't matter what, some idiot with an agenda will exploit if for their own ends, and they'll have you by the short and curlies.

I don't have to imagine it. It is here. Nevertheless, you would think common sense would dictate a correction from the part of citizenry.
The truth; they are complacently being driven to slavery on a voluntary bases. Incarcerated in a jail without walls, for a promise of "risk free" society; security and comfort no matter the cost.

Aia 1,977 Nearly a Posting Maven

Maybe the airlines should charge by the pound.

Keep regurgitating what you hear from sources that has an agenda and benefit behind those ideas.
In what way would that improve your experience as a traveler? That would be engineered to make traveling difficult for people that has more mass, punishing them for something that it should not be punished for.

Wouldn't be better for you to demand from air lines to provide seats with more space? Now there's an original idea? Why don't you ask and propagate that kind of ideas? Because you know you'll be dismissed or at best, you would have to pay more for it. Just exactly, what you are proposing that your fellow traveler do, instead of you.
It is common for people to support policies and regulations that they think it would not affect them.

[disclaimer]
I am in the slender side, by nature, not by any merit or effort in my part. My policy is to be conscious that what I don't want for me, I don't want for my neighbor.

Aia 1,977 Nearly a Posting Maven

snipped

Aia 1,977 Nearly a Posting Maven

>My code worked perfectly before this change.
>Does it cause any problems, when function has some unnecessary parameters? Please explain...
"Worked perfectly" it is a matter of perception.
It did what I expected at this time would be a more accurate to say.

There's always cost when passing arguments in a function. Nevertheless, functions makes code more manageable, readable and maintainable. Passing many unnecessary arguments in a function defeat those purposes.

>My code (posted above several times), HAS TO contain pointers... int transp(int m, int n, int mat[MAX_ROW][MAX_COL]) I will give you a couple of pointers (no pun intended)
You see int m, int n, int mat? Those are passed to the function as copies of the actual variables. Which means, after the function has completed its work, those values will disappear, as well as the making of that copies have taken some resources.
Wouldn't be great if instead of copies, it could be just a reference to the actual data? No more use of memory that the necessary, and in case that it is required the variable pointer passed, could be changed and retain a persisted value.

Aia 1,977 Nearly a Posting Maven

[...]
Also, the DNA database of everyone who's ever been questioned by the police. Guilty or not, you're on the database for life.

Every state performs the music according to what its citizen would not mind to hear, until it is too late to say: "I don't like that music".

Proper police, on the streets, talking to people is what gets it done.
Not sitting at desks, data mining everyone's private lives and showing up after the event with some cryptic analysis.

It seems logic, doesn't it? However, logic, reasoning, and common sense do not apply, because the purpose given is not security, nor a betterment of life, but rather to have a system of control established, that it can be manipulated to any particular advantage.
Whether is to tell the public: "Look what we are doing for your benefit", or to "purposely have dominion upon any citizen".

Where most "civil right" organizations "fail" rising awareness, is trying to approach the issue like if it were just a matter of some failure in logic.
e.g.

"There are much better things to do to spend our billions on than snooping on everybody in the country just on the off-chance that they're a criminal."

Or objecting, base in some previously overlooked flaw like:

"That, of course, includes all the spam."

When it should be irrelevant if those flaw of reasoning exist o not.

This is about privacy and liberty, freedom if you will, to …

Aia 1,977 Nearly a Posting Maven

Police say that such methods are necessary to investigate suspects who use cyberspace to carry out crimes. These include paedophiles, internet fraudsters, identity thieves and terrorists.

These days, any state craving control over citizenry thinks that throwing the word "terrorist" or "a variation for undesirable" is justification enough.
The sad part is, many are "nodding their heads in approval". After all, it is for our own good, and "who wants pedophiles, thieves and terrorists in our mist?"
Wake up ignorant!
In what way, creating at least a minimum of protection to safe guard the innocent; like obtaining a warrant, would hinder any investigation? Who's there, beside "self monitoring" to insure that the power is not abuse (if there's such a thing), or that the individual "liberties and privacy" are not trampled under feet, and taken for naught?

Aia 1,977 Nearly a Posting Maven

>This is strange, I've always thought because, if they didn't want you to edit your crontab file on the fly, why do they include the -e option to do just that?

My understanding is that by using crontab -e, it checks for the validity of the job and proper format needed, while editing the file directly you are throwing the file as it is to cron, which undoubtedly could have repercussion in a negative way.

Furthermore by the use of the utility crontab, users can create jobs without the need of having access to root privileges or other users cron jobs.

Aia 1,977 Nearly a Posting Maven

>Why do you think i should rethink whole function? Could you be more specific?
You are passing copies of arguments that don't need to be passed as parameters?
Indexes like i and j don't need to be part of arguments in a function; they can be local declared to the function. That applies to variable temp as well.
In general, if your function has more than four parameters, perhaps you need to rethink what it is been done in it.

Aia 1,977 Nearly a Posting Maven

A few other things you need to learn before you can make that piece of code work properly.

Every time you are about to allocate dynamic memory (like with malloc), you need to keep in mind two things. First, you need to check that the setting apart of that memory was successful.

char *linep = malloc(sizeof(char) * 1000);
if ( linep == NULL) {
     /* if you got here, memory was not successfully allocated */
    /* you need to handle the error in a proper way, but you can't */
    /* count with that memory */
}

And second. You need to free that memory when you are finished, using it.

free(linep);

These two things are a must, every time you make use of dynamic memory.

As a bonus. I can let you know about a third concept that you need to be aware.
If you reassign a pointer from dynamic memory like linep = line; (that would have been the correct way of doing linep = (char *)&line; ), that memory previously allocated is lost for you to use. You don't have a way of getting to it, and it will stay allocated to the program until the program is closed, if the operating system is smart to release it, afterward.
As you can see, if you put that in a loop, you could waste a lot of memory pretty easily.

inputFilePtr = fopen("bubble.txt", "r");

When you want to open a file …

Aia 1,977 Nearly a Posting Maven

>any chance you could explain what I was doing and what strcpy() does differently, just for future reference?

char *linep = malloc(sizeof(char) * 1000);

malloc sets apart the memory you specified. And you keep track of that memory via the pointer linep

But that pointer can be reassigned to point to some other piece of memory. That's what you are forcing it to do here.

linep = (char *)&line;

I said forcing it, because you are casting it.
Like this linep = &line ; would have gave you an error.
What where you pointing to? To the memory that fgets() used to store the last read string. line[1000]; And then, you stored the same location address, in fifty different pointers. You accomplished that with strings[i] = linep; .
When you printf'ed each one of these pointers, all of them yielded the same address, therefore, the last string line read was displayed.

Moral of the story? A pointer is not a string. A string is not a variable.
There's not such a thing as string variable in C, you make a string by joining together many single char variables and terminating it with the '\0'. That's all that keeps the string as a string. A fragile single '\0'.
strcpy() knows how to copy each and every one of this single chars terminating it with '\0', into a piece of memory that you have allocated before hand.

Salem commented: *agrees* +26
Aia 1,977 Nearly a Posting Maven

I want to replace a text in a file eg (old_text) with another text eg (new_text) . new_text is a variable in my shell script. few methods that i tried ..
but this doesnt work
cat $line".sh" sed -e 's/abc/ABC/g' $line".sh"

sed 's/ordprg/new_string/g' $line".sh" > $line".sh"

Could some one can help me on this?

You don't need cat to display the content of `$line".sh"' to sed, in order to substitute some text. sed 's/ordprg/new_string/g' $line".sh" > $line".sh" redirecting to the same file you have opened, is the best way of corrupting your file or at best getting a empty file.
You need to redirect the output to another file and then rename it to your original filename. If you use the GNU sed, the -i option will do that for you automatically behind the scene. sed 's/old_string/new_string/g' "$filename" > temp_file After that rename: mv temp_file "$filename"

Aia 1,977 Nearly a Posting Maven

>I have been afraid of Vim editor, due to NO-knowledge.
Practice will change that. Learn as you go according to your need. Vim has much to offer, but until you start using it, you'll will not know what it is applicable to you in your way of writing.
Thanks for the link.

Aia 1,977 Nearly a Posting Maven

>I tried this and now I have the opposite problem. The last number doesn't print. Suggestions?

start i in the loop at 0 or stop it when is < or equal to NUMBERS

Aia 1,977 Nearly a Posting Maven

>What am I missing? How do I get the first number to display?

Move #define NUMBERS 7 outside of main so the function print can see it.
In print() remove i= *nums++ which is doing harm and producing nothing since you change the value of i inside the loop to start at 1 anyway.

Aia 1,977 Nearly a Posting Maven

>The first if(!pf1) return a value, like... Null?
No, it is not returning a NULL, if is evaluating the return of fopen()

Environment Read_Environment()
{
	char slight[4], spresence[4], stemperature[4], swind[4], shumidity[4];
	FILE *pf1 = fopen("environment.txt","r");
	if(!pf1)
	{
		printf("ERROR: Problem in file opening!\n");
               /* do something  more to ensure that the function Read_Enviroment will continue to exist, */
               /* or if it finished it will return a value */
       	}
	else // Read data
	{
        ....
        }
        /* need to return a structure Environment */
}
Aia 1,977 Nearly a Posting Maven

It's better to be safe than sorry in my opinion

Don't touch that keyboard then. ;)

As a serious note:

char *sub = (char*)malloc( fin - beg + 1 );

casting malloc() is not necessary if the proper header file is included.

Aia 1,977 Nearly a Posting Maven

temp[strlen(temp)-1]='\0'; assumes that there's always a '\n' at the end of line, and that could be not the case.

/* wanted MAX_LEN of data, is there something to read? */
if (fgets(temp, MAX_LEN, input) != NULL) {
    size_t net_len = strlen(temp) -1;

    /* trim the newline/return if it's there */
    if (temp[net_len] == '\n') {
        temp[net_len] = '\0';
    }
} else {
    /* handle the error... */
}
Aia 1,977 Nearly a Posting Maven

Awesome!!! The program works! But what about the first warning?

>Autom.cpp(137) : warning C4715: 'Read_Environment' : not all control paths return a value
I am not familiar with that compiler error warnings, but my understanding is that there's not guarantee that Read_Environment function will return correctly.
Most likely, because Variables Environment is a local structure that could or could not receive proper values. That's my guess.

I can see, as well, you are compiling C code as it were C++.
Autom.cpp is for C++, proper C compiling should be *.c
Find the proper way of giving it the suffix .c in your compiler.

Aia 1,977 Nearly a Posting Maven

All sscanf() need the &

sscanf(slight, "%d", &Environment.light);
Aia 1,977 Nearly a Posting Maven

Could you post your main() test?

Aia 1,977 Nearly a Posting Maven

Perhaps, some better examples from someone more thorough than me.
All about structures.

Aia 1,977 Nearly a Posting Maven

>?
I am afraid that would not even compile.
You are going to have to practice a little more with structures.
My suggestion is to avoid typedef for now.
I don't know how much it is going to help you, but here's some examples.

#include <stdio.h>

/* define structure of type example */
struct example {
	int a;
	int b;
};

/* prototype of structure example */
struct example exampleAtWork(struct example exampleParameter);

/* prototype of pointers to a structure example */
struct example *examplePointer(struct example *parameterPointer);

int main(void)
{
	struct example ecopy;
	struct example *copyPointer; /* a pointer */

       /* using a copy and not pointer */
	ecopy = exampleAtWork(ecopy);
	printf("Display value in ecopy.a: %d\n", ecopy.a);
	printf("Display value in ecopy.b: %d\n", ecopy.b);

       /* using a pointer as a parameter */
	examplePointer(&ecopy);
	printf("Display value in ecopy.a: %d\n", ecopy.a);
	printf("Display value in ecopy.b: %d\n", ecopy.b);

	/* Now using the returned pointer to example ecopy */
	copyPointer = examplePointer(&ecopy);

	/* Display using the copyPointer */
	printf("Display value in copyPointer->a: %d\n", copyPointer->a);
	printf("Display value in copyPointer->b: %d\n", copyPointer->b);

	return 0;
}

/*
 * define function to use the structure example
 * Parameter and return are copies which cost memory and process
 */
struct example exampleAtWork(struct example exampleParameter)
{
	exampleParameter.a = 1;
	exampleParameter.b = 2;
	return exampleParameter;
}
/*
 * the return is a pointer and the paramenter is a pointer
 * the benefit would be that instead of creating a whole copy,
 * it makes just a reference to …
Aia 1,977 Nearly a Posting Maven

>That done, how do i return those values for use in if...else test function, for example, in main()?
To return so many values a structure it is required.

Aia 1,977 Nearly a Posting Maven

I used your test and I got this output:

Show me what's in stemperature >>
<<

So... stemperature is getting an \n?

The previous fgets(spresence, 2, pf1); is reading only part of the line, since 2 is not enough buffer to hold whatever the operating system puts into the file as return. That return is left behind, ready to be picked by the next fgets().
Increase spresence[] and the problem will not manifest as long as the integer will not overflow the buffer.

Aia 1,977 Nearly a Posting Maven

Since you don't check the return of fgets() for success, sscanf() is at the mercy of whatever got assigned into the variable stemperature

As a test, include printf("Show me what's in stemperature >>%s<<\n", stemperature); after fgets(stemperature, 4, pf1); to show you what I mean.
It is always a necessary practice to check the return of functions like fgets() and sscanf() for sucess.

Aia 1,977 Nearly a Posting Maven

[...]I disagree with this:

"The string is a constant, so calling strlen() every time is only going to waste time, because it always returns the same result."

I'm expecting my compiler will do that for me, it seems like a very simple optimization to make, right?

The rest is right on the money, thanks a bunch.

calling strlen() in the for loop will invoke an instance of strlen() every time it loops. As it has been pointed out, an unnecessary waste of CPU cycles.
Call strlen() once outside the for loop and store the result in a size_t variable.

Aia 1,977 Nearly a Posting Maven

Put Menu() inside a loop.

void main()

is a no no. Should always return an int.

Aia 1,977 Nearly a Posting Maven

Hello, I tried it as you said, but its still not working.

Show us the code.

Incidentally, its taking the value of Designation but no name!!!! and added to that, fgets is also putting 2 '/n' after I complete the input... May be that which I input(by pressing enter) + it adds an additional.

Show us the code; the modified code.

Can you suggest me some way around this?

I hope by now, you would understand that your words are pretty much irrelevant without showing us the pertaining portion of the code.
Any suggestion would be like shooting at a can of coke, with eyes closed.

Creator07 commented: Discipline +1
Aia 1,977 Nearly a Posting Maven

The errors come from using the fwrite() function in an incorrect way.
Here's an usage example
Pay attention to its prototype.

Aia 1,977 Nearly a Posting Maven

[...]
i need now a command (e.g. awk or sed) which deletes the whole line containing the name, plus next line. but it must be exact match, deleting "test " must not end up in deleting all lines including "test" like "testt" or "testtt"

i have no clue how to match the regexp for a word, followed by tab

sed '/test/d' < file ....... will delete all lines

sed '/test /d' < file ...... will not find anything

how can i check for the followed tab?

sed needs to be told that test is not a substring but a word: \<word_here\> will do that. Once the pattern is matched, it deletes from that line to the end of the following one.

sed -e '/\<test\>/,/$/d' < filesource > filedestination
Aia 1,977 Nearly a Posting Maven

>But i want to round the square-rooted numbers to about 6 digits e.g. 5.12345. How can this be done?

Take a look at round()

Aia 1,977 Nearly a Posting Maven

que?

I thought the math library was there by default. Like stdio.h.

You can include as many .h files as you want, but if the compiler doesn't link the library defined in the .h file by default, it can't see it.
GCC for example it doesn't. gcc -Wall -pedantic myfilesource.c -lm -o myprogramfile will let the compiler know.

Aia 1,977 Nearly a Posting Maven

You have to link the math library.

Aia 1,977 Nearly a Posting Maven
int n = 444;
printf("%09d", n);

More examples here.

devnar commented: %09d- ofcourse! *whacks self* +1
Aia 1,977 Nearly a Posting Maven

>What exactly does this mean?

typedef struct node { 
	char value;
	bool isroot;
	bool isend;
	struct node* sibling;
	struct node* child; 
} [B]aliasname[/B];

Where aliasname is any identifier you want to substitute struct node for.

Aia 1,977 Nearly a Posting Maven
typedef struct node { 
	char value;
	bool isroot;
	bool isend;
	struct node* sibling;
	struct node* child; 
};

He meant there.

Aia 1,977 Nearly a Posting Maven

>which CODE tags should i use?
Click here.

ddanbe commented: Thanks for the tip it always helps +1
Aia 1,977 Nearly a Posting Maven

Post your main array definition. Your code should work as expected.

Aia 1,977 Nearly a Posting Maven

I don't have any intention of debugging your whole code, but...

lop1 = 0;
while (read(fd, &rdr, 1) == 1)
{
fstrg[lop1] = rdr;
if (rdr == '\n' || rdr == EOF)
break;
}

fstrg[lop1] is always fstrg[0]

Aia 1,977 Nearly a Posting Maven

>I'm getting an unexpected error for my piece of code. It seems like a new line character is added into the string once is it called.

fgets() will read and add the ENTER key as a newline if there's space in the buffer given.
People are fond of using this piece of code to get rid of it:

size_t len = strlen(string) -1;
if (string[len] == '\n')
    string[len] = '\0';

[Edit:] Didn't see your last question.
>Doesn't my 'getInputByfgets' method takes the new line characters off?
Because sizeof(input) is the sizeof a pointer which is not the same that the length of the string given. printf("%d", sizeof(input)); simple test will prove it to you.

Aia 1,977 Nearly a Posting Maven

scanf is the only part i'm supposed to use unfortunately...

I am reluctant to give you a working code because I am afraid you are going to just copy and paste. But I also understand the frustration. scanf() is controlled by the while loop and stops when fails to read or find a new line.
the if inside the while loop controls how much it is keep from input. After while finishes input is clear. Modify it to your heart content.

#include <stdio.h>

int main(void) {
    char str[7] = { '\0' };
    int i = 0;
    char ch;

        /* everything from imput will be read */ 
        while ((scanf("%c", &ch) == 1) && ch != '\n') {
            if (i < 6) { /* keep only six characters from input */
                str[i] = ch;
                ++i;
            }
        }

    printf("%s", str);
    return 0;
}
jared_masc commented: very helpful +1
Aia 1,977 Nearly a Posting Maven

Is scanf() part of the requirement? Can you read input using another function?

Aia 1,977 Nearly a Posting Maven

ok, but how do you take in just 4 letters or spaces despite the user entering more than 4??

Honestly? You stop using scanf().
Use fgets() or create a function where you can control fgetc() or getchar().

Aia 1,977 Nearly a Posting Maven

i gt teh [...]
herez wat i have so far...

I have a hard time as it is understanding proper English; if you start cutting the words off and writing in chat mode I am afraid I won't be of much help. printf("Enter whole sentece (yeah, bring spaces too, I can handle it): No it can not handle it. string[7]; you can store only 6 characters plus the null terminator. scanf ("%[^\n]", string); you tell it to read until finds an new line. What happens when the user input more than six characters string? system("Pause"); it requires the inclusion of the header file stdlib.h

Aia 1,977 Nearly a Posting Maven

%s is a format substitute symbol. You are passing that to printf() and telling it to substitute for whatever string is contained in variable string, stopping where it finds a '\0'.
To access individual characters of that string the %c format specifier is used.
Placing a printf with %c inside a loop and incrementing the index subscript will imitate the same behavior that %s. scanf("%c", &dump); Do you understand what you are doing here?