LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 09-14-2012, 10:34 AM   #16
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197

Quote:
Originally Posted by Balvinder87 View Post
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.
 
Old 09-24-2012, 03:05 AM   #17
kotanjan
LQ Newbie
 
Registered: Sep 2012
Posts: 1

Rep: Reputation: Disabled
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;
}
 
Old 09-24-2012, 04:57 AM   #18
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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[*]}"
 
Old 09-24-2012, 07:26 AM   #19
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by kotanjan View Post
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.
 
Old 09-25-2012, 07:49 PM   #20
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by Balvinder87 View Post
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
awk experts, please advise.

Daniel B. Martin
 
Old 09-25-2012, 08:06 PM   #21
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
daniel: Why don't you just use if blocks/statements?
 
Old 09-25-2012, 08:38 PM   #22
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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.
  


Reply

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get file permission as digits? cpthk Linux - Software 1 06-28-2010 05:16 PM
[SOLVED] How to test for a range of digits? btacuso Linux - Newbie 2 05-20-2009 07:34 PM
I need help finding out last 4 digits s_b Linux - Newbie 1 10-16-2008 08:16 AM
BASH - convert single digits to double digits. rickenbacherus Programming 7 05-07-2008 06:53 AM
grep and digits gawain Linux - Software 7 02-20-2008 02:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration