Can't bring array values to function - c

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.

Related

I want to convert my array values from float to double to get smaller values

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

C Input won't add to array list

The 2 arrays wont store input, the increment of each index number is always at 0 as printf states it.
int main()
{
int ingredientsAmount;
double ingredientsPrice[10];
double ingredientsWeight[10];
scanf("%d", &ingredientsAmount);
for(int i = 0; i < ingredientsAmount; i++)
{
scanf("%lf", &ingredientsPrice[i]);
printf("Price stored at index %lf\n", i);
scanf("%lf", &ingredientsWeight[i]);
printf("Weight stored at index %lf\n", i);
}
return 0;
}
You are using the incorrect conversion specifier %lf with an object of the type int. Use the conversion specifier %d. Write
printf("Price stored at index %d\n", i);
and
printf("Weight stored at index %d\n", i);
Or maybe you mean the following
printf("Price stored at index %d is %f\n", i, ingredientsPrice[i]);
and
printf("Weight stored at index %d is %f\n", i, ingredientsWeight[i]);
Pay attention to that the for loop is unsafe because the value of the variable ingredientsAmount entered by the user can be greater than the sizes of the declared arrays.
At least you should write
if ( scanf("%d", &ingredientsAmount) != 1 || ingredientsAmount > 10 )
{
ingredientsAmount = 10;
}

Why array not working with function call?

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.

Passing struct array to function and calculating average in C?

Beginner here, I've been trying this for hours and can't get it to work, searched online too and couldn't find an answer.
I'm trying to write a program where you input people by putting their age and height then calculate the average of each, and the average ratio of age to height. Whenever I pass the struct to my getAverage function, it returns the address (I think) instead of the averages.
#include <stdio.h>
#include <stdlib.h>
typedef struct person
{
int age;
double height;
} Person;
double getAv (Person people[50], int max)
{
int i;
double total, retval;
total = 0;
for (i=0; i<=max; i++)
{
total = total + people[i].age;
}
retval = total / max;
return retval;
}
int main (void)
{
Person people[50];
int i;
while (i++, people[i].age>=0)
{
printf ("Person #%d\n", i);
printf ("enter an age: ");
scanf ("%d", &people[i].age);
if (people[i].age<0)
break;
printf ("enter a height: ");
scanf ("%lf", &people[i].height);
printf ("\n");
}
double averageAge;
averageAge = getAv (&people[50], i);
printf ("%.1lf\n", averageAge);
}
Haven't implemented average height or ratio yet, just trying to get average of ages to work for now. When I try taking the & out of averageAge = getAv(&people[50], I); I get a compile error that tells me it needs the &. Any help would be appreciated.
There are serveral issues in you code, the first one:
Person people[50];
int i;
while (i++, people[i].age>=0)
You are using i uninitialized, thus, incrementing a variable containing garbage (or 0 as intended, but it is impossible to guarantee)
To avoid such problems in the future enable warnings on your compiler, the compiler would have told you something like:
ā€˜iā€™ is used uninitialized in this function [-Wuninitialized]
So switch to
Person people[50] = {0};
int i = 0;
It seems (based on the pre-increment operator ++i) that you want to use arrays with base 1, there is nothing wrong with that, but in your average function you are starting from 0, so use a for:
for (i = 0; ; i++)
{
...
if (people[i].age <= 0) break;
...
}
Another issue:
double averageAge;
averageAge = getAv (&people[50], i);
In this way you are passing only element 50 which is not part of your array (remember arrays are base 0 in C), to pass the entire array:
averageAge = getAv (people, i);
or
averageAge = getAv (&people[0], i);
The last one:
for (i=0; i<=max; i++)
You are including the element discarded when you was checking for age > 0, switch to:
for (i=0; i<max; i++)
A minor issue:
Iif the user enters 0 for the first element of people you end up dividing by 0
double averageAge;
averageAge = getAv (&people[50], i);
printf ("%.1lf\n", averageAge);
should be
if (i > 0)
{
double averageAge;
averageAge = getAv (people, i);
printf ("%.1f\n", averageAge); // No need to use lf (long double)
}
If you do it this way, you need to receive in your function (Person& people[], int n)
Where n is the length of the array.
We use the '&' to not do copies of the array and also you are passing only the element with index 50 in the function, simply do only (people, n)
there are mistakes in this code ,first you should initialize int i to int i=0 in mian function.
how are using this condition while (i++, people[i].age>=0) when you haven't scanned any input it's like using an uninitialized variable. you need a do-while like this
do
{
printf("Person #%d\n", i);
printf("enter an age: ");
scanf("%d", &people[i].age);
if (people[i].age < 0)
break;
printf("enter a height: ");
scanf("%lf", &people[i].height);
printf("\n");
i++;
} while (people[i-1].age >= 0);
also you are sending wrong arguments to your getAv function you should use averageAge = getAv (people, i);.
and in your function you have to remove = in this loop for (i=0; i<=max; i++) .this should be for (i=0; i<max; i++) ,otherwise the negative entered age will also be add to total and will affect the average.

C Program forLeast Square Regression Line and Errors

I wanted to create a program to calculate the regression line of some given data, along with the errors, so I can use in my university assignments. This is the following program:
#include <stdio.h>
#include <math.h>
int main(void)
{
int i,n,N;
double x[n],y[n],a=0.0,b=0.0,c=0.0,d=0.0,D,P=0.0,p,Sx,A,B,dA,dB;
printf("Give the number of data you want to input for x : ");
scanf("\n%d", &N);
printf("\nGive the values of x : ");
for (i=1; i<=N; i++);
{
printf("\n Enter x[%d] = ", i);
scanf("%lf", &x[i]);
a+=x[i];
b+=pow(x[i],2);
}
printf("\nGive the values of y : ");
for (i=1; i<=N; i++);
{
printf("\n Enter y[%d] = ", i);
scanf("%lf", &y[i]);
c+=y[i];
}
D=N*b-pow(a,2);
A=(b*c-a*d)/D;
B=(N*d-a*c)/D;
for (i=1; i<=N; i++);
{
d+=x[i]*y[i];
p=y[i]-A-B*x[i];
P+=pow(p,2);
}
Sx=sqrt(P/N-2);
dA=Sx*sqrt(b/D);
dB=Sx*sqrt(N/D);
printf("\n x \t \t \t y");
for (i=1; i<=N; i++);
printf("\nx[%d] = %lf\t%lf = y[%d]", x[i],y[i]);
printf("\nA = %lf\t B = %lf", A,B);
printf("\nThe errors of A & B are dA = %lf and dB = %lf", dA,dB);
printf("\nThe equation of the regression line is y=%lfx+(%lf)", B,A);
return 0;
}
I have two problems.
Despite giving a value to N, the program runs so that I can only give one value for x and one value for y. Why and where is the mistake?
When printing the "Enter x[%d]", it displays x[11] and at the end when printing "x[%d] = %lf\t%lf = y[%d]", it displays x[0]. Again why and where is the mistake?
Thank you for your help!
You are trying to create a dynamic array in C.
To do that, you need to use dynamic memory allocation with malloc and free. So, your code should look something like this:
int N;
double *x;
printf("Give the number of data you want to input for x :\n");
scanf("%d", &N);
x = malloc(sizeof(double) * N);
Then, at the end of your program you need to free the memory:
free(x);
If you don't want to deal with manual memory management (or can't for some reason), you can use a static maximum array size like this:
#define MAX_N_X 100
int main(void) {
int N;
double x[MAX_N_X];
printf("Give the number of data you want to input for x :\n");
scanf("%d", &N);
if (N > MAX_N_X) {
printf("Can't handle that many inputs! Maximum %d\n", MAX_N_X);
return 0;
}
}
You simply missed two parameters to printf.
Yo wrote:
printf("\nx[%d] = %lf\t%lf = y[%d]", x[i],y[i]);
But it should be:
printf("\nx[%d] = %lf\t%lf = y[%d]", i, x[i], y[i], i);
I found the main problem with this program a few days after posting this and the fix would be removing the ; from the for commands, along with some other minor changes. I thought I might add this comment to let you know and now it is working like a charm. The simplest of mistakes fools even the trained eyes. After I found this, I was shocked that no one picked up on this mistake.

Resources