Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Assignment 2
- //Lauren Shver
- //500776374
- // I certify that this lab is entirely my own work
- #include <strings.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <stdbool.h>
- #include <math.h>
- #include <ctype.h>
- #include "assign2funcs.c"
- /*
- Name: main
- Purpose: Get user input & action based off input
- Parameters: N/A
- Return Values: 1 for bad input, 0 for good input
- Input: Input from keyboard, first digit is the length of the array and the following are the digits to be normalized
- Output: print the input in a formatted way & print a normalized version of the array
- */
- int main(void){
- //while loop condition & result to check if int
- bool condition = true; /* condition for while loop for continuous input*/
- int result; /* gets result value from input (0 if not an int) */
- while ( condition == true ){
- int max_value = 9; /* max value of input array */
- double input_array[max_value]; /* input array with size of max value*/
- //set array to [0,0,0,0,0,0,0,0,0] aka a blank array
- memset(input_array, 0, sizeof(input_array));
- //loop for input until reached max_value
- for ( int i = 0 ; i < max_value; i++ ){ /* index for input array */
- //sets result to 1 if int, 0 in not
- result = scanf("%lf", &input_array[i]);
- //if result is ever 0, exit loop
- if (result == 0){
- break;
- }
- //exit if first input is 0, set exit code to 0 & sets condition to false, ending the program
- if((int)input_array[0] == 0){
- condition = false;
- exit(0);
- }
- //exit if EOF is inputted, set exit code to 0 & sets condition to false, ending the program
- if(input_array[0] == EOF){
- condition = false;
- exit(0);
- }
- //break input reading if the first input if larger than the max value
- if ((int)input_array[0] > max_value){
- break;
- }
- //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
- if (input_array[0] <= max_value){
- max_value=(int)input_array[0]+1;
- }
- }
- //printing conditions//
- //if the result is 0, means there was a non-int inputted & should print BAD INPUT and reset stdin
- if (result == 0){
- printf("BAD INPUT\n");
- rewind(stdin);
- //if all the inputs are numeric then the array can be printed & normalized then reset stdin
- } else if ((int) input_array[0] <= max_value){
- vectorPrint(input_array, max_value);
- normalize(input_array, max_value);
- rewind(stdin);
- //all other bad input (first digit over max value) print BAD INPUT and reset stdin
- } else {
- printf("BAD INPUT\n");
- rewind(stdin);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement