this program is running, but the problem is., the function that I called didnt give me the accurate answer.. if you looki into my source.. what was the problem?? is the formula?? or the wrong way of calling functon?? or the whole source of my program.?

this program should able to compute the the Grades in averaging., then it will give the Equivalent..

thank you for your positive reply...

^_^

GRADINGS.CPP

int grade_average(int a,int b,int c)
{
int final_grade;

final_grade=a+b+c/3;

return final_grade;
}

The above function isn't working correctly. ( grade_average(5,5,5) == 11 in your version)

int grade_average(int a,int b,int c)
{
   return (a+b+c)/3;
}
Member Avatar for iamthwee

hmm your function returns and int when it should return a float or double

I reckon final_grade=(a+b+c)/3; should have brackets around a+b+c as well.

shouldn't your scanf("%d",&Gone); have a little ampersand sign as well?

And you need to work on your indentation.

Don't forget this is c++, so I have to ask the question why are you using c functions etc.

Please don't attach or link to code if it can be more easily embedded in your post. A 70 line program is short enough to fit, and it makes helping you easier.

>didnt give me the accurate answer
This is your problem:

final_grade=a+b+c/3;

You want an average, which means summing all of the items first, then dividing by the number of items. C++'s precedence rules (and C's as well, since this looks like C code) are intuitive in that multiplication and division go before addition and subtraction. You need to surround the addition in parens:

final_grade=(a+b+c)/3;

Your code is actually broken, I'm surprised it works at all.

>scanf("%s",&Subname);
Subname is already a pointer, you don't need to use the address-of operator here.

>scanf("%d",Gone);
>scanf("%d",Gtwo);
>scanf("%d",Gthree);
Gone, Gtwo, and Gthree are all integers, you do need to use the address-of operator here because they're not pointers.

int grade_equivalent(int grade)
{
  float equivalent;
  if(grade>=97&&grade<=100)
    equivalent=1.0;
  if(grade>=94&&grade<=96)
    equivalent=1.25;
  if(grade>=91&&grade<=93)
    equivalent=1.5;
  if(grade>=88&&grade<=90)
    equivalent=1.75;
  if(grade>=85&&grade<=87)
    equivalent=2.0;
  if(grade>=82&&grade<=84)
    equivalent=2.25;
  if(grade>=79&&grade<=81)
    equivalent=2.75;
  if(grade==75)
    equivalent=3.0;
  if(grade>=65&&grade<74)
    equivalent=4.0;
  else
    equivalent=5.0;

  return equivalent;
}

Note that without an intervening else if, an if statement is independent of any other if statements. Any of the previous if statements to the last one will essentially have no effect because functionally, your function looks like this:

int grade_equivalent(int grade)
{
  float equivalent;

  if(grade>=65&&grade<74)
    equivalent=4.0;
  else
    equivalent=5.0;

  return equivalent;
}

In other words, the else only applies to the last if. You can fix that by making an else if chain:

int grade_equivalent(int grade)
{
  float equivalent;
  if(grade>=97&&grade<=100)
    equivalent=1.0;
  else if(grade>=94&&grade<=96)
    equivalent=1.25;
  else if(grade>=91&&grade<=93)
    equivalent=1.5;
  else if(grade>=88&&grade<=90)
    equivalent=1.75;
  else if(grade>=85&&grade<=87)
    equivalent=2.0;
  else if(grade>=82&&grade<=84)
    equivalent=2.25;
  else if(grade>=79&&grade<=81)
    equivalent=2.75;
  else if(grade==75)
    equivalent=3.0;
  else if(grade>=65&&grade<74)
    equivalent=4.0;
  else
    equivalent=5.0;

  return equivalent;
}

how many characters i can input in the memory of Subname?? because whenever I write at least 27 character... it will end my program??

well anyways.. I change my codes into the one you taught.. Thank you for your so much kindness.

How funny: the grade_equivalent function performs a long chain of if-elses to get an exact floating-point equivalent of grade then return integer value! And where is your 1.25, 1.75 and other non-integer values after that?
Moreover, grade_equivalent(75) == 3 but it returns 5 for 76, 77 and 78...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.