For some reason my code will consistently print out zeros.
I was supposed to make a code in which I enter three numbers,
The first number
The ratio
The amount of numbers to be displayed
The code should display those numbers.
Here is the code:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
void forloop(int firstNum,int ratio,int repeats);
int main(void)
{
int firstNum = 0;
int ratio = 0;
int repeats = 0;
printf("First number of the series: ");
scanf("%d", &firstNum);
printf("the ratio of the series: ");
scanf("%d", &ratio);
printf("the amount of numbers to display is ");
scanf("%d", &repeats);
forloop(firstNum,ratio,repeats);
}
void forloop(int firstNum,int ratio,int repeats)
{
int i = 0;
for (i=1; i!=repeats+1;i++)
{
printf("%d ", firstNum*(pow( ratio, i)));
}
}
This should fix the issue with printing zeros:
void forloop(int firstNum,int ratio,int repeats)
{
int i = 0;
for (i=1; i!=repeats+1;i++)
{
printf("%.0f ", firstNum*(pow((double)ratio,(double)i)));
}
}
As mentioned by #JonathanLeffler in the comments, you have a bug. The line
printf("%d ", firstNum*(pow( ratio, i)));
is wrong because pow returns a double but in the printf specifications %d expects an integer.
So, you can either change the specifier in your printf to a floating point one, i.e., %f, %g, %e, %F, %G, %E et cetera or cast pow's output to int by doing (int)pow(ratio, i).
In the first case, as #JadMrad said, if you are printing a double and you don't want to have decimal numbers, you can specify the number of digits at the right of the decimal point using "%.Nf" instead of "%f" where N is the number of decimal points that you want.
Nevertheless, in your case it seems to me that you are expecting always integer numbers. Then,
printf("%d ", firstNum*((int)pow( ratio, i)));
sounds like a better solution to me.
Related
When I change the type of array from float to double (float array[][] to double array[][]), it doesn't scan the values correctly. All the values become zero. For example, if I enter 5 for input when it's float, it's 5.000000. However, when it's double, every value I enter is scanned as 0.0000000.
#include <stdio.h>
#include <math.h>
int main() {
int limestone, min=0;
//for entering height of array (number of limetstone)
printf("Enter number of limestone: ");
scanf("%d", &limestone);
float array[limestone][3];
//double array[limestone][3]; (the problem)
//for getting inputs
for(int i=0;i<limestone;i++)
{
printf("Enter the %d porosity, hydraulic conductivity (m/s), specific gravity: ", i+1);
scanf("%f %f %f", &array[i][0], &array[i][1], &array[i][2] );
}
//for print array (you can remove it)
for(int i=0;i<limestone;i++)
{
for(int j=0;j<3;j++)
{
printf("%f ",array[i][j]);
}
printf("\n");
}
//Comparing 3rd (Last) Column
for(int i=limestone-1;i>=0;i--)
{
if(array[i][2]<array[min][2])
{
min=i;
}
}
printf("The limestone with the lowest specific gravity is Limestone %d with a specific gravity of %f",min+1,array[min][2]);
return 0;
}
I got your code to work by changing your scanf line to
scanf("%lf %lf %lf", &array[i][0], &array[i][1], &array[i][2] );
All I did was change the %f's to %lfs.
This works for me at least, I think it's doing what you want
Why didn't this code work after adding "res=pow(arr[i],x)"
I want it to print like this
"printf("%d * %d = %d \n",i+1,x,pow(arr[i],x));"
The code doesn't work untill i print like this
"printf("%d * %d = %d \n",i+1,x,res)));"
#include<stdio.h>
#include<math.h>
int main()
{
int arr[5];
for(int i=0 ; i<5 ; i++){
printf("enter the numbers %d\n",i+1);
scanf("%d",&arr[i]);
}
int x;
printf("what is power would you like...\n");
scanf(" %d",&x);
printf("The power of the array elements is...\n");
for(int i=0 ; i<5 ; i++){
printf("%d * %d = %d \n",i+1,x,pow(arr[i],x));
}
return 0; // 1*2=1*1 , 3*2=3*3
}
pow returns a double. You need %lf format for the third argument or you'll get undefined behaviour trying to format a floating point value with an integer %d format (most compilers issue a warning about this BTW).
quickfix
printf("%d * %d = %lf \n",i+1,x,pow(arr[i],x));
Assigning the result to an integer workarounds the issue. That's why it works then (but sometimes it leads to rounding errors so beware!)
You may have a look at integer power algorithms instead. pow is more suited for floating point operations.
I'm new to C language and I could really use help with this question:
The abundancy of an integer is defined as the perfect divisors of a number (factors not including the number itself) divided by the number itself. For example, the abundance of 8 is (1+2+4)/8 = 7/8. Write a C function which takes a single integer as input and returns the abundance of that number.
This is as far as I got. I can compile it but I keep getting the incorrect answer. Please help and thanks in advance
#include <stdio.h>
int main()
{
int number, i, sum, abundancy;
sum==0;
abundancy==0;
printf("Enter an integer:");
scanf("%d", &number);
for(i=1; i<=number; i++)
{
if (i!=number)
{
if (number%i == 0)
{
sum+=i;
{
abundancy=sum/number;
printf("The abundancy is %d", abundancy);
}
}
}
}
return 0;
}
This is as far as I got on an online compiler
This is what I get when I compile it
You are doing abundancy=sum/number; inside the loop which is going to fetch you wrong results. Here is the correct version of your code with the changes:
#include <stdio.h>
int main()
{
int number, i, sum, abundancy;
sum=0; //USE ASSIGNMENT NOT EQUAL TO OPERATOR
abundancy=0; //USE ASSIGNMENT NOT EQUAL TO OPERATOR
printf("Enter an integer:");
scanf("%d", &number);
for(i=1; i<number; i++) //CHANGED
{
if (number%i == 0) //ONLY SINGLE IF NEEDED
{
sum+=i;
}
}
abundancy = sum/number;
printf("Abundancy is: %d/%d\n",sum,number); //ADDED
printf("abundancy is: %d\n",abundancy);
return 0;
}
Even then abundancy will contain floor value of sum/number - you should use float instead and %f as format specifier while printing.
INPUT:
12
OUTPUT:
abundancy is: 16/12
abundancy is: 1
I am trying to take user input, store it in an array for the number, and then take the dollars and cents portion of the numbers, store those in separate arrays, and then print the formatted number.
This is my code:
#include <stdio.h>
int main()
{
int i;
float amounts[5];
printf("Enter numbers as dollar amounts: \n");
for(i=0; i<5; i++) {
printf("Value %d: ", i+1);
scanf("%f", &amounts[i]);
}
long dollars[5];
long cents[5];
for (i=0; i<5; i++) {
dollars[i]=(int)amounts[i];
cents[i]=(amounts[i]-dollars[i]);
cents[i]*=100;
}
for (i=0; i<5; i++) {
printf("\n$%ld.", dollars[i]);
printf("%ld", cents[i]);
}
return 0;
}
I am somewhat unsure as to how I could take the cents and store them in an array as a long type. When executing the program, I get the correct dollars but the cents portion always comes out as 0. Any suggestions?
Edit:
So I attempted to solve some of the issues by doing this:
#include <stdio.h>
int main()
{
int i, j;
float amounts[5];
printf("Enter numbers as dollar amounts: \n");
for(i=0; i<5; i++) {
printf("Value %d: ", i+1);
scanf("%f", &amounts[i]);
}
long dollars[5];
long cents[5];
double decimal[5];
for (i=0; i<5; i++) {
dollars[i]=trunc(amounts[i]);
}
for (i=0; i<5; i++) {
printf("\n$%ld.", dollars[i]);
decimal[i]=100*(amounts[i]-dollars[i]);
cents[i]=(int)decimal[i];
if(cents[i]<10)
printf("0%ld", cents[i]);
else
printf("%ld", cents[i]);
}
return 0;
}
However, now I'm getting everything that is less than 10 for cents[i] to output 1 less than what it should be. So 3.06 prints as $3.05. The same for 3.12 for some reason.
Also, to some of the critique about what variable types I'm using, this is for an assignment and it dictates what types are to be used.
4 Write a program that will read five values from the keyboard (use a loop) and store them in an array of type float with the name amounts. Create two arrays of five elements of type long with the names dollars and cents. Store the whole number part of each value in the amounts array in the corresponding element of dollars and the fractional part of the amount as a two-digit integer in cents (e.g., 2.75 in amounts[1] would result in 2 being stored in dollars[1] and 75 being stored in cents[1]). Output the values from the two arrays of type long as monetary amounts (e.g., $2.75)
You're losing the fractional part here:
cents[i]=(amounts[i]-dollars[i]);
cents[i]*=100;
The difference between the two is less than 1, so assigning that value to an int results in it getting truncated to 0.
You need to multiply the difference before assigning to avoid truncation:
cents[i]=100*(amounts[i]-dollars[i]);
for (i=0; i<5; i++) {
dollars[i]=(int)amounts[i];
cents[i]=(amounts[i]-dollars[i]);
cents[i]*=100;
}
instead use:
for (i=0; i<5; i++) {
dollars[i]=(int)amounts[i];
double decimal = 100*(amounts[i]-dollars[i]);
cents[i]= (int)decimal;
}
OP's approach has problems
printf("\n$%ld.", dollars[i]);
printf("%ld", cents[i]);
Negative numbers amount[i] == -1.23 come out like "$-1.-23"
Values like amount[i] == 9.998 print as "$9.99" and likely should be "$10.00"
Missing leading zeros amount[i] == 1.03 print as "$1.3" #Jonathan Leffler
dollars[i]=(int)amounts[i] fails when amounts[i] is outside int range.
A key issue for OP
// problem
dollars[i]=(int)amounts[i];
cents[i]=(amounts[i]-dollars[i]); // cents[i] is an integer, cannot hold a fraction
cents[i]*=100;
// Alternative
// round
amount[i] = round(amount[i] * 100)/100.0;
// take apart;
float ipart;
cents[i] = (int) roundf(modff(amounts[i], &ipart)*100.0f);
dollars[i] = (int) ipart;
If code is to use FP for money recommend:
Use double.
Round to the nearest monetary unit after any calculation that is not exact. In OP's case this is 1/100.
amount[i] = round(amount[i] * 100)/100.0;
Use a unified print
printf("\n$%0.2f", amount[i]);
#include <stdio.h>
#include <math.h>
int main()
{
int i, j;
float amounts[5];
printf("Enter numbers as dollar amounts: \n");
for(i=0; i<5; i++) {
printf("Value %d: ", i+1);
scanf("%f", &amounts[i]);
}
long dollars[5];
long cents[5];
float ipart;
for (i=0; i<5; i++) {
cents[i] = (int) roundf(modff(amounts[i], &ipart)*100.0f);
dollars[i] = (int) ipart;
if(cents[i]==100){
dollars[i]++;
cents[i]=0;
}
}
for (i=0; i<5; i++) {
printf("\n$%ld.", dollars[i]);
if(cents[i]<10)
printf("0%ld", cents[i]);
else
printf("%ld", cents[i]);
}
return 0;
}
I would like to avoid 'floating point error in this code.
A purpose of this code is to gain 'The average of whole numbers' but the number of 'whole numbers' is limited by the input of users. Please help me.
#include <stdio.h>
int main(void)
{
int num=0;
int limit;
int result=0;
printf("number of integer: ");
scanf("&d", &limit);
while(num<limit)
{
int output;
printf("Input integer : ");
scanf("%d", &output);
result += output;
num++;
}
printf("average of total integer: %d \n", result/limit);
return 0;
}
Thank you for reading.
When you divide 2 integers, the result is also an integer.
To return a float, you need to cast one of the arguments as a float.
So your last line becomes
printf("average of total integer: %f \n", result/(float)limit);
As the result of two integer dividing is also an integer,so it as
printf("average of total integer: %f \n", result/(float)limit);
when you type cast the variable limit to float what happens is that result will be implicitly converted to float and so the result is a float.