Translate each line in this code to English (Paragraph?), and why we used every
ID: 3702176 • Letter: T
Question
Translate each line in this code to English (Paragraph?), and why we used every fuchtion in each line. ( Explain? the code)
#define _CRT_SECURE_NO_WARNINGS
#define PAUSE system("pause")
#define FLUSH myFlush()
#define SIZE 500
#define CLS system("cls")
//#include
//#include
#include <string.h> // for strlen() gets the length of a string
#include <ctype.h> // for the toupper() function
#include <conio.h> // so I can play with colors
// ProtoType Functions Here
void displayMessage(char m[]);
char getChoice();
void displayMenu();
int enterGPA(float*,int);
void displayAverage(float*,int);
void bubbleSort(float *, int );
void swap(int *, int *);
void displayHighest(float*, int);
void displayLowest(float*, int);
void displayAdjustedAvg(float*, int);
void searchGPA(float*, int);
void displayArray(float*, int);
void myFlush();
main(){
char choice;
float grades[SIZE] = {2.3, 4.3, 1.2, 4.0, 2.16, 3.33, 3.88, .9}; // I placed sample data in the array, can be removed
int count = 8; // this should be zero, but I entered 8 test values above...remove both
do{
choice = getChoice();
switch(choice){
case 'A': // enter a single GPA
count=enterGPA(grades,count);
PAUSE;
break;
case 'B': // Display the average of all GPAs
displayAverage(grades,count);
PAUSE;
break;
case 'C': // Display the highest GPA
displayHighest(grades,count);
PAUSE;
break;
case 'D': // Display the lowest GPA
displayLowest(grades,count);
PAUSE;
break;
case 'E': // Display the adjusted average
displayAdjustedAvg(grades,count);
break;
case 'F': // Search for a certian CPA
searchGPA(grades,count);
break;
case 'G': // Display The Values in the Array in ascending order
displayArray(grades,count);
break;
case 'Q': // Quit the program
CLS;
displayMessage("Thanks for using my pretty cool GPA grade program.");
PAUSE;
break;
default:
displayMessage("Invalid selection, pick again");
PAUSE;
break;
} // end of switch
}while(choice != 'Q');
}// end of main
////////////////////////////////////////////////////////////////////////////////////////////////////////
void displayMenu(){
CLS;
system("COLOR E0");
printf("****************************************** ");
printf("*** M A I N M E N U *** ");
printf("****************************************** ");
printf("A. Enter GPA ");
printf("B. Display Average of all GPA's ");
printf("C. Display the Highest GPA ");
printf("D. Display the Lowest GPA ");
printf("E. Display the adjusted average ");
printf("F. See if a certian GPA was entered ");
printf("G. Display The Contents of the Array ");
printf("Q. Quit ");
printf("****************************************** ");
printf(" Enter your selection: ");
}// end function displayMenu
void displayMessage(char m[]){
int length = strlen(m);
int i;
system("COLOR 96"); // yellow on blue
for(i = 0; i < length + 6; i++)
printf("*");
printf(" ** %s ** ", m);
for(i = 0; i < length + 6; i++)
printf("*");
printf(" ");
} // end displayMessage
char getChoice(){
char result;
displayMenu();
scanf("%c", &result); FLUSH;
return toupper(result);
} // end getChoice
int enterGPA(float* arr,int count){
float gpa;
if(count<SIZE){
printf("Enter your GPA ");
scanf("%f", &gpa); FLUSH;
//printf("%f ",gpa);
arr[count]=gpa;
count++;
if(gpa<2.0)
printf("You need to work harder ");
if(gpa>3.4)
printf("Nice work ");
}
else{
printf("Size of array exceeded");
}
return arr;
} // end getChoice
void displayAverage(float* arr,int count){
int i=0;
float sum=0;
if(count>0)
{
for(i=0;i<count;i++){
sum=sum+arr[i];
}
float avg=sum/count;
printf("Average=%f ", avg);
}
else
printf("No values entered ");
}
void displayHighest(float* arr, int n){
if(n>1)
{
bubbleSort(arr,n);
printf("Highest=%f ", arr[n-1]);
}
else if(n==1)
printf("Highest=%f ", arr[n-1]);
else
printf("No values entered ");
}
void displayLowest(float* arr, int n){
if(n>1)
{
bubbleSort(arr,n);
printf("Lowest=%f ", arr[0]);
}
else if(n==1)
printf("Lowest=%f ", arr[0]);
else
printf("No values entered ");
}
void displayAdjustedAvg(float* arr, int n){
if(n>1)
{
bubbleSort(arr,n);
float sum=0;
int i=0;
for(i=1;i<n;i++){
sum=sum+arr[i];
}
float adjustedAvg= sum/(n-1);
printf("Adjusted Average=%f ", adjustedAvg);
}
else if(n==1)
{printf("There is only one value");}
else
printf("No values entered ");
}
void searchGPA(float* arr,int count){
int i=0;
float sum=0;
int found=0;
float gpa=0;
if(count>0)
{
printf("Enter your GPA ");
scanf("%f", &gpa); FLUSH;
for(i=0;i<count;i++){
if(arr[i]==gpa)
{
found=1;
printf("Found at=%d ", i+1);
break;
}
}
if(found==0)
{
printf("Not Found ");
}
}
else
printf("No values entered ");
}
void displayArray(float* arr, int n){
if(n>0)
{
bubbleSort(arr,n);
float sum=0;
int i=0;
for(i=0;i<n;i++){
printf("GPA{%d}=%f ", i+1,arr[0]);
}
float adjustedAvg= sum/(n-1);
printf("Adjusted Average=%f ", arr[0]);
}
else
printf("No values entered ");
}
void myFlush(){
while(getchar() != ' ');
} // end myFlush
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void bubbleSort(float arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}
////////////////////////////////////////////////////////////////////////
/*
FORE GROUND BACK GROUND
0 Black 8 Gray
1 Blue 9 Light Blue
2 Green A Light Green
3 Aqua B Light Aqua
4 Red C Light Red
5 Purple D Light Purple
6 Yellow E Light Yellow
7 White F Bright White
*/
Explanation / Answer
Answer: Every function is written on each line for the sake of readablity of code. You can write the code on a single line and it will still work. Dont write #include code on the same line. It won't work.
#define _CRT_SECURE_NO_WARNINGS
#define PAUSE system("pause")
#define FLUSH myFlush()
#define SIZE 500
#define CLS system("cls")
//#include
//#include
#include <string.h> // for strlen() gets the length of a string
#include <ctype.h> // for the toupper() function
#include <conio.h> // so I can play with colors
// ProtoType Functions Here so that main() function knows that these functions are available already
void displayMessage(char m[]);
char getChoice();
void displayMenu();
int enterGPA(float*,int);
void displayAverage(float*,int);
void bubbleSort(float *, int );
void swap(int *, int *);
void displayHighest(float*, int);
void displayLowest(float*, int);
void displayAdjustedAvg(float*, int);
void searchGPA(float*, int);
void displayArray(float*, int);
void myFlush();
/*The program starts from main function each line is executed in sequence written in here*/
main(){
char choice; // a variable to store choice going to get from user
float grades[SIZE] = {2.3, 4.3, 1.2, 4.0, 2.16, 3.33, 3.88, .9}; // I placed sample data in the array, can be removed
int count = 8; // this should be zero, but I entered 8 test values above...remove both
/*below code will keep on taking input from user until the user enters "Q" letter*/
do{
choice = getChoice();
switch(choice){
/*Switch block checks what letter is given in "choice" variable and executes code below that letter case only*/
case 'A': // enter a single GPA
count=enterGPA(grades,count);
PAUSE; // Pauses the program
break; // break stops from executing below lines, if this break is removed case "B" will also get executed.
case 'B': // Display the average of all GPAs
displayAverage(grades,count);
PAUSE;
break;
case 'C': // Display the highest GPA
displayHighest(grades,count);
PAUSE;
break;
case 'D': // Display the lowest GPA
displayLowest(grades,count);
PAUSE;
break;
case 'E': // Display the adjusted average
displayAdjustedAvg(grades,count);
break;
case 'F': // Search for a certian CPA
searchGPA(grades,count);
break;
case 'G': // Display The Values in the Array in ascending order
displayArray(grades,count);
break;
case 'Q': // Quit the program
CLS; // This will clear screen
displayMessage("Thanks for using my pretty cool GPA grade program.");
PAUSE;
break;
default: /*This code is executed when a letter or number which is not present in options is entered by user*/
displayMessage("Invalid selection, pick again");
PAUSE;
break;
} // end of switch
}while(choice != 'Q');
}// end of main
////////////////////////////////////////////////////////////////////////////////////////////////////////
/*This function clears the screen and displays a nice menu*/
void displayMenu(){
CLS;
system("COLOR E0");
printf("****************************************** ");
printf("*** M A I N M E N U *** ");
printf("****************************************** ");
printf("A. Enter GPA ");
printf("B. Display Average of all GPA's ");
printf("C. Display the Highest GPA ");
printf("D. Display the Lowest GPA ");
printf("E. Display the adjusted average ");
printf("F. See if a certian GPA was entered ");
printf("G. Display The Contents of the Array ");
printf("Q. Quit ");
printf("****************************************** ");
printf(" Enter your selection: ");
}// end function displayMenu
void displayMessage(char m[]){
int length = strlen(m);
int i;
system("COLOR 96"); // yellow on blue
for(i = 0; i < length + 6; i++)
printf("*");
printf(" ** %s ** ", m);
for(i = 0; i < length + 6; i++)
printf("*");
printf(" ");
} // end displayMessage
/*Below function takes a single character from user as his choice and converts is to capital letter using toupper*/
char getChoice(){
char result;
displayMenu();
scanf("%c", &result); FLUSH;
return toupper(result);
} // end getChoice
/*Following code gets your GPA and shows a message accordingly*/
int enterGPA(float* arr,int count){
float gpa;
if(count<SIZE){
printf("Enter your GPA ");
scanf("%f", &gpa); FLUSH;
//printf("%f ",gpa);
arr[count]=gpa;
count++;
if(gpa<2.0)
printf("You need to work harder ");
if(gpa>3.4)
printf("Nice work ");
}
else{
printf("Size of array exceeded");
}
return arr;
}
/* This function calculates the average of the array passed. It adds all the numbers in array then divides the numbers by total number of element in array*/
void displayAverage(float* arr,int count){
int i=0;
float sum=0;
if(count>0)
{
for(i=0;i<count;i++){
sum=sum+arr[i];
}
float avg=sum/count;
printf("Average=%f ", avg);
}
else
printf("No values entered ");
}
/*Follwoing function finds the highest value from the array passed by comparing the values in it. It first sorts the array in ascending order using bubblesort algorithm then gives the last entry in ath array*/
void displayHighest(float* arr, int n){
if(n>1)
{
bubbleSort(arr,n);
printf("Highest=%f ", arr[n-1]);
}
else if(n==1)
printf("Highest=%f ", arr[n-1]);
else
printf("No values entered ");
}
*Follwoing function finds the lowest value from the array passed by comparing the values in it. It first sorts the array in ascending order using bubblesort algorithm then gives the 1st entry in the array*/
void displayLowest(float* arr, int n){
if(n>1)
{
bubbleSort(arr,n);
printf("Lowest=%f ", arr[0]);
}
else if(n==1)
printf("Lowest=%f ", arr[0]);
else
printf("No values entered ");
}
/*This function displays the adjusted array average.*/
void displayAdjustedAvg(float* arr, int n){
if(n>1)
{
bubbleSort(arr,n);
float sum=0;
int i=0;
for(i=1;i<n;i++){
sum=sum+arr[i];
}
float adjustedAvg= sum/(n-1);
printf("Adjusted Average=%f ", adjustedAvg);
}
else if(n==1)
{printf("There is only one value");}
else
printf("No values entered ");
}
/*This function searches each element one by one and finds whether the element is present in the array or not*/
void searchGPA(float* arr,int count){
int i=0;
float sum=0;
int found=0;
float gpa=0;
if(count>0)
{
printf("Enter your GPA ");
scanf("%f", &gpa); FLUSH;
for(i=0;i<count;i++){
if(arr[i]==gpa)
{
found=1;
printf("Found at=%d ", i+1);
break;
}
}
if(found==0)
{
printf("Not Found ");
}
}
else
printf("No values entered ");
}
/*This function simply display elelments of the array*/
void displayArray(float* arr, int n){
if(n>0)
{
bubbleSort(arr,n);
float sum=0;
int i=0;
for(i=0;i<n;i++){
printf("GPA{%d}=%f ", i+1,arr[0]);
}
float adjustedAvg= sum/(n-1);
printf("Adjusted Average=%f ", arr[0]);
}
else
printf("No values entered ");
}
/*Goes till ae end of a line is entered. This function will end when you press an Enter button*/
void myFlush(){
while(getchar() != ' ');
} // end myFlush
/*This function swaps two values with each other*/
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
/*This function sorts the array in ascending order*/
void bubbleSort(float arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}
////////////////////////////////////////////////////////////////////////
/*
FORE GROUND BACK GROUND
0 Black 8 Gray
1 Blue 9 Light Blue
2 Green A Light Green
3 Aqua B Light Aqua
4 Red C Light Red
5 Purple D Light Purple
6 Yellow E Light Yellow
7 White F Bright White
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.