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;
}
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.
Re: Newbie learning sorting algorithms and arrays
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];
}
Re: Newbie learning sorting algorithms and arrays
It helps if you actually read something from the file ;)
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];
}
Re: Newbie learning sorting algorithms and arrays
Re: Newbie learning sorting algorithms and arrays
its all good, its monday lol, but something is still wrong and i cant figure out what
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.
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;
}
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
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
Re: Newbie learning sorting algorithms and arrays
Quote:
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
Re: Newbie learning sorting algorithms and arrays
Re: Newbie learning sorting algorithms and arrays
Quote:
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