Advertisement
balrougelive

Untitled

Aug 10th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. /*
  2. Project 3
  3.  
  4. Make the following improvements to Project 2.
  5.  
  6. 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.
  7. 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).
  8. e.g. getInput(names[i], nameLen, &onCampus, &units);
  9. You can break this into smaller functions like getName, getHousing, and getUnits but they must be pass by reference.
  10. 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.
  11. e.g. printOutput(names[i], tuitionCost[i]);
  12.  
  13. Do NOT use global variables. The global constants defined in previous programs are OK.
  14.  
  15. 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.
  16.  
  17. Challenge Exercise 2 – Make a menu and allow the user to add or print students (up to 5) based on a menu selection.
  18.  
  19. Enter student name: Tim Berners-Lee
  20. Enter y if student lives on campus, n otherwise: y
  21. Enter current unit count: 15
  22.  
  23. Enter student name: Edsger W. Dijkstra
  24. Enter y if student lives on campus, n otherwise: n
  25. Enter current unit count: 7
  26.  
  27. Enter student name: Dennis Ritchie
  28. Enter y if student lives on campus, n otherwise: y
  29. Enter current unit count: 11
  30.  
  31. Enter student name: Ken Thompson
  32. Enter y if student lives on campus, n otherwise: n
  33. Enter current unit count: 13
  34.  
  35. Enter student name: Brian Kernighan
  36. Enter y if student lives on campus, n otherwise: y
  37. Enter current unit count: 12
  38.  
  39. Student name: Tim Berners-Lee
  40. Amount due: $2470
  41.  
  42. Student name: Edsger W. Dijkstra
  43. Amount due: $700
  44.  
  45. Student name: Dennis Ritchie
  46. Amount due: $2100
  47.  
  48. Student name: Ken Thompson
  49. Amount due: $1290
  50.  
  51. Student name: Brian Kernighan
  52. Amount due: $2200
  53.  
  54.  
  55. The average tuition cost for these 5 students is $1752.00.
  56. */
  57.  
  58. #include <string.h> //Using the string library
  59. #include <stdio.h> //Using the standard input output library
  60. #pragma warning(disable:4996) //Disables warnings with scanf function
  61.  
  62. //Defining known constants:
  63. #define UNIT_COST 100 //Constant cost of a unit
  64. #define DISCOUNT_VALUE 10 //Constant discount value of $10 per unit over 12
  65. #define DISCOUNT_THRESHHOLD 12 //Constant discount unit threshhold of 12 units
  66. #define STUDENT_COUNT 5 //Constant count of number of students
  67. #define HOUSING_COST 1000 //Constant cost of living on campus at $1000
  68. #define STRING_SIZE 30 //Constant string size for use in the multidimensional array
  69.  
  70. //Functions to calculate amountsDue
  71. float averageTuition(int, int); //Calculates the average of the inputs
  72. void getInput(char*, int, char*, int*); //Function to get inputs and store them
  73. void printOutput(const char*, const int); //Prints const from input
  74. int calculateInputs(char*, int*); //Function to take char and int inputs and store them as pointers
  75.  
  76. void main()
  77. {
  78. int units; //Int to use for units
  79. char name[STUDENT_COUNT][STRING_SIZE]; //Setting up storing the student names in a char string multidimensional array
  80. char campus; //Variable used to declare if student lives on campus or not
  81. int inputVariable = 0, amountTotals = 0; //Defining two ints to use for logic
  82. int amountDue[STUDENT_COUNT]; //amountDue of the constant Student Count
  83.  
  84. while (inputVariable < STUDENT_COUNT) //While structure to capture Inputs
  85. {
  86. getInput(name[inputVariable], STRING_SIZE, &campus, &units); //Populate both dimensions of the array
  87.  
  88. amountDue[inputVariable] = calculateInputs(&campus, &units); //Call amountDue
  89.  
  90. inputVariable++; //Increment Input Variable for loop logic
  91. }
  92.  
  93. for (int i = 0; i < STUDENT_COUNT; i++) //Logic for sum function
  94. {
  95. amountTotals += amountDue[i];
  96. }
  97.  
  98. for (int i = 0; i < STUDENT_COUNT; i++) //Print the results
  99. {
  100. printOutput(name[i], amountDue[i]);
  101. }
  102. 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
  103. }
  104.  
  105. void getInput(char *name, int size, char* onCampus, int* units) //Function to get input of each point in the array
  106. {
  107. printf("Enter student name : ");
  108. fgets(name, size, stdin); // Get input of student name without using "&", as it's pointed to
  109.  
  110. printf("Enter y if student lives on campus, n otherwise : "); // Gets the input of if the student lives on campus or not
  111. scanf("%c", onCampus);
  112.  
  113. printf("Enter current unit count: "); //Gets the input of the number of units a student is taking
  114. scanf("%d", units);
  115.  
  116. while ((getchar()) != '\n'); //Deleted the new line printed through the function
  117. }
  118.  
  119.  
  120. int calculateInputs(char* onCampus, int* unitCount) //Function to calculate the amount due for each student
  121. {
  122. if (*unitCount > DISCOUNT_THRESHHOLD && *onCampus == 'y')
  123. {
  124. return (*unitCount - DISCOUNT_THRESHHOLD) * (UNIT_COST - DISCOUNT_VALUE) + (DISCOUNT_THRESHHOLD * UNIT_COST) + HOUSING_COST;
  125. }
  126. if (*unitCount > DISCOUNT_THRESHHOLD)
  127. {
  128. return (*unitCount - DISCOUNT_THRESHHOLD) * (UNIT_COST - DISCOUNT_VALUE) + (DISCOUNT_THRESHHOLD * UNIT_COST);
  129. }
  130. if (*onCampus == 'y')
  131. {
  132. return (*unitCount * UNIT_COST) + HOUSING_COST;
  133. }
  134. else
  135. {
  136. return (*unitCount * UNIT_COST);
  137. }
  138. }
  139.  
  140. void printOutput(char const* name, const int tuitionCost) //Function to print the output of the multidimensional array
  141. {
  142. printf("\nStudent name : %s\rAmount due : $%d\n\n", name, tuitionCost); //Prints the results of tuitionCost
  143. }
  144.  
  145. float averageTuition(int total, int count) //Function to calculate the average tuition
  146. {
  147. float duesAverage = 0; //Declare a duesAverage variable (float)
  148.  
  149. return duesAverage = (total * 1.0f) / count; //Calculate the average of the 5 students
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement