Featured Worklog

Price Search



PC Apex Sponsor


PC Apex Sponsors



PC Apex RSS Feeds

RSS Feed for PC Apex Reviews & ArticlesRSS Feed for PC Apex PC Modding WorklogsRSS Feed for the PC Apex Daily DisturbanceRSS Feed for the latest PC Apex Site NewsRSS Feed for PC Apex Affiliate and Web NewsRSS Feed for PC Apex Deals and Steals

Go Back   Apex Community Forums // Other Forums // Designers Corner // Coding / Scripting / Programming

Coding / Scripting / Programming Discussions on all manner of coding and scripting.

Reply
 
LinkBack Thread Tools Display Modes
Old 15-September-05, 07:03 PM   #1 (permalink)
Apex Advanced Techie
NiSm0's Avatar
Default Matlab function help

hey guys heres the problem i have to do...im totally stumped.

%% Write a function called sumDigits() that takes in a number and returns
%% the sum of the digits in the number as a single digit number. Your
%% function should sum the digits in the number, and if that sum is greater
%% than 9, then you should repeat the above process till you end up with a
%% single digit.
%%
%% Note: You MUST use iteration to get credit for this problem.
%%
%% Example:
%% sumDigits(34) should return 7 (since 3+4 = 7)
%% sumDigits(345) should return 3 (since 3+4+5 = 12; 2+1 = 3)
%% You can assume that the input will always be positive.

%% The function header is given below:
%% function digit = sumDigits(num)


and here is what i have so far:

function digit = sumDigits(num)
%sumDigits takes a number and adds all of its digits until
%the sum of all digits is a single digit number.
% ex: 15 = 1 + 5 = 6
while(num > 9)
mod(num, 10);
num = floor(num/10)
end
num

--------------------------------------------------------------------------------------

ok so i know theoretically what needs to happen but i cant execute! What i am trying to do is use mod to get the remainder of the last number...then somehow add that to the digit before it. mabye i could create a vector , remove the last digit from the number somehow and put it in the vector... then get the sum of the vector and reciprocate the process...idk what to do.. help Yikes =)
NiSm0 is offline     Reply With Quote
Old 15-September-05, 08:09 PM   #2 (permalink)
Apex Master Tech Apprentice
mrcracker's Avatar
Default

It's been a long time since I've touched matlab, so I don't remember the functions, but you need to parse the digits and dump them into an array. Then sum the array and put it back in the loop.
mrcracker is offline     Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
producing phage > function of kanamycin username#132 Anything Goes 0 21-January-06 03:00 PM
Usb Boot Function on NF7-S Dr3w Motherboards / CPUs 2 13-July-05 06:52 PM
Slashdot // MATLAB Programming Contest Winner Announced Gizmo Slashdot RSS 0 22-May-05 03:12 PM


All times are GMT -5. The time now is 09:13 PM.


Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0
Copyright PCApex.com, GameApex.com, ForumApex.com 2001 - 2008
Advertisements

Page generated in 0.11908 seconds with 9 queries