Hi everybody, I'm trying to develop an application for personal use, that will just make my life at work a lot easier and save me from doing a repetitive process by hand, when I know it could be done with relative ease in C++.
Anyway, I want to be able to read information from a file, and sort it, for example, CSV file contains letters followed by their frequency.
So
A 7
B 4
C 5
However, the sorting I want to do is to place the the letters as far away from each other as possible. I think I have found a way to do it;
I imagine the letter with highest frequency needs to be placed into an array first
step 1 : A A A A A A A
The second highest frequency would then need to be placed between the A's so:
step 2 : A C A C A C A C A C A A
In this case tho there are two adjacent As at then end.
So the Bs need to be placed in between the adjacent As
A C A C A C A C A C A B A
And then distribute the Bs equally
B A C A B C A C A B C A C A B A
All well and good in theory, but I'm not sure how to implement it, ideas?
I have been reading the forums, and luckily there were some threads about CSV files, so far I have got:
#include <stdio.h>
struct record
{
int a, b;
};
int main(void)
{
const char filename[] = "letters.csv";
FILE *file = fopen(filename, "r");
if ( file != NULL )
{
char line [ 80 ];
struct record record [ 750 ];
size_t count, i = 0;
while ( i < sizeof record / sizeof *record )
{
if ( fgets(line, sizeof line, file) == NULL )
{
break;
}
if ( sscanf(line, "%d,%d", &record[i].a, &record[i].b) == 2 )
{
++i;
}
}
}
Thanks in advance.