473,387 Members | 3,801 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,387 software developers and data experts.

A more efficient loop structure?

wmichaelv
Hi, folks. I just started learning the C programming. I just finished a program to switch the 100 digit and 1 digit of a float number with the least amount of the two.

This is my program:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. /**
  4.  * Function Prototypes
  5.  */
  6.  
  7. int modifyIntegralPartMichaelW();
  8.  
  9. int main() {
  10.  
  11.   double z;
  12.  
  13.   printf("\nTest #1:\n\n"
  14.          "  Calling modifyIntegralPartMichaelW() --\n");
  15.  
  16.   modifyIntegralPartMichaelW();
  17.  
  18.   printf("\nTest #2:\n\n"
  19.          "  Calling modifyIntegralPartMichaelW() --\n");
  20.  
  21.   modifyIntegralPartMichaelW();
  22.  
  23.   return 0;
  24. }
  25.  
  26. /**
  27.  * Function Definition
  28.  */
  29.  
  30. int modifyIntegralPartMichaelW() {
  31.  
  32.   int v;
  33.   int w;
  34.   int x;
  35.   int y;
  36.   double z;
  37.  
  38.   printf("    Enter a floating-point: ");
  39.   scanf("%lf", &z);
  40.  
  41.   w = z;
  42.   x = w %10;
  43.   v = x;
  44.   y = w /100 %10;
  45.   w = y;
  46.   x = x * x;
  47.   y = y * y;
  48.  
  49.   if (x > y) {
  50.     z = z - v + w;
  51.   } else {
  52.       z = z - w * 100 + v *100;
  53.   }
  54.  
  55.   printf("\n  Printing in main() : modified value : %.4lf\n\n", z);
  56.  
  57.   return (z);
  58. }
Been browsing through the net for couple days, and stuck at "nested if" loop. The program is done, and proved to be working well. Just need to polish the appearance a bit, especially that loop. Any guidance is much appreciated.
Jul 3 '13 #1
6 1495
weaknessforcats
9,208 Expert Mod 8TB
You can't do this with a float.

You had to assign the float to an int, which you can't do since int has no decimal portion. This gives you an int that will not convert back to the original float.

The first thing to do is stick with int only.

However, now it is binary and not decimal. That is, there is no 100 digit to switch with a 1 digit.
Jul 3 '13 #2
Thank you for the input. I'll start modifying the code right away.
Jul 3 '13 #3
Thanks, weaknessforcats(?).

I realized that I couldn't take the 100 digit and 1 digit when the input is in float, thus I convert the input into int using a function.

In that function, the program then take the 100 digit and 1 digit of the intInput; compare them, and do some calculation with the float input so that the float output shows the desired result, which is changing the 100 digit and 1 digit with the smaller of the two.

I have finished modifying the code, changing the variables with better nicknames, and having some fun with the return function. Is there a better structure for this code?

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. /**
  4.  * Function Prototypes
  5.  */
  6.  
  7. double modifyIntegralPartMichaelW();
  8.  
  9. int main() {
  10.  
  11.   double doubleInput;
  12.   double result;
  13.  
  14.   printf("\nTest #1:\n\n"
  15.          "  Calling modifyIntegralPartMichaelW() --\n");
  16.   printf("    Enter a floating-point: ");
  17.   scanf("%lf", &doubleInput);
  18.  
  19.   result = modifyIntegralPartMichaelW(doubleInput);
  20.  
  21.   printf("\n  Printing in main() : modified value : %.4lf\n\n", result);
  22.  
  23.   printf("\nTest #2:\n\n"
  24.          "  Calling modifyIntegralPartMichaelW() --\n");
  25.   printf("    Enter a floating-point: ");
  26.   scanf("%lf", &doubleInput);
  27.  
  28.   result = modifyIntegralPartMichaelW(doubleInput);
  29.  
  30.   printf("\n  Printing in main() : modified value : %.4lf\n\n", result);
  31.  
  32.   return 0;
  33. }
  34.  
  35. /**
  36.  * Function Definition
  37.  */
  38.  
  39. double modifyIntegralPartMichaelW( double functionInput) {
  40.  
  41.   int intInput;
  42.   int digit1;
  43.   int digit100;
  44.   double functionOutput;
  45.  
  46.   intInput = functionInput;
  47.   digit1 = intInput % 10;
  48.   digit100 = intInput / 100 % 10;
  49.  
  50.   if (digit1 * digit1 > digit100 * digit100) {
  51.       functionOutput = functionInput - digit1 + digit100;
  52.   } else {
  53.       functionOutput = functionInput - digit100 * 100 + digit1 *100;
  54.   }
  55.  
  56.   return functionOutput;
  57. }
