Thread: Repetitive Output!!

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Repetitive Output!!

    Hey guys.

    I have this function:
    PHP Code:
    // NOTE:  "P" IS A MACRO FOR printf()...(#define P printf) 
    void find_four(struct atom *atomoint size)
    {
    int w=0;
    int k=0
    int t=0;
    int u=0;
    int proceed 1;

    do{
    P("Please Type The Element Number:"); fgets(choice,50,stdin);
    clrscr();
    atoi(choice);
    clrscr();
    for(
    int j 0j<szj+=1)
    {
      if(     (
    atomo[j].numb == c)  )  
       {
        
    k=j;  // Assign loop variable to the selected Atom 
        
    P("   Most Probable Mates For This Element Forming A Four-Atom Molecule:\n");
        
    P("__________________________________________\n");
        if(
    atomo[k].takes == 0)
         { 
    clrscr();
            
    P("This is a Noble Gas. \n\nThis means it does not bond with other \nElements except under very special circumstances.\n\nIt has no known mates.\n\n\n");
            break; 
         }
     for(
    w=0;w<sz;w+=1)
       {
        for(
    t=0;t<sz;t+=1)
          {
           for(
    u=0;u<sz;u+=1)
             {
              if(
    atomo[t].gives != && atomo[u].gives != && atomo[w].gives != 0)  // Noble gasses filtered out...
                
    {
                 if(
    atomo[t].gives atomo[u].gives atomo[w].gives == atomo[k].takes)    /// if the valence electrons fill this attoms shell...
                  
    {
                   
    P("FOUND for: #%i   %-12s   %4s          \n"
                      "                #%i   %-12s   %4s          \n"
                      "AND        #%i   %-12s   %4s          \n"
                      "AND        #%i   %-12s   %4s        \n\n"
                      "To Form A \"%s-%s-%s-%s\" Molecule...\n"
                      "__ __________________________________\n\n"
                     
    ,atomo[k].numb,atomo[k].elem,atomo[k].symb
                     
    ,atomo[t].numb,atomo[t].elem,atomo[t].symb
                     
    ,atomo[u].numb,atomo[u].elem,atomo[u].symb
                     
    ,atomo[w].numb,atomo[w].elem,atomo[w].symb
     
    ,atomo[k].symb,atomo[t].symb,atomo[u].symb,atomo[w].symb);

                  
    fgets(choice,50,stdin);  //If tired of this element,press x...
                     
    if(  strstr(choice,"x")    || strstr(choice,"x")  )
                     {
    find_four(atomo,sz);} //...and return to beginning 
                   
    }
                 }
               }
            }
          }
       }
    }
    P("\nCONTINUE? ('X' Exits)\n\n\n");
    fgets(choice,50,stdin);
    clrscr();
    if(
    strstr(choice,"x") || strstr(choice,"X") ){proceed 0;}
    else{
    proceed=1;}
                                                 }while(
    proceed != 0);


    }  
    // <---End of function 
    The basic problem here is that I am getting every possible combination for the four given elements that are likely to combine.
    Example:
    H-Na-K-P
    Na-P-H-K
    P-H-K-Na
    H-P-Na-K
    ...etc,etc,etc!!!
    ...when only ONE of those is necessary.

    I am currently tweeking the loops so that each one only runs through a certain part of the array of structs a certain number of times. But I don't want to lose information either. I just want a mechanism to control it so that only one combination gets printed.

    I am not looking for code, here, just theory, but if you need to use code to explain something, that's fine, too.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I'm not sure how it is switching around the position of the first atom... looking at your code, it appears that that atom accepting the electrons is going to have to always be first... *shrug*

    Here's what I suggest...
    PHP Code:
    for (0szw++)
    {
     for (
    0<= wt++)
     {
      for (
    0<= tu++)
      {
        
    // Stuff here...
      
    }
     }

    This will just keep these three variables in order... so of all the redundant collections of these variables like...
    (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)
    Only the ones that are ordereds should be processed. (Only (1, 2, 3))

    This will not prevent the program from printing more than one solution however... it will still give

    H-Na-K-P and
    H-Na-K-F
    both as solutions if they are both right, so I dunno if this is what you're asking for.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Nope. Left out many matches.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    ... I am so sure that what I posted was the solution....
    Still, it's not printing duplicates anymore I assume.

    Maybe this is just a copy-n-paste problem, but what about the variable passed to the function being called 'size', but within the function the variable 'sz' is used instead? It's unlikely, but maybe there's a global int sz that's being used, which is too small, so not all solutions are posted, but the code can still compile?

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    1
    existing looping structure


    for(int j = 0; j<sz; j+=1)
    {
    for(w=0;w<sz;w+=1)
    {
    for(t=0;t<sz;t+=1)
    {
    for(u=0;u<sz;u+=1)

    assuming you do not need to repeat elements

    try this
    (notice the starting points for each inner loop)

    for(int j = 0; j<sz; j+=1)
    {
    for(w=j+1;w<sz;w+=1)
    {
    for(t=w+1;t<sz;t+=1)
    {
    for(u=t+1;u<sz;u+=1)

    This will loop through and get every set without duplicates

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  2. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM