I need to code a function that takes a number, and if it's not negative or zero (if it's zero then the function quits), it checks if it's digits are rising from left to right (123, for example), and if they are, it prints it.
One problem I have with this is that if I knew the number had only 3 digits then I would divide it by 10, 100, 1000 and check those three numbers, but I don't know how many digits will be in the number the user types in, so I don't know what to do with that.
Can anyone help me with this?
Thanks.
|
Mentor
|
![]() |
| 28Mar2009,03:05 | #2 |
|
There are two ways you could look at this.
(1) If they're rising from left to right then they're descending from right to left. So modulo (%) the number with 10, record that, divide the test number by 10, repeat until the number is zero. (2) Divide by 10, 100, 1000 etc as originally planned, but do it in a loop multiplying the number you divide by, by 10, until that power of 10 is greater than the number. So if you've got 1234, then you stop at 10000. so: x=10 divide num by x x*=10 repeat until x>num |
|
Light Poster
|
|
| 28Mar2009,10:10 | #3 |
|
Thank you very much, xpi0t0s!
|
|
Light Poster
|
|
| 28Mar2009,16:09 | #4 |
|
Here is the code I wrote for the function (I deleted my comments to make the code cleaner):
Code:
int number()
{
int num;
int num_test;
int num_ok = 2;
int mod1, mod2;
int sum = 0;
while(num > 0)
{
printf("Please insert an integer:\n");
fflush(stdin);
scanf("%d",&num);
num_test = num;r
while(num_test > 0 && num_ok != 0)
{
mod1 = num_test%10;
mod2 = (num_test/10)%10;
if(mod2 > mod1);
num_ok = 1
else num_ok = 0;
if(num_test = 0 && num_ok == 1);
sum = sum + num;
}
printf("%d\n", &sum);
}
}
|
|
Light Poster
|
|
| 28Mar2009,17:55 | #5 |
|
hey guys, I like the subject here, I personally had some problems with this stuff
so I done some time looking for a path and I found one! ok what you do is get the number of course (must get it as string for best results) then with an if statement check if it's lower then zero, if it is you start a new array string, and with a for loop you will do this list_num[i] = num; list_num[i].Remove(1); num.Remove(0, 1); that way you will have the array having all the numbers for you, then with a while loop you can check wether the number are increasing or not, you can also switch some number's positions or remove any index or even replace a single number with a high number like 1000 :P anyway I hope I helped you enough, sorry that I didn't just post the code but it's better to use logic so the reader gets better view of what will happen and why, if you can't get it don't hasitate to reply, I will post code then! --- ClayC |
|
Light Poster
|
|
| 28Mar2009,18:29 | #6 |
|
Quote:
Originally Posted by ClayC I think my code does the job okay, though it had a mistake that I fixed (= instead of a == near the end). About the array idea, we aren't allowed to use arrays yet, since we haven't studied them in the course yet (this is the first exercise). |
|
Light Poster
|
|
| 28Mar2009,22:26 | #7 |
|
oh ok, well I tried to help you out
![]() and you haven't done arrays yet and you did the % stuff after the line which I never saw lol, that's kinda confusing cause arrays are variables and variables are first steps to learn a new language lol anyway it's good to know you have a working code for what you wanted, if there's something else you want help with ask me and I will help you out
|
|
Light Poster
|
|
| 28Mar2009,22:59 | #8 |
|
Here is the entire program(with comments):
Code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void rectangle(); //function that will draw a rectangle with dimensions provided by the user
void number(); //function for printing the sum of positive integers whose digits are rising from left to right
void main()
{
int menu = 1; //while this variable is set to 1, the loop that shows the menu continues
int choice = 0; //a variable to store the menu choice the user makes
char exit = 'n'; //stores the choice the user makes in the 3rd menu item
while(menu == 1)
{
while(choice <= 0 || choice > 3) //if the user enters an invalid value, the menu will appear again
{
printf("\nChoose an item from the menu:\n");
printf("1. Draw a rectangle!\n");
printf("2. Calculate a sum!\n");
printf("3. Exit?\n");
fflush(stdin);
scanf("%d", &choice);
}
if(choice == 1) //for each choice, except 3, the appropriate function is executed
rectangle();
if(choice == 2)
number();
if(choice == 3) //this choice is built into the main function
{
printf("Do you really want to quit? Press 'y' or 'Y' to confirm, any other key to cancel");
scanf("%c", exit);
if(exit == 'Y' || exit == 'y')
menu = 0;
}
}
}
void rectangle()
{
int length, width; //dimensions of the rectangle
int dimensions; //variable to check if the coordinates of the symbol are okay
int row, col; //row and column coordinates of the symbol
int symbol_count; //how many “-“ were printed
int row_count; //used to track how many rows were printed in the rectangle
char symbol = 'n'; //the symbol the user chooses to print inside the coordinates
printf("Please enter the length and width of the rectangle, seperated by a space\n"); //requesting the dimensions of the rectangle from the user
scanf("%d %d",&length, &width);
if(length < 0) //checking if the coordinates of the rectangle given by the user are negative
length = -length;
if(width < 0)
width = -width;
dimensions = 0;
while(dimensions == 0) //checking if the coordinates given for the symbol are not negative or too big
{
printf("Please enter the row and column coordinates of a point in the rectangle,\n seperated by a space\n");
scanf("%d %d",&row, &col);
if(row >= 0 && row <= length)
{
if(col >=0 && col <= width)
dimensions = 1;
else
printf("Invalid column coordinates\n");
}
else
printf("Invalid row coordinates\n");
}
printf("Please enter a character to be printed in the rectangle\n"); //taking the character to be printed in the rectangle
fflush(stdin);
scanf("%c", symbol);
symbol_count = 0;
row_count = 0;
while(symbol_count < width*length) //once the symbol_count reaches the dimensions of the rectangle, the loop will break
{
if(symbol_count == col)
if(row_count == row)
printf("%c", symbol);
else if(symbol_count == width)
printf("\n");
else
printf("-");
symbol_count++;
}
}
void number()
{
int num; //this is the original number, which will be printed if it meets the criterion
int num_test; //the number will be working on in the function
int num_ok = 2; //the flag used to mark the number as good for adding to the sum that will be printed
int mod1, mod2; //variables that will be used to compare the digits of the number
int sum = 0; //the sum of "good" numbers, which is set to zero here and will be added to in the function.
while(num > 0) //the loop will continue for as long as the user enters positive numbers
{
printf("Please insert an integer:\n"); //taking the original integer
fflush(stdin);
scanf("%d",&num);
num_test = num; //setting test_number to the value of num, as entered by the user
while(num_test > 0 && num_ok != 0) //this loop will quit if the user enters a non-positive zero or a number with digits that aren't rising
{
mod1 = num_test%10;
mod2 = (num_test/10)%10;
if(mod2 > mod1) //once the user enters a number with digits that aren't rising, the expression in the if statement will be false and num_ok will be set to zero, which will break the loop
num_ok = 1; //if each of the digits passes the test, num_ok will remain equal to 1
else num_ok = 0;
if(num_test == 0 && num_ok == 1); //if the division reached the final stage(num_test is equal to zero, and the num_ok flag is still equal to 1, then the number is valid and will be added to the sum
sum = sum + num;
}
printf("%d\n", sum); //printing the final sum
}
}
Advice, anyone? |
|
Mentor
|
![]() |
| 29Mar2009,23:28 | #9 |
|
I don't know what "the first menu item when entering the character to be printed" means - could you clarify? Could you perhaps give the line that crashes (commented) plus one or two lines each side for context?
e.g. Code:
some code some code some code // this line crashes some code |




if there's something else you want help with ask me and I will help you out