Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Why this doesn't work (http://www.velocityreviews.com/forums/t741182-why-this-doesnt-work.html)

Milutin_Popovski 12-31-2010 09:41 AM

Why this doesn't work
 
//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);
}

luser- -droog 12-31-2010 10:12 AM

Re: Why this doesn't work
 
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;
}

luser- -droog 12-31-2010 10:16 AM

Re: Why this doesn't work
 
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;
>
> }
>
>



August Karlstrom 12-31-2010 10:30 AM

Re: Why this doesn't work
 
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

Ike Naar 12-31-2010 11:07 AM

Re: Why this doesn't work
 
On 2010-12-31, Milutin_Popovski <milutin.popovski@gmail.com> 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.

Ulrich Bellgardt 12-31-2010 11:28 AM

Re: Why this doesn't work
 
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

Ike Naar 12-31-2010 11:42 AM

Re: Why this doesn't work
 
On 2010-12-31, Milutin_Popovski <milutin.popovski@gmail.com> 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.

BartC 12-31-2010 12:23 PM

Re: Why this doesn't work
 
"Milutin_Popovski" <milutin.popovski@gmail.com> wrote in message
news:f38cea35-de0d-41f2-a83e-b0af9913e410@n10g2000yqd.googlegroups.com...
> //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


Ben Pfaff 12-31-2010 06:15 PM

Re: Why this doesn't work
 
cri@tiac.net (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

James Waldby 12-31-2010 06:41 PM

Re: Why this doesn't work
 
On Fri, 31 Dec 2010 10:15:27 -0800, Ben Pfaff wrote:
> cri@tiac.net (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


All times are GMT. The time now is 11:46 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


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