Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 28 days ago.
Improve this question
I am new to C, and my first homework assignment has us using C to run an equation off of inputs. The problem I'm running into is that the outputs need to be whole numbers. The equation I came up with for the problem works if every output is rounded up to the next whole number. We cannot use math.h functions like round, ceil, or floor. We can only use stdio.h functions.
I have tried making the output variable both an integer and a float, but neither seems to work.
Any ideas?
Your question is not sufficiently clear, and you did not provide any specific input/output examples.
But here's my attempt at what I think you want:
#include <stdio.h>
// May return 0, -1 or +1
int sign(int x)
{
return (x>0)-(x<0);
}
typedef struct
{
int numerator;
int denominator;
char* test_name;
} testcase_t;
int main(void) {
testcase_t tests[] = {
{ 1, 2, "+0.5 Rounded to 1" },
{23, 10, "2.3 Rounded to 2" },
{-1, 2, "-0.5 Rounds to -1" },
{-1, -2, "0.5 (double negative) rounds to +1" }
};
for(int i=0; i<sizeof(tests)/sizeof(*tests); ++i)
{
int numerator = tests[i].numerator;
int denominator = tests[i].denominator;
char* name = tests[i].test_name;
// Calculate integer division, with rounding away from zero.
// eg. -2.6 rounds to -3; +2.6 rounds to +3 (always "away" from zero)
// using only integer operations
int result = (numerator + sign(numerator)*sign(denominator)*denominator/2) / denominator;
printf("Test: %0.3f ==> %d (%s)\n", (float)numerator/denominator, result, name);
}
return 0;
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Given g(x) = cos(log(2.21-x)) and that x = 0.99, estimate the value given by the nested g(g(g(x)... equation as the nesting approaches infinity (you don't have to actually go to infinity, but just set up a way to recurse this nesting n times or so).
I've tried some basic stuff like g(x) = cos(log(2.21-x))
and then creating a for loop defined by g(x) = g(g(x)). None of this works.
In these attempts I've gotten end of void errors and overflow errors. If anyone can figure out if this question is viable in C, even though C doesn't support "nested" functions, I will be amazed and very thankful.
Recursive solution:
// assuming g(double x) is already defined and returning a double value
double get_g_x_to_n(double x, int n) {
if (n > 1)
return g (get_g_x_to_n(x, n - 1);
return g(x);
}
get_g_x_to_n(0.99, 1024);
Iterative solution:
// assuming g(double x) is already defined and returning a double value
double get_g_x_to_n(double x, int n) {
double cur_operand = x;
for (int i = 0; i < n; i = i + 1) {
cur_operand = g(cur_operand);
}
return cur_operand
}
get_g_x_to_n(0.99, 1024);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I want to write (a^n-1) in c programming. I wrote pow(a,n-1) and the output is
wrong. How can I solve this problem? Thanks.
Here is my code:
#include <stdio.h>
#include<math.h>
int main() {
float a, r,n;
float sum = 0;
a = 1.04*pow(a, n-1);
r = 1.02*pow(1.04,-1);
n = 2;
sum = 360000*pow(1.04,n)-50000*(a * (1 - pow((r), n ))) / (1- (r));
printf("\n%.2f", sum);
return 0;
}
The correct output should be 286376 but the program showed 2903773
Every C program executes line by line. So, at the time when compiler came on
a = 1.04*pow(a, n-1);
this line, variable a and n was not assigned with any value, resulted in giving you a garbage value...
So, the problem is, you had not assigned values in variables, and still, you were using them.
You have to first assign values in variables before using them. Otherwise, they will pick any garbage value from memory (Any Random number).
Assign value in a and n and try again.
Edit: As chux's comment suggests, if your program is supposed to give 286376 as output, then value of a should be 1 and value of n should be 2.
So, your correct code would be this:
#include <stdio.h>
#include <math.h>
int main() {
float a=1, r, n=2;
float sum = 0;
a = 1.04*pow(a, n-1) ;
r = 1.02*pow(1.04,-1);
sum = 360000*pow(1.04,n)-50000*(a * (1 - pow((r), n ))) / (1- (r));
printf("\n%.2f", sum);
return 0;
}
pow(a, n-1) translates into exp(log(a) * (n-1)) which isn't precisely the same.. You can try to round the output like this:
round(pow(a, n-1))
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 6 years ago.
Improve this question
I had this written until i realized * is an arithmetic operation.
I want to return a nonnegative integer representation of the binary sequence input. IE. 0x4a returns 74 in decimal
typedef unsigned bit16;
unsigned int bin_to_dec(bit16 x)
{
int dec=0;
int k=0;
int remainder;
while (x!= 0){
remainder = x%10;
dec+=remainder*k;
k=k+2;
x=x/10;
}
:(
How would I go about this conversion if I can't use arithmetic operations other than +/-?
Since + is also an arithmetic operation, it becomes difficult. Depending on the exact rules, using a lookup table might be acceptable: return lookuptable[x];
As + and - are allowed...Instead of multiplying into k*reamainder try looping this way
int n;//consider new int
In the while loop write first line as
n=remainder;
Instead of *
for(i=0;i<k;i++)
remainder+=n;
This would do the multiplication :).
And for x%10, construct a function
int mod(int n)
{
int m;
while(n>0)
{
n=n-10;
m=n;
}
return m;
}
And for x/10 it'd be the same but you must return number of times you subtracted like this :
int mod(int n)
{
int count=0;
while(n>0)
{
count=count+1;
n=n-10;
}
return count;
}
edit : If + and - are also not allowed try making functions for them using binary operators and use them instead of + and - in the above answer!
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
The program is suppose to convert binary number to decimal form. Only using scanf() and printf() library functions. Takes in a char array from user ---no prompt outputs decimal form, function must be used with parameter (char binaryString[]) after conversion result must be printed out in main. Program does not work don't think I'm converting the binary form to decimal form correctly in function binaryToDecimal since i cant use pow() I'm lost
#include <stdio.h>
#include <math.h>
int binaryToDecimal(char binaryString[]) {
int c, j = 1, decimalNumber = 0;
for (binaryString[c = 0]; binaryString[c] > binaryString[33];
binaryString[++c]) {
while (binaryString[c] != 0) {
remainder = binaryString[c] % 10;
decimalNumber = decimalNumber + remainder * j;
j = j * 2;
binaryString[c] = binaryString[c] / 10;
}
}
return decimalNumber;
}
int binaryToDecimalMain() {
int arraysize = 33;
char binaryString[arraysize];
scanf("%32s", binaryString);
printf("%d",binaryToDecimal(binaryString []);
return 0;
}
I not give you the algorithm because it's seems that you are learning how to program and it is important to you to learn to discover how to solve the problems that are given to you.But I can give you some hints:
use binaryString only to compare with '0' or '1'. Don't try to make any operations like '%' on it.
iterate on the binaryString character by character (no while inside for [this is only for this case, there some algorithm that is necessary to do something like this])
your logic to convert is on the right track
Also you should call your main function main.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to write a function that identifies the leading digit of a integer number.I am confused and in lack of knowledge to write a function for this process.Can someone help me?
The simple, and naive, solution is to convert the number to a string, and get the first character of the string and convert it back into a number.
Another solution is to divide by ten in a loop, until the result is zero, while remembering the last digit of the number. When the result is zero, the last digit is the first digit of the whole number.
This can be done easily in two steps.
Change the number to string.
Return the first char of the string.
int num = 12345;
// left most digit:
int digit = abs(num);
while (digit > 9) {
digit /= 10;
}
// right most digit:
int digit = abs(num) % 10;
If you feel more confortable by using arithmetic instead strings and loops, try this approach:
Calculate the integer part of the base 10 logarithm
Divide the source number by 10 power to the integer part of the log
{
int value = abs(978);
int valuelog = (int)log10(value);
int leadingDigit = value / (pow(10,valuelog));
printf("%d", leadingDigit);
}
Example 978:
log10(978)->2.990; int 2
pow(10, 2)->100
978/100->9.78; int 9
#include <stdio.h>
int lead(const int number) {
int divider = 10;
while (number/divider) {
divider *= 10;
}
return number/(divider/10);
}
main()
{
int number = 678;
int leadNumber = lead(number);
printf("%d",leadNumber);
}
you can do that by converting the integer to string and return the first character.
or you can do a simple program as below.
int a;
scanf("%d",&a);
while(a/10>0)
{
a=a/10
}
printf("leading no. of given input is %d\n",a);
int lead_int(const int n) {
return (abs(n) < 10) ? abs(n) : lead_int(n /10);
}
Edited after seeing comments in this post (to include abs()), And made this a one lined function.