473,385 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Looping issues...

10
Hello, so I was working this out yesterday and I am working on a function (w/o using arrays) that moves through the digits of a user inputted number and finds max/min ...this is the max option and it works ( up to < 10000), but I was wondering if there is a better format for looping this ...and if so where should I begin to look?
Expand|Select|Wrap|Line Numbers
  1. int FindDigitAtPosition ( int x, double counter )
  2. {
  3.     for ( i = 1; i <= counter; ++i )
  4.     { 
  5.         if ( x < 10)
  6.         {
  7.             x = x % 10;
  8.  
  9.             if ( digit <= x )
  10.             {
  11.                 digit = x;
  12.             }
  13.         }
  14.         if ( x < 100 && x > 99 )
  15.         {
  16.             j = x % 10;
  17.             x = x / 10;
  18.             if ( digit < j )
  19.             {
  20.                 digit = j;
  21.                 if ( digit < x )
  22.                 {    
  23.                 digit = x;
  24.                 }
  25.             }
  26.         }
  27.         if ( x < 1000 && x >= 100 )
  28.         {
  29.             j = x % 100;
  30.             j = j % 10;
  31.             if ( digit < j)
  32.             {
  33.                 digit = j;
  34.             }
  35.             j = x / 100;
  36.             j = j % 10;
  37.             if ( digit < j )
  38.             {
  39.                 digit = j;
  40.             }
  41.             j = x / 10;
  42.             j = j % 10;
  43.             if ( digit < j )
  44.             {
  45.                 digit = j;
  46.             }
  47.  
  48.         }
  49.         if ( x < 10000 && x >= 1000 )
  50.         {
  51.             j = x / 1000;
  52.             if ( digit < j)
  53.             {
  54.                 digit = j;
  55.             }
  56.             j = x / 100;
  57.             j = j % 10;
  58.             if ( digit < j )
  59.             {
  60.                 digit = j;
  61.             }
  62.             j = x / 10;
  63.             j = j % 10;
  64.             if ( digit < j )
  65.             {
  66.                 digit = j;
  67.             }
  68.             j = x % 10;
  69.             if ( digit < j )
  70.             {
  71.                 digit = j;
  72.             }
  73.         }
  74.  
  75.     }
  76.  
  77. return digit;
  78. }
  79.  
Any suggestions would be greatly appriciated!!!

Thanks
Feb 29 '08 #1
1 1272
sicarie
4,677 Expert Mod 4TB
Hello, so I was working this out yesterday and I am working on a function (w/o using arrays) that moves through the digits of a user inputted number and finds max/min ...this is the max option and it works ( up to < 10000), but I was wondering if there is a better format for looping this ...and if so where should I begin to look?
I don't see how it works anywhere in the following:
Expand|Select|Wrap|Line Numbers
  1.         if ( x < 1000 && x >= 100 )
  2.         {
  3.             j = x % 100;
  4.             j = j % 10;
  5.             if ( digit < j)
  6.             {
  7.                 digit = j;
  8.             }
  9.             j = x / 100;
  10.             j = j % 10;
  11.             if ( digit < j )
  12.             {
  13.                 digit = j;
  14.             }
  15.             j = x / 10;
  16.             j = j % 10;
  17.             if ( digit < j )
  18.             {
  19.                 digit = j;
  20.             }
  21.  
  22.         }
  23.         if ( x < 10000 && x >= 1000 )
  24.         {
  25.             j = x / 1000;
  26.             if ( digit < j)
  27.             {
  28.                 digit = j;
  29.             }
  30.             j = x / 100;
  31.             j = j % 10;
  32.             if ( digit < j )
  33.             {
  34.                 digit = j;
  35.             }
  36.             j = x / 10;
  37.             j = j % 10;
  38.             if ( digit < j )
  39.             {
  40.                 digit = j;
  41.             }
  42.             j = x % 10;
  43.             if ( digit < j )
  44.             {
  45.                 digit = j;
  46.             }
  47.         }
  48.  
  49.     }
  50.  
  51. return digit;
  52. }
  53.  
At no point do you save the values of x that you are modifying, in short - x never changes, is the same value as when it entered the loop, and will never exit the loop.

I would also recommend writing an function for any part of that series that is repeated - it seems like you do the if digit < j ... part a lot, that could be a function you call instead of 4 lines each time you want to do it.
Feb 29 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

45
by: Trevor Best | last post by:
I did a test once using a looping variable, first dimmed as Integer, then as Long. I found the Integer was quicker at looping. I knew this to be true back in the 16 bit days where the CPU's (80286)...
5
by: masood.iqbal | last post by:
My simplistic mind tells me that having local variables within looping constructs is a bad idea. The reason is that these variables are created during the beginning of an iteration and deleted at...
1
by: Diva | last post by:
Hi, I have a data grid in my application. It has 20 rows and I have set the page size as 5. I have a Submit button on my form and when I click on Submit, I need to loop through the rows in the...
2
by: randy1200 | last post by:
My hope is that somebody has a thought on this, or can point to toward an article that's useful. I have the following: DataSet ds = new DataSet(); ds.ReadXml("MyData.xml"); Each MainTable...
4
by: musosdev | last post by:
Hi I need to extend the contents of the src element of the <link relcss tags in the <headsection of my masterpage. From code, how can I access the <link elements within the <head? Cheers
0
by: anthon | last post by:
Hi all - first post! anywho; I need to create a function for speeding up and down a looping clip. imagine a rotating object, triggered by an action, and slowly decreasing in speed, till it...
20
by: Ifoel | last post by:
Hi all, Sorry im beginer in vb. I want making programm looping character or number. Just say i have numbers from 100 to 10000. just sample: Private Sub Timer1_Timer() if check1.value= 1...
4
by: wbosw | last post by:
I'm trying to take a word (stEAdy) and find the index positions of the uppercase characters(2,3) in the word. I have them stored in an array (positionarray). Then I reverse the word and set all...
2
by: Davaa | last post by:
Dear all, I am a student making a MS Form application in C++. I would ask a question about "Timer". Sample code which I am developing is below. private: System::Void...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.