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

  1. #16
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >>You mean like this?

    Yes.

    Now change this:

    bubbleSort(int numbers[], int array_size); //this is a function declaration

    to this:

    bubbleSort(numbers[], array_size); //this is a function call

    and once you've sorted them display the sorted results as you indicated you would with this statement.

    cout << "Your sorted grades are: ";

    Lastly, download one of the free compilers/IDE, like Dev-C++, so you can do homework at home. It is almost impossible to do/learn a computer language without having a compiler in your immediate possession.
    Last edited by elad; 11-27-2006 at 11:14 AM.
    You're only born perfect.

  2. #17
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37
    Cool, thanks! So the final result should be like this:
    Code:
    #include <iostream.h>
    #include<iomanip.h>
    
    void bubbleSort(numbers[], int array_size);
    
    int main(void)
    {
    	int numbers[10], i, j;
    
    
    
    	for (int i = 1; i < 10; i++);
    	{
    		cout << "Enter 10 grades: ";
    		cin >> numbers[i];
    	}
    		cout << "Your sorted grades are: ";
    
    	bubbleSort(int numbers[], int array_size);
    
    	return 0;
    }
    
    void bubbleSort(numbers[], int array_size)
    {
    	int i, j, grades;
    
    	for (i = (array_size -1); i >=1; i--)
    	{
    		for (j = 1; j <=i; i++)
    		{
    			if (numbers[j-1] > numbers[j])
    			{
    				grades = numbers[j-i]
    				numbers[j-1] = numbers [j];
    				number[j] = grades;
    			}
    		}
    	}
    
    }
    And this should deffinitely work right?... No need to answer this.
    Quote Originally Posted by elad
    Lastly, download one of the free compilers/IDE, like Dev-C++, so you can do homework at home. It is almost impossible to do/learn a computer language without having a compiler in your immediate possession.
    Yeah, I tried that but had trouble. In the end I gave up and decided to by Microsof Visual,.. but so far I haven't.


    But how do I make these numbers so that the number input must be >= 0 and <= 100 ?


    Also I'm gonna try working on finding the average. But I cant understand this part
    Code:
    void mean( const int answer[], int arraySize )
    {
    	int total = 0;
    
    	cout << "********\n  Mean\n********\n";
    
    	for ( int j = 0; j < arraySize; j++ )
    		total += answer[ j ];
    
    	cout << "The mean is the average value of the data\n"
    		<< "items. The mean is equal to the total of\n"
    		<< "all the data items divided by the number\n"
    		<< "of data items (" << arraySize 
    		<< "). The mean value for\nthis run is: " 
    		<< total << " / " << arraySize << " = "
    		<< setiosflags( ios::fixed | ios::showpoint )
    		<< setprecision( 4 )
    		<< static_cast< double >( total ) / arraySize << "\n\n";
    }
    Mainly this part
    Code:
    		<< setiosflags( ios::fixed | ios::showpoint )
    		<< setprecision( 4 )
    		<< static_cast< double >( total ) / arraySize << "\n\n";

  3. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Cool, thanks! So the final result should be like this

    No. The function prototype should look like this:
    Code:
    void bubbleSort(int numbers[], int array_size);
    The function definition should look like this:
    Code:
    void bubbleSort(int numbers[], int array_size)
    and the function call should look like this:
    Code:
    bubbleSort(numbers, ???);
    where you replace ??? with whatever value is supposed to be there.

  4. #19
    Registered User
    Join Date
    May 2006
    Posts
    903
    Yeah, I tried that but had trouble. In the end I gave up and decided to by Microsof Visual,.. but so far I haven't.
    That would be the most stupid thing you could do, sorry. MVC++ is really expansive and Dev-C++ is *really* easy to install and make work. Simply go on bloodshed.net and download the latest edition (not beta). Create a project and paste your code then compile. It's simple as that and it costs nothing. Besides, it's really impossible to learn a computer language without a compiler. Geez, you don't even know what works and why. If you had a compiler at hand you could modify small parts of the code and see what happens.

  5. #20
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Actually, you want to download the beta (it's very stable): 4.9.9.2.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  6. #21
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37
    I had a thread here asking for help on downloading Dev C++. Just couldn't get it. Right now I'm in The Com Lab so I mannage to test the code.

    EDIT:
    Here's the code:
    Code:
    #include <iostream>
    #include<iomanip>
    
    void bubbleSort(int numbers[], int array_size);
    
    int main(void)
    {
    	int numbers[10];
    
    	for (int i = 1; i < 10; i++);
    	{
    		std::cout << "Enter 10 grades: ";
    		std::cin >> numbers[i];
    
    	}
    		std::cout << "Your sorted grades are: ";
    
    	bubbleSort(numbers, 10);
    
    	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; i++)
    		{
    			if (numbers[j-1] > numbers[j])
    			{
    				grades = numbers[j-i];
    				numbers[j-1] = numbers [j];
    				numbers[j] = grades;
    			}
    		}
    	}
    
    }
    Its not working. I was able to run it, but after imputing all the numbers, the sorted numbers doesn't come out.

    Why isn't it working?... I'm trying to fix it right now. Can't seem to understand. Alos, I didin't really understand this:
    Quote Originally Posted by Daved
    and the function call should look like this:
    Code:
    bubbleSort(numbers, ???);
    where you replace ??? with whatever value is supposed to be there.
    What value should be here?
    Last edited by [Z-D]; 11-27-2006 at 09:56 PM.

  7. #22
    Registered User
    Join Date
    May 2006
    Posts
    903
    void bubbleSort(int numbers[], int array_size)

    That value.

    Edit: By the way, try not to ever say "it's not working". If you are asking a question, chances are that it is because your code is either not working or not efficient enough for your needs. For example, you could have said " The program compiles without errors but when I run it, it doesn't output the sorted array".

    The answer to your problem is simple but try to figure it on your own. How do you output text to the screen ? Using cout. What did you do to output the array ?
    Last edited by Desolation; 11-27-2006 at 10:05 PM.

  8. #23
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37
    Quote Originally Posted by Desolation
    void bubbleSort(int numbers[], int array_size)

    that value.
    I don't know wheter I Edited my post for the thrid time before you posted this, but I used 10 in the '???' spot. But after inputing all the values, the sorted numbers doesn't show up.
    Quote Originally Posted by Desolation
    Edit: By the way, try not to ever say "it's not working". If you are asking a question, chances are that it is because your code is either not working or not efficient enough for your needs. For example, you could have said " The program compiles without errors but when I run it, it doesn't output the sorted array".
    Ooops. I forgot to take that out after Editing. Originally there were errors. So I used 'It's not working'... But I fixed it then edited this post, but left that in. Yeah, sorry for that...

    Quote Originally Posted by Desolation
    The answer to your problem is simple but try to figure it on your own. How do you output text to the screen ? Using cout. What did you do to output the array ?
    Thanks. I really appreciate that you want me to figure it out myself. I want to too. Okay so I tried
    Code:
    std::cout << "Your sorted grades are: " << bubbleSort;
    But some wierd code... I'm still trying to figure it out... working on it right now.
    Last edited by [Z-D]; 11-28-2006 at 12:27 AM.

  9. #24
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> What value should be here?
    10 is correct, that is the size of the array.

  10. #25
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37
    Plz plz plz...... I can't seem to be able to output the modified array. I tried somanythings.
    Right now I have this:
    Code:
    #include <iostream>
    #include<iomanip>
    
    void bubbleSort(int numbers[], int array_size);
    
    
    int main(void)
    {
    	int numbers[10];
    
    	for (int i = 1; i < 10; i++);
    	{
    		std::cout << "Enter 10 grades: ";
    		std::cin >> numbers[0];
    		std::cin >> numbers[1];
    		std::cin >> numbers[2];
    		std::cin >> numbers[3];
    		std::cin >> numbers[4];
    		std::cin >> numbers[5];
    		std::cin >> numbers[6];
    		std::cin >> numbers[7];
    		std::cin >> numbers[8];
    		std::cin >> numbers[9];
    	}
    
    	std::cout << "Your sorted grades are: " ;
    
    	bubbleSort(numbers, 10);
    
    	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; i++)
    		{
    			if (numbers[j-1] > numbers[j])
    			{
    				grades = numbers[j-i];
    				numbers[j-1] = numbers [j];
    				numbers[j] = grades;
    			}
    		}
    	}
    
    }
    I'm still a little confused with using
    Code:
    std::cin >> numbers[i];
    so that's why I used
    Code:
    std::cin >> numbers[0];
    std::cin >> numbers[1];
    std::cin >> numbers[2];
    std::cin >> numbers[3];
    std::cin >> numbers[4];
    ...

    So I tried looking and comparing with examples and adjusted my code to this:
    Code:
    #include <iostream>
    #include<iomanip>
    
    void bubbleSort(int numbers[], int array_size);
    
    
    int main(void)
    {
    	int i, numbers[10];
    
    	std::cout << "Enter 10 grades: ";
    	std::cin >> numbers[0];
    	std::cin >> numbers[1];
    	std::cin >> numbers[2];
    	std::cin >> numbers[3];
    	std::cin >> numbers[4];
    	std::cin >> numbers[5];
    	std::cin >> numbers[6];
    	std::cin >> numbers[7];
    	std::cin >> numbers[8];
    	std::cin >> numbers[9];
    
    	bubbleSort(numbers, 10);
    
    	std::cout << "Your sorted grades are: " ;
    	for (int i = 1; i < 10; i++)
    		std::cout << numbers[10];
    
    	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; i++)
    		{
    			if (numbers[j-1] > numbers[j])
    			{
    				grades = numbers[j-i];
    				numbers[j-1] = numbers [j];
    				numbers[j] = grades;
    			}
    		}
    	}
    
    }
    and I got one error
    Code:
    --------------------Configuration: sorting - Win32 Debug--------------------
    Compiling...
    sorting.cpp
    Z:\MM1 PST\C++\Assignment2\sorting\sorting.cpp(26) : error C2086: 'i' : redefinition
    Error executing cl.exe.
    
    sorting.obj - 1 error(s), 0 warning(s)

  11. #26
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by Desolation
    That would be the most stupid thing you could do, sorry. MVC++ is really expansive and Dev-C++ is *really* easy to install and make work. Simply go on bloodshed.net and download the latest edition (not beta). Create a project and paste your code then compile. It's simple as that and it costs nothing. Besides, it's really impossible to learn a computer language without a compiler. Geez, you don't even know what works and why. If you had a compiler at hand you could modify small parts of the code and see what happens.
    I personally really like VC++. I didn't find the learning curve significantly different than any other IDE I've used.

    Code:
    int i, numbers[10];
    You don't need the "i" part, just int numbers[10]. You declare i at the for loop, which is the much better place to do so.

    Code:
    for (j = 1; j <=i; i++)
    Shouldn't that be j++ ?

    Also I'd remove the "int i, j" and put "int" before i and j in the for loop, it is best practice to put a variable in the most narrow scope possible:

    Code:
    void bubbleSort(int numbers[], int array_size)
    {
    	int grades;
    
    	for (int i = (array_size -1); i >= 1; i--)
    	{
    		for (int j = 1; j <=i; j++)
    		{
    			// ...
    		}
    	}
    }
    The difference in scope is that in my version, i and j exist only inside their loops, and do not exist before or after. In this example it is irrelevant, but in general unless you need the value to exist outside the loop it shouldn't. Keep your variables existing in the smallest possible scope they can.
    Last edited by Cat; 11-28-2006 at 01:57 AM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  12. #27
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    and do not exist before or after
    it exists after

    Code:
    #include <iostream>
    
    
    int main()
    {
    	for(int i= 0; i<1; i++)
    		;
    
    	std::cout << i;
    	return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #28
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    >I personally really like VC++. I didn't find the learning curve significantly different than any >other IDE I've used.

    True Cat. Most people start off with DevC++ and move onto MSVC++ when they are more applyed to the language. I own DevC++ and MSVC++ 2003.net and use Dev for toy or testing little snippet then use MS when I create a large project, like a game for example or an application.

    For beginners, MS IDE's can be a little over-whelming.
    Double Helix STL

  14. #29
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37
    Quote Originally Posted by Cat
    I personally really like VC++. I didn't find the learning curve significantly different than any other IDE I've used.

    Code:
    int i, numbers[10];
    You don't need the "i" part, just int numbers[10]. You declare i at the for loop, which is the much better place to do so.
    Ohh,... Okay. I wasn't sure so I just put that in. Thanks.
    Quote Originally Posted by Cat
    Code:
    for (j = 1; j <=i; i++)
    Shouldn't that be j++ ?
    OH MY GOD!!!! Even I would know if I saw that!!... Why didn't I see that!?.....
    Quote Originally Posted by vart
    it exists after

    Code:
    #include <iostream>
    
    
    int main()
    {
    	for(int i= 0; i<1; i++)
    		;
    
    	std::cout << i;
    	return 0;
    }
    Yeah,... I think I understand that now. Thanks you all.


    So I'm gonna try installing Dev-C++ again. I hope I'm successful this time.... If I don't succeed, I'll have to test this code tomorrow.



    Oh, and another question. If I want to use
    Code:
    std::cin >> numbers[i];
    instead of
    Code:
    std::cin >> numbers[0];
    std::cin >> numbers[1];
    std::cin >> numbers[2];
    std::cin >> numbers[3];
    std::cin >> numbers[4];
    std::cin >> numbers[5];
    std::cin >> numbers[6];
    std::cin >> numbers[7];
    std::cin >> numbers[8];
    std::cin >> numbers[9];
    How should the code be input when the code is ran. Should there be spaces between each number? Because I wasn't sure, I used the long version..
    Last edited by [Z-D]; 11-28-2006 at 09:43 AM.

  15. #30
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37
    OMGOD!!!... I mannaged to install Dev-C++!!!..... All this while... fersds ...sdfdsf.. #$%#$%@


    WOW!!!!...... IT'S WORKING!!!!.... All this while it was so easy,... I don't know what I did last time!>!>SLJLADHLD

    @#$$%#%$@!

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