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

#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]);

Related

cs50 pset1 water.c I am getting error while compiling

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);
}

How to pre-define an array in c?

#include <stdio.h>
#include <math.h>
#define Hey {0.9501, 0.2311, 0.6068, 0.4860, 0.8913, 0.7621, 0.4565, 0.0185, 0.8214, 0.4447, 0.6154, 0.7919, 0.9218, 0.7382, 0.1763, 0.4057, 0.9355, 0.9169, 0.4103, 0.8936, 0.0579, 0.3529, 0.8132, 0.0099, 0.1389, 0.2028, 0.1987, 0.6038, 0.2722, 0.1988, 0.0153, 0.7468, 0.4451, 0.9318, 0.4660, 0.4186, 0.8462, 0.5252, 0.2026, 0.6721, 0.8381, 0.0196, 0.6813, 0.3795, 0.8318, 0.5028, 0.7095, 0.4289, 0.3046, 0.1897, 0.1934, 0.6822, 0.3028, 0.5417, 0.1509, 0.6979, 0.3784, 0.8600, 0.8537, 0.5936, 0.4966, 0.8998, 0.8216, 0.6449, 0.8180, 0.6602, 0.3420, 0.2897, 0.3412, 0.5341, 0.7271, 0.3093, 0.8385, 0.5681, 0.3704, 0.7027, 0.5466, 0.4449, 0.6946, 0.6213, 0.7948, 0.9568, 0.5226, 0.8801, 0.1730, 0.9797, 0.2714, 0.2523, 0.8757, 0.7373, 0.1365, 0.0118, 0.8939, 0.1991, 0.2987, 0.6614, 0.2844, 0.4692, 0.0648,0.9883}
float average(float Hello[]){
int i;
float sum;
for (i=0; i<100;i++) {
sum+= Hello[i];
}
return sum/100;
}
int main(){
printf("%f\n",average(Hey));
//so here the compiler says that expected error without giving me what the error actually is. and i suspect because of the bad definition of the vector that i have at the very beginning of my code.
}
Yeah, so as i stated in my comment the problem is with the c preprocesses as its referred to here in stack overflow. is my predefinition for the array Hey wrong? and why?
Usually this is how to predefine an array in C, omit the size and provide the values in brackets. Eg,
const float Hey[] = {0.9501, 0.2311, 0.6068 };
If you really want to, you can do something like this.
#include <stdio.h>
#include <math.h>
#define HEY_INIT { 0.9501, 0.2311, 0.6068, 0.4860, 0.8913, 0.7621, 0.4565, 0.0185, 0.8214, 0.4447, 0.6154, 0.7919, 0.9218, 0.7382, 0.1763, 0.4057, 0.9355, 0.9169, 0.4103, 0.8936, 0.0579, 0.3529, 0.8132, 0.0099, 0.1389, 0.2028, 0.1987, 0.6038, 0.2722, 0.1988, 0.0153, 0.7468, 0.4451, 0.9318, 0.4660, 0.4186, 0.8462, 0.5252, 0.2026, 0.6721, 0.8381, 0.0196, 0.6813, 0.3795, 0.8318, 0.5028, 0.7095, 0.4289, 0.3046, 0.1897, 0.1934, 0.6822, 0.3028, 0.5417, 0.1509, 0.6979, 0.3784, 0.8600, 0.8537, 0.5936, 0.4966, 0.8998, 0.8216, 0.6449, 0.8180, 0.6602, 0.3420, 0.2897, 0.3412, 0.5341, 0.7271, 0.3093, 0.8385, 0.5681, 0.3704, 0.7027, 0.5466, 0.4449, 0.6946, 0.6213, 0.7948, 0.9568, 0.5226, 0.8801, 0.1730, 0.9797, 0.2714, 0.2523, 0.8757, 0.7373, 0.1365, 0.0118, 0.8939, 0.1991, 0.2987, 0.6614, 0.2844, 0.4692, 0.0648, 0.9883 }
static const float Hey[] = HEY_INIT; // global array
float average(float Hello[]) {
int i;
float sum;
for (i = 0; i < 100; i++) {
sum += Hello[i];
}
return sum / 100;
}
int main(void) {
printf("%f\n", average(Hey));
return 0;
}
example: an array containing 4 integer values of type int called abc could be represented as: int abc [4];
by default, regular arrays of local scope (in this example, those declared inside the function) are ignored.
But the elements in an array can be explicitly initialized to specific values when it is declared, by enclosing those initial values in braces {}. For example:
int abc [4] = { 1, 2, 47, 50, -371 };
in other case you can let empty the brackets so you can initialize them when the program is running e.g. int abc [] ; or you can use a matrix like :int abc [] []; with X lines & Y columns.

sscanf statement stops the program

#include <stdio.h>
#include <stdlib.h>
struct urunler {
int kod;
char Ad[16];
int stok;
float fiyat;
};
void urunTara(struct urunler* inputs,int *amount);
int main()
{
struct urunler Urun[50];
int amount = 0;
urunTara(Urun,&amount);
}
void urunTara(struct urunler *inputs,int *amount){
char Temp[150];
FILE *fPtr;
fPtr = fopen("urunler.txt","r");
if(fPtr == NULL){
printf("File not found!");
} else {
while(!feof(fPtr)){
fgets(Temp,100,fPtr);
sscanf(Temp,"%d %s %d %f",&(inputs[*amount].kod),inputs[*amount].Ad,&(inputs[*amount].stok),&(inputs[*amount].fiyat));
*amount++;
}
}
};
I am relatively new to C, and just started learning about structs. The text file contains these:
25 televizyon 1000 150.25
40 video 500 25.45
50 plazma 75 2300.50
76 dvd 20000 90.00
85 supurge 700 110.75
90 buzluk 250 10.00
95 teyp 1250 8.99
The problem i have here is with the sscanf. When i do all these inside the main function, it works great. However when i try to do it in the function urunTara something goes wrong with the sscanf statement and the program stops working. I successfully passed values to &(inputs[*amount].kod) and other adresses by using scanf. But can't understand what's the problem with this sscanf statement.
*amount++;
is similar to
*(amount++);
which means dereference amount and then increment amount to the element after that. Which is not correct.
Change *amount++; to (*amount)++;, which will increment the dereferenced value.
Refer this and this for more details

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.
`

Variable value changes for no apparent reason

In my code I define a step size "h" before a while loop. Somehow it seems to change by itself when I try to use it in the loop. If I define it inside the loop it seems to be ok, but the data I get doesn't seem to be right so I'm guessing the problem might be related.
Even when I print it at this location (see the printf in the code) the output is 3 values and I have no idea why. If you see anything else that's not related but seems wrong please tell me, as I said I'm getting unexpected values (it may just be my formulas).
int main()
{
FILE *f1;
f1 = fopen("Question2 part 3 solution.txt", "w");
double r0=0.05;
double dr0=-a*a*r0/sqrt(1+pow(a*a*r0,2)),h=0.01;
double k[4][3],x[]={dr0,z0,T0,r0},x1[]={0,0,0,0}, s=0;
int i,j;
while(s<=1)
{
//Runge-Kutta
for (j=0;j<4;j++)
{
for (i=0;i<4;i++)
{
if (j==0){k[i][0]=h*System(i,x[0],x[1],x[2],x[3]);}
if (j==1){k[i][1]=h*System(i,x[0]+k[0][0]/2.0,x[1]+k[1][0]/2.0,x[2]+k[2][0]/2.0,x[3]+k[3][0]/2.0);}
if (j==2){k[i][2]=h*System(i,x[0]+k[0][1]/2.0,x[1]+k[1][1]/2.0,x[2]+k[2][1]/2.0,x[3]+k[3][1]/2.0);}
if (j==3){k[i][3]=h*System(i,x[0]+k[0][2],x[1]+k[1][2],x[2]+k[2][2],x[3]+k[3][2]);}
}
}
for (i=0;i<4;i++)
{
x[i]=x[i]+(k[i][0]+2.0*k[i][1]+2.0*k[i][2]+k[i][3])/6.0;
}
printf("%8.3lf",h);
s+=h;
}
fclose(f1);
return(0);
}
double System(int i,double dr, double z, double T, double r)
{
//printf("%e\t%e\t%e\t%e\n",dr,z,T,r);
if (T==T0 && z==z0 && i==0) {return (-a*a*dr)*pow(1-dr*dr,3/2)/2.0;}
if (i==0 && T!=0){return (-a*a*r*(1-dr*dr)-dr*sqrt(1-dr*dr))/T;}
if (i==1){return (-sqrt(1-dr*dr));}
if (i==2){return (-a*a*r*dr+sqrt(1-dr*dr));}
if (i==3){return (dr);}
//if (i==3){return (-m2*l1*l2*B*theta1dt*theta2dt*sin(theta2-theta1)-l2*m2*g*B*sin(theta2));}
}
Thanks in advance!
See the declaration of k:
double k[4][3]
And then see this statement
k[i][3]=...
Here you write beyond the boundaries of the array, leading to undefined behavior.
You are overrunning the memory, variable k is defined as double k[4][3], but you are updating k[i][3]when j==3

Resources