plz give me solution

S

Seebs

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.

-s
 
M

Michael Foukarakis

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

Seebs

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.

-s
 
A

Alan Curry

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

Michael Foukarakis

Not necessarily!


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

Michael Foukarakis

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

Richard Bos

Michael Foukarakis said:
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
 
S

Seebs

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

-s
 
M

Michael Foukarakis

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

Seebs

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.

-s
 
R

Richard Bos

Seebs said:
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
 
O

osmium

Richard Bos said:
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?

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

Seebs

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

-s
 
D

Default User

Seebs said:
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
 
B

bartc

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.)-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*gradetable[grades-'A']; /* assume ascii */
sumi+=hours;
}

return sumip/sumi;
}

int main(void) {
float hours[]={1.5,3,3};
printf("%f\n",cpga(3,hours,"ACB"));

}
 
M

Michael Foukarakis

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


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

Nick Keighley

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top