Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

plz give me solution

3 views
Skip to first unread message

wahid

unread,
Jan 10, 2010, 2:22:57 AM1/10/10
to
• Write a program, that will have letter grade as
input of 3 subjects, and their corresponding
credit hour, and calculate the CGPA based on
those three subjects. (Use getch to input letter
grades) The Grade Point Associated with the
letter grades is: A = 4.0, B = 3.5, C = 3.0, D =
2.5, F = 0.0

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

spinoza1111

unread,
Jan 10, 2010, 2:29:51 AM1/10/10
to

We don't do homework assignments here. Instead, we fight each other
about C.

Antoninus Twink

unread,
Jan 10, 2010, 8:19:14 AM1/10/10
to
On 10 Jan 2010 at 7:22, wahid wrote:
> • Write a program, that will have letter grade as input of 3 subjects,
> and their corresponding credit hour, and calculate the CGPA based on
> those three subjects. (Use getch to input letter grades)

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

Thad Smith

unread,
Jan 10, 2010, 10:26:59 PM1/10/10
to

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

pete

unread,
Jan 11, 2010, 5:45:31 PM1/11/10
to

/* 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

Keith Thompson

unread,
Jan 11, 2010, 5:59:08 PM1/11/10
to
pete <pfi...@mindspring.com> writes:
> wahid wrote:
>> • Write a program, that will have letter grade as
>> input of 3 subjects, and their corresponding
>> credit hour, and calculate the CGPA based on
>> those three subjects.
[snip]
>
> /* BEGIN new.c */
>
[...]
>
> /* END new.c */

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"

pete

unread,
Jan 11, 2010, 6:08:26 PM1/11/10
to
Keith Thompson wrote:
> pete <pfi...@mindspring.com> writes:
>
>>wahid wrote:
>>
>>>• Write a program, that will have letter grade as
>>>input of 3 subjects, and their corresponding
>>>credit hour, and calculate the CGPA based on
>>>those three subjects.
>
> [snip]
>
>>/* BEGIN new.c */
>>
>
> [...]
>
>>/* END new.c */
>
>
> pete, just who do you think you're helping?

Maybe pedagogy is on topic at alt.comp.lang.learn.c-c++ ?

--
pete

Keith Thompson

unread,
Jan 11, 2010, 6:21:45 PM1/11/10
to

Certainly, but I'm not aware that helping students cheat is on topic
anywhere.

Antoninus Twink

unread,
Jan 12, 2010, 2:59:14 PM1/12/10
to
On 11 Jan 2010 at 22:45, pete wrote:
> rc = scanf("%c", &letter);

Unfortunately, this doesn't meet the spec, which requried the grade
letter to be read in using getch().

Antoninus Twink

unread,
Jan 12, 2010, 3:00:15 PM1/12/10
to
On 11 Jan 2010 at 23:08, pete wrote:
> Maybe pedagogy is on topic at alt.comp.lang.learn.c-c++ ?

It's certainly frowned upon by the "regulars" here, unfortunately.

pete

unread,
Jan 12, 2010, 8:41:02 PM1/12/10
to

rc = getc(stdin);
if (!feof(stdin)) {
getc(stdin);
}
if (rc != EOF
&& (p = strchr(grades, tolower(rc))) != NULL)
{

--
pete

Richard Heathfield

unread,
Jan 12, 2010, 11:00:24 PM1/12/10
to
pete wrote:
> Antoninus Twink wrote:
>> On 11 Jan 2010 at 22:45, pete wrote:
>>
>>> rc = scanf("%c", &letter);
>>
>>
>> Unfortunately, this doesn't meet the spec, which requried the grade
>> letter to be read in using getch().
>
> rc = getc(stdin);
> if (!feof(stdin)) {
> getc(stdin);

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

Phil Carmody

unread,
Jan 13, 2010, 5:00:35 AM1/13/10
to

Fail.

Phil
--
Any true emperor never needs to wear clothes. -- Devany on r.a.s.f1

jameskuyper

unread,
Jan 14, 2010, 8:41:32 AM1/14/10
to
Keith Thompson wrote:
...

> Certainly, but I'm not aware that helping students cheat is on topic
> anywhere.

Not even alt.cheat.cheat.cheat? ;-)

Lionel Pinkhard

unread,
Jan 19, 2010, 11:11:02 AM1/19/10
to
On 1/13/2010 6:00:34 PM, Phil Carmody wrote:
> pete <pfi...@mindspring.com> writes:
>> Antoninus Twink wrote:
>>> On 11 Jan 2010 at 22:45, pete wrote:
>>>
>>>> rc = scanf("%c", &letter);
>>>
>>>
>>> Unfortunately, this doesn't meet the spec, which requried the grade
>>> letter to be read in using getch().
>>
>> rc = getc(stdin);
>> if (!feof(stdin)) {
>> getc(stdin);
>> }
>> if (rc != EOF
>> && (p = strchr(grades, tolower(rc))) != NULL)
>> {
>
> Fail.
>
> Phil

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

Richard Heathfield

unread,
Jan 19, 2010, 11:41:14 AM1/19/10
to
Lionel Pinkhard wrote:
> On 1/13/2010 6:00:34 PM, Phil Carmody wrote:
>> pete <pfi...@mindspring.com> writes:
>>> Antoninus Twink wrote:
>>>> On 11 Jan 2010 at 22:45, pete wrote:
>>>>
>>>>> rc = scanf("%c", &letter);
>>>>
>>>> Unfortunately, this doesn't meet the spec, which requried the grade
>>>> letter to be read in using getch().
>>> rc = getc(stdin);
>>> if (!feof(stdin)) {
>>> getc(stdin);
>>> }
>>> if (rc != EOF
>>> && (p = strchr(grades, tolower(rc))) != NULL)
>>> {
>> Fail.
>>
>> Phil
>
> 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:

Joe Wright

unread,
Jan 19, 2010, 5:42:00 PM1/19/10
to
Richard Heathfield wrote:
> Lionel Pinkhard wrote:
>> On 1/13/2010 6:00:34 PM, Phil Carmody wrote:
>>> pete <pfi...@mindspring.com> writes:
>>>> Antoninus Twink wrote:
>>>>> On 11 Jan 2010 at 22:45, pete wrote:
>>>>>
>>>>>> rc = scanf("%c", &letter);
>>>>>
>>>>> Unfortunately, this doesn't meet the spec, which requried the grade
>>>>> letter to be read in using getch().
>>>> rc = getc(stdin);
>>>> if (!feof(stdin)) {
>>>> getc(stdin);
>>>> }
>>>> if (rc != EOF
>>>> && (p = strchr(grades, tolower(rc))) != NULL)
>>>> {
>>> Fail.
>>>
>>> Phil
>>
>> 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 (?)

--
Joe Wright
"If you rob Peter to pay Paul you can depend on the support of Paul."

Seebs

unread,
Jan 19, 2010, 6:00:19 PM1/19/10
to
On 2010-01-19, Joe Wright <joeww...@comcast.net> wrote:
> Still not. getc() blocks, getch() does not. I think (?)

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!

Richard Heathfield

unread,
Jan 19, 2010, 6:02:47 PM1/19/10
to
Joe Wright wrote:
> Richard Heathfield wrote:
>> Lionel Pinkhard wrote:

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

Ersek, Laszlo

unread,
Jan 19, 2010, 10:30:03 PM1/19/10
to
In article <slrnhlcec3.al3...@guild.seebs.net>, Seebs <usenet...@seebs.net> writes:
> On 2010-01-19, Joe Wright <joeww...@comcast.net> wrote:
>> Still not. getc() blocks, getch() does not. I think (?)
>
> 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.

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

Lionel Pinkhard

unread,
Jan 20, 2010, 3:07:02 AM1/20/10
to
On 1/20/2010 7:02:47 AM, Richard Heathfield wrote:
> Joe Wright wrote:
>> Richard Heathfield wrote:
>>> Lionel Pinkhard wrote:
>
> <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.
>

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.

James Dow Allen

unread,
Jan 20, 2010, 3:33:19 AM1/20/10
to
On Jan 10, 2:22 pm, wahid <wahid.1...@gmail.com> wrote:
> • Write a program,...
> [snip]

> • Enter Letter grade 1: A

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

Richard Heathfield

unread,
Jan 20, 2010, 5:49:58 AM1/20/10
to
Lionel Pinkhard wrote:
> On 1/20/2010 7:02:47 AM, Richard Heathfield wrote:
>> Joe Wright wrote:
>>> Richard Heathfield wrote:
>>>> Lionel Pinkhard wrote:
>> <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.
>>
>
> 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.

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.

osmium

unread,
Jan 20, 2010, 6:41:48 AM1/20/10
to
"Richard Heathfield" wrote:

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.


Michael Foukarakis

unread,
Jan 20, 2010, 6:50:36 AM1/20/10
to
On Jan 20, 10:07 am, "Lionel Pinkhard" <lio...@breezysoft.com> wrote:
> On 1/20/2010 7:02:47 AM, Richard Heathfield wrote:
>
>
>
> > Joe Wright wrote:
> >> Richard Heathfield wrote:
> >>> Lionel Pinkhard wrote:
>
> > <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.
>
> 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.
>
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.

Richard Heathfield

unread,
Jan 20, 2010, 8:09:16 AM1/20/10
to
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.

<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. :-)

Nick Keighley

unread,
Jan 20, 2010, 9:05:40 AM1/20/10
to
On 10 Jan, 07:22, wahid <wahid.1...@gmail.com> wrote:
> • Write a program, that will have letter grade as
> input of 3 subjects, and their corresponding
> credit hour, and calculate the CGPA based on
> those three subjects. (Use getch to input letter
> grades)  The Grade Point Associated with the
> letter grades is: A  = 4.0, B = 3.5, C = 3.0, D =
> 2.5, F = 0.0
>
> 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

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

Ben Bacarisse

unread,
Jan 20, 2010, 9:08:11 AM1/20/10
to
Richard Heathfield <r...@see.sig.invalid> writes:

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

Andrew Poelstra

unread,
Jan 20, 2010, 10:54:04 AM1/20/10
to
On 2010-01-20, Michael Foukarakis <electr...@gmail.com> wrote:
>> 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.
>>
> 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.

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.


Richard Heathfield

unread,
Jan 20, 2010, 11:56:53 AM1/20/10
to
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,
and therefore they are aware of the nature and purpose of this group
when posting their first article to it.

Default User

unread,
Jan 20, 2010, 2:55:23 PM1/20/10
to
Richard Heathfield wrote:

> 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

Seebs

unread,
Jan 20, 2010, 4:00:53 PM1/20/10
to
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.

Seebs

unread,
Jan 20, 2010, 4:01:41 PM1/20/10
to
On 2010-01-20, Michael Foukarakis <electr...@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.

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.

Michael Foukarakis

unread,
Jan 21, 2010, 1:19:21 AM1/21/10
to
On Jan 20, 11:01 pm, Seebs <usenet-nos...@seebs.net> wrote:

> On 2010-01-20, 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.
>
> 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.. :-)

Seebs

unread,
Jan 21, 2010, 1:39:27 AM1/21/10
to
On 2010-01-21, Michael Foukarakis <electr...@gmail.com> wrote:
> Not only irrelevant, but also uninteresting.

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.

Alan Curry

unread,
Jan 21, 2010, 3:19:00 AM1/21/10
to
In article <slrnhlftkv.cr...@guild.seebs.net>,

Seebs <usenet...@seebs.net> wrote:
>On 2010-01-21, Michael Foukarakis <electr...@gmail.com> wrote:
>
>> 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

Michael Foukarakis

unread,
Jan 21, 2010, 3:28:59 AM1/21/10
to
On Jan 21, 8:39 am, Seebs <usenet-nos...@seebs.net> wrote:

> On 2010-01-21, Michael Foukarakis <electricde...@gmail.com> wrote:
>
> > Not only irrelevant, but also uninteresting.
>
> 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.

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.

Richard Heathfield

unread,
Jan 21, 2010, 5:05:51 AM1/21/10
to
Michael Foukarakis wrote:
<snip>

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

Michael Foukarakis

unread,
Jan 21, 2010, 5:46:29 AM1/21/10
to
On Jan 21, 12:05 pm, Richard Heathfield <r...@see.sig.invalid> wrote:
> > 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?

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.. :(

Richard Heathfield

unread,
Jan 21, 2010, 7:06:05 AM1/21/10
to
Michael Foukarakis wrote:
> 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.

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.

Richard Bos

unread,
Jan 21, 2010, 3:39:50 PM1/21/10
to
Michael Foukarakis <electr...@gmail.com> wrote:

> 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

Seebs

unread,
Jan 21, 2010, 5:17:07 PM1/21/10
to
On 2010-01-21, Alan Curry <pac...@kosh.dhis.org> wrote:
> You have to admit, BSD curses isn't an implementation of ncurses...

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

Message has been deleted

Michael Foukarakis

unread,
Jan 22, 2010, 1:49:00 AM1/22/10
to
On Jan 21, 10:39 pm, ralt...@xs4all.nl (Richard Bos) wrote:
> Thankfully, you _do_ have to worry about that, since I rather doubt that
> the getch() the OP uses is the one in 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. :(

Seebs

unread,
Jan 22, 2010, 3:19:37 AM1/22/10
to
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.

Michael Foukarakis

unread,
Jan 22, 2010, 6:23:33 AM1/22/10
to
On Jan 22, 10:19 am, Seebs <usenet-nos...@seebs.net> wrote:

> ...
> old
> ...

I love this word.

I love the way it signifies I've traveled so far away from 1989.

Richard Bos

unread,
Jan 22, 2010, 9:43:29 AM1/22/10
to
Seebs <usenet...@seebs.net> wrote:

> 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

osmium

unread,
Jan 22, 2010, 10:57:38 AM1/22/10
to
"Richard Bos" wrote:

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.

Seebs

unread,
Jan 22, 2010, 11:10:20 AM1/22/10
to
On 2010-01-22, Richard Bos <ral...@xs4all.nl> wrote:
> Still, doesn't that make it even more _our_ job to teach him that not
> all the world is a Mumbai college?

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

Default User

unread,
Jan 22, 2010, 2:38:51 PM1/22/10
to
Seebs wrote:

> 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

Richard Bos

unread,
Jan 22, 2010, 5:09:01 PM1/22/10
to
Michael Foukarakis <electr...@gmail.com> wrote:

> 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

bartc

unread,
Jan 23, 2010, 5:28:12 PM1/23/10
to
Nick Keighley wrote:
> On 10 Jan, 07:22, wahid <wahid.1...@gmail.com> wrote:
>> • Write a program, that will have letter grade as
>> input of 3 subjects, and their corresponding
>> credit hour, and calculate the CGPA based on
>> those three subjects. (Use getch to input letter
>> grades) The Grade Point Associated with the
>> letter grades is: A = 4.0, B = 3.5, C = 3.0, D =
>> 2.5, F = 0.0
>>
>> CGPA = ( ∑CHi GPi ) / (∑CHi )
>>
>> CH = Credit Hours
>> GP = Grade Point

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

}

Michael Foukarakis

unread,
Jan 24, 2010, 4:59:30 AM1/24/10
to

Maybe the time has finally come to form comp.lang.pseudocode...

Nick Keighley

unread,
Jan 24, 2010, 10:26:49 AM1/24/10
to

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>

bartc

unread,
Jan 24, 2010, 12:12:54 PM1/24/10
to
Nick Keighley wrote:
> On 23 Jan, 22:28, "bartc" <ba...@freeuk.com> wrote:
>> Nick Keighley wrote:
>>> On 10 Jan, 07:22, wahid <wahid.1...@gmail.com> wrote:

>>>> • 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

CarterLeonor24

unread,
Feb 22, 2011, 4:14:40 PM2/22/11
to
freelance writer


Sjouke Burry

unread,
Feb 22, 2011, 5:27:56 PM2/22/11
to
CarterLeonor24 wrote:
> freelance writer
>
>
Take a goose feather, sharpen the thick blunt end,
split that end, immerse in preferably black ink,
and write a story on reasonable quality paper.
0 new messages