Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Project 3
- Make the following improvements to Project 2.
- Store the student names in a multidimensional array and amounts due in a single dimension array. This will allow you to get input for all 5 students without printing. Then once all 5 students have been entered, print their names and amounts due as shown below.
- Write a function called getInput that returns void and takes char* (for name), int (for name length – may be optional for some), char* (for y or n response), and int* (for units). In the body of the function prompt the user and store input for a student name, char (y or n) depending on if they live on campus, and int for number of units. Remember when using scanf with pointers no ampersand is needed. fgets/gets stays the same (no ampersand).
- e.g. getInput(names[i], nameLen, &onCampus, &units);
- You can break this into smaller functions like getName, getHousing, and getUnits but they must be pass by reference.
- Write a function called printOutput that returns void and takes a const char* (for name) and const int (for tuition cost). In the body of the function print a label, student name and new line followed by a label and the amount due.
- e.g. printOutput(names[i], tuitionCost[i]);
- Do NOT use global variables. The global constants defined in previous programs are OK.
- Challenge Exercise 1 – Allow the user to stop entering data early if -1 is entered as input. The program should print all information currently stored. Calculations must be correct.
- Challenge Exercise 2 – Make a menu and allow the user to add or print students (up to 5) based on a menu selection.
- Enter student name: Tim Berners-Lee
- Enter y if student lives on campus, n otherwise: y
- Enter current unit count: 15
- Enter student name: Edsger W. Dijkstra
- Enter y if student lives on campus, n otherwise: n
- Enter current unit count: 7
- Enter student name: Dennis Ritchie
- Enter y if student lives on campus, n otherwise: y
- Enter current unit count: 11
- Enter student name: Ken Thompson
- Enter y if student lives on campus, n otherwise: n
- Enter current unit count: 13
- Enter student name: Brian Kernighan
- Enter y if student lives on campus, n otherwise: y
- Enter current unit count: 12
- Student name: Tim Berners-Lee
- Amount due: $2470
- Student name: Edsger W. Dijkstra
- Amount due: $700
- Student name: Dennis Ritchie
- Amount due: $2100
- Student name: Ken Thompson
- Amount due: $1290
- Student name: Brian Kernighan
- Amount due: $2200
- The average tuition cost for these 5 students is $1752.00.
- */
- #include <string.h> //Using the string library
- #include <stdio.h> //Using the standard input output library
- #pragma warning(disable:4996) //Disables warnings with scanf function
- //Defining known constants:
- #define UNIT_COST 100 //Constant cost of a unit
- #define DISCOUNT_VALUE 10 //Constant discount value of $10 per unit over 12
- #define DISCOUNT_THRESHHOLD 12 //Constant discount unit threshhold of 12 units
- #define STUDENT_COUNT 5 //Constant count of number of students
- #define HOUSING_COST 1000 //Constant cost of living on campus at $1000
- #define STRING_SIZE 30 //Constant string size for use in the multidimensional array
- //Functions to calculate amountsDue
- float averageTuition(int, int); //Calculates the average of the inputs
- void getInput(char*, int, char*, int*); //Function to get inputs and store them
- void printOutput(const char*, const int); //Prints const from input
- int calculateInputs(char*, int*); //Function to take char and int inputs and store them as pointers
- void main()
- {
- int units; //Int to use for units
- char name[STUDENT_COUNT][STRING_SIZE]; //Setting up storing the student names in a char string multidimensional array
- char campus; //Variable used to declare if student lives on campus or not
- int inputVariable = 0, amountTotals = 0; //Defining two ints to use for logic
- int amountDue[STUDENT_COUNT]; //amountDue of the constant Student Count
- while (inputVariable < STUDENT_COUNT) //While structure to capture Inputs
- {
- getInput(name[inputVariable], STRING_SIZE, &campus, &units); //Populate both dimensions of the array
- amountDue[inputVariable] = calculateInputs(&campus, &units); //Call amountDue
- inputVariable++; //Increment Input Variable for loop logic
- }
- for (int i = 0; i < STUDENT_COUNT; i++) //Logic for sum function
- {
- amountTotals += amountDue[i];
- }
- for (int i = 0; i < STUDENT_COUNT; i++) //Print the results
- {
- printOutput(name[i], amountDue[i]);
- }
- printf("The average tuition cost for these %d students is $%.2f.\n", STUDENT_COUNT, averageTuition(amountTotals, STUDENT_COUNT)); // Print the average and calculate the average
- }
- void getInput(char *name, int size, char* onCampus, int* units) //Function to get input of each point in the array
- {
- printf("Enter student name : ");
- fgets(name, size, stdin); // Get input of student name without using "&", as it's pointed to
- printf("Enter y if student lives on campus, n otherwise : "); // Gets the input of if the student lives on campus or not
- scanf("%c", onCampus);
- printf("Enter current unit count: "); //Gets the input of the number of units a student is taking
- scanf("%d", units);
- while ((getchar()) != '\n'); //Deleted the new line printed through the function
- }
- int calculateInputs(char* onCampus, int* unitCount) //Function to calculate the amount due for each student
- {
- if (*unitCount > DISCOUNT_THRESHHOLD && *onCampus == 'y')
- {
- return (*unitCount - DISCOUNT_THRESHHOLD) * (UNIT_COST - DISCOUNT_VALUE) + (DISCOUNT_THRESHHOLD * UNIT_COST) + HOUSING_COST;
- }
- if (*unitCount > DISCOUNT_THRESHHOLD)
- {
- return (*unitCount - DISCOUNT_THRESHHOLD) * (UNIT_COST - DISCOUNT_VALUE) + (DISCOUNT_THRESHHOLD * UNIT_COST);
- }
- if (*onCampus == 'y')
- {
- return (*unitCount * UNIT_COST) + HOUSING_COST;
- }
- else
- {
- return (*unitCount * UNIT_COST);
- }
- }
- void printOutput(char const* name, const int tuitionCost) //Function to print the output of the multidimensional array
- {
- printf("\nStudent name : %s\rAmount due : $%d\n\n", name, tuitionCost); //Prints the results of tuitionCost
- }
- float averageTuition(int total, int count) //Function to calculate the average tuition
- {
- float duesAverage = 0; //Declare a duesAverage variable (float)
- return duesAverage = (total * 1.0f) / count; //Calculate the average of the 5 students
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement