Link to home
Start Free TrialLog in
Avatar of elwayisgod
elwayisgodFlag for United States of America

asked on

Please answer tonight!!!!! I'm desparate. Calling a mulitdimensional array from a function

I cant get the getname function and the associated array in main to work properly. Can someone post the correct code and I'll up the point significantly..  Please answer.  Heres my code:


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define EXAM_1_WEIGHT .35
#define EXAM_2_WEIGHT .25
#define PROJECT_WEIGHT .40
#define MAX 100
 

void DisplayGreeting(void);
long GetId(void);
char GetName(void);
short GetScore(void);
short ComputeExamDifference(short, short);
float ComputeExamAverage(short, short);
float ComputeCourseAverage(short, short, short);
char ComputeGrade(float);
void GenerateCourseReport(long, short, short, short, short, float, float, char);
void GenerateSummaryReport(long[], float[], char[], short);

main()
{
      long id;
      short exam1, exam2, project, difference;
      float exam_average, course_average;
      char grade;
      short counter = 0;
      long id_array[MAX];
      float avg_array[MAX];
      char grade_array[MAX];
      char name;
      char name_array[MAX][MAX];

      DisplayGreeting();
      while(1)
      {
            id = GetId();
            id_array[counter] = id;
            
            name = GetName();
            name_array[0][counter] = GetName();
            strcpy(name_array[++counter], name);
            


            printf("\nPlease enter the score for student %ld on Exam 1: ", id);
            exam1 = GetScore();
            printf("\nPlease enter the score for student %ld on Exam 2: ", id);
            exam2 = GetScore();
            printf("\nPlease enter the score for student %ld on the Class Project: ", id);
            project = GetScore();
            difference = ComputeExamDifference(exam1, exam2);
            exam_average = ComputeExamAverage(exam1, exam2);
            course_average = ComputeCourseAverage(exam1, exam2, project);
            avg_array[counter] = course_average;
            grade = ComputeGrade(course_average);
            grade_array[counter] = grade;
            GenerateCourseReport(id, exam1, exam2, project, difference, exam_average, course_average, grade);
            counter++;
      }      
      GenerateSummaryReport(id_array, avg_array, grade_array, counter);

return 0;
}

void DisplayGreeting(void)
{
      printf("      **************************************\n");
      printf("      *                                    *\n");
      printf("      *    Welcome to the UCD ISMG 4950    *\n");
      printf("      *     On-line Information System     *\n");
      printf("      *                                    *\n");
      printf("      **************************************\n\n");

      return;
}

long GetId(void)
{
      long id;

      do
      {
            printf("Please enter the student ID (enter 0 to quit): ");
            scanf("%ld", &id);
            if(id <= 999999999 && id >= 0)
                  break;
            printf("\n*** Error! Student ID of %ld is out of range! ***\n\n", id);
      }
      while(1);

      return id;
}

char GetName(void)
{
      char name;

            fflush(stdin);
            printf("Please enter the student's name: ");
            scanf("%s", name);

      return name;
}

short GetScore(void)
{
      short score;

      do
      {
            scanf("%d", &score);
            if(score <=100 && score >= 0)
                  break;
            printf("\n*** Error! Score of %d is out of range! Please re-enter score: ", score);
      }
      while(1);

      return score;
}

short ComputeExamDifference(short exam1, short exam2)
{
      short difference;

      difference = exam2 - exam1;

      return(difference);
}

float ComputeExamAverage(short exam1, short exam2)
{
      float exam_average;

      exam_average = ((float)exam1 + exam2) / 2;
      
      return(exam_average);
}

float ComputeCourseAverage(short exam1, short exam2, short project)
{
      float course_average;

      course_average = (exam1 * EXAM_1_WEIGHT) + (exam2 * EXAM_2_WEIGHT) + (project * PROJECT_WEIGHT);

      return(course_average);
}

char ComputeGrade(float course_average)
{
      char grade;

      if(course_average >= 90)
            grade = 'A';
      else if(course_average >= 80)
            grade = 'B';
      else if(course_average >= 70)
            grade = 'C';
      else if(course_average >= 60)
            grade = 'D';
      else
            grade = 'F';

      return(grade);
}

void GenerateCourseReport(long id, short exam1, short exam2, short project, short difference, float exam_average, float course_average, char grade)
{
      printf("\n\n\n   **** Course Report for Student %ld ****", id);
      printf("\n\nStudent scored %d on Exam 1", exam1);
      printf("\nStudent scored %d on Exam 2", exam2);
      printf("\nStudent scored %d on Class Project", project);
      printf("\n\nStudent scored %d points differently on Exam 2 versus Exam 1", difference);
      printf("\n\nStudent's exam average is %1.1f", exam_average);
      printf("\n\nStudent's average for the course is %1.2f", course_average);
      printf("\n\nStudent's grade in the course is %c\n\n", grade);
      
      return;
}

void GenerateSummaryReport(long id_array[], float avg_array[], char grade_array[], short counter)
{
      int i = 0;

      system("cls");
      printf("\n*** ISMG 4950 Summary Course Report ***");
      printf("\n\n   Student ID      Average      Grade");
      printf("\n   __________      _______      _____");
      for(i = 0; i < counter; ++i)
            printf("\n %i %ld        %3.2f         %c", 1+i, id_array[i], avg_array[i], grade_array[i]);
            
      return;      
}
Avatar of RONSLOW
RONSLOW

what is not correct about GetName() .. does it not compile, or not work or ... ?

you variable 'name' should be a 'char[MAX]' (say) not just a single 'char'

Also GetName is trying to read in a single char instead of a string. A single char is not big enough

There are a number of other little problems

Please find corrected source here...

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define EXAM_1_WEIGHT .35
#define EXAM_2_WEIGHT .25
#define PROJECT_WEIGHT .40
#define MAX 100


void DisplayGreeting(void);
long GetId(void);
/* RKO char GetName(void); should pass in a char[] to fill in */
void GetName(char*);
short GetScore(void);
short ComputeExamDifference(short, short);
float ComputeExamAverage(short, short);
float ComputeCourseAverage(short, short, short);
char ComputeGrade(float);
void GenerateCourseReport(long, short, short, short, short, float, float, char);
void GenerateSummaryReport(long[], float[], char[], short);

main()
{
      long id;
      short exam1, exam2, project, difference;
      float exam_average, course_average;
      char grade;
      short counter = 0;
      long id_array[MAX];
      float avg_array[MAX];
      char grade_array[MAX];
      /* RKO char name; should be a string, not a single char */
      char name[MAX];
      char name_array[MAX][MAX];
      
      DisplayGreeting();
      /* RKO while(1) .. for(;;) is nicer for an 'infinite' loop */
      for (;;)
      {
            id = GetId();
            id_array[counter] = id;
            
            /* RKO name = GetName(); becomes... */
            GetName(name);
            /* RKO name_array[0][counter] = GetName(); this looks like rubbish */
            strcpy(name_array[++counter], name);
                        
            printf("\nPlease enter the score for student %ld on Exam 1: ", id);
            exam1 = GetScore();
            printf("\nPlease enter the score for student %ld on Exam 2: ", id);
            exam2 = GetScore();
            printf("\nPlease enter the score for student %ld on the Class Project: ", id);
            project = GetScore();
            difference = ComputeExamDifference(exam1, exam2);
            exam_average = ComputeExamAverage(exam1, exam2);
            course_average = ComputeCourseAverage(exam1, exam2, project);
            avg_array[counter] = course_average;
            grade = ComputeGrade(course_average);
            grade_array[counter] = grade;
            GenerateCourseReport(id, exam1, exam2, project, difference, exam_average, course_average, grade);
            counter++;
      }
      GenerateSummaryReport(id_array, avg_array, grade_array, counter);
      
      return 0;
}

