Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Alternate Digits sum
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

09-14-2012, 10:34 AM   #16
johnsfine
LQ Guru

Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep:

Quote:
 Originally Posted by Balvinder87 It is not giving the actual values not even for odd,even positions
We understand that it is not working and why it is not working. I added some detail to my earlier post (after pressing send the first time), so if you read my post too soon, please reread it. Otherwise, you answered too fast to have thought about the things I suggested you think about.

 09-24-2012, 03:05 AM #17 kotanjan LQ Newbie   Registered: Sep 2012 Posts: 1 Rep: This Will Work function AddAlterbnateDigits(num) { int r,i; int nOdd = 0, nEven = 0, nThree = 0; int length=CountDidgets(num); for(i = length; i >= 0; i--, num/=10) { r=num%10; if ( i % 2 == 0 ) { nEven+=r; } else { nOdd+=r; } if ((i) % 3 == 0 ) { nThree+=r; } } printf("odd Sum %d ",nOdd); printf("Even Sum %d ",nEven); printf("Position Three Sum %d ",nThree); return; } int CountDidgets(int val) { int d = 1, c; if (val >= 0) for (c = 10; c <= val; c *= 10) d++; else for (c = -10 ; c >= val; c *= 10) d++; return (c < 0) ? ++d : d; }
 09-24-2012, 04:57 AM #18 konsolebox Senior Member   Registered: Oct 2005 Distribution: Gentoo, Slackware, LFS Posts: 2,248 Blog Entries: 8 Rep: Quite interesting. This is doable in C of course, but just as a proof on concept, we could do it in bash as well: Code: ```#!/bin/bash read -p "Enter an integer: " INT shopt -s extglob if [[ \$INT != +([[:digit:]]) ]]; then echo "That's not an acceptable integer." exit 1 fi SUMS=() for (( I = \${#INT}; I; --I )); do D=\${INT:I - 1:1} if (( I % 2 )); then (( SUMS[1] += D )) else (( SUMS[2] += D )) fi if (( (I % 3) == 0 )); then (( SUMS[3] += D )) fi done echo "\${SUMS[*]}"```
09-24-2012, 07:26 AM   #19
johnsfine
LQ Guru

Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep:
Quote:
 Originally Posted by kotanjan function AddAlterbnateDigits(num)
1) When posting code, please use CODE tags. Reading code the way you just posted it is difficult. This time I didn't bother. Many experts here will never take the extra effort to read code that is posted badly.

2) When answering homework questions, please do not do the homework. There are lots of programmers in this forum for whom these homework assignments are trivial, so no one will be impressed that someone not taking the class can do the homework.
It is usually harder to give helpful information that enables the OP to do his own homework than it is to provide a solution the OP could copy. But that is the goal here.

09-25-2012, 07:49 PM   #20
danielbmartin
Senior Member

Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,562

Rep:
Quote:
 Originally Posted by Balvinder87 For eg: given an integer 158176 your program should add - 1,8,7 which are in the position 0,2,4 of the integer AND - 5,1,6 which are in position 1,3,5 of the integer AND - 8,6 which are in every 3rd place in the integer. So the answer will be 1+8+7=16, 5+1+6=12 and 8+6=14. The output will be 16,12,14
I'm learning awk, so I coded this ...
Code:
```echo 158176  \
|awk -F "" '{for (i=1;i<=NF;i++)
(1==i%2)?
s1=s1+\$i:
s2=s2+\$i}
END {print s1, s2}'```
... which produces this ...
Code:
`16 12`
Okay, that's two of the three desired results. Then I tried to extend the code to generate all three numbers ...
Code:
```echo 158176  \
|awk -F "" '{for (i=1;i<=NF;i++)
(1==i%2)?
s1=s1+\$i:
s2=s2+\$i
(2==i%3)?
s3=s3+\$1}
END {print s1, s2; s3}```
... and get this ...
Code:
```awk: cmd. line:5:     s3=s3+\$1}
awk: cmd. line:5:             ^ syntax error```

Daniel B. Martin

 09-25-2012, 08:06 PM #21 konsolebox Senior Member   Registered: Oct 2005 Distribution: Gentoo, Slackware, LFS Posts: 2,248 Blog Entries: 8 Rep: daniel: Why don't you just use if blocks/statements?
 09-25-2012, 08:38 PM #22 konsolebox Senior Member   Registered: Oct 2005 Distribution: Gentoo, Slackware, LFS Posts: 2,248 Blog Entries: 8 Rep: My version in awk. You might want to refer from this. Code: ```#!/usr/bin/env gawk -f BEGIN { printf "Enter an integer: "; getline number < "/dev/stdin" if (number !~ /^[[:digit:]]+\$/) { printf "That's not an acceptable integer.\n" exit 1 } for (i = length(number); i; --i) { d = int(substr(number, i, 1)) if (i % 2) { sum1 = sum1 + d } else { sum2 = sum2 + d } if ((i % 3) == 0) { sum3 = sum3 + d } } printf "%d %d %d\n", sum1, sum2, sum3 exit(0) }``` 1 members found this post helpful.

 Tags digits, sum

 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 Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post cpthk Linux - Software 1 06-28-2010 05:16 PM btacuso Linux - Newbie 2 05-20-2009 07:34 PM s_b Linux - Newbie 1 10-16-2008 08:16 AM rickenbacherus Programming 7 05-07-2008 06:53 AM gawain Linux - Software 7 02-20-2008 02:13 AM

LinuxQuestions.org

All times are GMT -5. The time now is 01:41 PM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -