how to fix my parameter so it gets the right value - c

I am trying to pass a number an found its root
I tried running this code to an online compiler and I get random numbers that I didn't entered with scanf. I tried it on an online compiler that can be found here https://www.onlinegdb.com/online_c_compiler#.
For some reason whatever I put I get 8. I also tried it in DEV-C++ which can found here https://sourceforge.net/projects/orwelldevcpp/ in that complier I always get 0 instead of my input number. There is also printf for confirmation to scanf and seems to me that the value actually becomes my input but when I call the function everything changes any ideas (thanks for ur time)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define g(x,num) 1.0/3.0*(2.0 * x + (num / x)) //f'(x,num)
#define var 1E-10//10^-10
#define f(x,num) x*x - num
double x;
int i;
double num;
void sqrtNR(double num){// **actual bug**
printf ("\nnum: %d\n", num);
x = num;
printf ("x: %d\n", x);
for(i;var < fabs(f(x,num)) ; i++) { **infinite loop**
x = x - ( f(x,num)/g(x,num) );
// printf ("x: %d\n", x);
}
printf ("metrhths %d\n", i);
return x;
}
int main(int argc, char** argv) {
printf("dwse ari9mo\n");
scanf("%lf", & num);
printf("%lf", num);
sqrtNR(num);
//printf("\nsrtNR : %lf", sqrtNR(num));
}
youknow actually get the for at least working cuz now its on infinite loop

