Thread: Don't Understand

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    6

    Don't Understand

    Hi guys I'm trying to get this program to sort my array in the second option and i think i have the bubble sort written correctly but it doesn't do anything. Any ideas


    //This program takes 20 inputs from the user, and gives the choice to show sorted data,
    //sort the data into descending order, display the sorted data, and display the address of the first element.



    Code:
    #include <iostream>
    using namespace std;
    #include <iomanip> 
    #include <algorithm>
    
    int main()
    {
    	int i,num[20],n,j,choice,tmp;
    
    
    	cout<< "Please enter 20 integers";
    	for (i=0; i<20; i++)
    	
    	{
    		cout<<"\nEnter next value:";
    		cin>>num[i];
    		
    	}
    	
    		
    
    		cout<<"\n1.Display original data.\n";
    		cout<<"2.Sort the data into descending order\n";
    		cout<<"3.Display the sorted data (Only if you've already sorted)\n";
    		cout<<"4.Get the address of the first element of array.\n\n";
    		cin>>choice;
    	
    	if (choice==1){
    		
    		for (i=0; i<20; i++){
    			cout<<"\n"<<num[i]<<endl;		
    		}
    	if (choice==2){
    		for (i=0; i<n-1; i++) {
    			for (j=0; j<n-1-i; j++)
        if (num[j+1] < num[j]) {  /* compare the two neighbors */
    		tmp = num[j];         /* swap a[j] and a[j+1]      */
    		num[j] = num[j+1];
    		num[j+1] = tmp;
    
    	cout<<"Here are your numbers:"<<tmp<<endl;
    } 
    }
    	}}}
    Last edited by Salem; 04-04-2008 at 11:47 PM. Reason: remove php tags

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Kindly edit your code is just use code bbcode tags instead of php bbcode tags (or just php bbcode tags, if you really want, but the syntax highlighting may not be correct). Also, indent your code more consistently so that it is easier for everyone to read.

    i think i have the bubble sort written correctly but it doesn't do anything
    You need another loop to print the array of numbers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    6
    I'm not sure I understand. Do you mean I need another for loop before my cout statement?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'm not sure I understand. Do you mean I need another for loop before my cout statement?
    Oh, I very badly misread your code. Really, you need to indent your code, e.g.,
    Code:
    #include <iostream>
    #include <iomanip>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
        int i,num[20],n,j,choice,tmp;
    
        cout<< "Please enter 20 integers";
        for (i=0; i<20; i++)
        {
            cout<<"\nEnter next value:";
            cin>>num[i];
    
        }
    
        cout<<"\n1.Display original data.\n";
        cout<<"2.Sort the data into descending order\n";
        cout<<"3.Display the sorted data (Only if you've already sorted)\n";
        cout<<"4.Get the address of the first element of array.\n\n";
        cin>>choice;
    
        if (choice==1)
        {
    
            for (i=0; i<20; i++)
            {
                cout<<"\n"<<num[i]<<endl;
            }
    
            if (choice==2)
            {
                for (i=0; i<n-1; i++)
                {
                    for (j=0; j<n-1-i; j++)
                        if (num[j+1] < num[j])    /* compare the two neighbors */
                        {
                            tmp = num[j];         /* swap a[j] and a[j+1]      */
                            num[j] = num[j+1];
                            num[j+1] = tmp;
    
                            cout<<"Here are your numbers:"<<tmp<<endl;
                        }
                }
            }
        }
    }
    The main change I did was to move down your using directive, but that probably has no effect on your code. More importantly, I ran it through a code formatter. Can you see your branches of logic, as implemented in your code, more clearly now, and thus see what is wrong?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    6
    I'll take a stab at it, I'm new at this so don't laugh, but to me it looks like my if(choice==2) is actually within if(choice==1) and thus the compiler doesn't know what i'm wanting it do....?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    but to me it looks like my if(choice==2) is actually within if(choice==1) and thus the compiler doesn't know what i'm wanting it do....?
    Well, the compiler never knows what you want to do, only what you told it to do. But yes, you have the right idea: that block of code that is supposed to be run when choice is 2 is unreachable since it can only be run if choice is both 1 and 2 simultaneously, which is impossible.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    6
    Ok i closed off my first if statement so that it wouldn't have 1 and 2 together. I recompiled and ran it and it does the same thing and tells me that my 'n' is uninitialized. So do i still have the same error as before?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I recompiled and ran it and it does the same thing and tells me that my 'n' is uninitialized. So do i still have the same error as before?
    Look carefully at where you use n, and see where you give it a value (or rather, fail to do so).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    6
    I do see that I never actually gave 'n' a value anywhere but I'm not sure what it's meant to be in the bubble loop we just covered those in class and I came away very confused. I'm assuming that it's wanting the users input to be n. So i should assign it to be the array that the user put in. Or am I way off base?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    n is the number of elements to be sorted. Since you require the user to enter all 20 numbers, you should set n to 20.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 10-14-2006, 04:38 AM
  2. Need software to help to understand C source code
    By Kincider in forum C++ Programming
    Replies: 1
    Last Post: 09-28-2006, 09:44 PM
  3. Replies: 13
    Last Post: 08-24-2006, 12:22 AM
  4. Need help to understand x["AC"] in the following code
    By jcourcel in forum C Programming
    Replies: 11
    Last Post: 06-06-2006, 01:13 AM
  5. I don't understand K&R example
    By rllovera in forum C Programming
    Replies: 8
    Last Post: 10-25-2004, 10:45 AM