You are here:

C/Follow up - C Function to work in larger program

Advertisement


Question
Hello, Zlatko.
Thanks you for responding to me so quickly. The sample program worked great. However when I place the function in my larger program, It doesn't seem to work correctly, so I may not be setting it up properly.
Perhaps you can give me some help on it? I would appreciate it greatly. I apologize on the size of it in advance. In a nutshell, the program is supposed to preferably take a six digit number, sort the digits in an ascending and descending number. From there it subtracts the smaller from the larger. It stops when a duplicate answer is obtained and at the end, gives a count of the number of iterations that it went through.

Thank you so much for any help that you can provide.

#include <stdio.h>
int CheckDups(int[],int);     //func proto
int SortNumber(int);         //func proto
int CountChain(void);  //func proto
int it=0;       //global variable iterations

int main()  //Begin main program
{
  int rep[20];    //repetition array
  int d;    //loop variable    
  int i=0,num;    //loop index, entered number and calc number

     /*Begin by asking for number...*/

   printf("Enter 6 digit number: ");
   scanf("%d",&num);

   if (num!=0 && num<1000000)  /*continues with calc if person doesn't enter 0, checks for digit length & continues if number proper length. If not, prgrm terminates*/

    {//operations between if
       //formulas to calculate digits & sortnumber function
   printf("Original number: %d\n",num);

    /*Start Duplicate check*/
   for (d=0;d<20;d++)

   {

       if (CheckDups(rep,i+1))
         //if any matching/repeated answers found....       
{
       goto Chain;     //..goto end of program printing the chain length

   }
   else
   {
       it++;       //increment iteration by one
   }

num=SortNumber(num);      //Sortnumber function
rep[d]=num;   //place final calculation in repeatition array in order to chk for duplicates
    }      //end for loop     
}  //end first if
    else
    {
        printf("You entered either 0 or number greater than 6 digits. Program terminated.");//program ends after conditions met
    }
    Chain: printf("\nChain length: %d\n",CountChain());   //print iterations amt
return (0);
}
/*End Main Program*/

/*Function*/
int CheckDups(int reps[], int count)
{
   int i, d;
  for (i=0;i<count;i++)
  {
      for (d=i;d<count;d++)
      {
        if (reps[i]==reps[d] && i!=d)
        {
        return (1);
        }
      }
  }
  return (0);
}
/*End function*/

/*Function*/
int SortNumber(int ans)
{
   int dnum,anum;   //original entered number;descending,acsending number and answer/compartive answser
   int dig[6]; //digit holder array
   int i,j,t;  //loop varibales for sorting

   dig[0]=ans/100000%10;  //calc 1st
   dig[1]=ans/10000%10;   //calc 2nd
   dig[2]=ans/1000%10;    //calc 3rd
   dig[3]=ans/100%10;     //calc 4th
   dig[4]=ans/10%10;      //calc 5th
   dig[5]=ans%10;         //calc 6th

   //sort command for descending number

   for (i=0;i<6;i++)
   {
       for(j=i+1;j<6;j++)
       {
         if(dig[i]<dig[j])
         {
         t=dig[i];
         dig[i]=dig[j];
         dig[j]=t;
         }
       }
   }
//end sort desending
//generate dnum
dnum=(dig[5]*1)+(dig[4]*10)+(dig[3]*100)+(dig[2]*1000)+(dig[1]*10000)+(dig[0]*100000);

   //sort command for ascending number
   for (i=0;i<6;i++)
   {
       for(j=i+1;j<6;j++)
       {
         if(dig[i]>dig[j])
         {
         t=dig[i];
         dig[i]=dig[j];
         dig[j]=t;
         }
       }
   }

 //end sort ascending
//generate anum
anum=(dig[5]*1)+(dig[4]*10)+(dig[3]*100)+(dig[2]*1000)+(dig[1]*10000)+(dig[0]*100000);
ans=dnum-anum;
printf("%d - %d = %d\n",dnum,anum,ans);
return(ans);   //returns calculated answer
}
/*End function*/

/*Function*/
int CountChain()
{
   int itno;
   itno=it;
   return(itno);
}
/*End Function*/

Answer
Hi Sashie

The problem is in the main loop. The comments with my initials (ZM) below explain it. Its a very interesting program. I was surprised to see the repetition.
Best regards
Zlatko

       /*Start Duplicate check*/
       for (d=0;d<20;d++)
       {
         /*ZM you want to do the sorting, and assigning to rep first,
         before checking for duplicates.
         You had duplicate checking first.
         */
         num=SortNumber(num);      //Sortnumber function
         rep[d]=num;   //place final calculation in repeatition array in order to chk for duplicates

         /* ZM here you are using d, not i, as an index into the array of rep
         so you need to pass d+1, not i+1, The i has no purpose in your
         program and can be removed. The d+1 indicates how many items are in the
         rep array, and CheckDups uses this so that it does not go
         checking uninitialized parts of the array.
         */
         if (CheckDups(rep,d+1))
         //if any matching/repeated answers found....
         {
         goto Chain;     //..goto end of program printing the chain length
         }
         else
         {
         it++;       //increment iteration by one
         }
       }      //end for loop

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.