CGPA = ( ∑CHi GPi ) / (∑CHi )
CH = Credit Hours
GP = Grade Point
• Enter Letter grade 1: A
• Enter Credit hours: 1.5
• Enter Letter grade 1: C
• Enter Credit hours: 3
• Enter Letter grade 1: B
• Enter Credit hours: 3
• CGPA = 3.40000
We don't do homework assignments here. Instead, we fight each other
about C.
#include <stdio.h>
#include <ctype.h>
#include <curses.h>
#define NSUBJS 3
int main(void)
{
int i, c, rv = 0;
double num = 0, denom = 0, ch, gp;
if(initscr() && cbreak() == OK && noecho() == OK) {
for(i = 0; i < NSUBJS; i++) {
printw("Enter letter grade %d: ", i + 1);
refresh();
for(;;) {
c = toupper(getch());
if(c >= 'A' && c <= 'D' || c == 'F')
break;
}
if(c == 'F')
gp = 0;
else
gp = 4 - .5 * (c - 'A');
printw("%c\n", c);
nocbreak();
echo();
do {
printw("Enter credit hours: ");
refresh();
} while(scanw("%lf", &ch) != 1);
cbreak();
noecho();
num += gp * ch;
denom += ch;
}
if(denom > 0)
printw("CGPA = %f\n", num / denom);
else
printw("Zero credit hours: can't form average\n");
printw("Press any key to exit...");
refresh();
getch();
endwin();
} else
rv = 1;
return rv;
}
There are three values for letter grade 1: A, C, and B. Normal computers
require separate variables for each letter grade. A quantum computer, however,
is able to hold multiple grades at the same time for a single class. You need a
quantum computer for this problem.
--
Thad
/* BEGIN new.c */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SUBJECTS 3
int main(void)
{
int index, rc;
char letter, *p;
double hours;
double total_grade = 0;
double total_hours = 0;
const char grades[] = "fdcba";
for (index = 0; SUBJECTS > index; ++index) {
printf("Enter Letter grade %d: ", index + 1);
fflush(stdout);
rc = scanf("%c", &letter);
if (!feof(stdin)) {
getc(stdin);
}
if (rc == 1
&& (letter = (char)tolower(letter)) != '\0'
&& (p = strchr(grades, letter)) != NULL)
{
printf("Enter Credit hours: ");
fflush(stdout);
rc = scanf("%lf", &hours);
if (!feof(stdin)) {
getc(stdin);
}
if (rc == 1) {
total_hours += hours;
if (p != grades) {
total_grade += (p - grades + 4) * hours / 2;
}
} else {
puts("Try again.");
break;
}
} else {
puts("Try again.");
break;
}
}
if (total_hours != 0) {
printf("CGPA = %f\n", total_grade / total_hours);
}
return 0;
}
/* END new.c */
--
pete
pete, just who do you think you're helping?
--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Maybe pedagogy is on topic at alt.comp.lang.learn.c-c++ ?
--
pete
Certainly, but I'm not aware that helping students cheat is on topic
anywhere.
Unfortunately, this doesn't meet the spec, which requried the grade
letter to be read in using getch().
It's certainly frowned upon by the "regulars" here, unfortunately.
rc = getc(stdin);
if (!feof(stdin)) {
getc(stdin);
}
if (rc != EOF
&& (p = strchr(grades, tolower(rc))) != NULL)
{
--
pete
printf("%s getch\n", strcmp("getc", "getch") == 0 ? "Uses" : "Doesn't use");
You can fix it easily, however, using something like:
int getch(FILE *fp)
{
return getc(fp);
}
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within
Fail.
Phil
--
Any true emperor never needs to wear clothes. -- Devany on r.a.s.f1
Not even alt.cheat.cheat.cheat? ;-)
Solution to the problem: There is no solution.
Why: There is no valid answer containing getch.
--
--------------------------------------------------------------
Professional mobile software development
BreezySoft Limited www.breezysoft.com
--------------------------------------------------------------
Not true. All you need is love, <stdio.h>, and:
--
Joe Wright
"If you rob Peter to pay Paul you can depend on the support of Paul."
The question is hard to answer portably. The classic DOS "getch()"
blocked, but returned as soon as a character was available, while
the default behavior of getc() on most systems is to be line buffered
when on a terminal. However, I think there have been systems or
compilers where getch() existed, but had different semantics.
-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
<snip>
>>> Solution to the problem: There is no solution.
>>> Why: There is no valid answer containing getch.
>>>
>>
>> Not true. All you need is love, <stdio.h>, and:
>>
>> int getch(FILE *fp)
>> {
>> return getc(fp);
>> }
>>
> Still not. getc() blocks, getch() does not. I think (?)
If getc blocks, so does getch. See definition of getch, above.
The point is this: C doesn't define a getch function, but it allows *us*
to define one. So just define one, and you're done.
getch() is an XCURSES function (among others).
http://www.opengroup.org/onlinepubs/007908775/xcurses/getch.html
The above is the SUSv2 link. The SUSv1 isn't available anymore on the
net to my knowledge, but its reference page on getch() mentions this:
CHANGE HISTORY
First released in Issue 2. [...]
Which probably refers to
Issue 2
X/Open Portability Guide, January 1987
I think the assignment is to write a simple Curses program or so (I've
been largely ignoring this thread till now).
The Curses input model:
http://www.opengroup.org/onlinepubs/007908775/xcurses/intov.html#tag_001_005
http://www.opengroup.org/onlinepubs/007908775/xbd/termios.html
Cheers,
lacos
The reason is say there's no solution - yes, we can define getch, but what
should it do? We can't make an assumption that getch is an alias for getc or
getchar or whatever. For all we know, getch could mean anything. A function
known as getch has been available in a few non-standard implementations, but
we don't know which, so using the return getc(fp) idea makes an assumption
that may or may not be accurate.
This looks like your first bug right here!
Can't do this problem without help and you're
expecting an A ?
By the way, I suspect some of the responses posted
may actually be entrants for the contest at:
http://underhanded.xcott.com/
I'd go with one of these! If instructor thinks
you're good enough to win that contest, maybe
you will get an A !
Hope this helps,
James
According to the spec, it should be used for garnering input. So it
could be a wrapper for getc*, fgets, fscanf, whatever.
> For all we know, getch could mean anything.
No, it means exactly what the programmer says it means. It's in user
namespace, so it's up to us what we do with that name, just as it's up
to us what we do with almost every other name.
The one thing we can't do with that name is assume that there is some
well-defined functional purpose attached to it, until /we/ provide that
purpose by writing the getch function.
In the US, if an instructor says use getch() (as in this case), it means
what the *instructor* says it means, not what some student, or his advisor,
or some random programmer says it means. Apparently, the English system is
different, or perhaps you haven't been exposed to it.
Whether the instructor should be teaching an obsolescent, but very useful,
dialect of C is an entirely separate issue. The point of the assignment was
to startle, amaze and perhaps amuse the user when he finally realizes that
input and output are two different things.
We have not the foggiest notion of what the instructor said he would teach,
that information is in the syllabus for the course, which we haven't seen.
By the way, as far as I know it is still legal in most of the world to teach
latin and other obsolete subjects.
<snip>
>>
>>> For all we know, getch could mean anything.
>> No, it means exactly what the programmer says it means. It's in user
>> namespace, so it's up to us what we do with that name, just as it's up to
>> us what we do with almost every other name.
>>
>> The one thing we can't do with that name is assume that there is some
>> well-defined functional purpose attached to it, until /we/ provide that
>> purpose by writing the getch function.
>
> In the US, if an instructor says use getch() (as in this case), it means
> what the *instructor* says it means, not what some student, or his advisor,
> or some random programmer says it means. Apparently, the English system is
> different, or perhaps you haven't been exposed to it.
You're forgetting context. The article was posted in comp.lang.c (only),
so we can reasonably assume that the intended solution relies only upon
the core C language.
<snip>
> We have not the foggiest notion of what the instructor said he would teach,
> that information is in the syllabus for the course, which we haven't seen.
> By the way, as far as I know it is still legal in most of the world to teach
> latin and other obsolete subjects.
Not in comp.lang.c it isn't. :-)
translate this p-code into C (I'll leave you to do the i/o)
;; defines fold
(load "library/srfi-1.scm")
(define (cpga credit-hours grade grade-point)
(/
(fold (lambda (ch g acc) (+ acc (* ch (grade-point g)))) 0
credit-hours grade)
(fold (lambda (ch acc) (+ acc ch)) 0 credit-hours) ))
(define (grade->grade-point grade)
(cond
((eq? grade 'A) 4.0)
((eq? grade 'B) 3.5)
((eq? grade 'C) 3.0)
((eq? grade 'D) 2.5)
((eq? grade 'F) 0.0)
(else (error "bad grade")) ))
(define (test)
(cpga '(1.5 3 3) '(A C B) grade->grade-point) )
typing (test) yields 3.4
> osmium wrote:
>> "Richard Heathfield" wrote:
>>
>>> Lionel Pinkhard wrote:
>
> <snip>
>
>>>
>>>> For all we know, getch could mean anything.
>>> No, it means exactly what the programmer says it means. It's in
>>> user namespace, so it's up to us what we do with that name, just as
>>> it's up to us what we do with almost every other name.
>>>
>>> The one thing we can't do with that name is assume that there is
>>> some well-defined functional purpose attached to it, until /we/
>>> provide that purpose by writing the getch function.
>>
>> In the US, if an instructor says use getch() (as in this case), it
>> means what the *instructor* says it means, not what some student, or
>> his advisor, or some random programmer says it means. Apparently,
>> the English system is different, or perhaps you haven't been exposed
>> to it.
>
> You're forgetting context. The article was posted in comp.lang.c
> (only), so we can reasonably assume that the intended solution relies
> only upon the core C language.
I don't find that assumption reasonable. I can see that it is
reasonable to give such answers, but I can't see how one can infer
that a beginner intended to get such answers.
<snip>
--
Ben.
Ah, but what standard (or what library)? There are more than one
that define getch() but none of them are C standards. This is
where the confusion stems from, though it is rather exagerrated
by some here to make a point about topicality.
>> You're forgetting context. The article was posted in comp.lang.c
>> (only), so we can reasonably assume that the intended solution relies
>> only upon the core C language.
>
> I don't find that assumption reasonable. I can see that it is
> reasonable to give such answers, but I can't see how one can infer
> that a beginner intended to get such answers.
We can infer this from the fact that all newcomers to a Usenet newsgroup
spend six months reading the feed before they post their first article,
and therefore they are aware of the nature and purpose of this group
when posting their first article to it.
> Ben Bacarisse wrote:
> >Richard Heathfield <r...@see.sig.invalid> writes:
> >
> <snip>
>
> > > You're forgetting context. The article was posted in comp.lang.c
> > > (only), so we can reasonably assume that the intended solution
> > > relies only upon the core C language.
> >
> > I don't find that assumption reasonable. I can see that it is
> > reasonable to give such answers, but I can't see how one can infer
> > that a beginner intended to get such answers.
>
> We can infer this from the fact that all newcomers to a Usenet
> newsgroup spend six months reading the feed before they post their
> first article
Six MONTHS! That's . . . amazing. I've never heard anyone suggest that
before. Six weeks, maybe. Let's hope no newbies need assistance in
their current class.
Yes, yes, they could spend time trying to read six months of backlog,
if they have nothing else to do with their time for a few hundred hours.
Regardless of whether you think that they did insufficient research
into the group first, it's still a lot more handy for all concerned to
just instruct them on the topicality, rather than the Richard
Heathfield willful misunderstanding game.
Brian
--
Day 352 of the "no grouchy usenet posts" project
I don't think this is a reasonable assumption. It's certainly not a safe
assumption.
A student new to C, still learning from college courses, is almost certainly
not going to have started out with a clear notion of the boundary between
the C language and a particular implementation's extensions.
Ugh, I hate to go off topic, but:
In several curses implementations I've used:
int main(void) {
int c = getch();
return 0;
}
will dump core.
Not only irrelevant, but also uninteresting.
You need to call initscr() or newterm() before any other function that
deals with windows/terminals. And yes, getch() deals with a window,
it's a macro (iirc) for wgetch().
I also doubt there are implementations of ncurses that are THAT
bad.. :-)
Not necessarily!
> You need to call initscr() or newterm() before any other function that
> deals with windows/terminals.
Exactly -- completely unlike the <conio.h> getch(). And that's sort of
the point -- once you get into the category of functions outside the standard,
you start seeing things where apparently similar programs do wildly
different things. If you think "I need getch(), where can I get it", and
check the man page on a Unix machine, you're quite likely to end up
with a program which dies horribly.
> I also doubt there are implementations of ncurses that are THAT
> bad.. :-)
There may not be now, but the one I used in college just coredumped.
Hmm. Just checked, BSD curses still does that. ncurses doesn't.
You have to admit, BSD curses isn't an implementation of ncurses...
--
Alan Curry
Well, if I were, say, a developer for a C library that does thing X
why would I have any interest in the namespace that some other library
uses? As long as I don't pollute the standard namespace I am perfectly
fine, for two reasons: 1) a library that does thing Y != X probably
wouldn't define functions that do things similar to mine, and 2) if
another framework for thing X defines similarly named functions, the
user can always RTFM. My point is, while confusion may occur inside a
user's brain, (s)he's had it coming.
And anyways, I've always thought in terms of "I want to do stuff like
get a character of input, is there anything that can help me with
that?", but that may well be due to crappy education, experiences and
other vices of my own. :-)
</offtopic> from my part.
>
> Well, if I were, say, a developer for a C library that does thing X
> why would I have any interest in the namespace that some other library
> uses?
Sure. In other words, if you know what you're doing, you know what's
standard and what isn't, and you know your libraries, why would this be
a problem for you? Answer: it wouldn't and isn't.
But I've got some news for you: not everybody knows what they're doing
to a sufficient extent that they can appreciate that the same
widely-used function name might mean different things in different
libraries.
<snip>
> My point is, while confusion may occur inside a
> user's brain, (s)he's had it coming.
Yes, but it doesn't hurt to de-confuse people, does it, by explaining
the kinds of things that can go wrong when one jumps to erroneous
conclusions?
<snip>
Sure, I'm all up for that. I'll even use the proper space for it - the
manuals. Making the API names comprehensible & unique is just good
practice and shows a great deal of good will on any developer's part
(probably because they had good sex the night before). But I'll be
completely honest with you - it is excruciatingly painful having to
explain every single caveat in the greatest detail possible in the
most comprehensive way possible. And in the end, it hasn't prevented
the annoying emails from reaching me.. :(
Yes, it is. But it is sometimes necessary.
> And in the end, it hasn't prevented
> the annoying emails from reaching me.. :(
I'm afraid that's one of the costs of knowing what you're talking about.
> On Jan 20, 10:07=A0am, "Lionel Pinkhard" <lio...@breezysoft.com> wrote:
> > The reason is say there's no solution - yes, we can define getch, but wha=
> > should it do? We can't make an assumption that getch is an alias for getc=
> > getchar or whatever. For all we know, getch could mean anything. A functi=
> > known as getch has been available in a few non-standard implementations, =
> > we don't know which, so using the return getc(fp) idea makes an assumptio=
> > that may or may not be accurate.
> >
> Thankfully, you don't have to worry about all that. getch() is already
> implemented as part of the ncurses library routines, part of most
> standard implementations.
Thankfully, you _do_ have to worry about that, since I rather doubt that
the getch() the OP uses is the one in curses.
Richard
Well, yeah. I just interpreted that as "implementation of curses",
because ncurses is a specific implementation, not a specification.
(ncurses is an implementation of SVR4 curses, rather than original
BSD curses.)
Perhaps a macro implemented in a previous exercise of the OP? Some
obscure derivation of the teacher to piss the students off? A wrapper
around the /dev/getchar character device as implemented by a kernel
module my grandfather wrote, perhaps? Maybe it's not even C - maybe
he's learning the Korn shell. If you had read the thread, you'd know
we've already been through this while you were gone.
If, in case you catch up with the discussion, you have some
alternatives that are more likely candidates for what "the OP uses",
you can provide some acceptable reply.
PS. The imperative really doesn't seem to suit you. Neither do
attempts to recycle irony. :(
The obvious competitor would be the getch() in the old <conio.h>
header/library from DOS C compilers, or a descendant thereof. Unrelated
to curses or ncurses, used very differently.
> ...
> old
> ...
I love this word.
I love the way it signifies I've traveled so far away from 1989.
> On 2010-01-20, Richard Heathfield <r...@see.sig.invalid> wrote:
> > You're forgetting context. The article was posted in comp.lang.c (only),
> > so we can reasonably assume that the intended solution relies only upon
> > the core C language.
>
> I don't think this is a reasonable assumption. It's certainly not a safe
> assumption.
>
> A student new to C, still learning from college courses, is almost certainly
> not going to have started out with a clear notion of the boundary between
> the C language and a particular implementation's extensions.
Especially when he is given assignments like this lot. Probably not even
his teachers know (or care) that C is not Turbo C.
Still, doesn't that make it even more _our_ job to teach him that not
all the world is a Mumbai college?
Richard
Yes, that's a good idea. And the way to do it is simply state that getch()
is not a part of ISO C, and then tell him what ISO is and so on. The way
NOT to do it is some "clever" allusion or obfuscation that only someone who
already knows the subject can pick up on. Another bad way is to tell him
that he can redefine words to suit himself, whenever he encounters some word
he is unfamiliar with. My experience has been that, whatever the field of
study, that leads to trouble downstream.
I agree that is it entirely possible that the instructor did/does not know
or care about the standardization process. Remember that there was a time,
in living memory, that huge numbers of people thought C and Turbo C were
synonyms, the instructor is quite likely from that era.
I tend to think so. I'd rather teach explicitly (by telling him) rather
than implicitly (by giving him advice which, if he follows it, will not
work).
> On 2010-01-22, Michael Foukarakis <electr...@gmail.com> wrote:
> > If, in case you catch up with the discussion, you have some
> > alternatives that are more likely candidates for what "the OP uses",
> > you can provide some acceptable reply.
>
> The obvious competitor would be the getch() in the old <conio.h>
> header/library from DOS C compilers, or a descendant thereof.
> Unrelated to curses or ncurses, used very differently.
The old Turbo C from Borland has become very popular in some parts of
the world. I think because at one point Borland had free downloads of
it from their "museum" site. It seems to be a common teaching
environment in some places.
Brian
--
Day 354 of the "no grouchy usenet posts" project
> On Jan 21, 10:39=A0pm, ralt...@xs4all.nl (Richard Bos) wrote:
> > Michael Foukarakis <electricde...@gmail.com> wrote:
> > > Thankfully, you don't have to worry about all that. getch() is already
> > > implemented as part of the ncurses library routines, part of most
> > > standard implementations.
> >
> > Thankfully, you _do_ have to worry about that, since I rather doubt that
> > the getch() the OP uses is the one in curses.
>
> Alternatives?
Turbo C, what else?
Richard
> translate this p-code into C (I'll leave you to do the i/o)
>
> ;; defines fold
> (load "library/srfi-1.scm")
>
> (define (cpga credit-hours grade grade-point)
> (/
> (fold (lambda (ch g acc) (+ acc (* ch (grade-point g)))) 0
> credit-hours grade)
> (fold (lambda (ch acc) (+ acc ch)) 0 credit-hours) ))
>
> (define (grade->grade-point grade)
> (cond
> ((eq? grade 'A) 4.0)
> ((eq? grade 'B) 3.5)
> ((eq? grade 'C) 3.0)
> ((eq? grade 'D) 2.5)
> ((eq? grade 'F) 0.0)
> (else (error "bad grade")) ))
>
> (define (test)
> (cpga '(1.5 3 3) '(A C B) grade->grade-point) )
> typing (test) yields 3.4
You've got a funny idea of what p-code is supposed to look like. I came up
with this:
println cpga((1.5, 3, 3),"ACB")
function cpga(hours,grades)=
sumip:=sumi:=0
forall i,t in hours do
sumip+:=t*(asc(grades.[i])-64|4.0, 3.5, 3.0, 2.5|0.0)
sumi+:=t
od
return sumip/sumi
end
which agrees with your answer, and it doesn't use a mysterious srfi-1.scm
library. Translating my p-code into C is far less painful too. And also
gives the right answer (I found pete's code gives an answer slightly off).
(I assume the homework deadline has passed).
#include <stdio.h>
float cpga(int n, float* hours, char* grades){
float sumip=0, sumi=0;
int i;
float gradetable[]={4.0,3.5,3.0,2.5,0.0,0.0};
for (i=0; i<n; ++i){
sumip+=hours[i]*gradetable[grades[i]-'A']; /* assume ascii */
sumi+=hours[i];
}
return sumip/sumi;
}
int main(void) {
float hours[]={1.5,3,3};
printf("%f\n",cpga(3,hours,"ACB"));
}
Maybe the time has finally come to form comp.lang.pseudocode...
p-code is supposed to look like something? :-)
Since I once wrote a p-code standard then I suppose the answer is yes!
> I came up with this:
>
> println cpga((1.5, 3, 3),"ACB")
>
> function cpga(hours,grades)=
> sumip:=sumi:=0
> forall i,t in hours do
> sumip+:=t*(asc(grades.[i])-64|4.0, 3.5, 3.0, 2.5|0.0)
> sumi+:=t
> od
> return sumip/sumi
> end
but where's the fun in that? The intent was not to help the person
who was too idle to do his own homework. My code avoided adding
implementation details like for loops and closely modelled the
solution given in the question.
> which agrees with your answer, and it doesn't use a mysterious srfi-1.scm
> library.
a slight implementation specific leakage of my own...
> Translating my p-code into C is far less painful too.
<snip>
>>>> • Write a program, that will have letter grade as
>>>> input of 3 subjects, and their corresponding
>>>> credit hour, and calculate the CGPA based on
>>>> CGPA = ( ∑CHi GPi ) / (∑CHi )
>>> translate this p-code into C (I'll leave you to do the i/o)
>>> (define (cpga credit-hours grade grade-point)
>> You've got a funny idea of what p-code is supposed to look like.
>
> p-code is supposed to look like something? :-)
> Since I once wrote a p-code standard then I suppose the answer is yes!
I thought you meant pseudo-code. And not Lisp (or something that looks
suspiciously like it, probably Scheme if that .scm is a clue).
>> println cpga((1.5, 3, 3),"ACB")
>
> but where's the fun in that? The intent was not to help the person
> who was too idle to do his own homework. My code avoided adding
> implementation details like for loops and closely modelled the
> solution given in the question.
Sometimes pseudo code has to spell out loops and things, as I'm not sure how
well list-ops are 'standardised' in it. Otherwise you might as well just use
the OP's solution as the path from that to C would be simpler than from your
p-code.
CGPA = ( ∑CHi GPi ) / (∑CHi )
--
Bartc