cs50 pset1 water.c I am getting error while compiling - c

following code is throwing compiling error. I am new to programming and so dont know what else to do. I did as taught in the class but cant get it right. cam anyone please help me solve it?
#include<stdio.h>
#include<cs50.h>
int main(void)
{
int y;
do
{
printf("Minutes: ");
int Minutes = get_int();
}
while(Minutes< 0);
y= Minutes * 12;
printf("Bottles: %i\n", y);
}
it is throwing following error:
water.c:10:8: error: unused variable 'Minutes' [-Werror,-Wunused-variable]
int Minutes = get_int();
^
water.c:12:7: error: use of undeclared identifier 'Minutes'
while(Minutes< 0);
^
water.c:14:4: error: use of undeclared identifier 'Minutes'
y= Minutes * 12;
^
3 errors generated.
make: *** [water] Error 1

You defined Minutes in the scope of the do block. That means it can only be used within that same scope - ie. between the {} enclosing the block.
You are however using the variable outside of that scope (twice in fact).
Instead, define Minutes before the do loop :
int Minutes = 0;
do {
printf("Minutes: ");
Minutes = get_int();
}
while(Minutes< 0);
This puts it in the scope of the main function body, so it can be used anywhere in the main function (after its definition).

int Minutes is declared local to the loop body, meaning that the loop condition while(Minutes< 0) has no idea what "Minutes" is.
Simply move the declaration out of the loop, above it.

This is the answer
#include <cs50.h>
#include <stdio.h>
int main(void)
{
printf("minutes: ");
int minutes = get_int();
printf("bottles: ");
int bottles = get_int();
printf("%i\n divided %i\n is %i\n", minutes, bottles, minutes / bottles);
}

Related

error: too few arguments to function ‘ptr’ , need to eval_time from a function

I have a small problem with my program.
I want to eval the time from a function and the compiler gives me an error.
I know were the problem is, but I just dont know how to fix it :/
#include <stdio.h>
#include <time.h>
int isPerfect(int zahl){
int zaehler1, zaehler2, summe;
for(zaehler1=1;zaehler1<=zahl;zaehler1++){
summe=0;
for(zaehler2=1;zaehler2<=zaehler1/2;zaehler2++){
if(zaehler1%zaehler2==0){
summe=summe+zaehler2;
}
}
}
return summe;
}
double eval_time(int(*ptr)(int)){
time_t begin,end;
begin=time(NULL);
(*ptr)(); //compiler shows error here!
end=time(NULL);
return difftime(end,begin);
}
int main(void){
int zahl;
for(zahl=1;zahl<=500;zahl++){
if(isPerfect(zahl)==zahl){
printf("%d ist eine perfekte Zahl!\n", zahl);
}
}
printf("Die Zeit die gebraucht wurde: %.2lf s\n",eval_time(isPerfect));
return 0;
}
So my question is what do i need to change there so it evals the time from the funtion "isPerfect" ?
Sorry my variables are in german, I hope that's not a problem ;)
You have to pass a parameter, perhaps like this:
double eval_time(int(*ptr)(int), int zahl){
time_t begin,end;
begin=time(NULL);
(*ptr)(zahl);
-------^
end=time(NULL);
return difftime(end,begin);
}
Another problem is that you might have to call the functions lots and lots of times between begin and end to actually get a measurable difference in the time() values.

too many arguments to function ' ' at this point in file

I'm having the problem...
error: too many arguments to function 'EPJ calc'
error: at this point in file
This code works perfectly if I reduce the number of variables to 6 instead of 11 in the whole process. Can anybody explain why, and a way around this? In the assigment I have to be able to repeat the process 10 times.
void loop()
{
calc(&person[cc].reacTime[0], &person[cc].reacTime[1], &person[cc].reacTime[2],
&person[cc].reacTime[3], &person[cc].reacTime[4], &person[cc].reacTime[5],
&person[cc].reacTime[5], &person[cc].reacTime[6], &person[cc].reacTime[7],
&person[cc].reacTime[8], &person[cc].reacTime[9], &person[cc].midvaerdi);
Serial.print("Average time for person ");
Serial.print(cc);
Serial.print(" ");
Serial.println(person[cc].midvaerdi);
}
struct EPJ calc(float *num1,float *num2,float *num3,float *num4,
float *num5,float *num6,float *num7,float *num8,
float *num9,float *num10,
float *midval)
{
float temp;
temp = (*num1 + *num2 + *num3 + *num4 + *num5 +
*num6 + *num7 + *num8 + *num9 + *num10);
Serial.print("Temp number : ");
Serial.println(temp);
*midval = (temp)/nRepeat;
Serial.print("midval number : ");
Serial.println(*midval);
}
It looks you are calling calc with 12 arguments, but it is defined accepting only 11.
It looks like a simple typo error while calling calc(). You've supplied &person[cc].reacTime[5] twice as parameter.

