|
-
December 8th, 2011, 09:22 PM
#1
Function problem... But i use if else
UNI Quality Assurance department has decided to do Teaching & Learning Evaluation for every lecturer in UNI. In this evaluation, each student will have to key in the grade for the lecturer whether A, B, C or D and the marks for each grade are shown in table 1.
Grade | Mark
A | 4
B | 3
C | 2
D | 1
Table 1
Using a modular programming approached (function by reference), write a program to calculate the average mark that a lecturer had obtained and then display the result. The program also should display the lecturer’s status based on the total marks whereby status for each range of marks is shown in table 2.
Mark | Status
3.5<=average<=100 | Excellence
3.0<=average<3.5 | Good
2.0<=average<3.0 | Average
average<2.0 | Poor
Table 2
Your program should at least have the following functions:-
i. Function Average_Calculation – to calculate average mark
ii. Function Status – to determine the status.
iii. Function Display – display the result
Notes: Assumed the lecturer has M number of students.
Example of output :
UNI
Teaching & Learning Evaluation Report
Name : Natasya
Subject Code : MTS 3013
Average Mark : 3.8
Status : Excellence
So, there is my coding, but i use, loop, and if else, BUT the one suppose is Function...
Code:
#include<iostream.h>
#include<string.h>
int main()
{
char lect_name[30],subject[30];
char grade,status;
int mark;
int numbof_student;
float sum=0;
float average;
cout<<"Enter Your Lecturer name : ";
cin>>lect_name;
cout<<"Enter Your Subject code : ";
cin>>subject;
cout<<"Enter Number Of Student : ";
cin>>numbof_student;
for(int s=0; s<numbof_student; s++)
{
cout<<s+1<<" student enter grade:";
cin>>grade;
if(grade=='A')
{
mark=4;
}
else if(grade=='B')
{
mark=3;
}
else if(grade=='C')
{
mark=2;
}
else if(grade=='D')
{
mark=1;
}
else
{
cout<<"Wrong Input"<<endl;
}
sum=sum+mark;
average=sum/numbof_student;
if ((average>=3.5) &&(average<=4.0))
{
cout<<"Excellence";
}
else if ((average<=3.0) &&(average<=3.5))
{
cout<<"Good";
}
else if ((average<=2.0) &&(average<=3.0))
{
cout<<"Average";
}
else if (average<=2.0)
{
cout<<"Poor";
}
}
cout<<"\n\n\t\tUniversiti Pendidikan Sultan Idris";
cout<<"\n\n\t\tTeaching & Learning Evaluation Report";
cout<<"\n\nName : "<<lect_name<<endl;
cout<<"Subject Code : "<<subject<<endl;
cout<<"Average :"<<average<<endl;
cout<<"Status : "<<status<<endl;
}
i did coding for function, but blablabla... still error and error and then hard i try the simplest coding...
can sumone just convert it to function? mine one cannot use... wanna see? too guilty to post mine that i'd convert it, but the one above is to make u ppl understand...
-
December 9th, 2011, 12:04 AM
#2
Re: Function problem... But i use if else
Does this make sense to you?
Code:
else if ((average<=3.0) &&(average<=3.5))
Shouldn't you be testing if the average is in-between two values? That code above does not do that.
A C++ if() statement does not work the same as a "math" statement. You want to check if the average is between 3.0 and 3.5 (greater than or equal 3.0 and less than or equal 3.5). You make the same mistake in the other if() statements also.
Second:
Code:
#include<iostream.h>
What compiler are you using? This no longer compiles with ANSI C++ compilers, and doesn't compile with any modern Visual C++ compiler (anything 7.1 and above). This is old and outdated.
The correct header is <iostream>, not <iostream.h>. If your book shows <iostream.h>, get another book. If your teacher is teaching you <iostream.h>, get another teacher.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; December 9th, 2011 at 12:07 AM.
-
December 9th, 2011, 01:29 AM
#3
Re: Function problem... But i use if else
There are some issues in your code and the result would not be corrected
Code:
else if ((average<=3.0) &&(average<=3.5))
{
cout<<"Good";
}
else if ((average<=2.0) &&(average<=3.0))
{
cout<<"Average";
this should be >=
And that's right, you must use functions to do this report, your code only works for 1 lecturer, the question ask you reporting for all lecturers (supposed in the beginning, program will ask how many lecturers or at the end of the report, it ask like want to add more lecturer's report ...etc?) and that will show the convenience of using functions
Question before help: Have you learn "structure" ?
-
December 9th, 2011, 05:54 AM
#4
Re: Function problem... But i use if else
I saw u posted this question in several C++ forums, so I'll help you this time. Hope you be better in tomorrow
Code:
#include<iostream>
#include<iomanip>
#include<cstring>
using namespace std;
double average_function(char[], int);
char* status_function(double);
void display_function(char*, char*, double, char*);
int main()
{
char lect_name[30],subject[30];
char *status;
int numbof_student;
double average;
cout<<"Enter Your Lecturer name : ";
cin>>lect_name;
cout<<"Enter Your Subject code : ";
cin>>subject;
cout<<"Enter Number Of Student : ";
cin>>numbof_student;
char *grade;
grade = new char[numbof_student];
for(int s=0; s<numbof_student; s++)
{
cout<<" --Enter grade of sudent "<<s+1<<": ";
cin>>grade[s];
}
cout<<fixed<<showpoint<<setprecision(2);
average = average_function(grade, numbof_student);
status = status_function(average);
display_function(lect_name,subject,average,status);
system("pause");
return 0;
}
double average_function(char *grade, int numbof_student)
{
double mark, sum = 0;
for(int i=0; i<numbof_student; i++)
{
if(grade[i]=='A'|| grade[i] == 'a') mark=4;
else if(grade[i]=='B'|| grade[i] == 'b') mark=3;
else if(grade[i]=='C'|| grade[i] == 'c') mark=2;
else if(grade[i]=='D'|| grade[i] == 'd') mark=1;
sum = sum + mark;
}
return (sum/numbof_student);
}
char* status_function(double average)
{
char* status_temp;
if ((average>=3.5) && (average<=4.0)) status_temp ="Excellence";
else if ((average>=3.0) &&(average<=3.5)) status_temp = "Good";
else if ((average>=2.0) &&(average<=3.0)) status_temp = "Average";
else if (average<=2.0) status_temp = "Poor";
return status_temp;
}
void display_function(char lect_name[30],char subject[30], double number, char *status)
{
cout<<"------------------------------------------"<<endl;
cout<<"\n\n\t Universiti Pendidikan Sultan Idris";
cout<<"\n\t Teaching & Learning Evaluation Report";
cout<<"\n\n Name : "<<lect_name<<endl;
cout<<" Subject Code : "<<subject<<endl;
cout<<" Average : "<<number<<endl;
cout<<" Status : "<<status<<endl<<endl;
}
Output:
Enter Your Lecturer name : Long
Enter Your Subject code : Cs2100
Enter Number Of Student : 5
--Enter grade of sudent 1: A
--Enter grade of sudent 2: B
--Enter grade of sudent 3: C
--Enter grade of sudent 4: D
--Enter grade of sudent 5: B
------------------------------------------
Universiti Pendidikan Sultan Idris
Teaching & Learning Evaluation Report
Name : Long
Subject Code : Cs2100
Average : 2.60
Status : Average
Press any key to continue . . .
-
December 9th, 2011, 10:23 AM
#5
Re: Function problem... But i use if else
Last edited by khelly; December 9th, 2011 at 10:26 AM.
-
December 9th, 2011, 10:25 AM
#6
Re: Function problem... But i use if else
@Paul Mckenzie
yes, tq for that one in-between you had done correction for me...
i do observe ur lesson...
then another one,
"The correct header is <iostream>, not <iostream.h>. If your book shows <iostream.h>, get another book. If your teacher is teaching you <iostream.h>, get another teacher."
its kinda sharp words, but funny too and meaningful...
TQ SO MUCH FOR UR HELP!
@longboodie99
u also did notice my mistake as Paul...
and about ur question, i never did, tthis 2nd time, last semester after long time 10 week attending class, i drop c++, consider to take next year in order to not having bad result.
this semester i try so HARD...
MY PROVE :
THIS IS MY CODING AFTER THAT ABOVE IF ELSE.
I did my own seems i my class i cannot make a friend with anyone, NO ONE IS MY FREN!
HOW I DID?
just learn and apply... (bcoz my lect said the scheme, is same as last sem, so i use it as a guide line even i dont think so, its quite different from last semester...)
Lets see...
Code:
# include <iostream>
# include <string>
# include <cstring>
void getinput(string, string, int, char);
void sum(int);
void average(float);
void status(char);
void display(string, string, float, char);
void main()
{
string name, subject;
char grade, answer;
int mark=0, counter=0, sum=0;
float average=0;
{
do
{
cout<<"Please Insert : "<<endl;
getinput(name,subject,counter,grade);
display(name,subject,average,status);
cout<<"\n\nPlease enter Y to continue and N to stop"<<endl;
cin>>answer;
cin.ignore();
}while(answer=='Y' || 'y');
cout<<"\n\nEnd of program"<<endl;
}
//start function of input
void getinput(string name, string subject, int counter, char grade);
{
cout<<"Enter Your Lecturer name : ";
cin>>name;
cout<<"Enter Your Subject code : ";
cin>>subject;
cout<<"Enter Number Of Student : ";
cin>>counter;
for(int s=0; s<counter; s++)
{
cout<<" Number "<<s+1<<"student enter grade : ";
cin>>grade;
if(grade='A')
{
mark=4;
}
else if(grade='B')
{
mark=3;
}
else if(grade='C')
{
mark=2;
}
else if(grade='D')
{
mark=1;
}
}
}
//start function to calculate
void getinput(int sum);
{
sum=sum+mark;
}
//start function to calculate
void getinput (float average);
{
average=sum/counter;
}
//start function to calculate
void display (char status);
{
if ((average>=3.5) &&(average<=4.0))
{
status="Excellent";
}
else if ((average>=3.0) &&(average<=3.5))
{
status="Good";
}
else if ((average>=2.0) &&(average<=3.0))
{
status="Average";
}
else if (average<=2.0)
{
status="Poor";
}
}
//start function to display
void display(string name, string subject, float average, char status);
{
cout<<"\n\n\tUniversiti Pendidikan Sultan Idris";
cout<<"\n\tTeaching & Learning Evaluation Report";
cout<<"\n\nName : "<<name<<endl;
cout<<"Subject Code : "<<subject<<endl;
cout<<"Average Mark : "<<average<<endl;
cout<<"Status : "<<status<<endl;
}
}
but longboodie's coding is better, i've run, and there is is still error, so i use urs to submit...
wow, boodie, u did notice i post it in several forum, i really apprciate just bcoz u notice it...
plz, boodie, dont think i'm lazy, i just cannot refer ato any fren(seriously, i dont have one..) i had try so much, to get that on my own it take non-stop study...
and also, i did post it on several forum not bcoz to have short-cut solve, i need a refering and several help, not a whole...
but the end, still cannot run... so i use urs boodie to submit...
to me, Function is most difficult, but i cannot bcoz next sem i have to take another one C++,
currently my lect teaching ARRAY, but still im lacking of function...
i came here not to ppl do my assignment, but to help me...
but i will observe boodie's coding bcoz this is not the end, i have my final project C++ assignment just in one week then final exam...
so i cant relax and still go on!...
TQ SO MUCH U GUYS, ESCPECIALLY BOODIE!
-
December 9th, 2011, 03:31 PM
#7
Re: Function problem... But i use if else
Ok, good to hear that. Next time you have problem like this, my suggestion is that, you only ask the part that you having difficult time with. Why? B/c it is shorter, it is simpler to see and easier to do so you can get more helps, no one will code the whole problem for you (I did b/c it was simple and you really need it as a reference for problems later ). You need help about function, ask about a specific function you want then people will help you (even help you code b/c it's only about 10 lines of code), after that, ask other related things...
-
December 9th, 2011, 03:43 PM
#8
Re: Function problem... But i use if else
 Originally Posted by longbodie99
no one will code the whole problem for you (I did b/c it was simple and you really need it as a reference for problems later ).
You should not be giving homework solutions. We don't do homework here, no matter how simple the problem is.
Regards,
Paul McKenzie
-
December 9th, 2011, 05:58 PM
#9
Re: Function problem... But i use if else
 Originally Posted by Paul McKenzie
You should not be giving homework solutions. We don't do homework here, no matter how simple the problem is.
Regards,
Paul McKenzie
You are absolutely right, I totally agreed. Reason I did that b/c, looking at his codes: using "iostream.h", main function with "void main()" and no returned value ...etc..., man, he have no basic idea what a function is, proving to him a completed program with function prototype, function defining and using function will be a reference note, whenever he got issue again, just look at it and figure out, that's easier. And again, my code is base on his first code, just organize it into functions.
Peace!
-
December 9th, 2011, 09:23 PM
#10
Re: Function problem... But i use if else
@boodie & paul...
TQ for ur consideration...
boodie, next time i have a problem, i will ask part that im having difficulty...
ask u consider...
@Paul... and to all ppl...
im here not bcoz to make ppl do my assignment, but bcoz as boodies said, i need it as a reference for problems later...
i came here to study and discuss... but maybe my question is overwhelming...
but plz dont consider im using this as a shortcut...
i have to work on my own... b4 work on my own, i have to my study not just came from a book, lect or my senior... i must study from this forum too.. OK?
anyway TQ ppl...
p/s: boodie, plz, consider me as she/her, im not a boy or even a man..
and u boodie, may i know ur gender? just in case u we are in this forum... i guess u boy, bcoz ur name... and how old are u... that's all...(if ask too much, we should not in this forum... lol)
-
December 9th, 2011, 09:50 PM
#11
Re: Function problem... But i use if else
 Originally Posted by khelly
p/s: boodie, plz, consider me as she/her, im not a boy or even a man..
and u boodie, may i know ur gender? just in case u we are in this forum... i guess u boy, bcoz ur name... and how old are u... that's all...(if ask too much, we should not in this forum... lol)
Please tell me English is not your native language. Generally we try to use real words here.
You're going to have a problem with this line. It doesn't do what you think it does.
}while(answer=='Y' || 'y');
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|