I was writing a function which checks if every digit in a number is odd. I came accross this weird behaviour. Why does the second function return different (incorrect) results, eventhough its basically the same? (implemented in an opposite way)
#include <stdio.h>
int all_odd_1(int n) {
if (n == 0) return 0;
if (n < 0) n = -n;
while (n > 0) {
if (n&1 == 1)
n /= 10;
else
return 0;
}
return 1;
}
int all_odd_2(int n) {
if (n == 0) return 0;
if (n < 0) n = -n;
while (n > 0) {
if (n&1 == 0)
return 0;
else
n /= 10;
}
return 1;
}
int main() {
printf("all_odd_1\n");
printf("%d\n", all_odd_1(-131));
printf("%d\n", all_odd_1(121));
printf("%d\n", all_odd_1(2242));
printf("-----------------\n");
printf("all_odd_2\n");
printf("%d\n", all_odd_2(131));
printf("%d\n", all_odd_2(121));
printf("%d\n", all_odd_2(2242));
return 0;
}
warning: suggest parentheses around comparison in operand of '&'
Well, how about adding them ? Change n&1 to (n&1). Always ask for warnings.
The == operator has higher precedence than the & operator, so your if (n&1 == 0) statement is not doing what you expect!
(and the if (n&1 == 1) statement works only by coincidence that 1 == 1 evaluates to 1 ;)
Operator precedence. n & 1 == 0 is equivalent to n & (1 == 0)
It is a problem related to the order of execution. Try to use if ((n&1) == 0) in all_odd_2 and everything will work.
Related
Here is the code:
#include <stdio.h>
#include <assert.h>
#include <stdbool.h>
// choose(n,m) returns how many ways there are to choose m items from
// a set of n items
// requires: 0 <= m, 0 <= n
int choose(int n, int m)
{
if (m == 0 || m == n)
{
return 1;
} // base case
// recursive step
return (choose(n - 1, m) + choose(n - 1, m - 1)); // continues until we have exhausted the
// number of possibilities to choose from, base case then terminates it
// taken from the assignment sheet
}
// num_divisors_up_to_k(n,k) returns the number of positive divisors
// of n that are less than or equal to k
// requires: 1 <= k <= n
int num_divisors_up_to_k(int n, int k)
{
if (k == 0)
{
return 0;
} // base case
if (n % k == 0) // if divisible by k return 1 and call recursively
{
// call function again moving to next number
return 1 + num_divisors_up_to_k(n, k - 1);
}
else // otherwise return 0 and call recursively
{
return 0 + num_divisors_up_to_k(n, k - 1); // next number
}
}
// is_prime(n) returns true if n is a prime number and false otherwise
// requires: 1 <= n
bool is_prime(int n)
{
if (n < 2) // 2 is the lowest prime number
{
return false;
}
for (int i = 2; (i * i) <= n; i++) // loop through each element
{
if (n % i == 0) // if divisible by i its not prime so return false
{
return false;
}
}
return true; // num is prime
}
// collatz(n) returns the number of steps it takes to reach 1 by
// by repeatedly applying the Collatz mapping on n; prints each
// number in the sequence starting at n
// requires: 1 <= n
int collatz(int n)
{
if (n == 1) // base case
{
return 0;
}
// print n
printf("%d ", n);
// even
if (n % 2 == 0)
{
// adding 1 and calling collatz again with next value
return 1 + collatz(n / 2);
}
else
{
// same thing as even just with the method for odd
return 1 + collatz(3 * n + 1);
}
}
int main()
{
// collatz code
int n;
printf("enter number:\n");
scanf("%d", &n);
while (n == 1)
{
// cant be negative or 0
if (n < 1)
{
printf("wrong input");
}
else
{
// else printing collatz sequence for input
printf("collatz(%d): ", n);
int steps = collatz(n);
// print out number of steps
printf("it took %d steps\n", steps);
}
// next input
printf("enter number: \n");
}
// TESTS CASES
// choose function
assert(choose(2, 1) == 2);
assert(choose(4, 2) == 6);
assert(choose(10, 10) == 1);
assert(choose(8, 2) == 28);
assert(choose(0, 0) == 1);
// num_divisors_up_to_k function
assert(num_divisors_up_to_k(9, 9) == 3);
assert(num_divisors_up_to_k(1, 1) == 1);
assert(num_divisors_up_to_k(100, 100) == 9);
assert(num_divisors_up_to_k(0, 0) == 0);
// is_prime function
assert(is_prime(10) == false);
assert(is_prime(2) == true);
assert(is_prime(7919) == true);
assert(is_prime(479001599) == true);
// collatz
assert(collatz(5) == 5);
assert(collatz(13) == 9);
assert(collatz(1) == 0);
printf("PASSED\n");
}
Code does not work in visual studio code but works in repl.it or any online complier. I pretty much get a runtime error on vscode and am forced to end the program myself. im not very good with the technicality of IDE's and I know this code doesn't work as intended but I can't fix it unless I can properly compile it and see the output. if anyone can help me my discord tag is ansh#1234 or you can reply here. looking forward to making this work and thank you in advance
Here is the output on VSCode:
Here is the output on repl.it:
There is configuration issue inyour Visual Studio Code setup. You should just run the program manually in a shell window by typing:
cd /Users/ansh/Desktop/c
gcc assignment1.c -o assignment1
./assignment1
I need to write a recursive function that returns 1 if digits of a whole number are ascending (left to right), return -1 if descending or return 0 if neither.
My solution attempt returns 0 every time and I know why but I don't know how to get around it.
Here's my code:
#include <stdio.h>
int check_order(int n)
{
if (n % 10 > n / 10 % 10)
{
return check_order(n / 10);
if (n == 0)
{
return 1;
}
}
else if (n % 10 < n / 10 % 10)
{
return check_order(n / 10);
if (n == 0)
{
return -1;
}
}
else
{
return 0;
}
}
int main()
{
int n;
printf("enter a whole number (n > 9):");
scanf_s("%d", &n);
printf("function returned: %d\n", check_order(n));
}
Here's a simple recursion:
int f(int n){
if (n < 10)
return 0;
int dr = n % 10; // rightmost digit
n = n / 10;
int dl = n % 10; // second digit from the right
int curr = dl < dr ? 1 : -1; // current comparison
if (dl == dr) curr = 0; // keep strict order
if (n < 10)
return curr;
return curr == f(n) ? curr : 0; // are the comparisons consistent?
}
Explain your algorithm?
Suppose you use the following:
You are given a number.
You need to turn that number into a sequence of digits.
If you are given a number, you can convert that number to a sequence of digits.
If you are given a sequence of digits, use
that.
Compare each pair of digits -> ascending, descending, or neither.
Combine the results from each pair, sequentially/recursively.
We can use a string to make the digit comparisons easier, and accept very long sequences of digits.
We can use an enum(erated) type to represent the ordering.
How do you combine the results? Define a function that combines the order of two adjacent, overlapping pairs, then you can combine results.
#include <stdio.h>
#include <string.h>
typedef enum { descending=-1, other=0, ascending=1 } order_t;
order_t pair_order(int a, int b) {
if( a < b ) return ascending;
if( a > b ) return descending;
return other;
}
//strict (increasing/decreasing)
order_t strict_order( order_t x, order_t y ) {
if( x == y ) return x;
return other;
}
//monotone (increasing/decreasing)
order_t monotone_order( order_t x, order_t y ) {
if( x == y ) return x;
if( other == x ) return y;
if( other == y ) return x;
return other;
}
order_t check_order( char* p, int remain ) {
//printf("p:%s\n",p); //uncomment to watch progress
if( remain<2 ) return other;
if( remain==2 ) return pair_order(p[0], p[1]);
return strict_order( pair_order(p[0], p[1]), check_order(p+1, remain-1) );
//return monotone_order( pair_order(p[0], p[1]), check_order(p+1, remain-1) );
}
char* order_name[] = {
"descending",
"other",
"ascending"
""
};
int main()
{
char line[666] = "none";
while ( strlen(line) > 0 ) {
printf("enter a number (at least 2 digits):");
fgets(stdin,line,sizeof(line)-1);
if( strlen(line) > 0 && line[strlen(line)-1] == '\n' )
line[strlen(line)-1] = '\0';
order_t order = check_order(line);
printf("function returned: (%d)%s\n", order, order_name[order+1]);
}
}
I think you were started on the right track but need to flesh out your code more. My solution borrows on that of #ChuckCottrill as I like his enum but I don't like that he doesn't play the ball as it lays (i.e. converts to a string instead of dealing with the int.) I also borrow the nice test examples of #ggorlen but I don't like that solution either as it can take multiple passes through the number to figure out the answer when only one pass should be needed:
#include <stdio.h>
typedef enum { descending=-1, other=0, ascending=1 } order_t; // a la #ChuckCottrill
order_t check_order(int n)
{
if (n > 9) {
int right = n % 10;
int left = n / 10 % 10;
if (right > left) {
n /= 10;
if (n > 9) {
return (ascending == check_order(n)) ? ascending : other;
}
return ascending;
}
if (right < left) {
n /= 10;
if (n > 9) {
return (descending == check_order(n)) ? descending : other;
}
return descending;
}
}
return other;
}
int main() { // a la #ggorlen
printf("12345: %d\n", check_order(12345));
printf("54321: %d\n", check_order(54321));
printf("54323: %d\n", check_order(54323));
printf("454321: %d\n", check_order(454321));
printf("1: %d\n", check_order(1));
printf("12: %d\n", check_order(12));
printf("21: %d\n", check_order(21));
}
OUTPUT
> ./a.out
12345: 1
54321: -1
54323: 0
454321: 0
1: 0
12: 1
21: -1
>
A version that works for any length since it takes the string as parameter.
And feeding the recursive function with previous status (ascending or descending) allows for some shorter code and less functions.
int check_order(char *str, int index, int previous) {
char current = str[index]; // char at index
char next = str[index+1]; // char at index+1
if (current == 0 || next == 0) {
return previous; // End of string
}
// Ascending or descending?
int status = next > current ? 1 : (next < current ? -1 : 0);
if (status == 0 || index > 0 && status != previous) {
// If neither -1/1 nor status == previous (while not initial call)
return 0;
}
return check_order(str, index+1, status); // Check from next index
}
The main function must ensure the string is at least 2 chars
int main(int argc, char **argv) {
char *str = *++argv;
// Some optional checks on str here... (like this is a number)
int status = 0; // Default value if string length < 2
if (strlen(str) >= 2) {
status = check_order(str, 0, 0);
}
printf("Check order for %s is %d\n", str, status);
return 0;
}
Code after a return statement like this is unreachable:
return check_order(n / 10);
if (n == 0)
{
return -1;
}
Beyond this, you're on the right track of checking the current digit against the next digit, but I don't see a clear base case (when n < 10, that is, a single digit).
Trying to check ascending and descending in one recursive function is difficult to manage. In particular, communicating state between stack frames and determining which cases are still valid at a given call suggests that the return value is overworked.
To save having to return a struct or use an enum or magic numbers as flags, I'd write two general helper functions, ascending_digits and descending_digits.
#include <stdbool.h>
#include <stdio.h>
bool ascending_digits(int n) {
if (n < 10) return true;
if (n % 10 < n / 10 % 10) return false;
return ascending_digits(n / 10);
}
bool descending_digits(int n) {
if (n < 10) return true;
if (n % 10 > n / 10 % 10) return false;
return descending_digits(n / 10);
}
int check_order(int n) {
if (ascending_digits(n)) return 1;
if (descending_digits(n)) return -1;
return 0;
}
int main() {
printf("12345: %d\n", check_order(12345));
printf("54321: %d\n", check_order(54321));
printf("54323: %d\n", check_order(54323));
printf("454321: %d\n", check_order(454321));
printf("1: %d\n", check_order(1));
printf("12: %d\n", check_order(12));
printf("21: %d\n", check_order(21));
return 0;
}
Output:
12345: 1
54321: -1
54323: 0
454321: 0
1: 1
12: 1
21: -1
Not only are these functions easier to understand and maintain individually, they're also more reusable than if they were inseparably tied together.
This doesn't handle negative numbers--you could apply abs and go from there if you want. Same goes for handling equal values; this implementation accepts numbers such as 1223 but you could use <= to enforce strict ordering.
I'm a first time programmer trying to complete a simple command line program as part of the first assignment for an online course I am taking, but I seem to have hit a roadblock that I can't figure out with GDB or my own research.
After hours of rewrites, and hours of debugging, I finally got the code below to compile. The program is supposed to take a credit card number as an input, and then check whether it's valid per the specifications of the assignment. I used a test number from here: PayPal Test Credit Cards
The odd thing is, when I enter an AMEX card number, it correctly produces the text "AMEX", but when I try a Visa or a Master Card, it prints "INVALID".
In GDB I broke at the Verify function and it seems to incorrectly skip these two if/else if statements without proceeding to the Checksum function even though conditions appear to be met.
if (firstDigit == 4 && totalDigits == (13 | 16) && Checksum(cardNumber, totalDigits) == 0) // checks for a valid Visa.
...
else if (firstDigit == 5 && secondDigit == (1 | 2 | 3 | 4 | 5) && totalDigits == 16 && Checksum(cardNumber, totalDigits) == 0) // checks for a valid Mastercard.
...
The AMEX line of code that correctly executes is:
else if (firstDigit == 3 && secondDigit == (4 | 7) && totalDigits == 15 && Checksum(cardNumber, totalDigits) == 0) // checks for a valid American Express.
The arguments for all three lines seem to be formatted exactly the same. That is far as I could get in GDB though. I would print totalDigits, firstDigit, and secondDigit in GDB right before stepping through the above two non-executing lines and everything looked correct. So I'm stumped, why is the AMEX line executing, but not the others?
Thanks in advance everyone. This is the first program after hello.c that I've tried to write, so I am open to absolutely any criticism or suggestions if it looks like I'm doing something weird/wrong.
Full code:
checker.c
#include <stdio.h>
#include <stdlib.h>
int MAX = 16;
int* DigitSort(unsigned long long x, int* array);
int Verify(int* array);
int main (void)
{
int* output = malloc (sizeof(int) * (MAX + 2)); // creates a blank array for the individual digits of the card number.
unsigned long long userInput = 0;
do
{
printf("Please enter a credit card number:\n");
scanf("%lld", &userInput);
}
while (userInput <= 0); // checks to make sure the user entered a number.
switch(Verify(DigitSort(userInput, output))) // sorts the user's input into individual digits and verifies the card type and validity.
{
case 1 :
printf("VISA\n");
break;
case 2 :
printf("MASTERCARD\n");
break;
case 3 :
printf("AMEX\n");
break;
case 0 :
printf("INVALID\n");
break;
default :
printf("INVALID\n");
}
free(output);
return 0;
}
int Verify(int* array) // verifies whether or not a card number is valid. Must pass the function a sorted array of individual digits.
{
int* cardNumber = array;
int firstDigit = cardNumber[0];
int secondDigit = cardNumber[1];
int totalDigits = 0;
int Checksum(int* cardNumber, int totalDigits);
int i = 0;
while (firstDigit >= 1 && cardNumber[i] >= 0) // this step counts the number of digits in the array.
{
totalDigits = totalDigits + 1;
i++;
}
if (firstDigit == 4 && totalDigits == (13 | 16) && Checksum(cardNumber, totalDigits) == 0) // checks for a valid Visa.
{
return 1;
}
else if (firstDigit == 5 && secondDigit == (1 | 2 | 3 | 4 | 5) && totalDigits == 16 && Checksum(cardNumber, totalDigits) == 0) // checks for a valid Mastercard.
{
return 2;
}
else if (firstDigit == 3 && secondDigit == (4 | 7) && totalDigits == 15 && Checksum(cardNumber, totalDigits) == 0) // checks for a valid American Express.
{
return 3;
}
else // if the card number doesn't match any of the above conditions or fails the checksum, an 'I' for Invalid is returned.
{
return 0;
}
}
int* DigitSort(unsigned long long x, int* array) // takes a long long as input and sorts it into individual digits
{
int* arrayReversed = malloc (sizeof(int) * (MAX + 2)); // creates a new array to hold the reversed order of digits.
int i = 0;
arrayReversed[0] = 0;
if (i < (MAX - 1) && x >= 10)
{
do
{
arrayReversed[i] = x % 10;
x = x / 10;
i++;
}
while (i < (MAX -1) && x >= 10);
}
if (i < MAX && x >= 1 && x <= 9)
{
arrayReversed[i] = (int) x;
x = (x - x);
}
if (x == 0)
{
int j = 0;
do
{
array[j] = arrayReversed[i]; // sorts the digits from the reversed array and places them into the sorted array.
j++;
i--;
}
while (j < MAX && i >= 0);
array[j] = -1;
}
free(arrayReversed);
return array;
}
int Checksum(int* cardNumber, int totalDigits)
{
int sum1 = 0;
int sum2 = 0;
int i = (totalDigits - 2);
int j = (totalDigits - 1);
while (i >= 0)
{
sum1 = ((cardNumber[i] * 2)%10) + ((cardNumber[i] * 2)/10) + sum1;
i -= 2;
}
while (j >= 0)
{
sum2 = (cardNumber[j] + sum2);
j -= 2;
}
if (((sum1 + sum2) % 10) == 0)
{
return 0;
}
else
{
return 1;
}
}
Your first problem is here:
if (firstDigit == 4 && totalDigits == (13 | 16) && ...
You need to write:
if (firstDigit == 4 && (totalDigits == 13 || totalDigits == 16) && ...
Your first check is looking for 0x1D == 29 as the number of digits (because, as paisanco points out in a comment, the | operator is the bitwise OR operator), and no credit card needs 29 digits (yet, and not for a long time to come). Note the extra parentheses for clarity and accuracy. Don't mess around risking removing them — the code won't work properly again. And in general, be explicit if your condition has both && and || operators and use parentheses to group terms explicitly.
You have similar problems elsewhere. As it happens, (4 | 7) is the same value as 7, so the condition works when the second digit is 7 (but not when it is 4). But it doesn't mean what you intended it to mean.
Computer languages don't work the same as human languages. Get used to writing out the condition somewhat more verbosely. Some other languages provide shorthands for these conditions; C is not such a language.
Function fun(n) is defined as such:
fun(n) = 1 (if n <=1)
fun(n) = fun(n/2) (if n is even)
fun(n) = 2*fun((n-1)/3) (if n> and n is odd)
I'm trying to write a recursive function to compute and return the result. I just started learning recursion, I got kind of lost while doing this function. Can someone correct me and explain to me? Thanks!
Here's what I did:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
int fun(int n);
int main()
{
int num;
printf("\nEnter a number: ");
scanf("%d", num);
printf("Result = %d\n", fun(num));
return 0;
}
int fun(int n)
{
if (n <= 1)
{
return 1;
}
else if (n % 2 == 0)
{
return fun(n / 2);
}
else if ((n > 1) && (n % 2 == 0))
{
return 2 * fun((n - 1) / 3);
}
}
Expected output:
Enter a number: 13
Result = 2
Enter a number: 34
Result = 4
Output I'm getting instead:
Enter a number: 13
Result = 1
Enter a number: 34
Result = 1
scanf takes a pointer to int as argument for %d, i.e.,
scanf("%d", &num);
Also, your function fun does not handle all cases and may fall off the bottom:
if (n <= 1)
{
return 1;
}
else if (n % 2 == 0)
{
return fun(n / 2);
}
else if ((n > 1) && (n % 2 == 0))
{
return 2 * fun((n - 1) / 3);
}
The last else if condition is never met, because the previous check for n % 2 == 0 already returns in that case. Also the n > 1 is pointless because the first n <= 1 returns in all other cases.
You can simply make it:
else
{
return 2 * fun((n - 1) / 3);
}
The culprit is the last else if condition. Change it to:
else if ((n % 2) != 0)
The condition that n is odd is written wrong here. You wrote the same thing as for when n is even.
Its probably better to explicitly make the cases disjoint so you always return and there's no warning, like this:
int fun(int n)
{
if(n <= 1)
return 1;
if(n % 2 == 0)
return fun(n/2);
//No need for a condition, we know the last one must be satisfied
return 2 * fun((n-1)/3);
}
or, add another "default" case that indicates there was some error.
I think last if should be:
else if ((n > 1) && (n % 2 != 0))
Notice the != instead of ==.
The third condition
else if ((n > 1) && (n % 2 == 0))
is wrong, but instead of fixing it just you else no else if - because all other conditions were checked already.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a project yet my teacher hasn't taught us about arrays. We need to output =<> signs corresponding to the comparison of one number to another. IE the main number is 1234 and I put in 2315, the output would be <<<> where the signs do not go in the order of the numbers but by this order =, <, >.
I have an idea and that to use an array then to use some code that would read out the whole array and apply rules to it, however I do not know how to implement this. I have been googling for awhile now and nothing I found really helps.
Just to let you know the program has way more steps than just this, all of which I have already completed, I just can't figure out this part. I do not want just the answer, I just want someone to point me in the right direction.
Thanks
EDIT:: The example 1234 and 2315 are bad examples. To give a more definitive idea without giving away too much of the problem so I have work to do is listing num1 and num2 (corresponding to 1234 and 2315) from least to greatest or greatest to least and compare that way. So another example would be 4751 is the main number and I put in 1294. The output would be ==<>. Thanks for the help guys so far. I am learning a lot.
EDIT2:: Thanks guys for the help. I learned a lot. I don't want any more submissions at least until I can upload my code.
Taking you at your word that you've already successfully completed most of your assignment, and by giving you code that you'll have to work through and understand to figure it out and adapt it to your needs, this will do what you want. The fact that you don't have to output the signs in the same order as the numbers themselves is what makes this easier.
#include <stdio.h>
int main(void) {
int num1 = 1234;
int num2 = 2315;
int lt = 0, gt = 0, eq = 0;
while ( num1 > 0 && num2 > 0 ) {
int op1 = num1 % 10;
int op2 = num2 % 10;
if ( op1 < op2 ) {
++lt;
} else if ( op1 > op2 ) {
++gt;
} else {
++eq;
}
num1 /= 10;
num2 /= 10;
}
for ( int i = 0; i < eq; ++i ) {
putchar('=');
}
for ( int i = 0; i < lt; ++i ) {
putchar('<');
}
for ( int i = 0; i < gt; ++i ) {
putchar('>');
}
putchar('\n');
return 0;
}
and outputs:
paul#MacBook:~/Documents/src/scratch$ ./eq
<<<>
paul#MacBook:~/Documents/src/scratch$
This code lets you get the nth digits you can compare and make a count of each symbol you need to return
char nthdigit(int x, int n)
{
while (n--) {
x /= 10;
}
return (x % 10) + '0';
}
And this is how you get the length of a number, check this post
As promised here is the rest of my code. It fixes the issue pointed out in the question but I have another issue. It is probably pretty noticeable but I wanted to post my code so I don't forget again.
#include<stdio.h>//standard inputs and outputs
#include<stdlib.h>//for compare, qsort, srand, and rand function
#include<time.h>//for random numbers at different times
#include<unistd.h>//for fun at the end
int main(void){
int guess, gdig1, gdig2, gdig3, gdig4, random, rdig1, rdig2, rdig3, rdig4;//variables for main
int lt, gt, eq, i, q, temp, holder;//this is for the hints
int g[3],r[3];
printf("Give the computer a few seconds to come up with a super secret passcode.\n");
do{//figuring out a random code
unsigned int iseed = (unsigned int)time(NULL);
srand (iseed);//using an unassigned integer
random = rand()%9000+1000;//generating a random number but limiting it
rdig4 = random%10;
rdig3 = (random/10)%10;
rdig2 = (random/100)%10;
rdig1 = (random/1000)%10;//figuring out the individual digits of the code
} while ((rdig1 == rdig2)||(rdig1 == rdig3)||(rdig1 == rdig4)||(rdig2 == rdig3)||(rdig2 == rdig4)||(rdig3 == rdig4)||(rdig1 == 0)||(rdig2 == 0)||(rdig3 == 0)||(rdig4 == 0));
//^^ some crazy boolean expression making sure the random integer doesnt have any of the same digits.
printf("\nThe actual passcode is:%d.\n",random);//testing in beginning comment out ********
do{
do{
printf("\nEnter in your guess for the passcode: ");
scanf("%d",&guess);//inputting and reading the guessed code
// printf("You entered:%d\n",guess);//just to check comment out at end**
gdig4 = guess%10;
gdig3 = (guess/10)%10;
gdig2 = (guess/100)%10;
gdig1 = (guess/1000)%10;//figuring out the individual digits of the guess code using modulus operator
if (guess > 9999){//the starting loop statement to make sure number is valid. this one is if it is greater than 4 digits
printf("\nPlease use a four digit number for the passcode.\n");
}
else if (guess < 1000){//this one is if it is less than 4 digits
printf("\nPlease use a four digit number for the passcode.\n");
gdig1 = 1;
gdig2 = 1;//used so the computer still loops
gdig3 = 1;
gdig4 = 1;
}
if ((gdig1 == 0) || (gdig2 == 0) || (gdig3 == 0) || (gdig4 == 0)){
break;
}
if ((rdig1 == gdig1) && (rdig2 == gdig2) && (rdig3 == gdig3) && (rdig4 == gdig4)){//to skip this codeblock and move onto next
break;
}
if (guess > 9999){
break;
}
if (guess < 1000){
break;
}
printf("\n%d %d %d %d\n",rdig1,rdig2,rdig3,rdig4); //used to testing comment out at end
printf("\n%d %d %d %d\n",gdig1,gdig2,gdig3,gdig4);
while (guess > 0){
g[i++] = guess % 10;
guess /=10;
}
do{//took a long long LONG time to get
for(i = 0; i<3;i++){
if(g[i] > g[i+1]){
holder = g[i+1];
g[i]=g[i+1];
g[i+1] = holder;
}
}
}while (i == 1);
for(i = 0;i<4;i++){
printf("%d",g[i]);
}
while (random > 0){
r[i++] = random % 10;
random /=10;
}
do{//took a long long LONG time to get
for(i = 0; i<3;i++){
if(r[i] > r[i+1]){
temp = r[i+1];
r[i]=r[i+1];
r[i+1] = temp;
}
}
}while (i == 1);
for(i = 0;i<4;i++){
printf("%d",r[i]);
}
/* for(digit=0;digit<4;digit++){
for(tmp=guess;tmp>0;tmp/=10){
if(tmp%10==digit){
printf("%d",digit);
g[i++]=digit;
}
}
}
printf("\n");
for(i=0;i<4;i++){
printf("%d",g[i]);
}//just to check
//this is for sorting the random
for(digit=0;digit<4;digit++){
for(tmp=random;tmp>0;tmp/=10){
if(tmp%10==digit){
printf("%d",digit);
r[i++]=digit;
}
}
}
for(i=0;i<4;i++){
printf("%d",r[i]);
}//just to check
*/
//this is for hints
rdig1=r[0];//redefining the random and guess digits so it is easier later
rdig2=r[1];
rdig3=r[2];
rdig4=r[3];
gdig1=g[0];
gdig2=g[1];
gdig3=g[2];
gdig4=g[3];
q = 0;
eq = 0;
lt = 0;
gt = 0;
if (random > 0){//loop that always holds true
if (gdig1 == rdig1){
eq++;
}else if (gdig1 < rdig1){
lt++;
}else if (gdig1 > rdig1){
gt++;
}
if (gdig2 == rdig2){
eq++;
}else if (gdig2 < rdig2){
lt++;
}else if (gdig2 > rdig2){
gt++;
}
if (gdig3 == rdig3){
eq++;
}else if (gdig3 < rdig3){
lt++;
}else if (gdig3 > rdig3){
gt++;
}
if (gdig4 == rdig4){
eq++;
}else if (gdig4 < rdig4){
lt++;
}else if (gdig4 > rdig4){
gt++;
}
}
for (q = 0; q < eq; q++){//counting step for the = <> no problems here^^
putchar('=');
}
for (q = 0; q < lt; q++){
putchar('<');
}
for (q = 0; q < gt; q++){
putchar('>');
}
//everything below is correct do not mess with *******************************************************************************************
} while (gdig1 > 0);//to loop inputs until they input correctly
if ((gdig1 == 0) || (gdig2 == 0) || (gdig3 == 0) || (gdig4 == 0)){//a nested if statement to check each digit if it is a 0
printf("\nYou have entered the Super Secret Code!!!!!\nThe actual passcode is:%d.\n",random);
break;
}
if ((rdig1 == gdig1) && (rdig2 == gdig2) && (rdig3 == gdig3) && (rdig4 == gdig4)){//to skip this codeblock and move onto next
break;
}
} while (((rdig1 != gdig1) && (rdig2 != gdig2) && (rdig3 != gdig3) && (rdig4 != gdig4)));//to loop inputs until they got it
//everything below is correct do not mess with *******************************************************************************************
if ((rdig1 == gdig1) && (rdig2 == gdig2) && (rdig3 == gdig3) && (rdig4 == gdig4)){
printf("\nCorrect Code inputted.");
sleep(1);
printf("\nImplementing unlocking procedures...\n");
for (i=0;i<=4;i++){//for fun cause why not. if you spend hours on code might as well have some fun :)
int p =25*i ;
printf("%d%% complete................\n",p);
sleep(1);
}
printf("Stand back!!! The vault is now opening.\n");
}
return 0;
}