Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.61 KB | None | 0 0
  1. //Assignment 2
  2. //Lauren Shver
  3. //500776374
  4. // I certify that this lab is entirely my own work
  5.  
  6. #include <strings.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <stdbool.h>
  10. #include <math.h>
  11. #include <ctype.h>
  12. #include "assign2funcs.c"
  13.  
  14. /*
  15. Name: main
  16. Purpose: Get user input & action based off input
  17. Parameters: N/A
  18. Return Values: 1 for bad input, 0 for good input
  19. Input: Input from keyboard, first digit is the length of the array and the following are the digits to be normalized
  20. Output: print the input in a formatted way & print a normalized version of the array
  21. */
  22.  
  23. int main(void){
  24. //while loop condition & result to check if int
  25. bool condition = true; /* condition for while loop for continuous input*/
  26. int result; /* gets result value from input (0 if not an int) */
  27.  
  28.     while ( condition == true ){
  29.         int max_value = 9; /* max value of input array */
  30.         double input_array[max_value]; /* input array with size of max value*/
  31.         //set array to [0,0,0,0,0,0,0,0,0] aka a blank array
  32.         memset(input_array, 0, sizeof(input_array));
  33.         //loop for input until reached max_value
  34.         for ( int i = 0 ; i < max_value; i++ ){ /* index for input array */
  35.             //sets result to 1 if int, 0 in not
  36.             result = scanf("%lf", &input_array[i]);
  37.             //if result is ever 0, exit loop
  38.             if (result == 0){
  39.                 break;
  40.             }
  41.             //exit if first input is 0, set exit code to 0 & sets condition to false, ending the program
  42.             if((int)input_array[0] == 0){
  43.                 condition = false;
  44.                 exit(0);
  45.             }
  46.             //exit if EOF is inputted, set exit code to 0 & sets condition to false, ending the program
  47.             if(input_array[0] == EOF){
  48.                 condition = false;
  49.                 exit(0);
  50.             }
  51.             //break input reading if the first input if larger than the max value
  52.             if ((int)input_array[0] > max_value){
  53.                 break;
  54.             }
  55.             //if the first digit is less than the max_value, the max value will change so that scanf stops reading input after its read the correct number of values
  56.             if (input_array[0] <= max_value){
  57.                 max_value=(int)input_array[0]+1;
  58.             }
  59.  
  60.         }
  61.         //printing conditions//
  62.         //if the result is 0, means there was a non-int inputted & should print BAD INPUT and reset stdin  
  63.         if (result == 0){
  64.             printf("BAD INPUT\n");
  65.             rewind(stdin);
  66.         //if all the inputs are numeric then the array can be printed & normalized then reset stdin
  67.         } else if ((int) input_array[0] <= max_value){
  68.             vectorPrint(input_array, max_value);
  69.             normalize(input_array, max_value);
  70.             rewind(stdin);
  71.         //all other bad input (first digit over max value) print BAD INPUT and reset stdin
  72.         } else {
  73.             printf("BAD INPUT\n");
  74.             rewind(stdin);
  75.         }
  76.  
  77.     }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement