I got an assigment that I need help with and I'm hoping that someone could give me some help with it. Here is what it's asking:
The data file contains lines of text, some of which are tab-delimited price entries, while others are text.
Each price line is easily distinguished by the fact that it begins with a digit. Each price line has an Intel CPU's
current price, followed by a tab character, followed by the CPU's name.
Begin by defining a struct type that can store a CPU name and its price. Write a C program that includes
three separate functions:
1. read_price_list() reads the price list from the data file and stores it in an array of your structs,
2. sort_price_list() sorts the price list by decreasing price,
3. write_price_list() writes the sorted price list into a new data file Sorted_Price_List.txt.
The sorted price list file must contain only sorted price data: a CPU price, followed by a tab, followed by the
CPU's name; one CPU per line.
Here is what we have to sort:
Intel® Processor Pricing
Effective Apr 29, 2012
Recommended Customer Price Tray Units
Intel® Core™ i7 processor Extreme Edition Desktop (LGA2011)
999i7-3960X (15M cache, 6 Cores, 12 Threads, 3.30 GHz, 32nm)
Intel® Core™ i7 processor Desktop (LGA2011/1155)
583i7-3930K (12M cache, 6 Cores, 12 Threads, 3.20 GHz, 32nm)
294i7-3820 (10M cache, 4 Cores, 8 Threads, 3.60 GHz. 32nm)
332i7-3770K (8M cache, 4 Cores, 8 Threads, 3.50 GHz. 22nm)
294i7-3770 (8M cache, 4 Cores, 8 Threads, 3.40 GHz. 22nm)
332i7-2700K (8M cache, 4 Cores, 8 Threads, 3.50 GHz, 32nm)
317i7-2600K (8M cache, 4 Cores, 8 Threads, 3.40 GHz, 32nm)
294i7-2600 (8M cache, 4 Cores, 8 Threads, 3.40 GHz, 32nm)
Intel® Core™ i5 processor Desktop (LGA1155)
225i5-3570K (6M cache, 4 Cores, 4 Threads, 3.40 GHz, 22nm)
205i5-3550 (6M cache, 4 Cores, 4 Threads, 3.30 GHz, 22nm)
184i5-3450 (6M cache, 4 Cores, 4 Threads, 3.10 GHz, 22nm)
225i5-2550K (6M cache, 4 Cores, 4 Threads, 3.40 GHz, 32nm)
216i5-2500K (6M cache, 4 Cores, 4 Threads, 3.30 GHz, 32nm)
205i5-2500 (6M cache, 4 Cores, 4 Threads, 3.30 GHz, 32nm)
195i5-2450P (6M cache, 4 Cores, 4 Threads, 3.20 GHz, 32nm)
184i5-2400 (6M cache, 4 Cores, 4 Threads, 3.10 GHz, 32nm)
177i5-2380P (6M cache, 4 Cores, 4 Threads, 3.10 GHz, 32nm)
177i5-2320 (6M cache, 4 Cores, 4 Threads, 3.00 GHz, 32nm)
177i5-2310 (6M cache, 4 Cores, 4 Threads, 2.90 GHz, 32nm)
There's more to sort but it takes up alot of space.
And he gave us a skeleton source file:
#include
#include
#include
typedef struct {
/* fill in */
} price_entry;
/* Reads a price list from a file and stores it in an array of structs.
Returns the number of price entries read from the file. */
int read_price_list(const char* input_name, price_entry* list, int list_size) {
/* only lines beginning with a digit contain price entries, ignore all other lines */
/* fill in */
}
int compare_price_entries(const price_entry* entry1, const price_entry* entry2) {
/* fill in */
}
/* Sorts a price list by decreasing price. */
void sort_price_list(price_entry* list, int list_size) {
/* use qsort() in conjunction with the compare_price_entries above, or write your own sort algorithm */
/* fill in */
}
/* Writes a price list into a new file. */
void write_price_list(const price_entry* list, int list_size, const char* output_name) {
/* fill in */
}
int main() {
int entry_count;
entry_count = read_price_list("Apr_29_12_Recommended_C… /* fill in */);
sort_price_list(/* fill in */);
write_price_list(/* fill in */, "Sorted_Price_List.txt");
return 0;
}
I've been working on in since Wed and I am completely stuck and any kind of help would be appreciated.
VulpesPosted May 8, 2012, 11:32 AM
I've assumed - rightly or wrongly - that the name of the processor doesn't include the stuff in brackets; it's just something like i7-3960X. If this assumption is wrong, then you'll need to use a different approach to sscanf() to split the lines in the file:
#include
#include
#include
typedef struct
{
int price;
char name[9]; /* assuming processor name not longer than 8 characters */
} price_entry;
/* Reads a price list from a file and stores it in an array of structs.
Returns the number of price entries read from the file. */
int read_price_list(const char* input_name, price_entry* list, int list_size)
{
char str[80]; /* assume no line is longer than 80 characters */
FILE *fp;
int index = 0;
if((fp = fopen(input_name, "r")) == NULL)
{
printf("Cannot open input file\n");
exit(1);
}
while(!feof(fp))
{
if (index == list_size) return;
fgets(str, 79, fp);
if(strlen(str) > 1 && str[0] >= '0' && str[0] <= '9') /* if the line starts with a digit */
{
sscanf(str, "%d%s", &list[index].price, &list[index].name);
index++;
}
}
fclose(fp);
return index; /* total number of entries */
}
/* sorts price_entry structs by decreasing price */
int compare_price_entries(const price_entry* entry1, const price_entry* entry2)
{
return entry2->price - entry1->price;
}
/* Sorts a price list by decreasing price. */
void sort_price_list(price_entry* list, int list_size)
{
qsort(list, list_size, sizeof(price_entry), (int(_cdecl *)(const void*, const void*))compare_price_entries);
}
/* Writes a price list into a new file. */
void write_price_list(const price_entry* list, int list_size, const char* output_name)
{
FILE *fp;
int index = 0;
if((fp = fopen(output_name, "w")) == NULL)
{
printf("Cannot open output file\n");
exit(1);
}
while(index < list_size)
{
fprintf(fp, "%d", list[index].price);
fputs("\t", fp);
fputs(list[index].name, fp);
fputs("\n", fp);
index++;
}
fclose(fp);
}
int main()
{
int entry_count;
price_entry list[20]; /* will need to adjust the size of this array if there are more entries in the file */
entry_count = read_price_list("Apr_29_12_Recommended_C.txt", list, 20);
sort_price_list(list, entry_count);
write_price_list(list, entry_count, "Sorted_Price_List.txt");
return 0;
}
MaximilianPosted May 8, 2012, 5:21 AM
:D