CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    Feb 2009
    Posts
    35

    Newbie learning sorting algorithms and arrays

    I am trying to create a program which read data and sorts it out, the data file contains id and grades of those ids and i am have to sort by order of grade and move the id with it, here is where i got so far but it is not working for me.

    thanks in advance

    Code:
    #include <iomanip>
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main ()
    {
    	int n,f,j,id[12],i;
    	double grade[12],hold;
    	ifstream input;
    
    	
    	for (f=0;f<12;f++)
    	{
    		input >> id[f];
    		input >> grade[f];
    	}
    	for (j=0; j<12; j++)
    	{
    		for (i=0; i<12; i++);
    		{
    			 if (grade[i-1] > grade[i])
    			 {
    				 hold = grade[i];
    				 grade [i-1] = grade[i];
    				 grade[i] = hold;
    			 }
    			 cout << grade[i] << "\n";
    		}
    	}
    	input.close();
    	system ("pause");
    	return 0;
    }
    Last edited by peste19; April 6th, 2009 at 01:56 PM. Reason: updated code

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Newbie learning sorting algorithms and arrays

    Code:
    input >> id[12];
    input >> grade[12];
    Are you going to put everything in a non-existing element ? 12 elements means from 0 to 11.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Newbie learning sorting algorithms and arrays

    This thread is a duplicate of Newbie learning sorting algorithms and arrays.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Feb 2009
    Posts
    35

    Re: Newbie learning sorting algorithms and arrays

    i was trying to delete the other one since its a wrong section of the forum but i cant find the option to delete it.

    @ Skizmo

    yea i noticed i messed up there i have change it to this

    Code:
    	for (f=0;f<=n;f++);
    	{
    		input >> id[f];
    		input >> grade[f];
    	}

  5. #5
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Newbie learning sorting algorithms and arrays

    It helps if you actually read something from the file

  6. #6
    Join Date
    Feb 2009
    Posts
    35

    Re: Newbie learning sorting algorithms and arrays

    but i have the command here or am i missing something

    Code:
    ifstream input;
    			
    	input.open("data.txt");
    	cout << "read how many entries" << endl;
    	cin >> n;
    	
    	for (f=0;f<=n;f++);
    	{
    		input >> id[f];
    		input >> grade[f];
    	}

  7. #7
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Newbie learning sorting algorithms and arrays

    Oops... my bad

  8. #8
    Join Date
    Feb 2009
    Posts
    35

    Re: Newbie learning sorting algorithms and arrays

    its all good, its monday lol, but something is still wrong and i cant figure out what

  9. #9
    Join Date
    Jul 2005
    Posts
    266

    Re: Newbie learning sorting algorithms and arrays

    Your sorting algorithm is wrong. Take a look at these:
    http://mathbits.com/MathBits/CompSci/Arrays/Arrays1.htm
    Mainly the topics from Bubble sort to Selection sort since they are the simplest.

  10. #10
    Join Date
    Feb 2009
    Posts
    35

    Re: Newbie learning sorting algorithms and arrays

    i changed it to this but not it only reads the fist set of data of the file
    Code:
    #include <iomanip>
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main ()
    {
    	int f,j,id[12],i;
    	double grade[12],hold;
    	ifstream input;
    			
    	input.open("data.txt");
    		
    	for (f=0;f<=12;f++);
    	{
    		input >> id[f];
    		input >> grade[f];
    	}
    	for (j=0; j<=12; j++);
    	{
    		for (i=0; i<=12; i++);
    		{
    			 if (grade[i+1] > grade[i])
    			 {
    				 hold = grade[i];
    				 grade [i] = grade[i+1];
    				 grade[i+1] = hold;
    			 }
    			 cout << grade[i] << "\n";
    		}
    	}
    	input.close();
    	system ("pause");
    	return 0;
    }

  11. #11
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: Newbie learning sorting algorithms and arrays

    All your loops have an empty body
    Code:
    	for (f=0;f<=12;f++);  // remove semicolon
    Also should the condition be f < 12; because there are only 12 elements.

    In the other loops you even access the elements with index 13 ( e.g. grade [i] = grade[i+1]; )

    You should swap the id elements as well.

    Kurt

  12. #12
    Join Date
    Feb 2009
    Posts
    35

    Re: Newbie learning sorting algorithms and arrays

    finally got it to work but now i have a question how would i add names for the program to read? my professor said we cannot use string with arrays

    thanks in advance

  13. #13
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: Newbie learning sorting algorithms and arrays

    Quote Originally Posted by peste19 View Post
    my professor said we cannot use string with arrays
    If he said it cannot be done then he lied. If he said you shouldn't then he's trying to make your life harder then necessary.
    So what are you supposed to use for the names ?
    Kurt

  14. #14
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: Newbie learning sorting algorithms and arrays

    [ moved thread ]

  15. #15
    Join Date
    Apr 2009
    Location
    Russia, Nizhny Novgorod
    Posts
    99

    Re: Newbie learning sorting algorithms and arrays

    Quote Originally Posted by peste19 View Post
    my professor said we cannot use string with arrays
    thanks in advance
    if you can't use arrays and strings you can still use pointers
    but I believe you've understood your professor incorrectly

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured