Hello Folks!
I am going NUTS!!!
I just can't get the last bit of my code to do it right.
Please somebody have a look and see what I am missing?
Thank you very much in advance.
/* Assignment 4 Sorting and Searching Students Records */
// Date: 15.10.01 by Nearly there!!!!!!!
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define COUNT 2
typedef struct RECORD {
int num;
char name[10];
float score;
}student;
int index, option, item;
void showmenu(void);
void dataentry(void);
void sortrec(void);
void calcavg(RECORD list[COUNT],int);
void searchncomp(RECORD list[COUNT],int);
float scoresum, avg, target;
struct RECORD list[COUNT];
void bubble(RECORD list[COUNT],int);
int main()
{
clrscr();
for(;
{
showmenu();
}
}
void showmenu(void)
{
printf("\tMENU:\n"
printf("\t-----\n"
printf("1. Enter 5 student records.\n"
printf("2. Display the 5 records in ascending order by record number.\n"
printf("3. Calculate class average.\n"
printf("4. Search and compare student score to average.\n"
printf("5. Exit the program.\n"
printf("Please enter a selection: \n"
scanf("%d", &option);
switch(option)
{
case 1: dataentry();
break;
case 2: sortrec();
break;
case 3: calcavg(list, COUNT);
break;
case 4: searchncomp(list, COUNT);
break;
case 5: exit(0);
break;
default: printf("Error! Try again.\n\n"
}
}
void dataentry(void)
{
for (index = 0; index < COUNT; ++index)
{
printf("Enter student number: "
scanf("%d",&list[index].num);
printf("Enter name: "
scanf("%s",&list[index].name);
printf("Enter student score: "
scanf("%f",&list[index].score);
}
printf("Press a key to continue.\n\n"
getch();
}
void sortrec(void)
{
bubble(list, COUNT);
printf("Sorted list by ascending student number is : \n"
{
for (index=0; index < COUNT; ++index)
printf("%10d", list[index].num);
printf("\n"
for (index=0; index < COUNT; ++index)
printf("%10s", list[index].name);
printf("\n"
for (index=0; index < COUNT; ++index)
printf("%10.2f", list[index].score);
}
printf("\nPress a key to continue.\n\n"
getch();
}
void bubble(RECORD list[], int n)
{
int i, pass, sorted;
RECORD temp;
clrscr();
pass = 1;
do{
sorted = 1;
for(i=0; i<n - pass; ++i)
{
if (list.num > list[i+1].num)
{
temp = list;
list = list[i+1];
list[i+1] = temp;
sorted = 0;
}
}
++pass;
} while(!sorted);
}
void calcavg(RECORD list[COUNT], int)
{
scoresum = avg = 0;
for (index=0; index < COUNT; ++index)
scoresum = scoresum + list[index].score;
avg = scoresum / COUNT;
printf("\nAverage is %.2f\n", avg);
printf("Press a key to continue.\n\n"
getch();
}
void searchncomp(RECORD list[COUNT], int)
{
item = 0;
index = 0;
printf("Please enter student number for search 'n compare: \n"
scanf("%d", &item);
do
{
if(item == (list[index].num))
{
printf("Score is %.2f. ", list[index].score);
if(list[index].score > avg)
printf("Above average. Press a key to continue\n"
else
printf("Below average. Press a key to continue\n"
}
else
printf("Error! Record not found. Press a key to continue.\n"
index++;
}
while (index > COUNT);
getch();
}
I am going NUTS!!!
I just can't get the last bit of my code to do it right.
Please somebody have a look and see what I am missing?
Thank you very much in advance.
/* Assignment 4 Sorting and Searching Students Records */
// Date: 15.10.01 by Nearly there!!!!!!!
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define COUNT 2
typedef struct RECORD {
int num;
char name[10];
float score;
}student;
int index, option, item;
void showmenu(void);
void dataentry(void);
void sortrec(void);
void calcavg(RECORD list[COUNT],int);
void searchncomp(RECORD list[COUNT],int);
float scoresum, avg, target;
struct RECORD list[COUNT];
void bubble(RECORD list[COUNT],int);
int main()
{
clrscr();
for(;
{
showmenu();
}
}
void showmenu(void)
{
printf("\tMENU:\n"
printf("\t-----\n"
printf("1. Enter 5 student records.\n"
printf("2. Display the 5 records in ascending order by record number.\n"
printf("3. Calculate class average.\n"
printf("4. Search and compare student score to average.\n"
printf("5. Exit the program.\n"
printf("Please enter a selection: \n"
scanf("%d", &option);
switch(option)
{
case 1: dataentry();
break;
case 2: sortrec();
break;
case 3: calcavg(list, COUNT);
break;
case 4: searchncomp(list, COUNT);
break;
case 5: exit(0);
break;
default: printf("Error! Try again.\n\n"
}
}
void dataentry(void)
{
for (index = 0; index < COUNT; ++index)
{
printf("Enter student number: "
scanf("%d",&list[index].num);
printf("Enter name: "
scanf("%s",&list[index].name);
printf("Enter student score: "
scanf("%f",&list[index].score);
}
printf("Press a key to continue.\n\n"
getch();
}
void sortrec(void)
{
bubble(list, COUNT);
printf("Sorted list by ascending student number is : \n"
{
for (index=0; index < COUNT; ++index)
printf("%10d", list[index].num);
printf("\n"
for (index=0; index < COUNT; ++index)
printf("%10s", list[index].name);
printf("\n"
for (index=0; index < COUNT; ++index)
printf("%10.2f", list[index].score);
}
printf("\nPress a key to continue.\n\n"
getch();
}
void bubble(RECORD list[], int n)
{
int i, pass, sorted;
RECORD temp;
clrscr();
pass = 1;
do{
sorted = 1;
for(i=0; i<n - pass; ++i)
{
if (list.num > list[i+1].num)
{
temp = list;
list = list[i+1];
list[i+1] = temp;
sorted = 0;
}
}
++pass;
} while(!sorted);
}
void calcavg(RECORD list[COUNT], int)
{
scoresum = avg = 0;
for (index=0; index < COUNT; ++index)
scoresum = scoresum + list[index].score;
avg = scoresum / COUNT;
printf("\nAverage is %.2f\n", avg);
printf("Press a key to continue.\n\n"
getch();
}
void searchncomp(RECORD list[COUNT], int)
{
item = 0;
index = 0;
printf("Please enter student number for search 'n compare: \n"
scanf("%d", &item);
do
{
if(item == (list[index].num))
{
printf("Score is %.2f. ", list[index].score);
if(list[index].score > avg)
printf("Above average. Press a key to continue\n"
else
printf("Below average. Press a key to continue\n"
}
else
printf("Error! Record not found. Press a key to continue.\n"
index++;
}
while (index > COUNT);
getch();
}