0% found this document useful (0 votes)
172 views

DownLoadFiles Programming Example CGPA

This document provides an example program to calculate a student's cumulative grade point average (CGPA) based on their semester grade point averages (SGPAs) and course credits over multiple semesters. It outlines the prototype for a calcCGPA function that takes a float array of SGPAs, an int array of corresponding credits, and the total number of semesters as parameters. The function calculates CGPA by summing the products of each SGPA and its credits and dividing this by the total credits. It returns -1 if any SGPA is negative or credit is zero to satisfy the constraints. Pseudocode and a Java program solution implementing this function are also provided.

Uploaded by

sanooppm
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
172 views

DownLoadFiles Programming Example CGPA

This document provides an example program to calculate a student's cumulative grade point average (CGPA) based on their semester grade point averages (SGPAs) and course credits over multiple semesters. It outlines the prototype for a calcCGPA function that takes a float array of SGPAs, an int array of corresponding credits, and the total number of semesters as parameters. The function calculates CGPA by summing the products of each SGPA and its credits and dividing this by the total credits. It returns -1 if any SGPA is negative or credit is zero to satisfy the constraints. Pseudocode and a Java program solution implementing this function are also provided.

Uploaded by

sanooppm
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Programming Example

Name of the Program: CGPA Calculation Level : Easy

CGPA Write a program to calculate the CGPA got by a student. CGPA will be calculated on the basis of SGPA and number of semester. CGPA, which is the cumulative grade point average is calculated on the basis of SGPA, which is the semester grade point average. CGPA is nothing but SUM of (SGPA * no of credits in that semester) / SUM of all the credits.

The Prototype of the Function is :

public float calcCGPA(float[] sgpa, int []credits, int noOfSem)


Where the sgpa is the float array contains the SGPAs of the student in different semesters, credits is the integer array contains the credits of the corresponding SGPA and the noOfSem is the number of semesters. The function will return the CGPA.

Constraints
All the sgpas are non negative, otherwise return -1 All the credits are greater than zero, otherwise return -1 The arraylength of sgpa and credits are equal to noOfSem.

Example 1
Input sgpa = {0.8, 0.7, 0.6, 0.5}, credits = {50, 100, 50, 100}, noOfSem = 4 Output The function calcCGPA() returns 0.633. Explanation CGPA = (SUM (sgpa * credits)) / SUM (Credits) = (0.8 * 50 + 0.7 * 100 + 0.6 * 50 + 0.5 * 100) / 300

Example 2
Input sgpa = {7.2, 8.32, 7.5}, credits = {50, 100, 50}, noOfSem = 3 Output The function calcCGPA() returns 7.835

Example 3
Input sgpa = {7.2, 8.32, 7.5} credits = {50, 100, 0} noOfSem = 3 Output The function calcCGPA() returns -1

For Java solutions


Package Name : test.cgpa File Name : CGPA.java Class Name : CGPA Function Name :public float calcCGPA(float[] sgpa, int []credits, int noOfSem)

General Instructions The package names, class names, method signatures to be used are mentioned in the problem statement.
Do not use your own names or change the method signatures and fields. You can add any number of additional methods.

Pseudo Code
CGPA
1. 2. 3. 4. Check for the constraint, all the sgpas are non negative, otherwise return -1 Check for the constraint, All the credits are greater than zero, otherwise return -1 Calculate the CGPA. Return the CGPA from the method.

Program Solution
package test.cgpa; public class CGPA { float calcCGPA(float[] sgpa, int[] credits, int noOfSem) { float cgpa = 0.0f; float sgpaC = 0.0f; int totalCredits = 0; for(int i = 0; i<noOfSem; i++) { if(sgpa[i] < 0 || credits[i] <= 0) return -1; sgpaC += sgpa[i] * credits[i]; totalCredits += credits[i]; } cgpa = sgpaC / totalCredits; return cgpa; }

public static void main(String[] args) { //TestCase 1 try { float[] sgpa = {0.8f, 0.7f, 0.6f, 0.5f}; int[] credites = {50, 100, 50, 100}; int noOfSem = 4;

float cgpa = new CGPA().calcCGPA(sgpa, credites, noOfSem); System.out.println(cgpa);


} catch(Exception e) { System.out.println(e); } } }

You might also like