Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Introduction The aim of this assignment is to make sure that you understand and

ID: 3564092 • Letter: I

Question

Introduction

The aim of this assignment is to make sure that you understand and are familiar with the concepts covered in the lectures, including, enumeration and array of structures. By the end of the assignment, you should have

Reading: chapter 2, appendix (sections B.1 and B.2), and course notes (slides).

You are expected to do the majority of the assignment outside the class meetings.   Should you need assistance, or have questions about the assignment, please contact the instructor or the TA during their office hours.  

You are encouraged to ask and answer questions on the course discussion board. (However, do not share your answers in the course discussion board.)

Programming Exercise (50 points)

Study the following C program, try to understand what it does and use the following code as a template to create your personalize grade book.                                                                                                                              [50 points]

#include <stdio.h>  

#include <string.h>

#include <ctype.h>

#pragma warning(disable: 4996)

#define max 100

struct contact {                                  // a node to hold personal details

       char name[30];

       int phone;

       char email[30];

};

struct contact contactbook[max];                 // an array of structures, 100 entries

int tail = 0;                                    // global variable

void flush();                                    // forward declaration of functions

void branching(char c);                          

int insertion();

int print_all();

int search_contact();

int delete_contact();

int main() { // print a menu for selection

       char ch = 'i';

       ungetc(' ', stdin); // inject input buffer with a return character

       do {

              printf("Enter your selection ");

              printf(" i: insert a new entry ");

              printf(" d: delete an entry ");

              printf(" s: search an entry ");

              printf(" p: print all entries ");

              printf(" q: quit ");

             

              flush();                                   // flush input buffer

              ch = tolower(getchar());                  

              branching(ch);

       } while (ch != 113);

       return 0;

}

void flush() {

       int c;

       do {

              c = getchar();

       } while (c != ' ' && c != EOF);

}

void branching(char c) {    // branch to different tasks

       switch (c) {

       case 'i':

              insertion();

              break;

       case 's':

              search_contact();

              break;

       case 'd':

              delete_contact();

              break;

       case 'p':

              print_all();

              break;

       case 'q':

              break;

       default:

              printf("Invalid input ");

       }

}

int insertion() {    // insert a new entry at the end

       if (tail == max) {

              printf("There are no more places to insert. ");

              return -1;

       }

       else {

              printf("Enter name, phone, email: ");

              scanf("%s", contactbook[tail].name);

              // &contactbook[tail].name is an array. No "&" is needed

              scanf("%d", &contactbook[tail].phone, sizeof(contactbook[tail].phone));

              scanf("%s", contactbook[tail].email);

              tail++;

              printf("The number of entries = %d ", tail);

              return 0;

       }

}

int print_all() {    

// print name, phone, and email for each contact in the contactbook

       int i;

       if (tail == 0) {

              printf("No entries found.");

       }

       else {

              for (i = 0; i < tail; i++) {

                      printf(" name = %s ", contactbook[i].name);

                      printf("phone = %d ", contactbook[i].phone);

                      printf("email = %s ", contactbook[i].email);

              }

       }

       return 0;

}

int search_contact() {      // print phone and email via name

       char sname[30]; int i;

      

       printf("Please enter the name to be searched for: ");

       scanf("%s", sname); //sname is an array, no & needed

      

       for (i = 0; i<tail; i++)

              if (strcmp(sname, contactbook[i].name) == 0) {

                      printf("phone = %d ", contactbook[i].phone);

                      printf("email = %s ", contactbook[i].email);

                      return i;

              }

       printf("The name does not exist. ");

       return -1;

}

int delete_contact() {

       int i, k;

       k = search_contact();

       if (k == -1) {

              printf("The name does not exist. "); return -1;

       }

       else {

              for (i = k; i<tail; i++) {

                      strcpy(contactbook[i].name, contactbook[i + 1].name);

                      contactbook[i].phone = contactbook[i + 1].phone;

                      strcpy(contactbook[i].email, contactbook[i + 1].email);

                      printf("The index deleted is: %d ", k);

              }

              tail--;

              return k;

       }

}

In this problem, you will also define a GradeType enumeration which will include a predefine set of categories, e.g. Final, Midterm, Quiz, Project, and Homework.                                                                                                                                                                                                 

              Users will select from the following options: I, D, P, or Q, which correspond to insert, delete, print, or quit respectively. (Hitting Q will exit the application.)                                                                                                                                                                                                                           [5]

              Use the following formula as a guide:

              Final Grade = 0.22 * (Final Exam PE / Final Exam PP) + 0.18 * (Midterm PE / Midterm PP) + 0.10 * (Quizzes PE / Quizzes PP) + 0.20 * (Project PE / Project PP) + 0.30 * (Homework PE / Homework PP);

#include <stdio.h>  

#include <string.h>

#include <ctype.h>

#pragma warning(disable: 4996)

#define max 100

