Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why this doesn't work

Reply
Thread Tools

Why this doesn't work

 
 
Milutin_Popovski
Guest
Posts: n/a
 
      12-31-2010
//It occures is a number palindrom,
//For example 1221 is plandrom, and 1231 is not...

#include<stdio.h>
#include<malloc.h>

struct lista{
int val;
struct lista *next;
struct lista *prev;
};

int main(int argc , char *argv[]){
int i,j, k, n, m;
struct lista *first, *temp, *temp_prev, *old, *last;
if(argc!=2 || !chk(argv[1])){
printf("\nWrong argument");
return 1;
}
m=atoi(argv[1]);
first=malloc(sizeof(struct lista));
temp=first;
old=first;
temp->next=NULL;
temp->prev=NULL;
old->prev=NULL;
n=m;
while(m>0){
temp->val=m%10;
old=temp;
temp->next=malloc(sizeof(struct lista));
temp=temp->next;
temp->next=NULL;
temp->prev=old;
m=m/10;
}
temp=first;
printf("\n======================\n");
while(temp->next){
printf("%d ", temp->val);
temp=temp->next;
}
temp=temp->prev;
last=temp;
printf("\n======================\n");
while(temp){
printf("%d ", temp->val);
temp=temp->prev;
}
temp=first;
temp_prev=last;
while(temp){
if(temp->val!=temp_prev->val) goto izlaz;
printf("\n<%d><%d>", temp->val, temp_prev->val);
temp=temp->next;
temp_prev=temp_prev->prev;
}
printf("\n%d is palindrom", n);
return 0;
izlaz:
printf("\n%d is not a palindrom", n);

return 0;
}



int chk(char *c){
if(!isdigit(*c) && *c!='\0') return 0;
if(*c=='\0') return 1;
*c++;
chk(c);
}
 
Reply With Quote
 
 
 
 
luser- -droog
Guest
Posts: n/a
 
      12-31-2010
On Dec 31, 3:41*am, Milutin_Popovski <milutin.popov...@gmail.com>
wrote:
> //It occures is a number palindrom,
> //For example 1221 is plandrom, and 1231 is not...
>

