Home > Software design >  How can I shorten this code? - C Programming
How can I shorten this code? - C Programming

Time:12-17

The goal is to prompt the user to enter grades in different subjects. It will display the input grades, numerical rating, and remarks. After that, I used do-while loop so the user will be able to reuse the program. I know it's dumb to make a long-arse code when you can make a shorter one, but the thing is, I don't know what to use. Should I just use if / else statements inside a function?

The code works just fine, but I want to look for another 'method'

#include <stdio.h>

int it, prog, arts, comm, math, fil, pe;

int main()
{
  char n;

  do
  {
    printf("ACADEMIC RECORD\n");
    printf("********************************\n");
    printf("Intro to Computing: ");
    scanf("%d", &it);
    printf("Computer Programming 1: ");
    scanf("%d", &prog);
    printf("Arts Appreciation: ");
    scanf("%d", &arts);
    printf("Purposive Communication: ");
    scanf("%d", &comm);
    printf("Mathematics in the Modern World: ");
    scanf("%d", &math);
    printf("Filipino: ");
    scanf("%d", &fil);
    printf("Fundamentals of Physical Fitness: ");
    scanf("%d", &pe);

    printf("\nSubject\t\t\t\t\t\tGrade\t  Numerical Rating\tRemarks\t\n");
    if(it >=75 && it <=100)
    {
      if(it >= 75 && it <= 76)
      {
        printf("Intro to Computing\t\t\t\t %d\t\t", it);
        printf("3.00\t\t");
        printf("Passed\t\n");
      }
      else if(it >= 77 && it <= 79)
      {
        printf("Intro to Computing\t\t\t\t %d\t\t", it);
        printf("2.75\t\t");
        printf("Passed\t\n");
      }
      else if(it >= 80 && it <= 82)
      {
        printf("Intro to Computing\t\t\t\t %d\t\t", it);
        printf("2.50\t\t");
        printf("Passed\t\n");
      }
      else if(it >= 83 && it <= 85)
      {
        printf("Intro to Computing\t\t\t\t %d\t\t", it);
        printf("2.25\t\t");
        printf("Passed\t\n");
      }
      else if(it >= 86 && it <= 88)
      {
        printf("Intro to Computing\t\t\t\t %d\t\t", it);
        printf("2.00\t\t");
        printf("Passed\t\n");
      }
      else if(it >= 89 && it <= 91)
      {
        printf("Intro to Computing\t\t\t\t %d\t\t", it);
        printf("1.75\t\t");
        printf("Passed\t\n");
      }
      else if(it >= 92 && it <= 94)
      {
        printf("Intro to Computing\t\t\t\t %d\t\t", it);
        printf("1.50\t\t");
        printf("Passed\t\n");
      }
      else if(it >= 95 && it <= 97)
      {
        printf("Intro to Computing\t\t\t\t %d\t\t", it);
        printf("1.25\t\t");
        printf("Passed\t\n");
      }
      else if(it >= 98 && it <= 100)
      {
        printf("Intro to Computing\t\t\t\t %d\t\t", it);
        printf("1.00\t\t");
        printf("Passed\t\n");
      }
      }
      else if(it <75 && it >=0)
      {
        printf("Intro to Computing\t\t\t\t %d\t\t", it);
        printf("5.00\t\t");
        printf("Failed\t\n");
      }
      else if(it >100 || it <0)
      {
        printf("Please input valid grade!\n");
      }

    if(prog >=75 && prog <=100)
    {
      if(prog >= 75 && prog <= 76){
        printf("Computer Programming 1\t\t\t\t %d\t\t", prog);
        printf("3.00\t\t");
        printf("Passed\t\n");
      }
      else if(prog >= 77 && prog <= 79)
      {
        printf("Computer Programming 1\t\t\t\t %d\t\t", prog);
        printf("2.75\t\t");
        printf("Passed\t\n");
      }
      else if(prog >= 80 && prog <= 82)
      {
        printf("Computer Programming 1\t\t\t\t %d\t\t", prog);
        printf("2.50\t\t");
        printf("Passed\t\n");
      }
      else if(prog >= 83 && prog <= 85)
      {
        printf("Computer Programming 1\t\t\t\t %d\t\t", prog);
        printf("2.25\t\t");
        printf("Passed\t\n");
      }
      else if(prog >= 86 && prog <= 88)
      {
        printf("Computer Programming 1\t\t\t\t %d\t\t", prog);
        printf("2.00\t\t");
        printf("Passed\t\n");
      }
      else if(prog >= 89 && prog <= 91)
      {
        printf("Computer Programming 1\t\t\t\t %d\t\t", prog);
        printf("1.75\t\t");
        printf("Passed\t\n");
      }
      else if(prog >= 92 && prog <= 94)
      {
        printf("Computer Programming 1\t\t\t\t %d\t\t", prog);
        printf("1.50\t\t");
        printf("Passed\t\n");
      }
      else if(prog >= 95 && prog <= 97)
      {
        printf("Computer Programming 1\t\t\t\t %d\t\t", prog);
        printf("1.25\t\t");
        printf("Passed\t\n");
      }
      else if(prog >= 98 && prog <= 100)
      {
        printf("Computer Programming 1\t\t\t\t %d\t\t", prog);
        printf("1.00\t\t");
        printf("Passed\t\n");
      }
      }
      else if(prog <75 && prog >=0)
      {
        printf("Computer Programming 1\t\t\t\t %d\t\t", prog);
        printf("5.00\t\t");
        printf("Failed\t\n");
      }
      else if(prog >100 || prog <0)
      {
        printf("Please input valid grade!\n");
      }

    if(arts >=75 && arts <=100)
    {
      if(arts >= 75 && arts <= 76){
        printf("Arts Appreciation\t\t\t\t %d\t\t", arts);
        printf("3.00\t\t");
        printf("Passed\t\n");
      }
      else if(arts >= 77 && arts <= 79)
      {
        printf("Arts Appreciation\t\t\t\t %d\t\t", arts);
        printf("2.75\t\t");
        printf("Passed\t\n");
      }
      else if(arts >= 80 && arts <= 82)
      {
        printf("Arts Appreciation\t\t\t\t %d\t\t", arts);
        printf("2.50\t\t");
        printf("Passed\t\n");
      }
      else if(arts >= 83 && arts <= 85)
      {
        printf("Arts Appreciation\t\t\t\t %d\t\t", arts);
        printf("2.25\t\t");
        printf("Passed\t\n");
      }
      else if(arts >= 86 && arts <= 88)
      {
        printf("Arts Appreciation\t\t\t\t %d\t\t", arts);
        printf("2.00\t\t");
        printf("Passed\t\n");
      }
      else if(arts >= 89 && arts <= 91)
      {
        printf("Arts Appreciation\t\t\t\t %d\t\t", arts);
        printf("1.75\t\t");
        printf("Passed\t\n");
      }
      else if(arts >= 92 && arts <= 94)
      {
        printf("Arts Appreciation\t\t\t\t %d\t\t", arts);
        printf("1.50\t\t");
        printf("Passed\t\n");
      }
      else if(arts >= 95 && arts <= 97)
      {
        printf("Arts Appreciation\t\t\t\t %d\t\t", arts);
        printf("1.25\t\t");
        printf("Passed\t\n");
      }
      else if(arts >= 98 && arts <= 100)
      {
        printf("Arts Appreciation\t\t\t\t %d\t\t", arts);
        printf("1.00\t\t");
        printf("Passed\t\n");
      }
      }
      else if(arts <75 && arts >=0)
      {
        printf("Arts Appreciation\t\t\t\t %d\t\t", arts);
        printf("5.00\t\t");
        printf("Failed\t\n");
      }
      else if(arts >100 || arts <0)
      {
        printf("Please input valid grade!\n");
      }

    if(comm >=75 && comm <=100)
    {
      if(comm >= 75 && comm <= 76){
        printf("Purposive Communication\t\t\t\t %d\t\t", comm);
        printf("3.00\t\t");
        printf("Passed\t\n");
      }
      else if(comm >= 77 && comm <= 79)
      {
        printf("Purposive Communication\t\t\t\t %d\t\t", comm);
        printf("2.75\t\t");
        printf("Passed\t\n");
      }
      else if(comm >= 80 && comm <= 82)
      {
        printf("Purposive Communication\t\t\t\t %d\t\t", comm);
        printf("2.50\t\t");
        printf("Passed\t\n");
      }
      else if(comm >= 83 && comm <= 85)
      {
        printf("Purposive Communication\t\t\t\t %d\t\t", comm);
        printf("2.25\t\t");
        printf("Passed\t\n");
      }
      else if(comm >= 86 && comm <= 88)
      {
        printf("Purposive Communication\t\t\t\t %d\t\t", comm);
        printf("2.00\t\t");
        printf("Passed\t\n");
      }
      else if(comm >= 89 && comm <= 91)
      {
        printf("Purposive Communication\t\t\t\t %d\t\t", comm);
        printf("1.75\t\t");
        printf("Passed\t\n");
      }
      else if(comm >= 92 && comm <= 94)
      {
        printf("Purposive Communication\t\t\t\t %d\t\t", comm);
        printf("1.50\t\t");
        printf("Passed\t\n");
      }
      else if(comm >= 95 && comm <= 97)
      {
        printf("Purposive Communication\t\t\t\t %d\t\t", comm);
        printf("1.25\t\t");
        printf("Passed\t\n");
      }
      else if(comm >= 98 && comm <= 100)
      {
        printf("Purposive Communication\t\t\t\t %d\t\t", comm);
        printf("1.00\t\t");
        printf("Passed\t\n");
      }
      }
      else if(comm <75 && comm >=0)
      {
        printf("Purposive Communication\t\t\t\t %d\t\t", comm);
        printf("5.00\t\t");
        printf("Failed\t\n");
      }
      else if(comm >100 || comm <0)
      {
        printf("Please input valid grade!\n");
      }

    if(math >=75 && math <=100)
    {
      if(math >= 75 && math <= 76){
        printf("Mathematics in the Modern World\t\t\t %d\t\t", math);
        printf("3.00\t\t");
        printf("Passed\t\n");
      }
      else if(math >= 77 && math <= 79)
      {
        printf("Mathematics in the Modern World\t\t\t %d\t\t", math);
        printf("2.75\t\t");
        printf("Passed\t\n");
      }
      else if(math >= 80 && math <= 82)
      {
        printf("Mathematics in the Modern World\t\t\t %d\t\t", math);
        printf("2.50\t\t");
        printf("Passed\t\n");
      }
      else if(math >= 83 && math <= 85)
      {
        printf("Mathematics in the Modern World\t\t\t %d\t\t", math);
        printf("2.25\t\t");
        printf("Passed\t\n");
      }
      else if(math >= 86 && math <= 88)
      {
        printf("Mathematics in the Modern World\t\t\t %d\t\t", math);
        printf("2.00\t\t");
        printf("Passed\t\n");
      }
      else if(math >= 89 && math <= 91)
      {
        printf("Mathematics in the Modern World\t\t\t %d\t\t", math);
        printf("1.75\t\t");
        printf("Passed\t\n");
      }
      else if(math >= 92 && math <= 94)
      {
        printf("Mathematics in the Modern World\t\t\t %d\t\t", math);
        printf("1.50\t\t");
        printf("Passed\t\n");
      }
      else if(math >= 95 && math <= 97)
      {
        printf("Mathematics in the Modern World\t\t\t %d\t\t", math);
        printf("1.25\t\t");
        printf("Passed\t\n");
      }
      else if(math >= 98 && math <= 100)
      {
        printf("Mathematics in the Modern World\t\t\t %d\t\t", math);
        printf("1.00\t\t");
        printf("Passed\t\n");
      }
      }
      else if(math <75 && math >=0)
      {
        printf("Mathematics in the Modern World\t\t\t %d\t\t", math);
        printf("5.00\t\t");
        printf("Failed\t\n");
      }
      else if(math >100 || math <0)
      {
        printf("Please input valid grade!\n");
      }

    if(fil >=75 && fil <=100)
    {
      if(fil >= 75 && fil <= 76){
        printf("Kontekstwalisadong Komunikasyon sa Filipino\t %d\t\t", fil);
        printf("3.00\t\t");
        printf("Passed\t\n");
      }
      else if(fil >= 77 && fil <= 79)
      {
        printf("Kontekstwalisadong Komunikasyon sa Filipino\t %d\t\t", fil);
        printf("2.75\t\t");
        printf("Passed\t\n");
      }
      else if(fil >= 80 && fil <= 82)
      {
        printf("Kontekstwalisadong Komunikasyon sa Filipino\t %d\t\t", fil);
        printf("2.50\t\t");
        printf("Passed\t\n");
      }
      else if(fil >= 83 && fil <= 85)
      {
        printf("Kontekstwalisadong Komunikasyon sa Filipino\t %d\t\t", fil);
        printf("2.25\t\t");
        printf("Passed\t\n");
      }
      else if(fil >= 86 && fil <= 88)
      {
        printf("Kontekstwalisadong Komunikasyon sa Filipino\t %d\t\t", fil);
        printf("2.00\t\t");
        printf("Passed\t\n");
      }
      else if(fil >= 89 && fil <= 91)
      {
        printf("Kontekstwalisadong Komunikasyon sa Filipino\t %d\t\t", fil);
        printf("1.75\t\t");
        printf("Passed\t\n");
      }
      else if(fil >= 92 && fil <= 94)
      {
        printf("Kontekstwalisadong Komunikasyon sa Filipino\t %d\t\t", fil);
        printf("1.50\t\t");
        printf("Passed\t\n");
      }
      else if(fil >= 95 && fil <= 97)
      {
        printf("Kontekstwalisadong Komunikasyon sa Filipino\t %d\t\t", fil);
        printf("1.25\t\t");
        printf("Passed\t\n");
      }
      else if(fil >= 98 && fil <= 100)
      {
        printf("Kontekstwalisadong Komunikasyon sa Filipino\t %d\t\t", fil);
        printf("1.00\t\t");
        printf("Passed\t\n");
      }
      }
      else if(fil <75 && fil >=0)
      {
        printf("Kontekstwalisadong Komunikasyon sa Filipino\t %d\t\t", fil);
        printf("5.00\t\t");
        printf("Failed\t\n");
      }
      else if(fil >100 || fil <0)
      {
        printf("Please input valid grade!\n");
      }

    if(pe >=75 && pe <=100)
    {
      if(pe >= 75 && pe <= 76){
        printf("Fundamentals of Physical Fitness\t\t %d\t\t", pe);
        printf("3.00\t\t");
        printf("Passed\t\n");
      }
      else if(pe >= 77 && pe <= 79)
      {
        printf("Fundamentals of Physical Fitness\t\t %d\t\t", pe);
        printf("2.75\t\t");
        printf("Passed\t\n");
      }
      else if(pe >= 80 && pe <= 82)
      {
        printf("Fundamentals of Physical Fitness\t\t %d\t\t", pe);
        printf("2.50\t\t");
        printf("Passed\t\n");
      }
      else if(pe >= 83 && pe <= 85)
      {
        printf("Fundamentals of Physical Fitness\t\t %d\t\t", pe);
        printf("2.25\t\t");
        printf("Passed\t\n");
      }
      else if(pe >= 86 && pe <= 88)
      {
        printf("Fundamentals of Physical Fitness\t\t %d\t\t", pe);
        printf("2.00\t\t");
        printf("Passed\t\n");
      }
      else if(pe >= 89 && pe <= 91)
      {
        printf("Fundamentals of Physical Fitness\t\t %d\t\t", pe);
        printf("1.75\t\t");
        printf("Passed\t\n");
      }
      else if(pe >= 92 && pe <= 94)
      {
        printf("Fundamentals of Physical Fitness\t\t %d\t\t", pe);
        printf("1.50\t\t");
        printf("Passed\t\n");
      }
      else if(pe >= 95 && pe <= 97)
      {
        printf("Fundamentals of Physical Fitness\t\t %d\t\t", math);
        printf("1.25\t\t");
        printf("Passed\t\n");
      }
      else if(pe >= 98 && pe <= 100)
      {
        printf("Fundamentals of Physical Fitness\t\t %d\t\t", pe);
        printf("1.00\t\t");
        printf("Passed\t\n");
      }
      }
      else if(pe <75 && pe >=0)
      {
        printf("Fundamentals of Physical Fitness\t\t %d\t\t", pe);
        printf("5.00\t\t");
        printf("Failed\t\n");
      }
      else if(pe >100 || pe <0)
      {
        printf("Please input valid grade!\n");
      }

    printf("\nDo you like to try again? ");
    printf("Press 'y' for yes, 'n' for no\n-->");
    scanf("%s", &n);
  }while(n == 'y');
  printf("OK\n");
}

CodePudding user response:

What you are looking for is factorization. You should use a function to compute the numerical rating and print the output line for a given subject and grade, which you pass as arguments.

Also note that the numerical rating can be computed with a simple formula for grades in the Pass range: rating = 1.00 (100 - grade) / 3 * 0.25

Here is a modified version:

#include <stdio.h>

void print_grade(const char *subject, int grade) {
    if (grade < 0 || grade > 100) {
        printf("Invalid grade for %s: %d\n", subject, grade);
    } else {
        double rating;
        const char *pass;

        if (grade >= 75) {
            rating = 1.00   (100 - grade) / 3 * 0.25;
            pass = "Passed";
        } else {
            rating = 5.00;
            pass = "Failed";
        }
        printf("%-45s m .2f %s\n", subject, grade, rating, pass);
    }
}

int main() {
    do {
        char n = 'n';
        int it = -1, prog = -1, arts = -1, comm = -1, math = -1, fil = -1, pe = -1;

        printf("ACADEMIC RECORD\n");
        printf("********************************\n");
        printf("Intro to Computing: ");
        scanf("%d", &it);
        printf("Computer Programming 1: ");
        scanf("%d", &prog);
        printf("Arts Appreciation: ");
        scanf("%d", &arts);
        printf("Purposive Communication: ");
        scanf("%d", &comm);
        printf("Mathematics in the Modern World: ");
        scanf("%d", &math);
        printf("Kontekstwalisadong Komunikasyon sa Filipino: ");
        scanf("%d", &fil);
        printf("Fundamentals of Physical Fitness: ");
        scanf("%d", &pe);

        printf("\n%-45s %6s s %s\n",
               "Subject", "Grade", "Numerical Rating", "Remarks");

        print_grade("Intro to Computing", it);
        print_grade("Computer Programming 1", prog);
        print_grade("Arts Appreciation", arts);
        print_grade("Purposive Communication", comm);
        print_grade("Mathematics in the Modern World", math);
        print_grade("Kontekstwalisadong Komunikasyon sa Filipino", fil);
        print_grade("Fundamentals of Physical Fitness", pe);

        printf("\nDo you like to try again? ");
        printf("Press 'y' for yes, 'n' for no\n-->");
        scanf(" %c", &n);
    } while (n == 'y');

    printf("OK\n");
    return 0;
}

The code could be further reduced using an array of subjects and grades:

int main() {
#define NCOURSES  7
    const char *subject[NCOURSES] = {
        "Intro to Computing",
        "Computer Programming 1",
        "Arts Appreciation",
        "Purposive Communication",
        "Mathematics in the Modern World",
        "Kontekstwalisadong Komunikasyon sa Filipino",
        "Fundamentals of Physical Fitness",
    };
    int grade[NCOURSES];

    do {
        char n = 'n';
        int i;

        printf("ACADEMIC RECORD\n");
        printf("********************************\n");
        for (i = 0; i < NCOURSES; i  ) {
            printf("%s: ", subject[i]);
            if (scanf("%d", &grade[i]) != 1) {
                printf("input error\n");
                break;
            }
        }
        if (i < NCOURSES)
            break;
        printf("\n%-45s %6s s %s\n",
               "Subject", "Grade", "Numerical Rating", "Remarks");
        for (i = 0; i < NCOURSES; i  ) {
            print_grade(subject[i], grade[i]);
        }

        printf("\nDo you like to try again? ");
        printf("Press 'y' for yes, 'n' for no\n-->");
        scanf(" %c", &n);
    } while (n == 'y');

    printf("OK\n");
    return 0;
}

CodePudding user response:

#include<stdio.h>

  
void calculateGrades(int marks, char* subject);
int main()
{
  char n;
  int marks[7];
  char subjects[7][50] = {
                         "Intro to Computing",
                         "Computer Programming",
                         "Arts Appreciation",
                         "Purposive Communication",
                         "Mathematics in the Modern World",
                         "Kontekstwalisadong Komunikasyon sa Filipino",
                         "Fundamentals of Physical Fitness"
                     };

  do
  {
    printf("ACADEMIC RECORD\n");
    printf("********************************\n");
    for(int i=0;i<2;i  ){
        printf("%s : ",&subjects[i]);
        scanf("%d", &marks[i]);
    
    }
    
    printf("\nSubject\t\t\t\t\t\tGrade\t  Numerical Rating\tRemarks\t\n");
    
    for(int i=0;i<2;i  ){
        calculateGrades(marks[i],subjects[i]);
    }
    
    printf("\nDo you like to try again? ");
    printf("Press 'y' for yes, 'n' for no\n-->");
    scanf("%s", &n);
  }while(n == 'y');
  printf("OK\n");
  }
  
  
void calculateGrades(int mark, char* subject){
    
    printf("%s\t\t\t\t %d\t\t",subject,mark);
    if(mark >= 98 && mark <= 100)
    {
        printf("1.00\t\t");
        printf("Passed\t\n");
    }
    else if (mark >= 95 ){
        printf("1.25\t\t");
        printf("Passed\t\n");
    }
    else if (mark >= 92 ){
        printf("1.50\t\t");
        printf("Passed\t\n");
    }
    else if (mark >= 89 ){
        printf("1.75\t\t");
        printf("Passed\t\n");
    }
    else if (mark >= 86 ){
        printf("2.00\t\t");
        printf("Passed\t\n");
    }
    else if (mark >= 83 ){
        printf("2.25\t\t");
        printf("Passed\t\n");
    }
    else if (mark >= 80 ){
        printf("2.50\t\t");
        printf("Passed\t\n");
    }
    else if (mark >= 77 ){
        printf("2.75\t\t");
        printf("Passed\t\n");
    }
    else if (mark >= 75 ){
        printf("3.00\t\t");
        printf("Passed\t\n");
    }
    else if (mark < 75){
        printf("5.00\t\t");
        printf("Failed\t\n");
    }
    else{
        printf("Please input valid grade!\n");
    }
    
}

  • Related