#include <stdio.h>
int converttemperature(int f,int c)
int main()
{
int farenheit, celcius, upper, lower, step;
upper=300; lower=0; step=20;
farenheit=lower;
while(farenheit<=upper)
{
converttemperature(farenheit, celcius);
printf("%d\t %d\n", farenheit, celcius);
farenheit = farenheit+step;
}
return 0;
}
int converttemperature(int f, int c)
{
int f,c;
c = 5 * (f-32) / 9;
return c;
}
#include <stdio.h>
int converttemperature(int f);
int main(void)
{
int farenheit, celcius, upper, lower, step;
upper=300; lower=0; step=20;
farenheit=lower;
while(farenheit<=upper)
{
celcius = converttemperature(farenheit);
printf("%d\t %d\n", farenheit, celcius);
farenheit = farenheit+step;
}
return 0;
}
int converttemperature(int f)
{
int c;
c = 5 * (f-32) / 9;
return c;
}
Related
Calculating the sum of the first k numbers of the sequence a[0] = 1, a[k] = k*a[k-1] +1/k ( k = 1, 2, ... ).
UPD
There is still a problem with the recursive function ...What is the error?
#include <stdio.h>
#include <stdlib.h>
float m(float n){
float k=1;
float sum=k;
int i;
for (i=1; i<n;i++){
k = (i*k+1.0/i);
sum = sum+k;
}
return sum;
}
float Fn(float n)
{
if (n==0) {
return 1;}
return ((n*Fn(n-1)+1.0/n)+Fn(n-1));
}
int main(int argc, char *argv[]) {
float k;
printf("input k : ");
scanf("%f",&k);
printf("res %f \n",Fn(k));
return 0;
}
There were several issues in your code:
Integer division: 1/n = 0
There was a confusion between the term value Fn and the sum value
An iterative solution is simpler here than a recursive one
Here is a code, with both iterative and recursive implementations:
#include <stdio.h>
#include <stdlib.h>
float sum_fn(int n){
float Fk = 1;
float sum = Fk;
for (int i = 1; i <= n; i++){
Fk = i*Fk + 1.0/i;
sum += Fk;
}
return sum;
}
float sum_recursive(int n, float *sum){
if (n == 0) {
*sum += 1.0;
return 1.0;
}
float Fn = n * sum_recursive(n-1, sum) + 1.0/n;
*sum += Fn;
return Fn;
}
int main(int argc, char *argv[]) {
int k;
printf("input k : ");
scanf("%d", &k);
printf("k = %d\tsum = %f\n", k, sum_fn(k));
float sum = 0;
sum_recursive(k, &sum);
printf("k = %d\tsum = %f\n", k, sum);
return 0;
}
I have a problem with the compiler and I must have some logical mistakes (C language)
I want to have one void function that gives me the maximum price, the minimum price and the average of a table with maximum 100 prices. And if the user gives price=-1 I want the program to end!
Here is my code:
#include <stdio.h>
void function(float pin[],int j,float *min,float *max,float *mo,int cnt);
int main()
{
int i=0,count=0;
float prc[100],mo;
for(i=0;i<=99;i++)
{
printf("Enter price:");
scanf("%f",&prc[i]);
if(prc[i]==-1)
{
break;
}
count++;
}
int min=prc[0];
int max=prc[0];
void function(float prc,int i,float *min,float *max,float *mo,int count);
printf("Minimum price is:%f Maximum price is:%f and Mo is:%f",min,max,mo);
return 0;
}
void function(float pin[],int j,float *min,float *max,float *mo,int cnt)
{
float sum;
for(j=0;j<=cnt;j++)
{
if(pin[j]<*min)
{
min=pin[j];
}
if(pin[j]>*max)
{
*max=pin[j];
}
sum=+pin[j];
}
*mo=sum/j;
}
Looks like you have lots of little mistakes (missing dereference, using a variable out of scope, wrong operator, etc.):
#include <stdio.h>
void function(float pin[], float *min, float *max, float *mo, int cnt);
int main()
{
int i=0, count=0;
float prc[100], mo;
for (i=0; i<=99; i++)
{
printf("Enter price:");
scanf("%f", &prc[i]);
if (prc[i] == -1)
{
break;
}
count++;
}
float min=prc[0];
float max=prc[0];
function(&prc[0], &min, &max, &mo, count);
printf("Minimum price is:%f Maximum price is:%f and Mo is:%f", min, max, mo);
return 0;
}
void function(float pin[], float *min, float *max, float *mo, int cnt)
{
float sum = 0.0f;
for (int j=0; j < cnt; j++)
{
if (pin[j] < *min)
{
*min = pin[j];
}
if (pin[j] > *max)
{
*max = pin[j];
}
sum += pin[j];
}
*mo = sum / cnt;
}
There are a few mistakes in the function :
I have modified the code and looks to be fine except the function argument mo , decide whether to declare it as a float pointer or a float
Look at the declaration inside main, u are calling a function but the syntax is for declaration.
used mo as argument but mo is defined as float variable whereas the function accepts a pointer as per the declaration and there are a few warning related to the types
Declared ints but printing them using format specifiers for float %f
include
void function(float pin[],int j,float *min,float *max,float *mo,int cnt);
int main()
{
int i=0,count=0;
float prc[100],mo;
for(i=0;i<=99;i++)
{
printf("Enter price:");
scanf("%f",&prc[i]);
if(prc[i]==-1)
{
break;
}
count++;
}
int min=prc[0];
int max=prc[0];
function(prc,i,min,max,mo,count);
printf("Minimum price is:%f Maximum price is:%f and Mo is:%f",min,max,mo);
return 0;
}
void function(float pin[],int j,float *min,float *max,float *mo,int cnt)
{
float sum;
for(j=0;j<=cnt;j++)
{
if(pin[j]<*min)
{
*min=pin[j];
}
if(pin[j]>*max)
{
*max=pin[j];
}
sum=+pin[j];
}
*mo=sum/j;
}
wrong use of pointer and type,use
float min=prc[0];
float max=prc[0];
function(prc,i,&min,&max,&mo,count);
You declare as:
void function(float pin[],int j,float *min,float *max,float *mo,int cnt);
So you must deliver a float-pointer but not a float number.
I have to use:
float average(const int tab[], int size);
float stdDev(const int tab[], int size);
to printf average and stdDev in C.
I have problem with average and i think with const int.
When i add const int tab[101] i have error with a1;
So how can i make it work with const int (if i can).
And if it is anything wrong with this code.
Any help will be helpful.
#include<stdio.h>
#include<math.h>
float average(const int tab[], int size);
float stdDev(const int tab[], int size);
int main()
{
float ave, std;
int a1;
int j;
int tab[101];
printf("Podaj liczby: ");
for(j=0; j<=99; j++)
{
a1 = scanf("%d", &tab[j]);
if(a1<1)
{
printf("Incorrect input");
return 1;
}
if(tab[0]==0)
{
printf("not enough data available");
return 2;
}
if(tab[j]==0)
{
break;
}
}
ave = average(tab, j);
printf("%.2f\n", ave);
std = stdDev(tab, j);
printf("%.2f", std);
return 0;
}
float average(const int tab[], int size)
{
int i;
float y=0, x;
if(size<=0)
{
return -1;
}
for(i=0; i<size; i++)
{
x = x + tab[i];
}
y = x/size;
return y;
}
float stdDev(const int tab[], int size)
{
int i;
float y, z, z1, z2=0, z3=0;
if(size<=0)
{
return -1;
}
y = average(tab, size);
for(i=0; i<size; i++)
{
z = tab[i] - y;
z1 = pow(z, 2);
z2 = z2 + z1;
z=0;
z1=0;
}
z3 = sqrt(z2/size);
return z3;
}
You define the variable x in average here:
float y=0, x;
without giving it a value. Then here:
x = x + tab[i];
you are reading its value without setting it anywhere beforehand. Because you never gave x a value, its value will be indeterminate and reading it will cause undefined behavior, which means that your program could e.g. print garbage output.
Always initialize your variables:
float y=0, x=0;
I am given 5 integers that must be not less than 10 and not more than 100. Then, I must find their division remainders if we divide them by 10. Then, I must find the sum of the remainders that I found and (there it goes the difficult part) finally which one of the 5 remainders is the max.
Here, I give you the code that I wrote but I have no idea how to go further. Maybe with "for", but I don't imagine how exactly.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b, c, d, e, a10,b10,c10,d10,e10, sum, max;
printf("give 5 integers \n");
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
a10 = a % 10;
b10 = b % 10;
c10 = c % 10;
d10 = d % 10;
e10 = e % 10;
printf("division remainder is: %d %d %d %d %d\n",
a10,b10,c10,d10,e10);
sum = a10 + b10 + c10 + d10 + e10;
printf("the sum of the remains is: %d\n", sum);
system("pause");
}
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
int *rands_range(int n, int low, int high);
void print(FILE *fp, int n, int *nums);
int *map(int n, int *nums, int (*f)(int));
int fold(int n, int *nums, int init, int (*f)(int, int));
int add(int x, int y);
int rem10(int x);
int greater(int x, int y);
int main(void){
srand(time(NULL));
int n = 5;
printf("give 5 integers\n");
int *integers = rands_range(n, 10, 100);
print(stdout, n, integers);
putchar('\n');
printf("division remainder is: ");
int *rems = map(n, integers, rem10);
print(stdout, n, rems);
putchar('\n');
int sum = fold(n, rems, 0, add);
printf("the sum of the remains is: %d\n", sum);
putchar('\n');
int max = fold(n, rems, *rems, greater);
printf("the max of the remains is: %d\n", max);
putchar('\n');
free(integers);
free(rems);
system("pause");
}
int *rands_range(int n, int low, int high){
assert(n > 0 && RAND_MAX >= high - low && high >= low);
int *values = malloc(n * sizeof(*values));
if(values){
for(int i = 0; i < n; ++i){
values[i] = low + rand() % (high-low+1);
}
}
return values;
}
void print(FILE *fp, int n, int *nums){
for(int i = 0; i < n; ++i){
if(i)
fprintf(fp, ", ");
fprintf(fp, "%d", nums[i]);
}
fprintf(fp, "\n");
}
int remainder_int(int x, int y){
return x % y;
}
int add(int x, int y){
return x + y;
}
int rem10(int x){
return remainder_int(x, 10);
}
int greater(int x, int y){
return x > y ? x : y;
}
int *map(int n, int *nums, int (*f)(int)){
int *result = malloc(n * sizeof(*result));
if(result){
for(int i = 0; i < n; ++i){
result[i] = f(nums[i]);
}
}
return result;
}
int fold(int n, int *nums, int init, int (*f)(int, int)){
int acc = init;
for(int i = 0; i < n; ++i){
acc = f(acc, nums[i]);
}
return acc;
}
Functions with variable arguments:
#include <stdio.h>
#include <stdarg.h>
#include <limits.h>
int max(int n, ...){
int ret = INT_MIN;
va_list ap;
va_start(ap, n);
while(n--){
int wk = va_arg(ap, int);
if(ret < wk)
ret = wk;
}
va_end(ap);
return ret;
}
int sum(int n, ...){
int sum = 0;
va_list ap;
va_start(ap, n);
while(n--){
sum += va_arg(ap, int);
}
va_end(ap);
return sum;
}
int main(void){
int a = 6, b = 3, c = 9, d = 2, e = 8;
printf("sum:%d\n", sum(5, a, b, c, d, e));
printf("max:%d\n", max(5, a, b, c, d, e));
}
Following is the code using 'for' and int array. To use 'for' you must need to use array.
#include<stdio.h>
#include <stdlib.h>
int main()
{
int i;
int max;
int sum=0;
int aryNum[5];
int aryRem[5];
printf("give 5 integers \n");
for(i=0;i<5;i++)
{
scanf("%d",&aryNum[i]);
}
printf("division remainder is: ");
for(i=0;i<5;i++)
{
aryRem[i]=aryNum[i]%10;
printf("%d ",aryRem[i]);
}
printf("\n");
max=aryRem[0];
sum = max;
for(i=1;i<5;i++)
{
if(max<aryRem[i])
{
max=aryRem[i];
}
sum+=aryRem[i];
}
printf("the sum of the remains is: %d\n", sum);
printf("maximum remains is: %d\n", max);
system("pause");
return 0;
}
If you don't want to use 'for' and int array then you can use following code. But it is not advisable:
#include <stdio.h>
#include <stdlib.h>
int greater(int a, int b)
{
return (a>b)? a:b;
}
int main()
{
int a, b, c, d, e, a10,b10,c10,d10,e10, sum, max;
printf("give 5 integers \n");
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
a10 = a % 10;
b10 = b % 10;
c10 = c % 10;
d10 = d % 10;
e10 = e % 10;
printf("division remainder is: %d %d %d %d %d\n",
a10,b10,c10,d10,e10);
sum = a10 + b10 + c10 + d10 + e10;
printf("the sum of the remains is: %d\n", sum);
/*New added code*/
max=greater(a10,b10);
max=greater(max,c10);
max=greater(max,d10);
max=greater(max,e10);
printf("maximum remains is: %d\n", max);
system("pause");
return 0;
}
This code is showing following errors:
missing ) before type
calc: too few arguments to call
syntax error ) Visual stuio 2013 platform
MyCode:
#include "math.h"
void main()
{
float num[5];
float (calc (float num[5]));
calc(float num);/* transferring control to calc function)*/
getch();
}
float calc(float nun[5])
{
int i;
float num[5];
float sum, avg, sqmn1, sumsqmn = 0, sqsd = 0; float sd;
printf("\nEnter 5 numbers");
for (i = 0; i < 5; i = i + 1)
{
scanf("%f", &num[i]);
}
sum = 0;
for (i = 0; i < 5; i = i + 1)
{
sum = sum + num[i];
}
avg = sum / 5;
for (i = 0; i < 5; i = i + 1)
{
sqmn1 = (avg - num[i])*(avg - num[i]);
sumsqmn = sumsqmn + sqmn1;
}
sqsd = sumsqmn / 5;
sd = sqrt(sqsd);
printf("\nThe sum is %f", sum);
printf("\nThe average is %f", avg);
printf("\nThe stabdard deviation is %f", sd);
getch();
}
float (calc (float num[5]));
in your main(), what is this exactly?
IMO, it can be,
float ff;
ff = calc(num);
Other than that,
#include <stdio.h> is missing.
Forward declaration of float calc(float nun[5]) is missing.
You can rewite your main() as
int main()
{
float num[5];
float ff;
ff = calc(num);/* transferring control to calc function)*/
getch();
return 0;
}
but then also, you're passing num from main() to calc() but i see you never used it. What are you upto?