Thread: Easy weard C++ problem

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    3

    Unhappy Easy weard C++ problem

    ok here is the thing. I'm working on this easy simple script now for 2 whole days and i decided to get some help before i throw out my laptopt . The thing is it hangs when i enter double numbers. some examples
    999
    12334
    43321
    even this one :S 9871234 they all hang
    while 987123 (minus the 4) 4321 and 1234 all work. What goes wrong here. If i leave out the aantal[tel]==aantal[tel+1] it al works. I just don't get it. TANX!!! ohw and i'm from holland so therefore the qeard variables . and i have to use the void for the assignment!!!

    PS a little other question i know i should use search but i've had it behind this pc for the next day so i would like to know wich program i can use to type C++ and compile. Use windows commands like ctrl-c and ctrl-v and don't have to install. So i put my usb stick in a pc and run it everywhere. I know have turbo C 1 but that's a not user friendly old program :P.

    Code:
    /*
    Door Erwin Gillissen H1IA03
    24 december 2005
    Input: Positive numbers
    Verw.: How much different numbers are there, filter double numbers
    Output: Print these different numbers to the screen
    */
    
    # include <iostream.h>
    # include <conio.h>
    
    int main()
    {
    int aantl[30], getal=0, n=0, totaal=0;
    aantl[n]=0;
    //clrscr();
    void volgorde(int aantal[],int&totaal); //subroutine
    
    cout <<"Voer een reeks positieve getallen in:\nEindig met een 0\n";
    while (cin >> getal, getal!=0) //end reading with 0
    {
       if (getal<1)  cout << "Voer een getal hoger dan 0 in\n" ; //enter number higher then 0
       else aantl[n++]=getal;
    }
    
    totaal=n; n=0; //totaal is latest array number
    
    volgorde (aantl, totaal);
    //clrscr();
    cout << "De volgende "<<totaal<<" verschillende getallen zijn ingevoerd:\n";
    while (n<totaal) // print while n is smaller then total
       {
       cout << aantl [n++] << endl;
       }
    
    cin.get();cin.get();
    return 0;
    }
    
    
    
    
    
    void volgorde(int aantal[],int&totaal)
    {
     int gedaan=0,tel=0,hulp=0,correctie=0;
     while (gedaan==0)
     {
    	
     gedaan=1;
     while (tel<totaal)
    	{if (aantal[tel]==aantal[tel+1]) //to filter equal numbers
    	    {
    		aantal[tel]=0; //the first of the equal numbers gets value 0
    		gedaan=0; // to enter the first while again when tel equals total
    		correctie++;
    	    }    
    	else if ((aantal[tel]>aantal[tel+1])||(aantal[tel]==0))//to put numbers in order first double with value 0 gets placed at bottom for later use
    		{
    		hulp=aantal[tel]; 
    		aantal[tel]=aantal[tel+1];
    		aantal[++tel]=hulp;
    		gedaan=0;
    		}
    	else tel++; 
    	}
    tel=0;
     }
    totaal=totaal-correctie; //when there was a double number it gets 0 and placed last, with this line it won't get printed later on
    
    }

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Two problems in volgorde() that I can see:

    - The inner while loop runs off the end of the array (you're comparing the last input number with whatever is in the next memory slot).

    - Whenever the input includes a duplicated number, the inner while loop ends with the "else if" statement setting gedaan=0. Then the outer while loop sets tel=0. So you loop forever.

    You're getting into trouble trying to "bubble-up" zeros and "big" numbers at the same time. When you get near the end of the array and find a 0, you swap it with the "big" number following it. Then you find the "big" number which is bigger than the 0 now following it, so you swap them again. And so on.

    It would be easier if you first just sort the array; then after sorting is finished, remove the zeros.

    (Hint: If you insist on removing the duplicates at the same time that you sort, think "bigger".)
    Last edited by R.Stiltskin; 12-25-2005 at 10:31 PM.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    3
    At first i'd like to thank you a lot . But there are some things that in my opinion should work.

    Quote Originally Posted by R.Stiltskin
    Two problems in volgorde() that I can see:

    - The inner while loop runs off the end of the array (you're comparing the last input number with whatever is in the next memory slot).
    It runs tel<totaal so it can not go past totaal. tel will be smaller than totaal en tel+1 wil exactly be totaal. Right????? So it won't run off the end off the arry

    Quote Originally Posted by R.Stiltskin
    - Whenever the input includes a duplicated number, the inner while loop ends with the "else if" statement setting gedaan=0. Then the outer while loop sets tel=0. So you loop forever.
    You won't loop forever because when it is set to 0 it goes back into the loop again looking from the start (tel=0) and the outer loop sets gedaan to 1. Then you won't find that same double anymore and the else if will start bubbling it upwards. If something changes, gedaan is set to 0 so it will enter the while loop again to maybe bubble again. If it isn't entering any if statement anymore gedaan stays 1 and you will finish the outer while loop!!!!

    The only problem in my eyes (wich you opened with the text below here ) is that the "big" || 0 statement will make it loop forever so i will put an if in there to check if aantal[tel]=0 for tel=aantal (end of array) and then stop bubbling

    !!!!But why does it hang with 9871234 ??????????

    Quote Originally Posted by R.Stiltskin
    You're getting into trouble trying to "bubble-up" zeros and "big" numbers at the same time. When you get near the end of the array and find a 0, you swap it with the "big" number following it. Then you find the "big" number which is bigger than the 0 now following it, so you swap them again. And so on.

    It would be easier if you first just sort the array; then after sorting is finished, remove the zeros.

    (Hint: If you insist on removing the duplicates at the same time that you sort, think "bigger".)
    I can't seem to figure out the "bigger" hint ????? And is there a program in wich you can step trough this program and see where you hang or where it keeps looping. That would be awsome!!!

    And is there a grogram without installation???

    Tanx a million. I will try some things now and post the result later on.
    Last edited by Airwin; 12-26-2005 at 05:46 AM.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    3
    I think i was right about the things working. I got it to work now with just changing the fact that value 0 gives errors. Still don't know what you mean with bigger though?? And would like to know a good program to debug and to use without installation!!! Tanx a lot. Was stuck for 2 days and solved it within 20 minutes after your post

    Code:
    /*
    Door Erwin Gillissen H1IA03
    24 december 2005
    Input: Positive numbers
    Verw.: How much different numbers are there, filter double numbers
    Output: Print these different numbers to the screen
    */
    
    # include <iostream.h>
    # include <conio.h>
    
    int main()
    {
    int aantl[30], getal=0, n=0, totaal=0;
    aantl[n]=0;
    //clrscr();
    void volgorde(int aantal[],int&totaal); //subroutine
    
    cout <<"Voer een reeks positieve getallen in:\nEindig met een 0\n";
    while (cin >> getal, getal!=0) //end reading with 0
    {
       if (getal<1)  cout << "Voer een getal hoger dan 0 in\n" ; //enter number higher then 0
       else aantl[n++]=getal;
    }
    
    totaal=n; n=0; //totaal is latest array number
    
    volgorde (aantl, totaal);
    //clrscr();
    cout << "De volgende "<<totaal<<" verschillende getallen zijn ingevoerd:\n";
    while (n<totaal) // print while n is smaller then total
       {
       cout << aantl [n++] << endl;
       }
    
    cin.get();cin.get();
    return 0;
    }
    
    
    
    
    
    void volgorde(int aantal[],int&totaal)
    {
     int gedaan=0,tel=0,hulp=0;
     while (gedaan==0)
     {
    	
     gedaan=1;
     while (tel<totaal)
    	{if ((aantal[tel]==aantal[tel+1])&&(aantal[tel+1]!=0)) //to filter equal numbers
    	    {
    		aantal[tel]=0; //the first of the equal numbers gets value 0
    		gedaan=0; // to enter the first while again when tel equals total
    		}    
    	else if (((aantal[tel]>aantal[tel+1])&&(aantal[tel+1]!=0))||((aantal[tel]==0)&&(aantal[tel+1]!=0)))//to put numbers in order first double with value 0 gets placed at bottom for later use
    		{
    		hulp=aantal[tel]; 
    		aantal[tel]=aantal[tel+1];
    		aantal[++tel]=hulp;
    		gedaan=0;
    		}
    	else if (aantal[tel+1]==0) totaal--; //erase the last array position holding 0
    	else tel++; 
    	}
    tel=0;
     }
    
    }

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    You fixed the looping problem, but ...

    Quote Originally Posted by Airwin
    It runs tel<totaal so it can not go past totaal. tel will be smaller than totaal en tel+1 wil exactly be totaal. Right????? So it won't run off the end off the arry
    antal[totaal-1] is the last item you put into the array. Your last comparison is antal[totaal-1] : antal[total]. This will cause errors when there is a non-zero in that memory location.

    Quote Originally Posted by Airwin
    And is there a program in wich you can step trough this program and see where you hang or where it keeps looping.
    I guess you could use a debugger. Or you could simply put in some cout statements at various places to see where it hangs (statements that tell you where it is in the program, and maybe printing the values of variables you're interested in).


    Quote Originally Posted by Airwin
    !!!!But why does it hang with 9871234 ??????????
    I don't see any problem with 9871234.


    The "bigger" hint: in your original program, try using 42000000 (or any integer bigger than the biggest input number) instead of 0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  2. Relatively easy math problem...
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-18-2005, 08:48 PM
  3. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  4. Math Problem....
    By NANO in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 11-11-2002, 04:37 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM