C programming assignment, Sorting And Searching Assignment 1- The program specif
ID: 3701979 • Letter: C
Question
C programming assignment, Sorting And Searching Assignment
1- The program specifications.
YOU MUST NOT Use global variables
YOU MUST NOT Use the word goto
YOU MUST NOT Use the break command outside a case statement
DO NOT ignore comments
The sort and search Assignment
Outcome:
Student will demonstrate the ability to create and populate an array
Student will demonstrate the ability to pass an array to and from a function
Student will demonstrate the ability to sort and search an array
Student will demonstrate the ability to control the arrays size and effective size
Student will demonstrate the ability to use an array of values in math formulas
Program Specifications:
**************************
*** MAIN MENU *******
**************************
A. Enter GPA
B. Display Average of all GPA's
C. Display the Highest GPA
D. Display the Lowest GPA
E. Display the adjusted average
F. See if a certain GPA was entered
G. Display The contents of the Array
Q. Quit
**************************
Enter your selection:
You are to write a WELL written C program that uses the menu above. All your code will be written using functions. Main will only have a menu system and a switch. Main will call all other functions. The program needs to allow for about 1000 or so GPAs or just some number, you pick, it should work with any reasonable size.
Enter GPA:
If this is selected, the user will then enter a signal value for his or her GPA (example 3.55). Remember you cannot exceed the SIZE of the array. If a GPA below 2.0 is entered: Display the following message: You need to study harder. If GPA is above 3.5 Display the following message: Nice work.
Display Average of all GPA’s
If this is selected, the program will display the average of the values within the array. If now values have been entered, then the program will not display the average, instead it will message the user to enter values first.
Display Highest GPA
If this is selected, you should call a bubble sort routine, sort the array and display the value located at either array[0] or array[SIZE -1] depending on if you sorted it in ascending or descending order. Finally, if no values have been entered, the program will notify the user to enter values first.
Display Lowest GPA
If this is selected, you should call a bubble sort routine, sort the array and display the value located at either array[0] or array[SIZE -1] depending on if you sorted it in ascending or descending order. Finally, if no values have been entered, the program will notify the user to enter values first.
Display the adjusted average
If this is selected, the program will show the average of all the values in the array except for the lowest GPA value. In other words, it drops the lowest value and averages the rest. Keep in mind, that you cannot drop the lowest should you only have one value. Finally, if no values have been entered, the program will notify the user to enter values first.
See if a certain GPA was entered
The program will ask the user to enter any GPA value. Then the program will search the array for the first occurrence of that value. If the value is not found, the program will inform the user that the GPA was never entered. If the value was found, then the program would say that the value was found at the xx location in the array. For example, The GPA 3.55 the fifth value of the array. Finally, if no values have been entered, the program will notify the user to enter values first.
Display the Contents of the Array
If this is selected the array will be sorted in ascending order and the all values will be echoed to the screen.
Q. Quit
You can guess what happens here.
Submission Requirements:
You must attach your *.c source code and your design tool to the assignment thread.
Make sure you have a comment header with your name and the names of anyone else who joined you (maximum 3 people).
2- Here is: A structure chart for the program.
3-Here is: A template for you to use for this program. Copy and paste into a project.
MAIN OR START B. Display Avg All GPAs C. Display Highest D. Display Lowest E. Display Adj Avg F. Search G. Display contents of array Q. Quit progranm A. Enter GPA for GPA Make sure that at least one GPA has been ordered Sort from high to low and display value at array[0] Make sure that at least one GPA has been ordered Ask user for a GPA for the search. Search for the value Make sure that at least one GPA has been ordered Loop through the entire array and display all values Test for max size of array Prompt user Store next Make sure that at least one GPA has beer ordered Add up GPAs and divide by effective size Make sure at least one value has been entered Sort from low to high and display value at array[0] sort from low to Say good bye and exit Ig Save value at array[0] Add up the remaining values Divide by effective Size minus oneExplanation / Answer
Please upvote if you find the answer satisfactory :)
Comment if you face any issues.
///////////////////////////////////////////////////
// Written by..: your name goes here //
// Date Written: 7th April 2018 //
// Purpose.....: The sort and search assignment //
///////////////////////////////////////////////////
#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
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.