struct contact {                                  // a node to hold personal details

       char name[30];

       int phone;

       char email[30];

};

struct contact contactbook[max];                 // an array of structures, 100 entries

int tail = 0;                                    // global variable

void flush();                                    // forward declaration of functions

void branching(char c);                          

int insertion();

int print_all();

int search_contact();

int delete_contact();

int main() { // print a menu for selection

       char ch = 'i';

       ungetc(' ', stdin); // inject input buffer with a return character

       do {

              printf("Enter your selection ");

              printf(" i: insert a new entry ");

              printf(" d: delete an entry ");

              printf(" s: search an entry ");

              printf(" p: print all entries ");

              printf(" q: quit ");

             

              flush();                                   // flush input buffer

              ch = tolower(getchar());                  

              branching(ch);

       } while (ch != 113);

       return 0;

}

void flush() {

       int c;

       do {

              c = getchar();

       } while (c != ' ' && c != EOF);

}

void branching(char c) {    // branch to different tasks

       switch (c) {

       case 'i':

              insertion();

              break;

       case 's':

              search_contact();

              break;

       case 'd':

              delete_contact();

              break;

       case 'p':

              print_all();

              break;

       case 'q':

              break;

       default:

              printf("Invalid input ");

       }

}

int insertion() {    // insert a new entry at the end

       if (tail == max) {

              printf("There are no more places to insert. ");

              return -1;

       }

       else {

              printf("Enter name, phone, email: ");

              scanf("%s", contactbook[tail].name);

              // &contactbook[tail].name is an array. No "&" is needed

              scanf("%d", &contactbook[tail].phone, sizeof(contactbook[tail].phone));

              scanf("%s", contactbook[tail].email);

              tail++;

              printf("The number of entries = %d ", tail);

              return 0;

       }

}

int print_all() {    

// print name, phone, and email for each contact in the contactbook

       int i;

       if (tail == 0) {

              printf("No entries found.");

       }

       else {

              for (i = 0; i < tail; i++) {

                      printf(" name = %s ", contactbook[i].name);

                      printf("phone = %d ", contactbook[i].phone);

                      printf("email = %s ", contactbook[i].email);

              }

       }

       return 0;

}

int search_contact() {      // print phone and email via name

       char sname[30]; int i;

      

       printf("Please enter the name to be searched for: ");

       scanf("%s", sname); //sname is an array, no & needed

      

       for (i = 0; i<tail; i++)

              if (strcmp(sname, contactbook[i].name) == 0) {

                      printf("phone = %d ", contactbook[i].phone);

                      printf("email = %s ", contactbook[i].email);

                      return i;

              }

       printf("The name does not exist. ");

       return -1;

}

int delete_contact() {

       int i, k;

       k = search_contact();

       if (k == -1) {

              printf("The name does not exist. "); return -1;

       }

       else {

              for (i = k; i<tail; i++) {

                      strcpy(contactbook[i].name, contactbook[i + 1].name);

                      contactbook[i].phone = contactbook[i + 1].phone;

                      strcpy(contactbook[i].email, contactbook[i + 1].email);

                      printf("The index deleted is: %d ", k);

              }

              tail--;

              return k;

       }

}

Explanation / Answer

#include <stdio.h>   
#include <string.h>
#include <ctype.h>

#pragma warning(disable: 4996)

#define max 10

enum GradeType{ Final = 0, Midterm, Quiz, Project, Homework };
struct Entry { // a node to hold personal details
char name[255];
float points_possible;
float points_earned;

enum GradeType exam;

struct Entry *next;
};

struct Entry Gradebook[max]; // an array of structures
int tail = 0;

int insertion();
//int print_all();
//int delete();

int main() { // print a menu for selection
char command;

//print menu options
printf("Enter your selection ");
printf(" i: insert a new grade book entry ");
printf(" d: delete a grade book entry ");
printf(" p: print grades ");
printf(" q: quit Application ");
scanf("%c", &command);
  
do {
switch (command) {   
case 'i':
insertion();
break;
/* case 'd':
delete();
break; */
case 'q':
break;

default:
printf("Invalid input. Please enter an option listed above.");
}
} while (command != 'q');

return 0;
}

int insertion() { // insert a new entry at the end of Gradebook
struct Entry gradeEntry;
int j=0;
if (tail == max) {
printf("There are no more places to insert. ");
return -1;
}
else {
printf("Enter assignment name: ");
scanf("%s", Gradebook[tail].name);

printf("Enter the number of possible points: ");
scanf("%f", &Gradebook[tail].points_possible);

printf("Enter the numbers of points earned: ");
scanf("%f",&Gradebook[tail].points_earned);

printf("Enter Grade Type (Final = 0, Midterm = 1, Project = 3, Homework = 4): ");


scanf("%d", &Gradebook[tail].exam);

tail++;
printf("The number of entries = %i ", tail);
return 0;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote