Thread: My C++ test is COMING!!...

  1. #31
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Welcome to the world of spoiled programmers.
    One piece of advise, we don't talk to vi and emacs users. They are a bunch of conceited brats.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #32
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Nano all the way!! By the way, I use Code::Blocks, which is another great Windows IDE (Dev-C++ died one me )
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #33
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37
    I'm gonna spend the next few late nights at my computer using Dev-C++ deffinitely!!...

    Okay,... so I think i'm slightly getting there. Here's the code I adjusted.
    Code:
    int main(void)
    {
        int numbers[10];
    
        std::cout << "Enter 10 grades: ";
        for (int i = 1; i < 10; i++)
            
            std::cin >> numbers[i];
    
        bubbleSort(numbers, 10);
    
        std::cout << "Your sorted grades are: " ;
    
            for (int k = 1; k < 10; k++)
                std::cout << numbers[k];
    
        return 0;
    }
    ...
    ...
    But I got something which looks like this
    Last edited by [Z-D]; 11-28-2006 at 07:28 PM.

  4. #34
    Registered User
    Join Date
    May 2006
    Posts
    903
    First, you want to set i to 0, otherwise you're jumping over the first index which would expect a bit of the junk you have on the screen. Same thing for k. I honestly don't see anything right now... Huhhh.. Try changing what I told you and tell us if it worked ok.

  5. #35
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37
    Quote Originally Posted by Desolation
    First, you want to set i to 0, otherwise you're jumping over the first index which would expect a bit of the junk you have on the screen. Same thing for k. I honestly don't see anything right now... Huhhh.. Try changing what I told you and tell us if it worked ok.
    You mean this right?:
    Code:
    #include <iostream>
    #include<iomanip>
    
    void bubbleSort(int numbers[], int array_size);
    
    
    int main(void)
    {
    	int numbers[10];
    
    	std::cout << "Type down the 10 grades and press enter after each grade:\n";
    
    	for (int i = 0; i < 10; i++)
    	{
    		std::cin >> numbers[i];
    	}
    
    	bubbleSort(numbers, 10);
    
    	std::cout << "\n\nYour sorted grades are:\n" ;
    
    	for (int k = 0; k < 10; k++)
    	{
    		std::cout << numbers[k] << ", ";
    	}
    	
    	std::cout << "\n\n";
    
    
    	return 0;
    }
    
    void bubbleSort(int numbers[], int array_size)
    {
    	int i, j, grades;
    
    	for (i = (array_size -1); i >= 1; i--)
    	{
    		for (j = 1; j <=i; j++)
    		{
    			if (numbers[j-1] > numbers[j])
    			{
    				grades = numbers[j-i];
    				numbers[j-1] = numbers [j];
    				numbers[j] = grades;
    			}
    		}
    	}
    
    }
    Same thing happens.

  6. #36
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Try
    Code:
    void bubbleSort(int &numbers, int array_size)
    Or if you like, try using std::vector for the arrays.

  7. #37
    Registered User
    Join Date
    May 2006
    Posts
    50
    Your bubblesort function needs to go to 0 too.

    Code:
    void bubbleSort(int numbers[], int array_size)
    {
    	int i, j, grades;
    
    	for (i = (array_size -1); i >= 0; i--)
    	{
    		for (j = 1; j <=i; j++)
    		{
    			if (numbers[j-1] > numbers[j])
    			{
    				grades = numbers[j-i];
    				numbers[j-1] = numbers [j];
    				numbers[j] = grades;
    			}
    		}
    	}
    
    }
    Arrays in C are indexed from 0 to n-1. So, if you have an array of length 8, your indexes will be 0, 1, 2, 3, 4, 5, 6 and 7. So, bubblesort also needs to go down to 0.

  8. #38
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37
    Quote Originally Posted by g4j31a5
    Try
    Code:
    void bubbleSort(int &numbers, int array_size)
    This one just give me more errors... Or do I have to change the rest also?...


    Quote Originally Posted by g4j31a5
    Or if you like, try using std::vector for the arrays.
    How to use that?...
    Quote Originally Posted by Sfel
    Your bubblesort function needs to go to 0 too.
    Didn't work... after inputing the last integer, the whole thing just closes. That is in Dev-C++.

    The funny thing is the result in MV-C++, and Dev-C++ is slightly different. Is that normal?

  9. #39
    Registered User
    Join Date
    Oct 2006
    Location
    London
    Posts
    14

    Post

    I think :
    Code:
    grades = numbers[j-i];
    numbers[j-1] = numbers [j];
    should be:

    Code:
    grades = numbers[j-1];
    numbers[j-1] = numbers [j];

  10. #40
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    ZD, you really have to stop there. Stop for a moment take a deep breath and read on...

    You cannot post every time you get stuck with a problem. Part of the learning process is to look for answers yourself. And no, that doesn't mean just asking.

    You have already been told about the tutorials.
    If you read how to use these things then you will learn how to use them and all problems will magically go away.

    I understand it is a C++ test and all. But your aren't in school to just watch. There is a real intent of having you learn about things. And learn you should without trying to thwart it's meaning.

    Read the tutorials for arrays. Read the tutorials for vectors. Read the basic tutorials that introduce you to the for loop.

    Read, learn and then post your questions, if you still have any, without leaving the lasting impression you aren't really giving too much effort to this, and instead forcing others to teach you.

    The tutorials are here: http://www.cprogramming.com/tutorial.html
    Scroll that page and be amazed at what others have made available already. Be a chum and don't make them feel they have wasted their time explaining things.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #41
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37
    Then I guess you're saying that i'm asking for too much.... I undestand, but it wasn't really neccesary for you to UNDERLINE, READ and LEARN as if I never thought of any of that.

    I have been reading tutorials here and there. But I just can't seem to get it right and I didn't have Dev-C++ (now I do ofcourse). So that's why I decided to post here.

    I mean, this is my first attempt on using arrays... This thread started with me not knowing almost anything about arrays.

    Well okay then. I'll slow down my posts. But, I'm really getting worried. Thanks you all anyway.

  12. #42
    Registered User
    Join Date
    May 2006
    Posts
    903
    He didn't mean to hurt you. When you have a programming problem, use google and find tutorials on the topic you are having troubles with. If your compiler outputs an error message you don't understand, copy and paste that error message in google and chances are that you might find the meaning of the message. Also, don't take what the users on forums tell you as 100% correct. Someone pointed you to do this:

    void bubbleSort(int &numbers, int array_size)

    How could that be correct ? A reference to an integer... you are passing in an array. This cannot be correct.

    I think Stiopa has the answer to your problem though.

  13. #43
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37
    I'm not hurt,... everything is cool. And I get you guys.... I understand I mean.
    Quote Originally Posted by Desolation
    I think Stiopa has the answer to your problem though.
    Quote Originally Posted by Stiopa
    should be:

    Code:
    grades = numbers[j-1];
    numbers[j-1] = numbers [j];
    So now I know that my Dev-C++ actually isn't working properly, I had no time to get it right cause I had to go to sleep, Now I'm at my com lab testing my code!... So I adjusted this...

    And guess what!?... It's working!!!!!! I don't know what to do now. Should I cry or what?


    Thank you all guys!!... Now I'm gonna do more practicing with arrays and try to ask less repetitive questions.

  14. #44
    Registered User
    Join Date
    May 2006
    Posts
    903
    Wait.. you mean you used the same code with the proper modifications, thus getting rid of the bug, and then it worked ? And then you're saying Dev-C++ doesn't work ? Let me tell you, it IS working. I used that IDE for years and you know what ? It works.

  15. #45
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by Desolation
    Someone pointed you to do this:

    void bubbleSort(int &numbers, int array_size)

    How could that be correct ? A reference to an integer... you are passing in an array. This cannot be correct.

    I think Stiopa has the answer to your problem though.
    My mistake. What I really wanted to say was

    void bubbleSort(int *numbers, int array_size)

    But then again, it's practically the same with "int numbers[]". I was at the last minutes in the office so my head was so full. Couldn't think straight. I even saw the:

    grades = numbers[j-i];
    as

    grades = numbers[j-1];
    Sorry couldn't be of any help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Integer Emulation
    By Elysia in forum C++ Programming
    Replies: 31
    Last Post: 03-18-2008, 01:03 PM
  2. undefined reference
    By 3saul in forum Linux Programming
    Replies: 12
    Last Post: 08-23-2006, 05:28 PM
  3. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 12:55 PM
  4. MSVC Template Constructor/Assignment Errors
    By LuckY in forum Windows Programming
    Replies: 3
    Last Post: 07-22-2005, 02:57 PM
  5. Why is my program freezing?
    By ShadowMetis in forum Windows Programming
    Replies: 8
    Last Post: 08-20-2004, 03:20 PM