Docsity

Practice Midterm and Their Solutions, Papers of Engineering

Toggle sidebar
bg1
Name:________________________
EET 208
Introduction to Programming
for Embedded Systems
Practice Midterm Exam: Solutions
This is a practice exam (not graded). Solutions will be posted by this weekend.
The actual exam is open-book, open-notes, open-computer, open-Internet, but you may
not discuss the exam with anyone except me, in person or online, until the end of the
exam.
You may use your compiler to test code if you wish, but be aware that this takes time.
Partial credit will be given as long as each question is attempted.
1. (5 points) What is wrong with the following C program? How would you fix it?
(Note: Please assume all quotation marks in this exam are the correct type.)
(Hint: Don’t over-think this one.)
#include <stdio.h>
int main(){
printf ("Hello, World!\n"); //A semicolon is needed after this line
return (0);
}
2. (15 points) Write a C code snippet that uses a for loop to calculate
the sum of the even numbers from 2 to 100 (including both of those).
Declare and initialize any variables that you use, as needed.
//Here’s one way to do it:
int n;
int sum=0;
for(n=2;n<=100;n=n+2){
sum=sum+n;
}
pf3
pf4

Partial preview of the text

Download Practice Midterm and Their Solutions and more Papers Engineering in PDF only on Docsity!

Name:________________________

EET 208

Introduction to Programming

for Embedded Systems

Practice Midterm Exam: Solutions

This is a practice exam (not graded). Solutions will be posted by this weekend. The actual exam is open-book, open-notes, open-computer, open-Internet, but you may not discuss the exam with anyone except me, in person or online, until the end of the exam. You may use your compiler to test code if you wish, but be aware that this takes time. Partial credit will be given as long as each question is attempted.

  1. (5 points) What is wrong with the following C program? How would you fix it? ( Note: Please assume all quotation marks in this exam are the correct type.) (Hint: Don’t over-think this one.) #include <stdio.h> int main(){ printf ("Hello, World!\n") ; //A semicolon is needed after this line return (0); }
  2. (15 points) Write a C code snippet that uses a for loop to calculate the sum of the even numbers from 2 to 100 (including both of those). Declare and initialize any variables that you use, as needed. //Here’s one way to do it: int n; int sum=0; for(n=2;n<=100;n=n+2){ sum=sum+n; }
  1. (5 points) How many times will the following C code print out “test”? (Assume that the include files, main(), and declarations all exist.) for(x=1;x<10;x++){ printf(“test\n”); } This will print “test” nine times – one each for x=1 through x=9. When x=10, the loop will exit.
  2. (10 points) What is wrong with the following C code? How would you fix it? (Hint: It compiles, but doesn’t work like the description says…) //Print the numbers one through ten #include <stdio.h> int main(){ int x; for (x=1;x < 11;x++){ //The > should be a <. printf(“X is: %d\n”,x); } }
  3. (5 points) What command would you use to make an Arduino wait for one-tenth of a second? delay(100); //delay() takes milliseconds as its argument.
  4. (10 points) Write code to make Pin 10 on an Arduino Uno an output, and set it to output a High level. Put your code inside setup(), below. void setup(){ pinMode(10, OUTPUT); //Make the pin an output digitalWrite(10,HIGH); //Set it high }
  5. (5 points) You need to include a library file called sensor.h. Write the compiler directive to make sure this library is loaded. #include <sensor.h>
  1. (LAB EXERCISE) (25 points) Using your Arduino and LED board, write a program to do the following:  Blink the LED on for 100ms, then off for 100ms  Write “1” to the serial terminal on a line by itself  Wait one second  Blink the LED on for another 100ms, off for 100ms, twice  Write “2” to the serial terminal on a line by itself  Wait one second  Blink the LED on for another 100ms, off for 100ms, three times  Write “3” to the serial terminal on a line by itself  Wait one second Your code should loop continuously. void blink(); //We use this six times, so it’s worth making it into a function. (See below.) void setup(){ pinMode(13, OUTPUT); //If using another board, choose a relevant pin //and use that throughout the code. //(Pin 13 on the Arduino Uno has a built-in LED.) Serial.begin(9600); } void loop(){ //Blink once blink(); //We can use this because it’s defined below… Serial.println(“1”); delay(1000); //Blink twice blink();blink(); //You can put more than one statement per line (but don’t do this often.) Serial.println(“2”); delay(1000); //Blink thrice blink();blink();blink(); //(In this case, I think it’s understandable.) Serial.println(“3”); delay(1000); } //Definition of function called “blink” – which blinks the LED once. //If we take the extra time to make this into a function, we can then just write blink(); void blink(){ digitalWrite(13, HIGH); delay(100); digitalWrite(13, LOW); delay(100); }