Official Midterm Solutions are online, but here is the files I will use to show how the solution works
Q14
/*
* File: main.c
* Author: kmingk
*
* Created on October 18, 2015, 12:46 PM
*/
#include <stdio.h>
#include <stdlib.h>
/* Develop a program that would continuously ask the users for characters.
* When a user enters NAN, it should print out pattern found, and should be
* able to recognize NANAN as two separate instances.
*
* Program should end when F is entered
*/
int main(int argc, char** argv) {
// Declare Variables
char c1, c2; // c1 - previous input, c2 - the input before c1
char currentInput;
// Initialize variables to non-important characters
c1 = 'C';
c2 = 'C';
currentInput = 'C';
// Keep looping for user input of characters
do {
printf ("Please enter a character: ");
scanf (" %c", ¤tInput);
// Proper way to do this. Keeps a correct running history
if ((currentInput == 'N') && (c1 == 'A') && (c2 == 'N')) {
printf ("Pattern NAN Found\n");
}
// Keep a running history of characters
c2 = c1;
c1 = currentInput;
// Incorrect way other people are doing this - This misses NANAN or NNAN
// if (currentInput == 'N') {
// printf ("Please enter a character: ");
// scanf (" %c", &c1);
// if (c1 == 'A') {
// printf ("Please enter a character: ");
// scanf (" %c", &c2);
// if (c2 == 'N') {
// printf ("Pattern NAN Found\n");
// }
// }
// }
} while (currentInput != 'F');
printf ("Done!\n");
return (EXIT_SUCCESS);
}
Q11
/*
* File: main.c
* Author: kmingk
*
* Created on October 18, 2015, 1:21 PM
*/
#include <stdio.h>
#include <stdlib.h>
/* Create a function that takes in a user inputted integer and reverse the digits
* must use a function
*
*/
// 12345 --> 54321
// 21452 --> 25412
// 12340 --> 04321
int reverseDigit (int input) {
// We want to start with output = 0 since we use it later on, so
// make sure its initialized properly
int output = 0;
// we want to stop once we are done with the "1's" digit.
// Extra: Try changing loop so it is >= 10, 100, what happens?
while (input >= 1) {
int remainder; // temp variable to store the remainder
remainder = input%10;
input = input/10; // our loop relies on us to continues divide input
output = output*10 + remainder;
}
// Example of how this works:
// 12345 --> input
// 12345%10 --> 5 + output*10 (0*10) = 5 = output
// 12345/10 --> 1234 --> input
// 1234%10 --> 4 + output*10 (5*10) = 54 = output
// 1234/10 --> 123 --> input
// 123%10 --> 3 + output*10 (54*10) = 543 = output
// 123/10 --> 12
return output;
}
int main(int argc, char** argv) {
int userInput;
int output;
// Get User Input
printf ("Enter Number: ");
scanf("%d", &userInput);
output = reverseDigit (userInput);
printf ("Output: %d\n", output);
return (EXIT_SUCCESS);
}
Q13
/*
* File: main.c
* Author: Kmingk
*
* Created on October 18, 2015, 1:03 PM
*/
#include <stdio.h>
#include <stdlib.h>
/* Program to accept a number input from the user (0-9)
* The program will then output a pattern that is ten rows in total and ten
* digits.
* The pattern will shift to the left
*
* Example:
* Input: 0
* 0123456789
* 1234567890
* 2345678901
* 3456789012
* 4567890123
* 5678901234
* 6789012345
* 7890123456
* 8901234567
* 9012345678
*
* Input: 3
* 3456789012
* 4567890123
* 5678901234
* 6789012345
* 7890123456
* 8901234567
* 9012345678
* 0123456789
* 1234567890
* 2345678901
*
*/
int main(int argc, char** argv) {
// Declare Variables
int userInput = 0;
int row, col;
// Get User Input
printf ("Please enter index (0-9): ");
scanf ("%d", &userInput);
// Go through the 10 rows
for (row = 0; row < 10; row++) {
// Go through all the columns
for (col = 0; col < 10; col++) {
// Uncomment each of the lines (1-4) one by one to see the effect
// 1: Printing out col will repeat me 0123456789 for all rows
// printf ("%d", col);
// 2: Printing out col+row --> starting number increases by 1 each
// row, spacing added for visibility
// printf ("%d ", (col+row));
// 3: Printing out col+row%10 --> Gets me the shifted pattern
// printf ("%d", (col+row)%10);
// 4: Adding in the effect of userInput
// printf ("%d", (col+row+userInput)%10);
// Extra: What happens if i print newline here?
// A: Adds newline after every digit! --> not good
// printf ("\n");
}
// end of columns, print newline
printf ("\n");
}
return (EXIT_SUCCESS);
}