473,396 Members | 1,921 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,396 software developers and data experts.

Question regarding an assignment...

10
Hello, I'm really new to C++ and this forum, but I have read the "rules" regarding homework and I'm just looking for some guidance. I need to create a function that asks for the user to input an integer, finds the number of digits and digits at position, sum digits, average digits and find max and min from the number. What I am really trying to create is the finddigitsatpositon function. The rest I have either done or understand.

So, the following is what I have...keep in mind the user will have inputted an integer:

Expand|Select|Wrap|Line Numbers
  1. int FindDigitAtPosition ( int x, double counter )
  2. {
  3.        for ( i = 1; i <= counter; ++i )
  4.        {    
  5.     x = x % 10;
  6.  
  7.     if ( max <= x )
  8.     {
  9.                 max = x;
  10.     }
  11.     else
  12.     {
  13.                max = max;
  14.     } 
  15.     cout << x << ":" << max <<endl;
  16.     }
  17.  
  18.     return max;
  19. }
What I cannot seem to understand is the % and loops. What I am wanting to do is create a loop that will loop as many times as my counter (which was a part of my findnumberofdigits function) tell my loop to do so. This part work. The next thing ....I think is that I am wanting to catch the remainder of x % 10 on each loop and compare it to max (which is initialize to 0 ) and keep the larger number. What I know isn't happening is my x only captures the initial remainder from the last digit. Any suggestions???

Thanks so much....

Kay
Feb 29 '08 #1
4 1458
Hello, I'm really new to C++ and this forum, but I have read the "rules" regarding homework and I'm just looking for some guidance. I need to create a function that asks for the user to input an integer, finds the number of digits and digits at position, sum digits, average digits and find max and min from the number. What I am really trying to create is the finddigitsatpositon function. The rest I have either done or understand.

So, the following is what I have...keep in mind the user will have inputted an integer:

int FindDigitAtPosition ( int x, double counter )
{
for ( i = 1; i <= counter; ++i )
{
x = x % 10;

if ( max <= x )
{
max = x;
}
else
{
max = max;
}
cout << x << ":" << max <<endl;
}

return max;
}

What I cannot seem to understand is the % and loops. What I am wanting to do is create a loop that will loop as many times as my counter (which was a part of my findnumberofdigits function) tell my loop to do so. This part work. The next thing ....I think is that I am wanting to catch the remainder of x % 10 on each loop and compare it to max (which is initialize to 0 ) and keep the larger number. What I know isn't happening is my x only captures the initial remainder from the last digit. Any suggestions???

Thanks so much....

Kay
Hi,

I'm not sure what you want to achieve with this function, but if I'm correct you want to split a multi-digit number into one digit numbers and get the largest digit. If that's right, I think you're on the right track.
However you overwrite the input number in each loop with
x = x % 10;
% gives the integer remainder of a division, e.g. from 14 this will give 4, 29 % 10 => 9. You should store the remainder in another variable, and divide your original number with 10 after that.
Another suggestion: if count is the number of digits in the multi-digit number, it doesn't have to be a double (a floating point number), it can be an integer as well (even better, an unsigned integer).
And one more thing: the line
max = max;
should be removed: it's assignment to self (bad thing most of the time), and doesn't do anything anyway.
Good luck with the assignment!
Feb 29 '08 #2
gwenky
10
I agree that my x is just resetting each loop through...so I put in "13" the output would be :

3
3
3

...this is where I'm not sure how to create a variable to capture that digit and continue on...

int FindDigitAtPosition ( int x, double counter )
{
for ( i = 1; i <= counter; ++i )
{
x = x % 10;

if ( max <= x )
{
max = x;
}
cout << x << ":" << max <<endl;
}

return max;
}


would it look something like

while ( i < counter)
{
digit = x % 10;

if ( max <= x )
{
max = x;
}
}
????

Thanks.....
Feb 29 '08 #3
I agree that my x is just resetting each loop through...so I put in "13" the output would be :

3
3
3

...this is where I'm not sure how to create a variable to capture that digit and continue on...

int FindDigitAtPosition ( int x, double counter )
{
for ( i = 1; i <= counter; ++i )
{
x = x % 10;

if ( max <= x )
{
max = x;
}
cout << x << ":" << max <<endl;
}

return max;
}


would it look something like

while ( i < counter)
{
digit = x % 10;

if ( max <= x )
{
max = x;
}
}
????

Thanks.....
Yes, something like that, but declare digit before the while loop (e.g. int digit;) and you should divide x with 10 after you have the digit. Also, you need to increment i somewhere in the loop if you wan't it to finish.
Mar 1 '08 #4
gwenky
10
thanks so much!! it was the x/10 component that I was missing...now it all makes sense!
Mar 2 '08 #5

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

Similar topics

44
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there...
2
by: Arun Prasath | last post by:
Hi all, I have the following question regd pointer typecasting. Is the following type of pointer typecasting valid? #define ALLOC(type,num) ((type *)malloc(sizeof(type)*num)) /*begin...
1
by: rahul8143 | last post by:
hello, In kernel source code there is ip_fragment.c file my question is regarding pointer function and casting for that look at required snippet from that file There is structure defined for...
52
by: junky_fellow | last post by:
char *str1 = "Hello"; char arr1 = { "Hello" }; char arr2 = { 'H', 'e', 'l', 'l', 'o' }; Is it legal to modify str1, arr1 and arr2 ?
9
by: kiran.agashe | last post by:
Hi, Please refer program below: #include <string> #include <cstdio> using namespace std; const char* f(); main() {
17
by: Student | last post by:
Hi All, I have an assignment for my Programming language project to create a compiler that takes a C++ file as input and translate it into the C file. Here I have to take care of inheritance and...
5
by: Remco van Engelen | last post by:
Hello, I have a question regarding the ISO C grammar. The syntax of a direct-declarator reads (section A.2.2, page 413 in my copy; the (R1) is just to 'name' the rule for later reference): ...
1
by: Nathan | last post by:
Hello, I have a programming assignment due for my programming class. Basically, I have to code the program in visual studio..then I have to paste the code and the output into microsoft...
30
by: Logos | last post by:
I have what may be a bug, or may be a misunderstanding on how pass by reference and class inheritance works in PHP. Since I'm relatively new to PHP, I'm hoping for a little outside help to shed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.