Re: "Sorting" assignment
- From: "Bartc" <bc@xxxxxxxxxx>
- Date: Wed, 06 Feb 2008 11:01:35 GMT
"Ivica" <prljavi_bluzer@xxxxxxxxxxx> wrote in message
news:foagu1$pkc$1@xxxxxxxxxxxxxxxxx
"Ivica" <prljavi_bluzer@xxxxxxxxxxx> wrote in message
news:fo514c$li8$1@xxxxxxxxxxxxxxxxx
cut
Here is some nasty code, I've translated it from Croatian. Hope it makes
sense.
However, sorting looks pretty nasty and it's written badly. Any
suggestions in changing?
I am stuck with the deadline, only tomorrow is left for me for having fun
Sorry I thought the guys in comp.lang.c would be more helpful, but sometimes
they like to bicker amongst themselves.
I've changed the duplicated stuff in your code, and given the revised code
below. The new1...new9 are replaced by a single array newsarray[].
For finding out the 1st, 2nd, 3rd most read news, I've only changed the
first block, the rest are similar (the approach used is not ideal, but if
you are going to use it, might as well use a double nested loop, for
array[0], array[1], etc. Then you can show the top news 1000 if needed
without any extra code!).
Bart
#include <stdlib.h>
#include <stdio.h>
#define MAXLENGTH 20
#define elementtype News
#define N 9 //number of news
typedef struct {
int ID;
char name[60];
char text[500]; //structure
int read;
} News;
typedef struct {
int last;
elementtype elements[MAXLENGTH];
} LIST;
void bubblesort(int *array){
int i,j,k;
for (i=0; i<N; i++){
for (j=N-1; j>i; j--){
if (array[j-1]<array[j]){ //Bubble sort
k=array[j];
array[j]=array[j-1];
array[j-1]=k;
}
}
}
}
int main()
{
int n; /* how many times we will allow analysis before
reading */
int decision;
int i;
News newsarray[N] = {{1,"heading 1.", "body 1"},
{2,"heading 2.", "body 2"},
{3,"Heading 3.", "body 3"},
{4,"heading 4.", "body 4"},
{5,"heading 5.", "body 5"},
{6,"heading 6.", "body 6"},
{7,"Heading 7.", "body 7"},
{8,"Heading 8.", "body 8"},
{9,"heading 9.", "body 9"}};
printf("\t\t\tWelcome to the news sorting!\n\n\n\n");
for (n=0;n<15;n++){
for (i=0; i<N; ++i)
printf("%d--> %s\n", newsarray[i].ID, newsarray[i].name,0);
printf("\n");
printf("Enter number + ENTER for reading: ");
scanf("%d",&decision);
if (decision==0) break; /* early exit */
--decision; /* change to 0-based */
printf("---------------------------------------\n%s\n",
newsarray[decision].text);
newsarray[decision].read++;
};
system("pause");
system("cls");
int array[N];
/*={new1.read, new2.read, new3.read, new4.read, new5.read, new6.read,
new7.read, new8.read, new8.read}; */
for (i=0; i<N; ++i)
array[i]=newsarray[i].read;
bubblesort(array); // call bubblesort
//Most read news-------------------------------
for (i=0; i<N; ++i);
if (newsarray[i].read==array[0])
{ printf("Most read news is:\t%s\n", newsarray[i].name);
// break;
i=N; /* break not allowed for some reason */
};
/*
//2. Second most read news-------------------------------
if (new1.read==array[1]){
printf("2. Most read news is:\t%s", new1.name);}
if (new2.read==array[1]){
printf("2. Most read news is:\t%s", new2.name);}
if (new3.read==array[1]){
printf("2. Most read news is:\t%s", new3.name);}
if (new4.read==array[1]){
printf("2. Most read news is:\t%s", new4.name);}
if (new5.read==array[1]){
printf("2. Most read news is:\t%s", new5.name);}
if (new6.read==array[1]){
printf("2. Most read news is:\t%s", new6.name);}
if (new7.read==array[1]){
printf("2. Most read news is:\t%s", new7.name);}
if (new8.read==array[1]){
printf("2. Most read news is:\t%s", new8.name);}
if (new9.read==array[1]){
printf("2. Most read news is:\t%s", new9.name);}
//-------------------------------------------------
printf("\n");
//3. most read news-------------------------------
if (new1.read==array[2]){
printf("3. Most read news is:\t%s", new1.name);}
if (new2.read==array[2]){
printf("3. Most read news is:\t%s", new2.name);}
if (new3.read==array[2]){
printf("3. Most read news is:\t%s", new3.name);}
if (new4.read==array[2]){
printf("3. Most read news is:\t%s", new4.name);}
if (new5.read==array[2]){
printf("3. Most read news is:\t%s", new5.name);}
if (new6.read==array[2]){
printf("3. Most read news is:\t%s", new6.name);}
if (new7.read==array[2]){
printf("3. Most read news is:\t%s", new7.name);}
if (new8.read==array[2]){
printf("3. Most read news is:\t%s", new8.name);}
if (new9.read==array[2]){
printf("3. Most read news is:\t%s", new9.name);}
//-------------------------------------------------
*/
printf("\n\nDiffernce between the TOP news and one in the middle is:
%d - %d = %d", array[0], array[4], array[0] - array[4]);
printf("\n\n");
system("pause");
return 0;
}
.
- Follow-Ups:
- Re: "Sorting" assignment
- From: Ivica
- Re: "Sorting" assignment
- References:
- "Sorting" assignment
- From: Ivica
- Re: "Sorting" assignment
- From: Ivica
- "Sorting" assignment
- Prev by Date: Re: "Sorting" assignment
- Next by Date: Re: "Sorting" assignment
- Previous by thread: Re: "Sorting" assignment
- Next by thread: Re: "Sorting" assignment
- Index(es):
Relevant Pages
|