My array not taking inputs (Ex- 5.21).I am trying to take a float input for each of my array elements but this isn't doing this. and Also this function is not working with the array. please help
float maxJump (double jumpRecord[], int attempts)
{
float max = jumpRecord[0];
for(int i=0; i<attempts; ++i)
{
if(jumpRecord[i]>max)
max = jumpRecord[i];
}
printf("Longest Jump = %.2f meter\n",max);
}
int main()
{
double jumpRecord[5];
printf("Jumping information (in meter): \n");
for(int i=0; i<5; i++)
{
printf("Attempt %d. = ",i+1);
scanf("%.2f",&jumpRecord[i]);
}
maxJump(jumpRecord,5);
return 0;
}
When used with scanf(), %.2f (%f) format specifier is for reading float values.
The elements of jumpRecord are double, so you should use %lf (%.2lf) format specifier instead.
Note that %f should be used for printing double via printf(). %lf is allowed in C99 or later, but %f is more portable.
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 have prompted the user for 5 inputs in the main and saved them into an array called array. I've checked the array in main and it prints out the values as they were entered. However, when I pass it to a function which I've included below, I get the output that the array contains all 0 values.
What am I doing wrong?
conversion(float array[], int size)
{
float add = 0.0;
float change, num;
printf("\nThe array is: \n");
for (i=0;i < size;i++)
{
printf("%.2f\n",&array[i]);
}
/*calculate and store the conversion values in a new array*/
for(i=0; i<s; i++)
{
num = array[i];
change = (num*100.50);
for(j=0; j<1; j++)
{
printf("\n %.2f your number is %.2f in float percent\n", &num, &change);
}
}
}
&cArray[i]
you don't need to address of the ith element in order to print it, you just need the ith element
Change
printf("%.2f\n",&cArray[i]);
printf("\n %.2f in Celsius is %.2f in Fahrenheit\n", &temp, &con);
to
printf("%.2f\n",cArray[i]);
printf("\n %.2f in Celsius is %.2f in Fahrenheit\n", temp, con);
The same with con, printf() doesn't need the address of a scalar variable in order to print it.
Like Johnny Mopp said you have an extraneous &,
But depending on the size of the array you're dealing with you may just want to pass a pointer to the function instead.
#include <stdio.h>
int main()
{
int n, i;
double num[100], sum = 0.0, average;
printf("Enter the numbers of elements: ");
scanf("%d", &n);
for(i = 0; i < n; i++)
{
printf("Enter number %d: ", i+1);
scanf("%f", &num[i]);
sum += num[i];
}
average = sum / n;
printf("Average = %f", average);
return 0;
}
Hi, I have a problem with displaying the average, displaying numbers from space, how to fix it?
Correct format specifier for double would be (you will have to use this)
scanf("%lf", &num[i]);
For printf both %f or %lf will do. Note that when using scanf check the return value of scanf - in case of failure you will take necessary action. (Wrong input given or some other error).
Using the correct format specifier for double as stated by #coderredoc resolves it. Use %lf instead of %f.
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;
}