thanks for your reply Dkalita, I indeed like your answer.
and i hope you also verify this and guide me rich.
Iam3R 24 Junior Poster
written a code for finding nth last element but no idea how to caluculate complexity.
how can i make this, of 0(n) complexity.
// p is position of element to find
int nthlast(struct node *n, int p) {
struct node *f, *s;
if(n) {
int cnt=0;
f = n;// start address store in first pointer
while(n != NULL && cnt<p-1)
n=n->next, cnt++; // move the the pointer p-1 times
if(!n) /* if the entered poistion is greater than the no of elements */
return -1;
if(p <= 0)
return -2; /* if entered poisition is lessthan or equal to zero */
s = n;
while(n) {
int cnt = 0;
while(n != NULL && cnt < p-1)
n=n->next, cnt++;
if(n) {
f = s;
s = n;
}
else {
int i=0;
while(i<cnt-1) {
i++;
f=f->next;
}
return f->data;
}
if(p==1) {
n=n->next;
s = n;
}
}
return f->data;
}
else
return 0;
}
//Driver code:
int main()
{
struct node*p=NULL;
int n,d,i=0,ele,a[10]={1,2,3,4,5,6,7,8,9,10};
while(i<10)
{
insert(&p,a[i++]);
}
display(p);
printf("nter the pos\n");
scanf("%d",&n);
if((ele = nthlast(p,n)) > 0)
printf("\n%d",ele);
else if(!ele)
printf("list empty\n");
else if(ele == -1)
printf("no of elements less than the pos\n");
else
printf("positio shuold be > 0\n");
return 0;
}
inserting elements function:
void insert(struct node**p,int num)
{
if(*p==NULL)
{
(*p)=malloc(sizeof(struct node));
(*p)->next=NULL;
(*p)->data=num;
}
else
{
insert(&((*p)->next),num);
}
}
Iam3R 24 Junior Poster
Here we go,
my program is compiling and running but a long number is printing and not the age.
you are printing the address of variable
printf("\n You have %.2d years to retirement\n", &retirement_years);
is wrong.
remove '&' before retirement_years in both if and else parts.
change this condition to
while ((idnumber ==000) || (age== -1));
to
while ( (idnumber !=0) || (age != -1) );
the program will terminate when idnumber is equal to 0 or age is -1.
1. no need of using getch().
2. remove conio.h
if you require functions you can use the below.
int get_retirement_age(int age, int idnumber);
int main()
{
int age, idnumber, valid;
do {
printf("enter age and idnumber\n");
scanf("%d\n",&age);
printf("Enter idnumber\n");
scanf("%d\n",&idnumber);
valid = get_retirement_age(age,idnumber) ;
if(valid == 0 )
printf( "Invalid details entered: terminating program\n");
} while(valid);
return 0;
}
int get_retirement_age(int age, int idnumber)
{
int retirement_years;
if(age == -1 || idnumber == 0)
return 0;
if (age >45) {
retirement_years = 65-age;
printf(" you have %d years to retirement\n", retirement_years);
}
else {
retirement_years = 70-age;
printf("You have %d years to retirement\n",retirement_years);
}
return 1;
}
Note : its not compiled or tested just on my head..
Iam3R 24 Junior Poster
I'm currently working with a group of my classmates on a recipe measurement converter, and I'm currently on the most difficult code of the three that is my part of the project. The code is basically for getting the amount from the user, a fraction. I have based my algorithm as reading the fraction as a string and converting it into an int. I haven't got a decent piece of code together yet, I'm still jotting out my algorithm and just keep getting turned around...please if someone can help with any ideas please do so I might can make a start at this!!! Thanks so much! Kat
without code its highly difficult to get the help here (- :)
Iam3R 24 Junior Poster
i have posted this 3 days ago but i dint get any reply.
this time hope some people will reply.
thanks in advance,
Hello ,
i have written the code for finding the middle element of the list with
0(n) complexity.please gurus, verify that and let me know if it needs any modifications
and ofcourse i have question to ask
when the no of elements are odd
we can easily find the middle element
suppose
1 2 3 4 5 the middle is 3
but when the no of elements are even
1 2 3 4 5 6
how can we decide the middle element
is there any most widely used way or experts follow.please let me know
NOTE : in my previous post i have given the insert function you can have a look at it if you need
insertint find_mid(struct node *nrml) { struct node *adv=nrml; while(adv) { if(adv->next !=NULL) { //if(adv->next->next != NULL) adv = adv->next->next ; nrml = nrml -> next; } else { printf("with odd :\n"); return nrml->data; } } printf("with even :\n"); return nrml->data; } Driver code : #define ODD 9 #define EVEN 10 int main() { struct node *podd=NULL; struct node *peven=NULL; int n,d,i=0,ele; int odd[ODD]={1,2,3,4,5,6,7,8,9}; int even[EVEN]={1,2,3,4,5,6,7,8,9,10}; while(i<ODD) { insert(&podd,odd[i++]); } i=0; while(i<EVEN) { insert(&peven,even[i++]); } display(podd); ele = find_mid(podd); printf("\n%d",ele); printf("\n"); display(peven); ele = find_mid(peven); printf("\n%d",ele); return 0; }
Iam3R 24 Junior Poster
Hello ,
i have written the code for finding the middle element of the list with
0(n) complexity.
please gurus, verify that and let me know if it needs any modifications
and ofcourse i have question to ask
when the no of elements are odd
we can easily find the middle element
suppose
1 2 3 4 5 the middle is 3
but when the no of elements are even
1 2 3 4 5 6
how can we decide the middle element
is there any most widely used way or experts follow.
please let me know
NOTE : in my previous post i have given the insert function you can have a look at it if you need
insert
int find_mid(struct node *nrml)
{
struct node *adv=nrml;
while(adv) {
if(adv->next !=NULL) {
//if(adv->next->next != NULL)
adv = adv->next->next ;
nrml = nrml -> next;
}
else {
printf("with odd :\n");
return nrml->data;
}
}
printf("with even :\n");
return nrml->data;
}
Driver code :
#define ODD 9
#define EVEN 10
int main()
{
struct node *podd=NULL;
struct node *peven=NULL;
int n,d,i=0,ele;
int odd[ODD]={1,2,3,4,5,6,7,8,9};
int even[EVEN]={1,2,3,4,5,6,7,8,9,10};
while(i<ODD)
{
insert(&podd,odd[i++]);
}
i=0;
while(i<EVEN)
{
insert(&peven,even[i++]);
}
display(podd);
ele = find_mid(podd);
printf("\n%d",ele);
printf("\n");
display(peven);
ele = find_mid(peven);
printf("\n%d",ele);
return 0;
}
Iam3R 24 Junior Poster
I am very poor in calculating complexities.
Please some one let me know what is the complexity of the below program.
I actually want to write a program for finding the nth last element with 0(n) complexity.
but unable write, please give me some idea.
the below code works perfectly and i considered all the posibilities.
if list empty : returns zero
if postion is < 0 : returns -2
if position is > no of elements in the list : returns -1
please mention if any other possibilities can be covered.
Iam3R 24 Junior Poster
i have read in one of links, there its documented
The job of printw is to update a few flags and data structures and write the data to a buffer corresponding to stdscr. In order to show it on the screen, we need to call refresh() and tell the curses system to dump the contents on the screen.
but i am using following code
int main ()
{
char ch;
initscr();
printw("Enter a char :");
ch=getch();
printw("You Entered '%c' ",ch);
getch();
endwin();
return 0;
}
the code does not have refresh();
but have one getch()
extra
its showing out put on the window.
without that getch()
the program is getting terminated in falsh so we cant observe the out put .
my question is how its printing o/p on window without refresh().
Iam3R 24 Junior Poster
Hello every one,
i have written the following program for testing getch function behaviour.
but, as soon as i run the program i am getting segmentation fault.
i dont understand the problem.
i am using GCC compiler on Linux Environment.
i want to test the key scan program.
#include<ncurses.h>
int main()
{
char ch;
ch=getch();
printf("%c",ch);
return 0;
}
running: ./a.out -lncurses
i have read the manual page of getch, where it is written that
The getch, wgetch, mvgetch and mvwgetch, routines read a character from the window. In no-delay mode, if no input is waiting, the value ERR is returned. In delay mode, the program waits until the system passes text through to the program. Depending on the setting of cbreak, this is after one character (cbreak mode), or after the first newline (nocbreak mode). In half-delay mode, the program waits until a character is typed or the specified timeout has been reached.
1.
The getch, wgetch, mvgetch and mvwgetch, routines read a character from the window.
can't i use getch to get a character from the keyboard.
please, some one give me a small example program on how to use
getch.
Thanks In Advance
Iam3R 24 Junior Poster
if i use this in a program its keep on taking the return key
the program not getting terminated, whats the problem?
scanf("%d\n",&j)