Jul 3 '13 #4
divideby0
131 128KB
Reading numbers of any type with scanf is risky. You might want to check its return value to make sure your number is valid before you enter your algorithm

Expand|Select|Wrap|Line Numbers
  1. if(scanf("%lf", &doubleInput) == 1)
  2. {
  3.    // proceed with program
  4. }
  5. else
  6. {
  7.    // deal with possible stream error
  8. }
You can bypass the loop altogether by reading user input into a char array - you can keep the whole and fractional parts too.

Expand|Select|Wrap|Line Numbers
  1. char user_input[] = "876.543210";
  2. int whole, fraction;
  3. double output;
  4.  
  5. if((sscanf(user_input, "%d.%d", &whole, &fraction)) == 2)
  6. {
  7.     // algorithm to swap whole part
  8.  
  9.     // build the output
  10.  
  11.     sprintf(user_input, "%d.%d", whole, fraction);
  12.     if((sscanf(user_input, "%lf", &output)) == 1)
  13.         printf("number = %f", output);
  14. }
Jul 4 '13 #5
weaknessforcats
9,208 Expert Mod 8TB
Using a float like it was an array is not going to work. It's not an array and it does rounding so your answers may vary.

If you really need to switch the 100 digit with the 1 digit, then I suggest you convert your float input to a C string, which is a char array. You should be able to use ftoa to convert your double to a string.

Then you look for the decimal point. From there you can find the char in the 1 and 100 position. Make the swap. Then use atof to convert your string back to a float.
Jul 4 '13 #6
Thanks for the great feedbacks!!

I'm in no hurry to modify this, so my update might come a bit too slow, but I'll be sure to experiment with every given method before posting the next code structure.
Jul 5 '13 #7

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

Similar topics

4
by: omission9 | last post by:
Suppose I have a lists of tuples A_LIST= and an int i=1 What is the fastest way in python to get out all the tuples from the list whose first element is equal to i? A have a very large list...
16
by: Daniel Tonks | last post by:
First, please excuse the fact that I'm a complete MySQL newbie. My site used forum software that I wrote myself (in Perl) which, up until now, has used flat files. This worked fine, however...
10
by: Raghavendra Mahuli | last post by:
i need to store a lot of integers(say 10,000) in a datastructure. The constraints are: 1. It should be fast. 2. It should be orderded or sorted. 3.Insterting and deleting should be fast. 4. The...
10
by: Duncan M Gunn | last post by:
Hi, I need to store the following matrix of values: T U V A 2 1 1 B 2 - - C - - 2 Where (-) is an empty cell.
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
5
by: B. Chernick | last post by:
Is this a bug or just bad programming? I've never encountered this problem before. (Bare minimum sample form to illustrate.) I've also tried this with a class. Same result. The error is: For...
7
by: ayesha | last post by:
hi i am beginner in java and i need help with programining ,Please if you could suggest me some sites where i can see some solved examples of codes as well as i need code for following ********...
3
by: ajayraj | last post by:
I found that the only difference between the class and structure in C++ is :- 1. class have 'private' as default access specifier while structure have 'public' as default access specifier, but...
1
by: SAIRAAM | last post by:
hi all I am right now in a work of enhancing a project which is already availiable. Its a log file analyser project.in that the existing system takes single log file as input.. my target...
4
by: damjanovic | last post by:
Hi all, I've got some code in a form (frmTimer) which checks another form (frmOrders) every 2 seconds to see if any new records have been added. If yes, then a MsgBox pops up on the admin's...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.