Lab 5 : Arrays Please the attachment which is been given to Develop a C code that asks for a class size (maximum can be 20). Then it asks the user to enter all the marks and saves them in an array. Then it generates a report about the class status that includes the following 1- Class average 2- Best mark and worst mark 3- number of failed students 4- Number of students with a grade of A and higher (80 or above) 5- number of students who got a mark above average

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Lab 5 : Arrays

Please the attachment which is been given to Develop a C code that asks for a class size (maximum can be 20).

Then it asks the user to enter all the marks and saves them in an array.

Then it generates a report about the class status that includes the following

1- Class average

2- Best mark and worst mark

3- number of failed students

4- Number of students with a grade of A and higher (80 or above)

5- number of students who got a mark above average

Expert Solution
Program Instruction
  • The program asks the user to input number of students in the class.
  • To generate an array for number of students, malloc() is used.
  • The user is then prompted to input marks for the students.
  • To input marks for loop is used. Sum is calculated at the time of entry of marks.
  • At the end of loop average is calculated.
  • Another loop is used to calculate maximum marks, minimum marks, number of students who failed, number of students
    with marks above 80 and number of students who scored above class average.
  • The passing limit is set to 33.
  • The allocated memory is freed using free().
Program

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n, *marks, sum = 0;
 int max, min;
 float avg = 0;
 int pass = 33;
 int fail = 0;
 int above_avg = 0;
 int above_A = 0;
 
    printf("Enter number of students: ");
    scanf("%d", &n);

 if (n > 20)
 {
  printf("Invalid input, exiting the program.");
  exit(0);
 }

    marks = (int*) malloc(n * sizeof(int));
 
    // if memory cannot be allocated
    if(marks == NULL)                     
    {
        printf("Error! memory not allocated. Exiting program");
        exit(0);
    }

    printf("Enter marks for %d students: \n", n);
    for(int i = 0; i < n; ++i)
 {
  scanf("%d", marks + i);
  sum += *(marks + i);
 }
    
    // Class average
    avg = (float)sum / n;
    max = min = marks[0];
    
    /*
 * Loop for finding maximum marks, minimum marks,
 * number of students who failed, number of students
 * with marks above 80 and number of students who scored
 * above class average
    */
    for (int i = 0; i < n; ++i)
    {
     if (max < marks[i])
      max = marks[i];
      
  if (min > marks[i])
   min = marks[i];
   
     if (marks[i] < pass)
      fail += 1;
     
     if (marks[i] > avg)
      above_avg += 1;
     
     if (marks[i] > 80)
      above_A += 1;
 }
    
    printf ("Class Average: %f \n", avg);
    printf ("Best marks: %d \n", max);
    printf ("Worst marks: %d \n", min);
    printf ("Number of students failed: %d \n", fail);
    printf ("Number of students with marks above 80: %d \n", above_A);
    printf ("Number of students above average: %d \n", above_avg);
    
    free(marks);

    return 0;
}

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education