|
-
April 6th, 2009, 09:29 AM
#1
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
-
April 6th, 2009, 10:15 AM
#2
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.
-
April 6th, 2009, 10:18 AM
#3
Re: Newbie learning sorting algorithms and arrays
-
April 6th, 2009, 10:35 AM
#4
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];
}
-
April 6th, 2009, 10:58 AM
#5
Re: Newbie learning sorting algorithms and arrays
It helps if you actually read something from the file
-
April 6th, 2009, 12:54 PM
#6
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];
}
-
April 6th, 2009, 01:05 PM
#7
Re: Newbie learning sorting algorithms and arrays
Oops... my bad
-
April 6th, 2009, 01:06 PM
#8
Re: Newbie learning sorting algorithms and arrays
its all good, its monday lol, but something is still wrong and i cant figure out what
-
April 6th, 2009, 01:13 PM
#9
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.
-
April 6th, 2009, 01:25 PM
#10
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;
}
-
April 6th, 2009, 01:48 PM
#11
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
-
April 6th, 2009, 02:14 PM
#12
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
-
April 6th, 2009, 02:21 PM
#13
Re: Newbie learning sorting algorithms and arrays
 Originally Posted by peste19
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
-
April 6th, 2009, 02:26 PM
#14
Re: Newbie learning sorting algorithms and arrays
-
April 6th, 2009, 02:47 PM
#15
Re: Newbie learning sorting algorithms and arrays
 Originally Posted by peste19
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|