Your printf formats are wrong and cause the UB.
Abstracting from the math logic of your function.
void sqrtNR(double num)
{
printf ("\nnum: %f\n", num);
x = num;
printf ("x: %f\n", x);
while(var < fabs(f(x,num))
{
x = x - ( f(x,num)/g(x,num) );
}
return x;
}
you should also put your macro parameters in the parenthesizes

Related

How to make a float number an integer in C?

I want to turn a number like 0.1235 to 1235.
i tried to do it through a loop by multiplying by 10 but i didnt know how to stop the loop.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main (){
double a;
printf("a:");
scanf("%lf", &a);
while (/* condition */)
{
a = a*10;
}
printf("a: %lf",a)
getch ();
return 0;
}
int var = (int)round(0.1235 * 10000);
This is not an easy problem to solve as it may seem at first due to decimal precision and lack of implementation details in your question. Here is an attempt of a solution to your problem, but you might need to adjust it depending on your needs.
#include <stdio.h>
#include <math.h>
#define DELTA 0.00001
int get_number_of_decimal_places(double num)
{
int count = 0;
do {
num = num * 10;
++count;
} while (num - (int)num > DELTA);
return count;
}
int main()
{
double a;
int result = 0;
printf("a:");
scanf("%lf", &a);
int decimal_places = get_number_of_decimal_places(a);
do {
a *= 10;
result += (int)a * pow(10, --decimal_places);
a -= (int)a;
} while (decimal_places != 0);
printf("result: %d", result);
getch();
return 0;
}
For input value 0.12345, the output is:
12345
Keep in mind that this solution treats input values 0.1, 0.0001, 0.010 etc. the same way, so the output would be:
1

No output in C program

This my first program of C programming using recursive functions, it calculates the sum of the first n natural numbers. I am not able to get an output from it, it asks for the number but after that the program doesnt respond, can someone please help me out?
int n();
int main(){
int num;
printf("Enter num:\n");
scanf("%d", &num);
n(num);
printf("The sum of %d is: %f", num, n(num));
return 0;
}
int n(int x){
if (x != 0){
return n(x) + n(x-1);
**strong text** }
else{
return x;
}
}
Firstly, in the recursive function, return n(x) + n(x-1); should have been return x + n(x-1); as in the first case, n(x) will continuously make a called to another n(x), therefore making an infinite loop, or, more formally, return a 0xC00000FD exception, a Stack Overflow exception.
Also, in the last printf() function, %f should have been %d:
#include <stdio.h>
int n(int x)
{
if (x > 0) {return x + n(x-1);} return x;
}
int main()
{
int num;
printf("Enter num: ");
scanf("%d", &num);
printf("The sum of %d is: %d", num, n(num));
return 0;
}
Using %f to print an integer will caused an undefined behavior, because %f is a float format specifier, as noted here.
If you really want to convert it to a float:
#include <stdio.h>
int n(int x)
{
if (x > 0) {return x + n(x-1);} return x;
}
int main()
{
int num;
printf("Enter num: ");
scanf("%d", &num);
printf("The sum of %d is: %f", num, (double) n(num));
return 0;
}
*Note: Ran on Code::Blocks 20.03, Windows 10 64bit.
More info on format specifiers : https://www.tutorialspoint.com/format-specifiers-in-c
It should be return x instead of calling the same function,
you can use type conversion for changing the integer into a float,
int n();
int main(){
int num,x;
printf("Enter num:\n");
scanf("%d", &num);
x=n(num);
printf("The sum of %d is: %f", num, float(x));
return 0;
}
int n(int x){
if (x != 0){
return x + n(x-1);
**strong text** }
else{
return x;
}
}

Whats Happening with the algorithm here, the code is related to a recursion problem in C?

The problem was to add the digits of a given number using recursion, for which I wrote the following code:
#include <stdio.h>
#include <math.h>
int addition(signed int x);
int main() {
signed int num;
printf("enter the number : ");
scanf("%d", &num);
printf("%d", addition(num));
return 0;
}
int addition(signed int x) {
signed int sum;
sum = x % 10;
x = x / 10;
if (log10(x) + 1 == 1) {
sum = sum + x;
} else {
sum = sum + addition(x);
}
return(sum);
}
This code works, but the weird problem with it is that, it is only adding digits of the number which starts with one. I can not understand whats going on and why is this happening. If any one can explain, that would be really helpful.
ps: I am a beginner with C so please tolerate and bear me.
thank you.
When you hit the else condition you will recurse infinitely because you're doing addition(x) with an x value of 0 which does not change the argument any further.
You don't really need log10 [and the test is probably wrong].
Here's a cleaned up and working version:
#include <stdio.h>
#include <math.h>
int addition(int x);
int
main()
{
int num;
printf("enter the number : ");
scanf("%d", &num);
printf("%d\n", addition(num));
return 0;
}
int
addition(int x)
{
int sum;
sum = x % 10;
x /= 10;
if (x != 0)
sum += addition(x);
return sum;
}

Square root not diving properly

I've created a program which takes an integer x input, then loops until x is met while also taking other integer inputs. I then do various calculations, and then find a square root of a certain value. When I divide by square root however I get a 0 when I know I should be getting a different value as the maths doesn't add up.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main(void) {
int multiply1, multiply2, add, squareRoot;
int i;
int n;
int x;
int s;
double divide, test = 0;
scanf("%d", &x);
for (s = 0; s < x; s++) {
scanf("%d %d", &i ,&n);
}
multiply1 = i * i;
multiply2 = n * n;
add = multiply1 + multiply2;
squareRoot = sqrt(add);
printf("%d", i);
test = (i / squareRoot);
printf("Multiplication = %d\n", multiply1);
printf("Multiplication = %d\n", multiply2);
printf("Added together = %d\n", add);
printf("square root = %d\n", squareRoot);
printf("First output = %.3f\n", test);
return 0;
}
You are dividing two integers so the actual division returns the result rounded down. You should instead cast to double and then divide.
test = ((double)i/squareRoot);
There are two things you can do,
Without changing your program, simply cast the i and squareRoot variables to double
test = (double) i / (double) squareRoot;
Change your program and make i and squareRoot a double.
I, would choose 2 because sqrt() returns a double and that might cause an integer overflow.

Extra values when printing an array(converting from %s to %c)

I am trying to create a simple program where the user will have to enter a series of numbers and the program should output the square and the cube of the given number. However, when I try to use an array, it prints some random numbers I didn't even input. Any help would be appreciated to eliminate the unecessary input. Thank you.
#include <stdio.h>
int main()
{
char *value;
value = malloc(sizeof(20));
float answer;
int x;
int y;
scanf("%s" , value);
for(x=0; x < 20; x++)
{
y = value[x] - '0';
printf("\nThe square of %d is: %d" , y , y*y);
printf("\nThe cube of %d is: %d \n" , y , y*y*y);
}
return 0;
}
You are taking input in char and doing arithmetic operations on it.
Use this code, it will give you correct output.
#include <stdio.h>
int main()
{
int *value;
value = (int *)malloc(20 * sizeof(int));
//float answer;
int x;
int y;
for(x=0; x < 20; x++)
{
scanf("%d" , value + i);
}
for(x=0; x < 20; x++)
{
y = value[x];
printf("\nThe square of %d is: %d" , y , y*y);
printf("\nThe cube of %d is: %d \n" , y , y*y*y);
}
return 0;
}
The problem is with your malloc statement.
sizeof is used to determine the parameter size - in your case a hard-coded integer. The generated array is of size 4, which is exactly sizeof(20) instead of 20 integers which is 20*sizeof(int). It will be best to allocate the array statically if you know what size you need, see code below:
#include <stdio.h>
int main()
{
// This line sets value to an array of 20 ints
int value[20];
// Another, less favorable option, but still works:
// char *value = malloc(20 * sizeof(int))
float answer;
int x;
int y;
scanf("%s" , value);
for(x=0; x < 20; x++)
{
y = value[x] - '0';
printf("\nThe square of %d is: %d" , y , y*y);
printf("\nThe cube of %d is: %d \n" , y , y*y*y);
}
return 0;
}
The expression sizeof(20) returns the size of an int (the literal 20 is an int), which is typically only 4 bytes. In other words, you are only allocating a single integer for your array. All access outside of that single integer will result in undefined behavior.
You need to allocate sizeof(int) times the number of elements, if you want to dynamically allocate the memory. Or (which I recommend) use a normal array:
int value[20];
There is also another problem, in that you only read a single value from the user. You should probably be reading in the loop too.
But if you read in the loop, then there is really no need to even have an array to begin with, only a single int Variable which you read into, and then print its value as squared and cubed.
So the code could be simplified as
#include <stdio.h>
int main(void)
{
int value;
for (unsigned i = 0; i < 20 && scanf("%d", &value) == 1; ++i)
{
printf("The square of %d is: %d\n", value, value * value);
printf("The cube of %d is: %d\n", value, value * value * value);
}
return 0;
}
You also need to be careful of overflows when multiplying.

Resources