473,796 Members | 2,864 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Looping issues...

10 New Member
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 1292
sicarie
4,677 Recognized Expert Moderator Specialist
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
7482
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) word size was 16 bits same as an integer. Now with a 32 bit CPU I would have expected the long to be faster as it's the same size as the CPU's word size so wouldn't need sawing in half like a magician's assistant to calculate on like an...
5
2419
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 the end of the iteration. Kindly note that I am not talking about dynamically allocating memory within a loop, which is a perfectly valid operation (albeit with a chance of resulting in a memory leak, unless we are careful). The most common...
1
5391
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 datagrid. Using the items collection just gives me the 5 rows that are displayed on the screen. Is there any way of looping through all the rows in the grid?
2
6351
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 entry has subtable relations. Here's my current approach: foreach(DataRow row in ds.Tables.Rows {
4
1249
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
1964
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 reaches a point 0 (compare a hand spinning a fortune wheel). now, this is quite an easy this to achieve, since you just have to set an interval to increase rotation, with a value that decreases over time (on every call), till it finally reaches a...
20
2853
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 then
4
1212
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 characters to lowercase. I've done that. Now, I want to take the reversed word (ydaets) and make the index positions from the array(2,3) uppercase for the reversed word, so that it will read (ydAEts). Below I'm looping thur the array incorrectly. ...
2
2084
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 buttonStart_Click(System::Object^ sender, System::EventArgs^ e){ for (int i=1; i<=10; i++){ String^ strValue; FunctionName(&strValue); int interval=1000; // time...
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9525
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10169
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.