void DisplayGreeting(void)
{
      printf("      **************************************\n");
      printf("      *                                    *\n");
      printf("      *    Welcome to the UCD ISMG 4950    *\n");
      printf("      *     On-line Information System     *\n");
      printf("      *                                    *\n");
      printf("      **************************************\n\n");
      
      return;
}

long GetId(void)
{
      long id;
      
      /* RKO do use for(;;) instead of do{}while(1) */
      for (;;)
      {
            printf("Please enter the student ID (enter 0 to quit): ");
            scanf("%ld", &id);
            if(id <= 999999999 && id >= 0)
                  break;
            printf("\n*** Error! Student ID of %ld is out of range! ***\n\n", id);
      }
      /* RKO while(1); use for(;;) instead */
      
      return id;
}

/* char GetName(void) becomes */
void GetName(char* name)
{
      /* RKO char name; this is passed in now and is a string */
      
      fflush(stdin);
      printf("Please enter the student's name: ");
      scanf("%s", name);
      
      /* RKO return name; don't need to return now */
}

short GetScore(void)
{
      short score;
      
      /* RKO do use for(;;) instead of do{}while(1) */
      for (;;)
      {
            scanf("%d", &score);
            if(score <=100 && score >= 0)
                  break;
            printf("\n*** Error! Score of %d is out of range! Please re-enter score: ", score);
      }
      /* RKO while(1); use for(;;) instead */
      
      return score;
}

short ComputeExamDifference(short exam1, short exam2)
{
      short difference;
      
      /* RKO be careful of overflows here */
      difference = exam2 - exam1;
      
      return(difference);
}

float ComputeExamAverage(short exam1, short exam2)
{
      float exam_average;
      
      exam_average = ((float)exam1 + exam2) / 2;
      
      return(exam_average);
}

float ComputeCourseAverage(short exam1, short exam2, short project)
{
      float course_average;

      /* calcs in C are done in doubles, so this looses precision */
      /* I'd change all your float vars to double */
      course_average = (exam1 * EXAM_1_WEIGHT) + (exam2 * EXAM_2_WEIGHT) + (project * PROJECT_WEIGHT);
      
      return(course_average);
}

char ComputeGrade(float course_average)
{
      char grade;
      
      if(course_average >= 90)
            grade = 'A';
      else if(course_average >= 80)
            grade = 'B';
      else if(course_average >= 70)
            grade = 'C';
      else if(course_average >= 60)
            grade = 'D';
      else
            grade = 'F';
      
      return(grade);
}

void GenerateCourseReport(long id, short exam1, short exam2, short project, short difference, float exam_average, float course_average, char grade)
{
      printf("\n\n\n   **** Course Report for Student %ld ****", id);
      printf("\n\nStudent scored %d on Exam 1", exam1);
      printf("\nStudent scored %d on Exam 2", exam2);
      printf("\nStudent scored %d on Class Project", project);
      printf("\n\nStudent scored %d points differently on Exam 2 versus Exam 1", difference);
      printf("\n\nStudent's exam average is %1.1f", exam_average);
      printf("\n\nStudent's average for the course is %1.2f", course_average);
      printf("\n\nStudent's grade in the course is %c\n\n", grade);
      
      return;
}

void GenerateSummaryReport(long id_array[], float avg_array[], char grade_array[], short counter)
{
      int i = 0;
      
      system("cls");
      printf("\n*** ISMG 4950 Summary Course Report ***");
      printf("\n\n   Student ID      Average      Grade");
      printf("\n   __________      _______      _____");
      for(i = 0; i < counter; ++i)
            printf("\n %i %ld        %3.2f         %c", 1+i, id_array[i], avg_array[i], grade_array[i]);
      
      return;
}

ASKER CERTIFIED SOLUTION
Avatar of RONSLOW
RONSLOW

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Jeez, where did you guys learn not to indent??

I feel sorry for the one who has to check your homework :-/
It is EE that removes the formatting  .. not our fault .. leading tabs get removed .. so cut and paste from the IDE results in this awful looking stuff.  But, of course, the god news is that putting it back into the IDE (VC) lets you reindent anyway.