Solved

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

Posted on 1998-04-15
5
183 Views
Last Modified: 2010-04-15
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;      
}
0
Comment
Question by:elwayisgod
  • 4
5 Comments
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1258126
what is not correct about GetName() .. does it not compile, or not work or ... ?

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1258127
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;
}

0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 30 total points
ID: 1258128
that should have been an answer
0
 
LVL 1

Expert Comment

by:Talence
ID: 1258129
Jeez, where did you guys learn not to indent??

I feel sorry for the one who has to check your homework :-/
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1258130
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.


0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question