Re: "Sorting" assignment
- From: Ben Bacarisse <ben.usenet@xxxxxxxxx>
- Date: Tue, 05 Feb 2008 23:42:03 +0000
"Ivica" <prljavi_bluzer@xxxxxxxxxxx> writes:
"Ivica" <prljavi_bluzer@xxxxxxxxxxx> wrote in message
news:fo514c$li8$1@xxxxxxxxxxxxxxxxx
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?
The sorting code is fine, it is the rest that needs work. OK, I'd
choose another sort, but that is not really the problem. Looking at
the code, I don't think you understand arrays yet. The deadline means
that learning about arrays may have to wait, but don't put it off --
get a good book (do you have access to a good library?) and work
through some examples of using arrays.
I am stuck with the deadline, only tomorrow is left for me for having fun
with this code.
Thanks in advance. (even to the "now filtered spinoza moron")
#include <stdlib.h>
#include <stdio.h>
#define MAXLENGTH 20
#define elementtype News
Macros should have BIG_NAMES so everyone knows.
#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;
This is confusing: elementype is a macro defined to be News with is
typedef for a struct. It is almost like you want to hide the type.
I'd just give the struct a tag and use that so everyone knows what is
going on:
struct news { ... };
struct list {
int last;
struct news elements[MAXLENGTH];
} LIST;
You don't get extra marks (in my class) for using more language
features -- especially if they obscure things. Anyway, it turns out
you don't use this type at all, so you should get rid of it.
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;
}
}
}
}
OK, but this looks like it is right out of a book (or web site). Why?
Because if you could have written it, why not do the simpler array
manipulations required by the code below? I am not getting at you,
just giving you a teacher's-eye view of your work.
Another give-away is that the function is not properly parametrised --
you have written a function (good) that can sort any array of ints
(good) provided it has N == 9 elements (bad). You should pass in the
size of the array.
int main()
{
int n; // how many times we will allow analysis before
reading
int decision;
News new1 = {1,"heading 1.", "body 1"};
News new2 = {2,"heading 2.", "body 2"};
News new3 = {3,"Heading 3.", "body 3"};
News new4 = {4,"heading 4.", "body 4"};
News new5 = {5,"heading 5.", "body 5"};
News new6 = {6,"heading 6.", "body 6"};
News new7 = {7,"Heading 7.", "body 7"};
News new8 = {8,"Heading 8.", "body 8"};
News new9 = {9,"heading 9.", "body 9"};
We have computers so we don't have to write this kind of thing! Every
time you find your self writing repetitive code you should put it in a
function (or at least wrap it up as the body of a loop). I won't
repeat myself, but there a lots of example below.
printf("\t\t\tWelcome to the news sorting!\n\n\n\n");
for (n=0;n<15;n++){
printf("%d--> %s\n", new1.ID, new1.name,0);
printf("%d--> %s\n", new2.ID, new2.name,0);
printf("%d--> %s\n", new3.ID, new3.name,0);
printf("%d--> %s\n", new4.ID, new4.name,0);
printf("%d--> %s\n", new5.ID, new5.name,0);
printf("%d--> %s\n", new6.ID, new6.name,0);
printf("%d--> %s\n", new7.ID, new7.name,0);
printf("%d--> %s\n", new8.ID, new8.name,0);
printf("%d--> %s\n\n", new9.ID, new9.name,0);
What is the extra 0 there for?
printf("Enter number + ENTER for reading: ");
scanf("%d",&decision);
if (decision==1){
printf("---------------------------------------\n%s\n",
new1.text);
new1.read++;}
if (decision==2){
printf("---------------------------------------\n%s\n",
new2.text);
new2.read++;}
if (decision==3){
printf("---------------------------------------\n%s\n",
new3.text);
new3.read++;}
if (decision==4){
printf("---------------------------------------\n%s\n",
new4.text);
new4.read++;}
if (decision==5){
printf("---------------------------------------\n%s\n",
new5.text);
new5.read++;}
if (decision==6){
printf("---------------------------------------\n%s\n",
new6.text);
new6.read++;}
if (decision==7){
printf("---------------------------------------\n%s\n",
new7.text);
new7.read++;}
if (decision==8){
printf("---------------------------------------\n%s\n",
new8.text);
new8.read++;}
if (decision==9){
printf("---------------------------------------\n%s\n",
new9.text);
new9.read++;}
system("pause");
system("cls"); }
At this point I am thinking... So the student can
int array[N]={new1.read, new2.read, new3.read, new4.read, new5.read,
new6.read, new7.read, new8.read, new8.read};
bubblesort(array); // call bubblesort
Classic redundant comment. Comments should explain things for people
who know the language but not your mind.
//Most read news-------------------------------
if (new1.read==array[0]){
printf("Most read news is:\t%s", new1.name);}
if (new2.read==array[0]){
printf("Most read news is:\t%s", new2.name);}
if (new3.read==array[0]){
printf("Most read news is:\t%s", new3.name);}
if (new4.read==array[0]){
printf("Most read news is:\t%s", new4.name);}
if (new5.read==array[0]){
printf("Most read news is:\t%s", new5.name);}
if (new6.read==array[0]){
printf("Most read news is:\t%s", new5.name);}
if (new7.read==array[0]){
printf("Most read news is:\t%s", new7.name);}
if (new8.read==array[0]){
printf("Most read news is:\t%s", new8.name);}
if (new9.read==array[0]){
printf("Most read news is:\t%s", new9.name);}
//-------------------------------------------------
This is repetitive again, but the real problem is a logical one.
Having sorted the list of number of times the news has been read you
have lost the linkage between that number and the news item itself so
you have to search the list again for the matching item (or items).
Ideally you should sort the list of news items themselves, or at least
something the references the news items. A C programmer would choose
to sort an array of "struct news *" (pointers to news structures).
You then would have access to every part of the most-read as
news[0]->name, news[0]->read and so on.
printf("\n");
//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");
Is there some real programming system that requires this sort of
thing? When I run a program, I get to see the output and it stays
there!
return 0;
}
--
Ben.
.
- 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
|