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[])
Related
I tried to do something by combining functions, arrays and pointers, but I got this error, I couldn't figure out why, I would appreciate if you could help.
double findaverage(int howmany, *int grades[]);
#include <stdio.h>
double findaverage(int howmany, *int grades[]) {
int i;
int sum = 0;
for (i = 0; i < howmany; i++) {
sum = grades[i] + sum;
}
return sum / howmany;
}
int main()
{
const int size = 5;
int grades[5] = {30,56,23,44,45};
int average= findaverage(size, grades);
printf("%d", average);
return 0;
}
My interpretation on how to transform the comments into code.
removed obsolete prototype definition (it is obsolete due to function call order)
renamed function from findaverage to calculate_average due to what it does
changed *int grades[] to int grades[]
moved iterator declaration (int i) into loop as it is not used outside of the loop
changed type of sum from int to double
used += operation instead of sum = sum + ..., to keep the code short and expressive
changed type of average from int to double
added helper function to print input array
changed output type format, added two digital places and newline to output of average
add pointer versions of calculate_average to illustrate/clarify differences
The original data are structured as array. Therefore, they should be accessed in array style. Nevertheless, it is possible to process them in pointer style. See also the answers to What is the difference between char array and char pointer in C? for clarification.
calculate_average.c
#include <stdio.h>
double calculate_average(int howmany, int grades[]) {
double sum = 0.0;
for (int i = 0; i < howmany; i++) {
sum += grades[i];
}
return sum / howmany;
}
double calculate_average_pointers(int howmany, int *grades) {
double sum = 0.0;
for (int i = 0; i < howmany; i++) {
// calculate position of next int in memory 'grades + i' and
// retrieve int from it with dereference operator '*'
sum += *(grades + i);
}
return sum / howmany;
}
// working but bad mix of semantics, array as paramter, pointer arithmetic inside
double calculate_average_pointers_2(int howmany, int grades[]) {
double sum = 0.0;
for (int i = 0; i < howmany; i++) {
sum += *(grades + i);
}
return sum / howmany;
}
void print_array(int howmany, int grades[]) {
printf("[");
for (int i = 0; i < howmany; i++) {
char *comma_or_not = i < howmany - 1? "," : "";
printf(" %d%s", grades[i], comma_or_not);
}
printf(" ]\n");
}
int main()
{
const int size = 5;
int grades[5] = {30, 56, 23, 44, 45};
printf("Input: ");
print_array(size, grades);
double average = calculate_average(size, grades);
printf("Average: %.2lf\n", average); // two decimal places
average = calculate_average_pointers(size, grades);
printf("Average (pointers): %.2lf\n", average); // two decimal places
average = calculate_average_pointers_2(size, grades);
printf("Average (pointers 2): %.2lf\n", average); // two decimal places
return 0;
}
$ gcc -Wall calculate_average.c
$ ./a.out
Input: [ 30, 56, 23, 44, 45 ]
Average: 39.60
Average (pointers): 39.60
Average (pointers 2): 39.60
$
I am a beginner and am doing pretty bad in my class right now, I just can't get some of this stuff down. I am working on one of the final homeworks and can't figure out the problem with my code. It's probably really messy but the main thing I'm having trouble with right now is the getArea function. It won't run correctly when I call it and says 'expected expression before int'. Any help is greatly appreciated, thanks
//This program takes 18 numbers and puts it in a single dimensional array and a two dimensional array.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define COLMAX 3
#define ROWMAX 6
double triArea(double a, double b, double c);
double checkValidation(double a, double b, double c);
double func1(double values[][COLMAX], int);
int main(void)
{
unsigned int seed;
double tArea = 0.0;
int rand_int();
int loop;
int col;
int row;
int counter = 0;
srand(time(NULL));
int RandomArray[18];
int ArrayTwo[6][3];
printf("\nOne dimensional array\n");
for(loop = 0; loop < 18; loop++)
{
RandomArray[loop] = rand_int();
printf("%d ", RandomArray[loop]);
}
printf("\nTwo dimensional array\n");
for(row = 0; row < ROWMAX; row++)
{
for(col = 0; col < COLMAX; col++)
{
ArrayTwo[row][col] = RandomArray[counter];
counter++;
//int total = 0;
//total = total + array2[row][col];
printf("%d\t", ArrayTwo[row][col]);
getArea(ArrayTwo[row][col]);
//printf("The total is %d", total);
}
printf("\n");
}
printf("\n");
double total = 0.0;
double ArrayTwoTotal[ROWMAX][COLMAX];
total = func1(ArrayTwoTotal, ROWMAX);
printf("total is %.2lf \n", total);
system ("PAUSE");
return 0;
}
double checkValidation(double a, double b, double c)
//triangleValueA + triangleValueB >= triangleValueC) && (triangleValueB + triangleValueC >= triangleValueA) && (triangleValueA + triangleValueC >= triangleValueB
{
int count = 0;
//a = base, b = height, c = area
if ((a + b >= c) && (b + c >= a) && (a + c >= b))
{
}
}
//Calculate area of a triangle
double getArea(int[int][int])
{
double base, height, area;
area = base*height/2.0;
printf("The area is %.lf\n", area);
checkValidation(base, height, area);
return area;
}
//Function to get the totals of the 1 dimensional array
double func1(double ArrayTwoTotal[][COLMAX], int rows)
{
double sum = 0.0;
int i, j;
for(i = 0 ; i < rows ; i++)
{
for(j = 0 ; j < COLMAX ; j++)
{
sum += ArrayTwoTotal[i][j];
}
}
return sum;
}
//Function to find a random number
int rand_int()
{
int x = 0;
x = ((rand() % 100)+1);
return x;
}
There are a lot of things that you need to look at.
First getArea() function doesn't have any declaration. Please declare it first and in a correct way.
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 am pretty new to programming and I was working on printing arrays using functions and I ran to the following error.
Test.c: In function ‘main’:
Test.c:21:54: error: incompatible type for argument 1 of ‘theta’
21 | printf("The theta values are = %lf\n", x[i], theta(x[i]));
| ~^~~
| |
| double
Test.c:5:21: note: expected ‘double *’ but argument is of type ‘double’
5 | double theta(double x[N])
| ~~~~~~~^~~~
And here is the code.
#include <stdio.h>
#include <stdlib.h>
#define N 50
double theta(double x[N]);
int main(){
int i;
double x[i];
printf("The theta values are = %lf\n", x[i], theta(x[i]));
return 0;
}
double theta(double x[N]){
int i;
for(i = 0; i < 50; i++){
x[i] = (double)(i)/ ((double)(N) - 1.0);
}
return x[i];
}
I just want to print 50 values between 0 and 1. Just like linspace(0:1:50) in MATLAB.
Thanks for the help.
First, in declare function, you cannot set size of array.
change to
double theta(double* X)
Second, do double theta(double x[N]); in main function.
and this is compiler didn`t advise you but I found bug in your code.
First, i in theta is different with i in main
Second, use for to print
I think you want this code
#include<stdio.h>
#include<stdlib.h>
#define N 50
double* theta()
{
int i;
double x[N];
for(i = 0; i < N; i++){
x[i] = (double)(i)/ ((double)(N) - 1.0);
}
return x;
}
int main(){
int i;
double* x=theta();
for(i=0;i<N;i++){
printf("The theta values are = %lf\n", x[i]);
}
return 0;
}
And I like this code more
#include<stdio.h>
#include<stdlib.h>
#define N 50
int main(){
int i;
for(i=0;i<N;i++){
printf("%lf\n",i/(N-1.0));
}
return 0;
}
I found the solution. Here it is.
#include <stdio.h>
#include <stdlib.h>
#define N 50
double* theta(double x[], int nb_elements){
int i;
for(i = 0; i < nb_elements; i++){
x[i] = (double)(i)/ ((double)(nb_elements) - 1.0);
}
return &x[0];
}
int main(){
int i;
double x[N];
theta(x, N);
for(i = 0; i < N; i++){
printf("%lf\n", x[i]);
}
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?