[...]
> int main(int argc , char *argv[]){
> * *int i,j, k, n, m;
> * *struct lista *first, *temp, *temp_prev, *old, *last;
> * *if(argc!=2 || !chk(argv[1])){
> * * * printf("\nWrong argument");
> * * * return 1;
> * *}

[...]
> int chk(char *c){
> * *if(!isdigit(*c) && *c!='\0') return 0;
> * *if(*c=='\0') return 1;
> * **c++;
> * *chk(c);
>
> }


This is as far as I read. Is chk() supposed to
check that the char array contains all digits?
if not isdigit *c and *c is not nul?
The more I look at it, the less it makes sense..
How about something like ... [OTOH]

int chk(char *c){
do {
if (!isdigit(*c)) return 0;
} while(*c++);
return 1;
}
 
Reply With Quote
 
 
 
 
luser- -droog
Guest
Posts: n/a
 
      12-31-2010
On Dec 31, 4:12*am, luser- -droog <mijo...@yahoo.com> wrote:

> int chk(char *c){
> * * do {
> * * * * if (!isdigit(*c)) return 0;
> * * } while(*c++);

make that: *++c
> * * return 1;
>
> }
>
>


 
Reply With Quote
 
August Karlstrom
Guest
Posts: n/a
 
      12-31-2010
On 2010-12-31 10:41, Milutin_Popovski wrote:
> //It occures is a number palindrom,
> //For example 1221 is plandrom, and 1231 is not...


As a first step I would fix the code so that it compiles without warnings.

$ gcc -Wall -Wextra -ansi -pedantic test.c
test.c: In function ‘main’:
test.c:13: warning: implicit declaration of function ‘chk’
test.c:17: warning: implicit declaration of function ‘atoi’
test.c:11: warning: unused variable ‘k’
test.c:11: warning: unused variable ‘j’
test.c:11: warning: unused variable ‘i’
test.c: In function ‘chk’:
test.c:66: warning: implicit declaration of function ‘isdigit’
test.c:68: warning: value computed is not used
test.c:70: warning: control reaches end of non-void function


/August

--
The competent programmer is fully aware of the limited size of his own
skull. He therefore approaches his task with full humility, and avoids
clever tricks like the plague. --Edsger Dijkstra
 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      12-31-2010
On 2010-12-31, Milutin_Popovski <> wrote:
> //It occures is a number palindrom,
> //For example 1221 is plandrom, and 1231 is not...


If you write 1231 in base 11 notation, it becomes a1a which _is_ a palindrome.
 
Reply With Quote
 
Ulrich Bellgardt
Guest
Posts: n/a
 
      12-31-2010
On 31.12.2010 10:41, Milutin_Popovski wrote:
> //It occures is a number palindrom,
> //For example 1221 is plandrom, and 1231 is not...
>
> #include<stdio.h>
> #include<malloc.h>
>
> struct lista{
> int val;
> struct lista *next;
> struct lista *prev;
> };
>
> int main(int argc , char *argv[]){
> int i,j, k, n, m;
> struct lista *first, *temp, *temp_prev, *old, *last;
> if(argc!=2 || !chk(argv[1])){
> printf("\nWrong argument");
> return 1;
> }
> m=atoi(argv[1]);
> first=malloc(sizeof(struct lista));
> temp=first;
> old=first;
> temp->next=NULL;
> temp->prev=NULL;
> old->prev=NULL;
> n=m;
> while(m>0){
> temp->val=m%10;
> old=temp;
> temp->next=malloc(sizeof(struct lista));
> temp=temp->next;
> temp->next=NULL;
> temp->prev=old;
> m=m/10;
> }


Now you have allocated one "struct lista" too many, the last
one being empty.

> temp=first;
> printf("\n======================\n");
> while(temp->next){
> printf("%d ", temp->val);
> temp=temp->next;
> }
> temp=temp->prev;
> last=temp;


This "last" struct lista contains the last digit, but it also
has a successor, namely the empty one that has been allocated
above.

> printf("\n======================\n");
> while(temp){
> printf("%d ", temp->val);
> temp=temp->prev;
> }
> temp=first;
> temp_prev=last;


The following loop iterates to the empty, "really last" list
element, but then temp_prev is NULL and the program crashes
while evaluating temp_prev->val.

> while(temp){
> if(temp->val!=temp_prev->val) goto izlaz;
> printf("\n<%d><%d>", temp->val, temp_prev->val);
> temp=temp->next;
> temp_prev=temp_prev->prev;
> }


[... the rest snipped ...]


One fix would be to cleanup the initialisation phase, so that it
allocates only as many list elements as really required. A second,
rather quick and dirty repair would be to replace the while(temp)
condition by while(temp->next).

The code contains more issues, such as missing error handling in the
cases where the malloc() operations fail, or failing to free() the
allocated memory portions when they are not needed anymore.

-Uli
 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      12-31-2010
On 2010-12-31, Milutin_Popovski <> wrote:
> //It occures is a number palindrom,
> //For example 1221 is plandrom, and 1231 is not...
> [code snipped]


Hint: you can check if an integer is a palindrome without converting
it to a string representation (so, working with integers only).
This may lead to a program that is much simpler than what you wrote.
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      12-31-2010
"Milutin_Popovski" <> wrote in message
news:f38cea35-de0d-41f2-a83e-...
> //It occures is a number palindrom,
> //For example 1221 is plandrom, and 1231 is not...
>
> #include<stdio.h>
> #include<malloc.h>
>
> struct lista{
> int val;
> struct lista *next;
> struct lista *prev;
> };
>
> int main(int argc , char *argv[]){
> int i,j, k, n, m;
> struct lista *first, *temp, *temp_prev, *old, *last;


I'm pretty sure you don't need linked lists to find out if an int value is a
palindrome when represented as a decimal.

Use a different method, and it might be easier to debug.

> m=atoi(argv[1]);


(And if the number starts off as a string anyway, you might as well keep it
as one. That's then even easier. And means that 01210 will be palindromic,
but 1210 won't be after converting to a number)

--
Bartc

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      12-31-2010
(Richard Harter) writes:

> Are there any numbers that are not palindromes in any base,
> with the proviso that the palindrome must have at least two
> "digits"? If so, which is the smallest?


I think that 2 is the smallest, because it is not a palindrome in
base 2, and with bases higher than 2 it has only one digit.
--
"Some programming practices beg for errors;
this one is like calling an 800 number
and having errors delivered to your door."
--Steve McConnell
 
Reply With Quote
 
James Waldby
Guest
Posts: n/a
 
      12-31-2010
On Fri, 31 Dec 2010 10:15:27 -0800, Ben Pfaff wrote:
> (Richard Harter) writes:
>
>> Are there any numbers that are not palindromes in any base, with the
>> proviso that the palindrome must have at least two "digits"? If so,
>> which is the smallest?

>
> I think that 2 is the smallest, because it is not a palindrome in base
> 2, and with bases higher than 2 it has only one digit.


But 2 is a palindrome in base 1. Indeed, every number n>1 is
a palindrome in base 1 and in base n-1, so if n>1, n is not a
number that is not a palindrome in any base. However, no number
that is preceded by a minus sign is a palindrome in a
number system with a positive base.

--
jiw
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why why why does function not work Horace Nunley ASP .Net 1 09-27-2006 09:52 PM
Re: WHY, WHY WON'T IT WORK??? Marina ASP .Net 2 06-29-2004 02:40 PM
Why oh why doesn't my data view work? David Prowak ASP .Net 1 01-30-2004 04:19 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57