I have three c programs double.c to double the integer, root.c to take square root of integer and square to take square of a integer
./root 9
output:3
./square 9
81
./double 3
ouput:6
I want to chain these operation like pattern will be of left to right
./square root square double 8
pattern = double(square(root(square(8))))
or it can be somthing like
./root square 8
pattern = square(root(8))
What i tried is
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
double operate(char * operation, double operand) {
if (strcmp(operation, "square") == 0) {
return pow(operand, 2);
} else if (strcmp(operation, "root") == 0) {
return sqrt(operand);
} else if (strcmp(operation, "double") == 0) {
return 2 * operand;
}
return 0;
}
int main(int argc, char *argv[]) {
if (argc < 3) {
printf("Usage: %s <operand> <operation1> <operation2> ...\n", argv[0]);
return 1;
}
double operand = atof(argv[1]);
for (int i = 2; i < argc; i++) {
operand = operate(argv[i], operand);
}
printf("Result: %.2f\n", operand);
return 0;
}
Which work something like
gcc main.c -o main
./main 8 square root double
output = Works fine
But it is somehow different way to do it and there were some constraints before to implement this before
Following were constraints
• If your implementation results in parent-child relationship between
processes, then parent process must wait for its child process to exit
System calls and library functions
You must only use the below mentioned APIs to implement this question
- fork
- exec* family
- str* family
- ato* family
- printf, sprintf
- round
But i am unable to grasp this how it is related to process and how it will be done Please guide me some right direction so i can do complete this.
I want to chain these operation like pattern will be of left to right
./square root square double 8
pattern = double(square(root(square(8))))
Presumably, such a chain can start with any of the three operations, so each of your programs needs to know how to do such chaining. Also, as you seem to recognize, the code you present doesn't meet the requirements because it uses a separate command to run the chain.
Let's consider the constraints for a moment. The requirement (both practical and explicit) that parent processes must wait for their children, combined with omission from the allowed list of any function that would allow parent processes to do so, implies that fork() is a red herring. You must not fork() because you are not permitted to wait.
Also, neither pow() nor sqrt() is on your list of allowed functions. x * x is a better method of squaring than pow() anyway, but if you want to avoid sqrt() then you'll need to implement a numerical method to compute square roots. For example, you might perform a binary search for the root, or apply Newton's method.
As for the chaining, I see two main implementation approaches available:
All the programs are the same program, which identifies the first operation in the sequence based on the name by which it is launched (argv[0]). It reads the numeric argument, then just applies each operation in turn, starting at argument 0. It prints the result when there are no more operations to perform.
OR
The three programs are each specific to a single operation. When run, they pick the numeric operand from their last argument and perform their particular operation on it. Then,
if there are no other arguments (argc == 2), they print the result;
otherwise, they format the result as a string, and use, say, execve() (without forking) to run the tail of the chain of operations on the intermediate result.
Details are left as the exercise they are meant to be.
You can do it like this, instead:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
double operate(char * operation, double operand) {
if (strcmp(operation, "square") == 0) {
return operand * operand;
} else if (strcmp(operation, "root") == 0) {
return sqrt(operand);
} else if (strcmp(operation, "double") == 0) {
return 2 * operand;
}
return 0;
}
int main(int argc, char *argv[]) {
if (argc < 3) {
printf("Usage: %s <operand> <operation1> <operation2> ...\n", argv[0]);
return 1;
}
double operand = atof(argv[argc - 1]);
for (int i = argc - 2; i > 1; i--) {
operand = operate(argv[i], operand);
}
printf("Result: %.2f\n", operand);
return 0;
}
Like said in the comments, you need to start from the last arguments. So you start from argc - 2, which will be the 2nd last.
Also, you don't seem to be allowed to use pow, so just do operand * operand, instead. sqrt isn't in your list either, but I would guess that's a mistake. If not, you could use some approximation.
The simplest way to calculate the square root without the sqrt function would be to use Newton's method to find the positive root of the function
#define EPSILON 0.001
double simple_abs(double x)
{
return x > 0 ? x : -x;
}
double simple_sqrt(double x)
{
double previous = 0;
double guess = x;
while (simple_abs(guess - previous) > EPSILON)
{
previous = guess;
guess = previous - (previous * previous - x) / (2 * previous);
}
return guess;
}
This is not the most optimal solution, but is the one I find the most simple.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Lets say for example we have a function that does some math (for the sake of argument, multiply two numbers). It returns the result but if there is an error, the function returns -1.
However, -1 can actually a valid result (-1 * 1 for example). What is a better constant to return? I was thinking of returning INT_MAX on error but some of my math functions return double and float so I wanted to see if there was a better alternative.
This is for a library so I want to use the same constant for errors to minimize confusion.
The usual solution is to use the return value only to indicate success or error, and return the actual result via a pointer:
int multiply(int a, int b, int *result_out)
{
if (/* success... */) {
*result_out = a * b;
return 0;
} else {
return -1;
}
}
Floats and doubles can be NaN
NaN = Not a number.
You may want to read this: How to use nan and inf in C?
https://en.wikipedia.org/wiki/NaN
Set errno
Not all functions return floating point values, so NaN can't always be used.
Since some math functions can return any number of their type, you can't really use the return value to indicate that an error has occurred.
You could still unset and set errno. It does have the side effect that old values of errno will be overwritten.
In example.h:
#include <errno.h>
/* extern int errno; */
double division(double n, double d);
In example.c:
#include "example.h"
double division(double n, double d)
{
if (0 == d)
{
errno = EDOM;
return 0.0; /* Does not matter. */
}
else
return n/d;
}
In main.c:
#include <stdio.h>
#include "example.h"
int main(int argc, char *argv[])
{
division(1.0, 0.0);
if (EDOM == errno)
{
fprintf(stderr, "Couldn't divide 1.0 by 0.0\n");
errno = 0; /* Reset so it won't appear that the error has
occurred even when it hasn't. */
}
division(3.14, 2.78);
if (EDOM == errno)
{
fprintf(stderr, "Couldn't divide 3.14 by 2.78.\n");
errno = 0;
}
return 0;
}
Set an error flag
Or you could use a global variable of your own that you don't unset if no error has occured.
This would allow you to make a whole bunch of calls to these functions and only check for error once.
In example.h:
int error_flag = 0; /* Set to non-zero value on error. */
double division(double n, double d);
In example.c:
#include "example.h"
double division(double n, double d)
{
if (0 == d)
{
error_flag = 1;
return 0.0;
} else
return n/d;
}
In main.c:
#include <stdio.h>
#include "example.h"
int main(int argc, char *argv[])
{
double x;
error_flag = 0; /* External variable */
x = division(division(3.14, 1.3 - division(3.9, -3.0)), 7);
if (error_flag)
{
/* The last call to division() didn't unset error_flag. */
fprintf(stderr, "Something went wrong.\n");
return 1;
}
/* Not reached. */
printf("%f\n", x);
return 0;
}
Math domain errors can be avoided
Sometimes.
Or don't do anything to handle math errors.
If you try to open a file, it's difficult to predict the outcome, because knowing just the filename isn't enough; you'll have to check whether or not the file even exists and if it does exist you'll have to check the permissions.
Math (as I know it) isn't that difficult, you only need to know the arguments. Consider `f(x) = 1/x`: you only need to know the value of `x` to determine if the call to `f` will fail or not, and this well known function is well known to be defined for `x != 0`.
One liner: `double division(double n, double d) {return n/d;}`
(Proved myself wrong about that. Ex: f(a, b) = 1/(a+b))
If you're looking for the least likely integer constant to hit by accident, use INT_MIN. It's one larger in magnitude than INT_MAX. It also has the advantage of being copyable to float without losing its value, INT_MAX will get rounded to a different value.
Of course this is only if you need compatibility with C. In C++ you really should be using exceptions.
The standard library used a variable called errno for explicitly this purpose. You could implement something similar.
For example, in your source file:
int matherr;
enum {
SUCCESS,
ZERO_DIVIDE,
TOO_LARGE,
...
};
int multiply(int a, int b)
{
matherr = SUCCESS;
if (/* result too large*/) {
matherr = TOO_LARGE;
return 0;
} else {
return a*b;
}
}
int divide(int a, int b)
{
matherr = SUCCESS;
if (/* result too large*/) {
matherr = ZERO_DIVIDE;
return 0;
} else {
return a/b;
}
}
In your header file:
extern int matherr;
int multiply(int a, int b);
int divide(int a, int b);
In the code that calls these functions, it would then need to check the value of matherr if either function returns 0.
There is no value that will be suitable for int, float and double altogether, and diverting the function value isn't such a good idea.
But what about not checking for error condtions or even causing them on purpose ? I mean, if a product is subject to overflow, let it overflow; or if the argument of the function is out of range in such a way that you cannot even start the computation, call sqrt(-1) to simulate an out-of-range error.
Why ?
Because your function will adopt the same behavior as the ordinary expressions, and will be no better/no worse. At the same time you won't have to invent new mechanisms, the user won't have to learn them, and you will remain highly portable and in line with the programming language.
I am writing a program that finds if either -l for find largest or -s for find smallest appears when the user inputs the string into the command line. For example ./a.out -l 2 4 6 would find the largest of the users input. In my program I am unsure if I should use atoi or if I can get by without. Also am I able to use the a pointer for the first part of my program and user the actual array in the second half to find the smallest or largest number? Since the ./a.out and -l is stored in array[0] and array [1] can I skip those and still find the largest and smallest numbers? I have written the code to find the largest and smallest number in an array but I am unsure if it is compatible. Any help would be appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]){
int x,i;
for(; *argv != '\0'; argv++){ //checks to see if -l is in string
if (strcmp(*argv,"-l") == 0){
x = 1; //easier to use x value than nested fors and ifs
break;
}
if(strcmp(*argv,"-s") == 0){ //checks if -s is in string
x = 2;
break;
}
else{ //if neither
printf("Error invalid option");
break;
}
}
if(x == 1){ //is going to find the largest element
for(i=2;i<argc;++i){
if(argv[2]<argv[i])
argv[2]=argv[i];
}
printf("Largest element is %d", argv[2]);
}
if( x == 2){ //find smallest element
for(i=2;i<argc;++i){
if(argv[2]>argv[i])
argv[2]=argv[i];
}
printf("Smallest element is %d", argv[2]);
}
return 0;
}
This is not a good idea.
char **argv is a pointer to an array of strings.
When you run your program with command line arguments, argc is initialised to the number of arguments passed, while argv will point to an array of string pointers pointing to the strings, somewhat like this:
The first argument is always the name of your executable, and the last element of argv is always grounded (set to NULL).
For more information, see here.
Using arithmetic operations on the elements of argv will actually carry out arithmetic on the pointers pointing to those strings, leading to behaviour you most likely don't want.
You must either fall back on using atoi() or write your own function to do so.
Your set of arguments is:
argv[0] "program_name"
argv[1] "-l" (or "-s") [program options]
argv[2] "<first_number>"
…
argv[argc-1] "<last_number>"
NULL
They are all read by the program as "C-strings", which are '\0' terminated character arrays in fact. So, char *argv[] is an a pointer to an array of character arrays. For more information please see this SO post and this SO post. There is no point in running a for loop (which is erratic in your case, btw) in order to detect which argument is the program option if you know it is the first, and the rest of your code shows you know.
If you are sure that the arguments to follow are numbers and you want to compare their arithmetic values, you first have to interpret them as numbers. atoi() is the function which returns the converted integral number string as int value. You also need the result values initialized to minimal or maximal value for the given data type. I don't see much use of incrementing pointers (*++argv) and a while loop in counting through arguments of main(). Using array subscriptions and a for loop are much easier to read. Here is a quick, though not entirely fool-proof fix of your code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
int main(int argc, char *argv[])
{
int x,i,m,n = 0;
if (strcmp(argv[1],"-l") == 0)
x = 1;
else if (strcmp(argv[1],"-s") == 0)
x = 2;
else{
printf("Error: invalid option");
return(1);
}
if(x == 1){ //is going to find the largest element
m = INT_MIN;
for(i=2;i<argc;++i){
if(atoi(argv[i]) >= m){
m = atoi(argv[i]);
n = i;
}
}
printf("Largest is the element #%d, equaling %d\n", n-1, m);
}
if( x == 2){ //find smallest element
m = INT_MAX;
for(i=2;i<argc;++i){
if(atoi(argv[i]) <= m){
m = atoi(argv[i]);
n = i;
}
}
printf("Smallest is the element #%d, equaling %d\n", n-1, m);
}
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am working on a personal project where one part of it deals with counting squares and cubes under a certain bound (in this case 10,000). So, I wrote a simple C program I thought would work to verify my results. Here is the little program I put together to see all of the cubes:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main() {
double i;
int cubes = 0;
for (i = 1; i < 10000; i++) {
if ( i == cbrt(i) * cbrt(i) * cbrt(i) ) {
printf("%f --- %f\n",i, cbrt(i));
cubes++;
}
}
printf("%i\n", cubes);
return 0;
}
I got the (incorrect) output : 24. If you want to look at this see the problem look at numbers 15 and 20 on the output. Why I am getting the wrong answer (the correct answer is 21) is an entirely different matter. My question arose when I was messing around with my code to try and fix this and I temporarily changed it to this:
int main() {
double i;
int cubes = 0;
for (i = 1; i < 10000; i++) {
double temp = (cbrt(i) * cbrt(i) * cbrt(i));
if ( i == temp ) {
printf("%f -> %f\n", i, temp);
cubes++;
}
}
printf("%i\n", cubes);
return 0;
}
Now, the program is printing every number between 1 and 9999. So, am I missing something ridiculously easy or what is going on? All I did was instead of having cbrt(i)*cbrt(i)*cbrt(i) in the if conditional I set a double variable equal to result and placed that in the conditional. Why is my program doing this?
I am not sure why this got down voted. I feel like this is a legitimate question. Sorry S.O. community...
double cbrt(double x) returns the closest representable cubic root of x.
The inexactness of the result, then cubed, may not exactly equal 'x' again.
Why 2nd program differs:
C is not obliged to perform double math only to double precision. It may use wider (long double). Depending on many things, the 2nd code appears to have done more in long double than the first. With the extra precision, its easy to see that the results, rounded to double appear exact.
C11dr §5.2.4.2.2 9 Except for assignment and cast (which remove all extra range and precision), the values yielded by operators with floating operands and values subject to the usual arithmetic conversions and of floating constants are evaluated to a format whose range and precision may be greater than required by the type.
Why a typical program run (of either code) produces a result of about 3333.
Consider the double numbers from 2 to 4 and 8 to 64. double numbers are logarithmically distributed. There are as many different double from 2 to 4 as 8 to 16 as 16 to 32 as 32 to 64.
So now all 3 sets from 8 to 64 have a cube root of some answer in the 1 set of 2 to 4. Now if we cube the numbers 2 to 4, we get answers in the range 8 to 64. 1 set of numbers mapping into 3 sets. The round trip is not exact. See Pigeonhole principle. IOW: On average, 3 numbers in the range 8 to 64 have the same cubic root. Then the cube of that root will be 1 of the 3 original.
To find the count of the perfect integer cubes 0 to N
unsigned Perfect_Cube_Count(unsigned n) {
if (n == 0)
return 1;
unsigned i;
// overflow not possible
for (i = 0; i*i < n/i; i++);
return i;
}
Or
// valid for 0 <= x <= something_well_over_1e9
double Perfect_Cube_Count_d(double x) {
double y = cbrt(x);
return floor(y) + 1;
}
You probably want, as Andrew guessed, whole-number cube roots. Float math is quite tricky because of rounding errors. Generally you cannot rely on equality but must compare with an error margin.
To solve your problem though I'd construct the 21 cubes beforehand and then iterate over integers, comparing against the pre-constructed cubes. Or is that cheating? ;-)
In Samuel Becket's novel Watt there is a chapter about a Scottish "Math genius" who could in his head compute all integer third roots of integer cubes up to 10000 or so, too!
My uess, is your compiler does an optimization in the second case, eli inating cbrt calls. It just says the result of cbrt is strictly defined by the standard, so it might as well be always thte case that (i == temp)
You can twak this by some command line arguments, and force it to do exactly what is written in the code. As I recall, this should thhe default thing to do for C compilers regarding float arthimetic, but your compiler may think it is smarter than you or something.
EDIT
And yes, this code has nothing to do with finding perfect cubes...
EDIT
Totally not an answer to the question, but as a quick exercise, this I wrote this:
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
static unsigned long count_cubes(unsigned long max_n)
{
unsigned long n = 1;
while (n*n*n <= max_n) {
++n;
}
return n-1;
}
int main(int argc, char **argv)
{
unsigned long max_n;
char *p;
if (argc < 2) {
return EXIT_FAILURE;
}
max_n = strtoul(argv[1], &p, 10);
if (max_n < 1 || max_n == ULONG_MAX) {
return EXIT_FAILURE;
}
printf("%lu\n", count_cubes(max_n));
return EXIT_SUCCESS;
}
Note: no need for floating point arithmetic
EDIT
Sorry, I really got into this...
This one can be a bit faster:
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <math.h>
static unsigned long count_cubes(unsigned long max_n)
{
unsigned long n;
if (max_n < 256) {
n = 1;
}
else {
n = cbrtl(max_n) - 1;
}
while (n*n*n <= max_n) {
++n;
}
return n-1;
}
int main(int argc, char **argv)
{
unsigned long max_n;
char *p;
if (argc < 2) {
return EXIT_FAILURE;
}
max_n = strtoul(argv[1], &p, 10);
if (max_n < 1 || max_n == ULONG_MAX) {
return EXIT_FAILURE;
}
printf("%lu\n", count_cubes(max_n));
return EXIT_SUCCESS;
}
EDIT ( last time, I promise... )
To show an explanation of my little loop above, starting at cbrt(max_n)-1, I tried the one suggested by #chux , here are some results with slightly larger numbers:
PerfectCubes(18446724184312856125) == 2642246
which is fine but also
PerfectCubes(18446724184312856125-10) == 2642246
which is totally not fine, since 18446724184312856125 == 2642245^3 , meaning there are 2642245 perfect cubes <= 18446724184312856125-10 .
This also results from inaccuracies in floating point representation. You can try it for yourself, if your computer is somewhat similar to mine:
printf("%f\n", cbrt( 2642245UL * 2642245UL * 2642245UL));
/* prints 2642245.000000 */
printf("%f\n", cbrt( 2642245UL * 2642245UL * 2642245UL - 10UL));
/* prints 2642245.000000 */
These two numbers clearly don't have the same cubic root, yet cbrt returns the same results. In this case, floor doesn't help either. Anyways, one always needs to be very careful using floating point arithmetics. And now I really should go to sleep.
I was trying to write a function that would compute the sum of the digits of a number using recursion, but the output is incorrect. Here's the code:
/*Write a function to calculate sum of digits of a number using recursion*/
/*Author:Udit Gupta Date:10/08/2011*/
#include<stdio.h>
int sum (int);
int main () {
int n,s;
printf ("Enter the number:");
scanf ("%d",&n);
s = sum (n);
printf ("The sum of the digits of the number is %d",s);
}
int sum (int a) {
int f;
if (a == 0) {
return f;
}
f = (a% 10) + sum (a/10);
}
Here are some of the output values:
udit#udit-Dabba ~/Desktop/letusc/ch5/J $ ./a2.out
Enter the number:123
The sum of the digits of the number is 7
udit#udit-Dabba ~/Desktop/letusc/ch5/J $ ./a2.out
Enter the number:1234
The sum of the digits of the number is 2919930
udit#udit-Dabba ~/Desktop/letusc/ch5/J $ ./a2.out
Enter the number:123456
The sum of the digits of the number is 4620297
udit#udit-Dabba ~/Desktop/letusc/ch5/J $ ./a2.out
Enter the number:12345
The sum of the digits of the number is 15 /*Only this one seems correct*/
Can someone help me figure out why this isn't working correctly?
Let's look at this recursive function in more detail:
int sum (int a) {
int f;
if (a == 0)
return f;
f = (a% 10) + sum (a/10);
}
While you're on the right track and you have the right idea in general, your actual implementation is a bit buggy. For starters, let's look at these lines:
if (a == 0)
return f;
You have the right idea to terminate the recursion when a reaches zero, but the way you're doing it is a bit off. In particular, you're returning the value of the integer f, but you've never initialized it. This means that the return value is completely arbitrary. Instead of writing this, I think that you probably meant to write something closer to
if (a == 0)
return 0;
which correctly says "if the number is zero, the sum of its digits is zero."
Similarly, take a look at the last line of your function:
f = (a% 10) + sum (a/10);
Again, your intuition is spot-on: the sum of the digits of a number are given by the sum of its first digit and the sum of the rest of its digits. However, notice that while you're correctly computing the sum of the digits, you aren't correctly returning the sum of the digits. In fact, you don't return anything at all if you execute this code, so the return value from the function is unspecified, hence the garbage output. To fix this, consider rewriting the code like this:
return (a % 10) + sum (a / 10);
This actually says to hand back the value that you just generated right here, instead of storing it in a local variable that will be immediately cleaned up as soon as the function returns.
I believe that the reason you coded this function this way is that you're under the impression that the value of int f; is carried across the function calls. Unfortunately, it is not. When writing a recursive function, each instance of the function is completely independent of each other instance and local variables accessible in one recursive call are not accessible in other recursive calls. Consequently, even though each recursive call has its own variable int f, those variables are all completely independent of one another. The value isn't carried through them. If you want to communicate values across recursive functions, the best way to do it is by using the return value of the recursive calls, or (if you must) by passing a pointer to some value down through the recursion.
Hope this helps!
When a is 0, you are returning an uninitialized value (f was not initialized).
Change it to:
if (a == 0)
return 0;
You also forgot the return in the end of the function:
return (a% 10) + sum (a/10);
It is highly recommended that you always compile with the flag -Wall, which would warn you about those mistakes.
Your recursive function will calculate nothing it either returns an uninitialized int or nothing. You need to be returning the work you are doing in the function.
int sum (int a) {
if (a == 0) {
return 0;
}
return (a% 10) + sum(a/10);
}
return a == 0 ? 0 : ((a% 10) + sum (a/10));
You only return f is it is 0, but not if it isn't, which makes your return value undefined. I assume you want to do:
int sum (int a) {
int f;
if (a == 0)
return 0;
f = (a % 10) + sum (a / 10);
return f;
}
I have written a source code to print random numbers within a specified limit.But it is also returning some negative numbers,is this normal?If not how do I rectify it?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main( int argc, char* argv[])
{
int fd, n;
fd = open("/dev/urandom", O_RDONLY);
if(fd == -1)
printf("ERROR: Cannot Open %s\n",argv[1]);
read(fd, &n, sizeof(n)); //n=random number
printf("%d\n",1+n%6); //limiting n
/* 1+n%6 should give me random numbers only between
1-6(correct me if I'm wrong),
but somehow it even gives negative numbers*/
close(fd);
}
In case the random number you read is negative (which is certainly possible), the modulus of it can also be negative. You should use an unsigned integer in order to make sure the result is in the range you want.
More information can be found here.
1 + n % 6 does not magically constrain the result to between 0-6. Run this to see.
#include <stdio.h>
int main(int argc, char* argv[]) {
printf("%d\n", 1 + (-23) % 6);
return 0;
}
Wow, even I thought that the modulo operator when applied like
c=a%b
restricts c between integers [0,b-1].
However as K&R writes (pg 39, 2nd ed):
The expression x % yproduces the
remainder when x is divided by y, and
thus is zero when y divides x exactly.
So in effect what happens is:
c = sign(a) * ( abs(a)%abs(b) )
(where sign(a)=-1 for a<0 and +1 for a>=0)
It would be great if somebody could point out an actual reference to this behaviour in a book or C standard. This is what I figured out after experimentation with GCC v4.4.1.
Thanks for the great question. You kind of cleared my understanding of C modulus operator.