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?
Related
#include<stdio.h>
#include<omp.h>
#include<stdlib.h>
float FloatRandomizer(float a, float b)
{
float randomNumber = (a + (float)rand() / (float)(RAND_MAX / b));
if (randomNumber > b)
randomNumber = randomNumber - a;
return randomNumber;
}
float F(float x)
{
return x * x;
}
int main()
{
int n = 1000000;
float a;
printf("a = "); scanf_s("%f", &a);
float b;
printf("b = "); scanf_s("%f", &b);
float temp = (b - a) / n;
float sum = 0;
int i;
#pragma omp parallel for shared(sum)
for (i = 0; i < n; i++)
{
float randomNumber = FloatRandomizer(a, b);
sum = sum + F(randomNumber);
//printf("threadNum = %d\nsum = %f\n", omp_get_thread_num(), sum);
}
float result = temp * sum;
printf("result = %f", result);
}
There are not to much things to say. I am trying to get definite integral within given [a,b] range with Monte-Carlo method. So basically everything works, problems started when I wrote #pragma open mp for statement, it calculates result(that for a = 0 and b = 1 should be 1/3) in wrong way without commented line within for loop but with printf it calculates it correctly. I can't understand the problem
Without printf:
With printf for same a and b(last line of big output):
I am using Microsoft Windows 10 and Microsoft Visual Studio.
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;
}
So I have a to create a program that creates an array and adds 6 grades to it, then it has to calculate the average in a different function, and then check what grades are above the average.
It all worked until this little operation started to throw an error like such:
main.c:29:18: error: invalid operands to binary < (have 'float (*)(float *)' and 'float')
#include <stdio.h>
#include <stdlib.h>
float media(float grades[]){
int sum = 0;
for(int i = 0; i < 5; i++){
sum += grades[i];
}
float media = sum/6;
printf("A media é %f", media);
return media;
}
int acimamedia(float media(float grades[]), float vetor[], int x){
for(int i = 0; i < 6;i++){
float z = vetor[i];
if(media < z){ // <<<<<<<<<<<<< [Error here]
x += 1;
}
}
return x;
}
int main(int argc, char** argv) {
float grades[6];
int acimadamedia;
printf("As notas sao: \n");
for(int i = 0; i < 6; i++){
grades[i] = rand()%100;
printf("Nota [%d] -- %f",i , grades[i]);
}
printf("Existem %d notas acima da media.", acimadamedia());
media is a function declaration:
float media(float grades[])
and z is a single float value
so you are trying to compare a function pointer to a float hence the error is exactly as what it says:
error: invalid operands to binary < (have 'float (*)(float *)' and 'float')
In addition your call of acimadamedia() is trying to treat an integer as a function.
If I kind of understand what you are trying to do and do the minimum of changes to your code:
#include <stdio.h>
#include <stdlib.h>
float media(float grades[]){
int sum = 0;
for(int i = 0; i < 5; i++){
sum += grades[i];
}
float media = sum/6;
printf("A media é %f\n", media);
return media;
}
int acimamedia(float media(float grades[]), float vetor[]){
int x = 0;
float avg = media(vetor);
for(int i = 0; i < 6;i++){
float z = vetor[i];
if(avg < z){
x += 1;
}
}
return x;
}
int main(int argc, char** argv) {
float grades[6];
int acimadamedia;
printf("As notas sao: \n");
for(int i = 0; i < 6; i++){
grades[i] = rand()%100;
printf("Nota [%d] -- %f\n",i , grades[i]);
}
acimadamedia = acimamedia(&media, grades);
printf("Existem %d notas acima da media.\n", acimadamedia);
return 0;
}
Will show the grades, the avg grade and the number of grades above the average
the acimamedia function for passing a function usally is specified as:
int acimamedia(float (*media)(float grades[]), float vetor[])
I finally got code that's giving me random floats from 0 to 1.
My code looks like this:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
srand((int)time(NULL));
int N=10;
float a = 1.0;
for (int i=0;i<N;i++)
printf("%f\n", ((float)rand()/(float)(RAND_MAX)) * a);
return 0;
}
Now, I need to make program that will give me the average from given numbers for 10<=N<=10000, but im out of ideas how to make it works. Any ideas?
Used the 'a' variable as temporary storage and summation for the floats.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
srand((int)time(NULL));
int lowerLimit = 10 + 1, upperLimit = 10000;
//generates random number between 10 and 10000, if needed
int N = lowerLimit + rand() % (upperLimit - lowerLimit);
/* 'a' variable was not needed in previous*/
float a, sum = 0.0;
for (int i=0;i<N;i++){
a = ((float)rand()/(float)(RAND_MAX));
sum += a;
printf("Float %i = %f\n", i,a);
}
printf("Average = %f\n", sum/N);
return 0;
}
Before printing, store them and sum them up.
int main()
{
srand((int)time(NULL));
int N=10;
float a = 1.0f, r, sum = 0.0f;
for (int i=0;i<N;i++){
r = ((float)rand()/(float)(RAND_MAX)) * a;
printf("%f\n", r);
sum += f;
}
printf("sum = %f\n", sum / N);
return 0;
}
I've got simple code for Taylor's Theorem for cosh() function.
I'm trying to catch a mistake - the result is sometimes close the real answer.
How to do it correctly?
When my start is 0, end is 5, and subdivides is 5 it gave good results, but when I put 5 as start and 10 as end, the result is farther away from the expected value.
#include <stdio.h>
#include <math.h>
int poww( float number, int a )
{
float result = 1.0;
int i;
if( a != 0 );
{
for( i = 0; i < a; i++ ) {
result = result * number;
}
}
return result;
}
int factorial(int n)
{
switch (n) {
case 0:
return 1;
break;
default:
return n * factorial(n-1);
}
}
void main()
{
puts("Enter start: ");
float start;
scanf("%f", &start);
puts("\nEnter end: ");
float end;
scanf("%f", &end);
puts("\nSubintervals:");
int subinterval;
scanf("%d", &subinterval);
float h = (end - start) / (float)subinterval;
printf("h is : %3.2f \n", h);
double x, result, temp;
int n;
for( x = start; x <= end; x += h) {
result = 0;
for(n = 0 ; ; n++) {
temp = poww(x, 2 * n) / (factorial( 2 * n ) * 1.0);
if(temp < 0.00001) {
break;
} else {
result = result + temp;
printf("X = %f temp = %f, result = %f\n", x, temp, result);
}
}
printf("X = %f, result = %3.2f, cosH = %3.2f\n\n", x, result, cosh(x) );
}
puts("Press any key...");
getchar();
}
PROBLEM SOLVE:
function returns an integer instead of double, also I changed every float to double.
Change all float types to double and use double as the return type for the factorial() and poww() functions, too. It's the last two that are most important in this case.
Also, the return type on main() should be int, not void.
[I just finished removing the dead if statement in poww(), and noticed that the function only "speeds up" a pow() computation. If you're worried about performance, worry about computing a factorial and a power on every term, rather than multiplying the previous term by x^2 and dividing by (2*n)*(2*n-1).]
I get good results between 4 and 10 on this minor fix of your code:
#include <stdio.h>
#include <math.h>
double poww( float number, int a )
{
float result = 1.0;
int i;
for( i = 0; i < a; i++ )
{
result = result * number;
}
return result;
}
double factorial(int n)
{
switch (n)
{
case 0: return 1;
break;
default: return n * factorial(n-1);
}
}
int main(){
puts("Enter start: ");
float start;
scanf("%f", &start);
puts("\nEnter end: ");
float end;
scanf("%f", &end);
puts("\nSubintervals:");
int subinterval;
scanf("%d", &subinterval);
float h = (end - start) / (float)subinterval;
printf("h is : %3.2f \n", h);
double x, result, temp;
int n;
for( x = start; x <= end; x += h){
result = 0;
for(n = 0 ; ; n++){
temp = poww(x, 2 * n) / (factorial( 2 * n ) * 1.0);
if(temp < 0.00001){
break; }
else{
result = result + temp;
printf("X = %f temp = %f, result = %f\n", x, temp, result);
}
}
printf("X = %f, result = %3.2f, cosH = %3.2f\n\n", x, result, cosh(x) );
}
puts("Press any key...");
getchar();
return 0;
}