I'm trying to write this program that will read data
for one student and compute and print the students
average.
1)Define the template of a data structure to contain
the student's complete name,course number
(itec21011101), social security number(must be a long) and four test
grades(values 0 to 110), and an integer average. Define the template as
a global.
2) In main define the structure variable. The program
should pass a function to a pointer in the the
structure variable in main. The function will read in student's
name,course number(itec21011101), social security number and accept the
name for a max length of 20 characters, course number 11 characters and
social security number for 9 digits. (Use the function to get an integer
from lab 2 with the modification to allow for only 9 digits.)
There should be a separate function(Use the function
to get an integer from lab 2 ) to read in one grade
and and return it to main. This las function should
be executed four times. Check each grade and make
sure it is no more than three digits and the value of
each grade should be 0 to 110. Each grade should be in
the structure.
I must use the get avgerage function to compute the
grades dropping the lowest grade. When the average is
returned put in the structure average in main. Print
all information to the printer with all identifying
words from main.
I've been trying to fix this code to use the get_integer and get_avgerage function to include in the program. I haven't had much success with it and so I
left them as separate files and did not code them in my main program Lab3.c. What needs to be done so that I can use these two functions?
Below is the code for this lab. What's wrong with this code?
*-------------------------------------------------*/
/* Program Name: lab3.c */
/* program to enter information for up to 4 */
/* grades. The program prints a report */
/* based on the numbers entered. */
/*-------------------------------------------------*/
/*---------------------*/
/* included files */
/*---------------------*/
#include <stdio.h>
#include <stdlib.h>
#include "get_int.h"
#include "get_avg.h"
/*---------------------*/
/* defined constants */
/*---------------------*/
#define MAX 4
#define YES 1
#define NO 0
/*---------------------*/
/* variables */
/*---------------------*/
struct record {
char course_num[11+1]; /* course number + NULL */
char name[20+1]; /* last name + NULL */
long ssn[9+1]; /* social security + NULL */
int grade; /* student grades */
int average; /* student average */
};
struct record list[MAX]; /* declare actual structure */
int last_entry = 0; /* total number of entries */
/*---------------------*/
/* function prototypes */
/*---------------------*/
void main(void);
void get_data(void);
void display_report(void);
int continue_function(void);
void clear_kb(void);
/*---------------------*/
/* start of program */
/*---------------------*/
void main()
{
int cont = YES;
int ch;
while( cont == YES )
{
printf( "\n");
printf( "\n MENU");
printf( "\n ========\n");
printf( "\n1. Enter data");
printf( "\n2. Print report");
printf( "\n3. Quit");
printf( "\n\nEnter Selection ==> ");
ch = getchar();
fflush(stdin); /* remove extra characters from keyboard buffer */
switch( ch )
{
case '1': get_data();
break;
case '2': display_report();
break;
case '3': printf("\n\nThank you for using this program!\n");
cont = NO;
break;
default: printf("\n\nInvalid choice, Please select 1 to 3!");
break;
}
}
}
/*-----------------------------------------------------------*
* Function: get_data() *
* Purpose: This function gets the data from the user. It *
* continues to get data until either 4 grades are *
* entered, or the user chooses not to continue. *
* Returns: nothing *
* *
*-----------------------------------------------------------*/
void get_data(void)
{
int cont;
for ( cont = YES; last_entry < MAX && cont == YES;last_entry++ )
{
printf("\n\nEnter information for Person %d.",last_entry+1 );
printf("\n\nEnter name: ");
gets(list[last_entry].name);
printf("\nEnter ssn in 123456789 format: ");
scanf("%ld", &list[last_entry].ssn);
do
{ printf("\n\tGrades (0 - 110): ");
scanf("%d", &list[last_entry].grade);
}while ((list[last_entry].grade < 0) || (list[last_entry].grade > 110));
cont = continue_function();
}
if( last_entry == MAX)
printf("\n\nMaximum Number of grades has been entered!\n");
}
/*-----------------------------------------------------------*
* Function: display_report() *
* Purpose: This function displays a report to the screen *
* Returns: nothing *
* Notes: More information could be displayed. *
* Change stdout to stdprn to Print report *
*-----------------------------------------------------------*/
void display_report()
{
long grade_total = 0,
grand_total = 0; /* For totals */
int x, y;
fprintf(stdout, "\n\n"); /* skip a few lines */
fprintf(stdout, "\n REPORT");
fprintf(stdout, "\n ========");
for( x = 0; x <= 110; x++ ) /* for each grade, including 0 */
{ grade_total = 0;
for( y = 0; y < last_entry; y++ )
{ if( list[y].grade == x )
{ fprintf(stdout,"\n\t%s %s %s %ld",list[y].name,
list[y].course_num,list[y].ssn);
grade_total += list[y].grade;
}
}
fprintf(stdout, "\nTotal for grades %d is %d",x,grade_total);
grand_total += grade_total;
}
fprintf(stdout, "\n\n Grade Report totals:");
fprintf(stdout, "\nAverage Grade is %d", grand_total/last_entry );
fprintf(stdout, "\n\n* * * End of Report * * *");
}
/*-------------------------------------------------------------------*
* Function: continue_function() *
* Purpose: This function asks the user if they wish to continue. *
* Returns: YES - if user wishes to continue *
* NO - if user wishes to quit *
*-------------------------------------------------------------------*/
int continue_function( void )
{
int ch;
printf("\n\nDo you wish to continue? (Y)es/(N)o: ");
fflush(stdin);
ch = getchar();
while( ch != 'n' && ch != 'N' && ch != 'y' && ch != 'Y' )
{ printf("\n%c is invalid!", ch);
printf("\n\nPlease enter \'N\' to Quit or \'Y\' to Continue: ");
fflush(stdin); /* clear keyboard buffer (stdin) */
ch = getchar();
}
clear_kb(); /* this function is similar to fflush(stdin) */
if(ch == 'n' || ch == 'N')
return(NO);
else
return(YES);
}
/*--------------------------------------------------------------------*
* Function: clear_kb() *
* Purpose: This function clears keyboard of extra characters. *
* Returns: Nothing *
* Note: This function could be replaced by fflush(stdin); *
*--------------------------------------------------------------------*/
void clear_kb(void)
{
char junk[80];
gets(junk);
}
************************************************** *
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
/*-- This function gets the input string and checks for correct values --*/
void get_integer(char strings[] , int number)
{
char dummy [21] ; /*-- string to be used to get correct value --*/
int flags;
int index;
do
{ fflush(stdin) ;
flags = 0 ;
gets(dummy) ; /*-- string is entered --*/
if((strlen(dummy) > number)) /*-- if the string length of dummy is greater than 4 --*/
{ clrscr();
printf("Enter a digit: ");
flags = 1;
continue;
}
/*-- this checks for non-digits if the string length is the correct length --*/
for(index = 0 ; dummy [index] != '\0'; index++)
if (( isdigit(dummy[index]) == 0 ))
{ clrscr();
printf("Only digits please.\n");
flags = 1;
break;
}
}while(flags == 1) ; /*-- continues while input is bad --*/
/*-- copy the dummy string that is now valid into the user string --*/
strcpy(strings,dummy) ;
}
************************************************** **
/*-- this function recieves the input array and size of the array --*/
int get_average(int numbers[] , int number)
{
long lowest;
float sum = 0;
int average;
int index;
for( index = 0 ; index < number ; index++)
{ if(index == 0) lowest = numbers[0];
if(numbers [index] < lowest)
lowest = numbers [index] ;
sum += numbers [index] ; }
average = (sum - lowest) / (number - 1+ .5) ; return(int) average ;
}