-1

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.

Nick
  • 3
  • 2
  • Totally confused. Can you give some sample input/output? We need more details: array length fixed or dynamic, etc. – Fiddling Bits Feb 14 '14 at 21:25
  • 2
    @FiddlingBits: 1 < 2, 2 < 3, 3 > 1, and 4 < 5, so three < and one >, so desired output is <<<>. – Crowman Feb 14 '14 at 21:28
  • @PaulGriffiths Thanks for clearing that up. – Fiddling Bits Feb 14 '14 at 21:29
  • 3
    To the OP, not much more pointing can be done than suggesting you get a good C book and learn about arrays, if you want to do so. You don't actually need arrays to solve this problem, you can just go number by number, and count the equalities/inequalities. – Crowman Feb 14 '14 at 21:30
  • 1
    @PaulGriffiths That's probably the best advice you can give the OP without actually writing the code for him. – Fiddling Bits Feb 14 '14 at 21:31
  • Thank you guys. I was looking at doing number by number, but after some quick mental calculation that is another 162 lines of code ontop of my 147 so far. I also think that misses the point of the project to understand arrays. I was just looking for a more specific thing to search for other than just array iteration because that is the only thing I know to call it as. – Nick Feb 15 '14 at 00:04
  • so you sort the digits first and then compare digit-wise and return the comparisons in the sorted order? So that 4751 sorts to 1 4 5 7 and 1294 sorts to 1 2 4 9 to compare digit-wise? So shouldn't that be =>><, or in the sorted order =<>>? -- Also, the _point_ of the project is to understand arrays? Ok. – kbshimmyo Feb 15 '14 at 03:25
  • Haha that is what I thought at first too. But like I said above, it has to be in order = < >. I was just using a comparison digit wise from least to greatest as a way to accurately compare the number's digits. Sorry I am so bad at explaining. – Nick Feb 15 '14 at 03:58

3 Answers3

1

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$ 
Crowman
  • 25,242
  • 5
  • 48
  • 56
  • Thank you so much. This actually helped me a lot. The example with the 1234 and 2315 doesn't really point what the project is leading to so this code doesn't completely work but it gave me an idea of how to get it to work. I will post my full code within the next few days. – Nick Feb 15 '14 at 02:07
0

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

Community
  • 1
  • 1
Adnane.T
  • 280
  • 1
  • 4
  • 13
0

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;
}
Nick
  • 3
  • 2
  • There is an array-index-out-of-boundary problem: the code defined g[3] and r[3], but actually should be [4] instead of [3] if you check the code. – Heng Li Jul 11 '20 at 20:37