Please anybody check my code to see what's wrong with it

#include <stdio.h>
int getAvg(int a, int b, int c);
int main()
{
int a,b,c;
int i;
int avg[5];
char name[5][10] ;
int korean[5], english[5], maths[5] ;
char message[2][10] = {"Pass","No Pass"};
for ( i = 0; i < 5; i++ )
{
printf("Enter your marks <name,korean,english,maths \n");
scanf("%s%d%d%d",name[i],&korean[i],&english[i],&maths[i]);
}
for (i = 0; i < 5; i++)
{
printf("name:%s, korean:%d, english:%d, maths:%d, average:%d",name[i],korean[i],english[i],maths[i],avg[i]);
avg[i] = getAvg(a,b,c);
if (avg[i]>60)
{
printf("==%s",message[0]);
}
else
{
printf("==%s",message[1]);
}
}
int getAvg(int a, int b, int c)
{
int avg;
avg = (a+b+c)/3;
return avg;
}
}
I want to print like this
Enter your marks <name,korean,english,maths>
kim 10 50 60
Enter your marks <name,korean,english,maths>
hanna 50 60 70
Enter your marks <name,korean,english,maths>
lee 80 70 60
Enter your marks <name,korean,english,maths>
lori 70 80 90
Enter your marks <name,korean,english,maths>
kang 60 70 80
name:kim,korean:10,english:50,maths:60,average:40 == no pass
name:hanna,korean:50,english:60,maths:70,average:60 == no pass
name:lee,korean:80,english:70,maths:60,average:70 == pass
name:lori,korean:70,english:80,maths:90,average:80 == pass
name:kang,korean:60,english:70,maths:80,average:70 == pass
I'm really sorry if it turns out to be my mistakes or the question is too elementary.. it's due in 5 hours and i couldn't figure out what's wrong.. it keeps telling me that the getAve function is undefined reference and i see nothing wrong with it.. please anybody kindly help me? :(
Your getAvg is inside main. Move it out (or, equivalently, move one outer brace from end of your code to just above getAvg definition start). There is also something wrong with the calculation of averages, but that's a logical error, not a syntactic one. (Specifically, you're calculating the average of a, b and c - check where you define the value of those variables, and you're printing stuff before you calculate it).
You get "undefined reference to getAvg function" because you did not close main before defining the getAvg function. Just move the last } before the function definition and that error should be gone !
Then,You call the function like this
avg[i] = getAvg(a,b,c);
But a,b and c are uninitialized ! So you need
a=korean[i];
b=english[i];
c=maths[i];
Before it. You also print avg[i] before you calculate it. So, move the printf after the function call to get the desired results.
After fixing your brace positioning, also there is a problem here:
printf("name:%s, korean:%d, english:%d, maths:%d, average:%d",name[i],korean[i],english[i],maths[i],avg[i]);
avg[i] = getAvg(a,b,c);
You have to calculate the average before you display it. Also, a, b, c are uninitialized variables. You probably meant:
avg[i] = getAvg( korean[i], english[i], maths[i] );
printf("name:%s, korean:%d, english:%d, maths:%d, average:%d",
name[i], korean[i], english[i], maths[i], avg[i]);
first you put getAvg(...) outside of main()
now call getAvg(...) before printing avg[i];
avg[i] = getAvg(korean[i],english[i],maths[i]);
printf("name:%s, korean:%d, english:%d, maths:%d, average:%d",name[i],korean[i],english[i],maths[i],avg[i]);

Function definition not allowed / [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
My professor gave us this code in class to show how a program works and said "go home and try it and you'll see it works".... well after 30 minutes I cannot get it to run. can someone please help me and point me in the right direction. Thank you!
-I get function definition on the end "double g(double x)"
-On the first else where x_left = x_mid control reaches end of non-void function
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define FALSE 0
#define TRUE 1
#define NO_ROOT -99999.0
//function prototypes
double bisect(double, double, double, double f(double farg));
// evaluation of function
double g(double);
double h(double);
int main(void) {
double x_left, x_right, epsilon, root; //declare variables
// get endpoint and error tolerance
printf("\nEnter interval endpoints > ");
scanf("%lf%lf", &x_left, &x_right);
printf("\nEnter tolerance > ");
scanf("%lf", &epsilon);
//use bisect function to look for roots of functions
printf("\n\n For function g(x)");
root = bisect(x_left, x_right, epsilon, g);
if (root != NO_ROOT)
printf("\n g(%.7f) = %e\n", root, g(root));
printf("\n\n For function h(x)");
root = bisect(x_left, x_right, epsilon, h);
if (root != NO_ROOT)
printf("\n h(%.7f) = %e\n", root, h(root));
system("pause");
return (0);
}
// bisection method program coding
double bisect(double x_left, double x_right, double epsilon, double f(double farg)){
double x_mid, f_left, f_right, f_mid;
int root_found;
// computes function at initial end points
f_left = f(x_left);
f_right = f(x_right);
// if no change in sign
if (f_left * f_right > 0) {
printf("\nmay not be no root in [%.7f, %.7f]\n\n", x_left, x_right);
return NO_ROOT;
}
// searches as long as interval size is large enough
root_found = FALSE;
while (fabs(x_right - x_left) > epsilon && !root_found) {
// compute the mid point
x_mid = (x_left + x_right) / 2.0;
f_mid = f(x_mid);
if (f_mid == 0.0) {
root_found = TRUE;}
else if (f_left * f_mid < 0.0) {
x_right = x_mid;
} else {
x_left = x_mid;
}
// trace loop execution
if (root_found)
printf("\nRoot found at x = %.7f , midpoint of [%.7f, %.7f] ", x_mid, x_leftx_right);
else
printf("\nNew interval is [%.7f, %.7f] \n\n", x_left, x_right);
//if there is a root
return ((x_left + x_right)/2.0);
}
// functions for which roots are sought
double g(double x){
return (5 * pow(x, 3.0) - 2 * pow(x, 2.0) +3);
}
double h(double x){
return (pow(x, 4.0) - 3 * pow(x,2.0) - 8);
};
}
I get an error on this line:
printf("\nRoot found at x = %.7f , midpoint of [%.7f, %.7f] ", x_mid, x_leftx_right
saying that x_leftx_right is undeclared.
If I change this to x_left, x_right then it compiles OK except for "undefined reference to g" and "undefined reference to h".
The reason for the undefined reference to g is that you never provided a function definition for the function g that was prototyped by double g(double);. You did provide a nested function g within bisect. Nested functions are a non-standard extension, and bisect::g is a different function to g. Similarly for h.
To fix this, move the definitions of g and h to be after the end of the bisect function; instead of inside that function.
The reason for your "control reaches end of non-void function" warning is probably because there is no return statement after the while loop.
Your line return ((x_left + x_right)/2.0); line is within the loop begun by while (fabs(x_right - x_left) > epsilon && !root_found) {. If this loop finishes by the loop condition no longer being true, then the execution hits the end of the function without returning anything.
NB. If you indent your code properly so that you line up { then you are less likely to have this sort of problem. Your editor should have a key that you can use to find matching curly-braces. Also, operating your compiler in strict standard mode would have given an error about the use of nested function.
`

I am running into "error: expected expression before ‘Matrix’ " in C and can't figure out what is wrong

I am trying to add the Matrix*partiklar into my update_boids function so that I can use the values that I have saved in a struct earlier in the main function. I am bashing my head against this error as I can’t understand where it’s coming from. Could you please help me understand how to solve this?
I am quite new to C and am using gcc with the SDL library installed on my machine for the graphics.
I get this compiler error:
main.c: In function ‘main’:
main.c:123:20: error: expected expression before ‘Matrix’
make: *** [main] Error 1
Which points to these lines of code:
update_boids(Matrix *partiklar);
With Matrix defined as follows:
typedef struct Matrix
{
double MatX;
double MatY;
double MatZ;
} Matrix;
And partiklar like:
Matrix partiklar[NR_BIRDS];
Matrix hastighet[NR_BIRDS];
Matrix *p[NR_BIRDS];
Matrix *v[NR_BIRDS];
int t = 0;
while(t<NR_BIRDS)
{
partiklar[t].MatX = rand()%100;
partiklar[t].MatY = rand()%100;
partiklar[t].MatZ = rand()%100;
p[t] = &partiklar[t];
hastighet[t].MatX = rand()%10;
hastighet[t].MatY = rand()%10;
hastighet[t].MatZ = rand()%10;
v[t] = &hastighet[t];
/*printf("%f\n", partiklar[t].MatX);
printf("%f\n", partiklar[t].MatY);
printf("%f\n", partiklar[t].MatZ); */
t++;
}
The line the error points to looks like a function prototype with an implicit return type of int. If it is a function CALL, then you need to leave off the type (the compiler already knows it from wherever partiklar was declared).

Resources