midterm review
Grant R. Vousden-Dishington
Fall 2013
Data types
- char
- (single) character
- int
- integer values
- float
- single-precision floating point number
- double
- double-precision floating point number
- twice as much memory as float
These are 'numerical' data types
Variables & names
Data is assigned
to memory using the assignment operator (=)
and referenced through the use of
variables
Names may ONLY contain alphanumeric characters and underscores
Names may NOT begin with numbers
Printing and scanning
- %c - char
- %d - int
- %f - float
- %lf - double
- Stands for 'long float'
special characters
- ' '
- single space
- '\t'
- tab space
- '\n'
- new line
- '\b'
- backspace (i.e. delete one character, move back one space)
- '\0'
- null character
- same as '', two single quotes with no space between
Conditional Statements
if(CONDITIONAL)
x++;
if(CONDITIONAL)
x++;
else
x--;
if(CONDITIONAL) {
x++; printf("Why is the midterm hand written?");}
else
x--;
Switch-Case Statements
Just another form of if-else
int x;
scanf("Gimme an x: %d", &x);
switch(x%2) { case 0: printf("x is even.");
case 1: printf("x is odd.");
default: printf("I don't know what that is but I don't like it.");
}
// Same as the above, using if-else structure
if(x%2 == 0)
printf("x is even.");
else if(x%2 == 1)
printf("x is odd.");
else
printf("I don't know what that is but I don't like it.'"
Loops
100% interchangeable - anything one can do, the other can dofor(THINGS_TO_DO_ONCE; CONDITIONAL; THINGS_TO_DO_EVERY_LOOP)
while(CONDITIONAL)
Same rules about { } as if-else statements
No 'else' statements for loops (in C)
1-D Arrays
int myArray[10]; // Declares an array of 10 integers
myArray[0] = 5; // Sets the FIRST element of the array to 5
myArray[1] = 7; // Sets the SECOND element of the array to 7
// Print out all the values in myArray
for(int i = 0; i < 10; i++)
printf("%d ", myArray[i]); // prints out all the elements of the array
// Short way to scan values into the array
i = 0;
while(scanf("%d", &myArray[i++]) > 0);
// This works too
for(i = 0; i < 10; i++)
scanf("%d", &myArray[i]);
PRACTice
Output 1
int i1 = 5, i2 = 10; double d1 = 5.0, d2 = 10.0, d3; d3 = i1/i2;
printf("line 1: %d\n, i1/i2); printf("line 2: %.1lf\n", d1/d2); printf("line 3: %.1lf\n", i1/d2); printf("line 4: %d\n", (int)d1/i2); printf("d3 = %.1lf\n", d3);
line 1: 0
line 2: 0.5
line 3: 0.5
line 4: 0
d3 = 0.0
Output 2
int x = 0, y = 0, i = 1, j = 1;
for (i = 0; i < 4; i++)
{
x = i;
for (j = 0; j < 10; j++)
y +=x;
}
printf("x = %d y = %d i = %d j = %d", x, y, i, j);
x = 3 y = 60 i = 4 j = 10
Output 3
int x = 2, y = 1;
switch(x+y)
{
case 1: x+=1;
case 2: x+=2; break;
case 3: x+=3;
case 4: x+=4; break;
default: x+=5;
}
printf("x = %d\n", x);
x = 9
Output 4
int i, x = 0;
for (i = 1; i < 6; i++)
{
if (i%2 == 0) continue;
if (i == 4) break;
x++;
}
printf("i = %d x = %d\n", i, x);
i = 6 x = 3
Output 5
double x = 5.2, y = 3.8, z = -5.2, w = -3.8;
int x2 = (int) (x + ( x < 0 ? -0.5 : 0.5) );
int y2 = (int) (y + ( y < 0 ? -0.5 : 0.5) );
int z2 = (int) (z + ( z < 0 ? -0.5 : 0.5) );
int w2 = (int) (w + ( w < 0 ? -0.5 : 0.5) );
printf("x2 = %d y2 = %d z2 = %d w2 = %d", x2, y2, z2, w2);
x2 = 5 y2 = 4 z2 = -5 w2 = -4
Output 6
int x = 0, y = -1, z = 1;
x-- || ++y && z++;
printf("x = %d y = %d z = %d\n", x, y, z)
x = -1 y = 0 z = 1
int x, y, z;
x = y = 1;
z = 0;
x-- || y-- && z++ ;
printf(“x = %d y = %d z = %d\n”, x, y, z);
x = 0 y = 1 z = 0
Program Rewriting
#include <stdio.h>
int main()
{
int i, j;
for (i = 3; i > 0; i--)
{
for (j = 0; j < i; j++)
printf("*");
printf("\n");
}
return 0;
}
***
**
*
#include <stdio.h> int main() { int i, j;
i = 3; while(i > 0) {
j = 0; while(j < i)
{ printf("*");
j++;
} printf("\n");
i--; } return 0; }
Program reWriting
Write the code that prints the following, using nested loops
|*****|
|**** |
|*** |
|** |
|* |
for(int i = 5, j; i > 0; --i)
{
printf("|");
for(j = 0; j < 5; j++)
printf("%c", j < i ? '*' : ' ');
printf("|\n");
}
Write your own code
A number n is a perfect number if its factors, including 1, but not including the number itself,
sum to n. For example, 6 is a perfect number, because the factors of 6 (not including 6) are 1, 2
and 3, and 1 + 2 + 3 = 6. 28 is also a perfect number, because 1 + 2 + 4 + 7 + 14 = 28.
Write code to print out all of the perfect numbers between two values entered by the user, where the first number entered is the starting point of the range, and the second number entered is the
ending point of the range. Your code should check to make sure that the numbers entered by th
user are strictly positive, and that the second number is larger than the first number. You may
assume the user enters integer values. After printing all of the perfect numbers between (and
including) the values entered by the user, your code should either print out “No perfect numbers
in range” or a message indicating the quantity of perfect numbers that were in the range. Note
that the word printed in the final message is either “number” or “numbers” depending on
whether 1 or more perfect numbers were found, but the line listing the perfect numbers always
says “Perfect numbers are:”
Write your own code
A number n is a perfect number if its factors, including 1, but not including the number itself,
sum to n. For example, 6 is a perfect number, because the factors of 6 (not including 6) are 1, 2
and 3, and 1 + 2 + 3 = 6. 28 is also a perfect number, because 1 + 2 + 4 + 7 + 14 = 28.
Write code to print out all of the perfect numbers between two values entered by the user, where the first number entered is the starting point of the range, and the second number entered is the
ending point of the range. Your code should check to make sure that the numbers entered by the user are strictly positive, and that the second number is larger than the first number. You may
assume the user enters integer values. After printing all of the perfect numbers between (and
including) the values entered by the user, your code should either print out “No perfect numbers
in range” or a message indicating the quantity of perfect numbers that were in the range. Note
that the word printed in the final message is either “number” or “numbers” depending on
whether 1 or more perfect numbers were found, but the line listing the perfect numbers always
says “Perfect numbers are:”
Common midterm mistakes
- Missing '&' using scanf and using '&' in printf
- Forgetting to increment/decrement value with ++ and --
- Missing scanf or printf all together
- Using < when should use <=, or vice-versa
- Makes you do one more or less loop than you should've
- Using wrong number of loops
- Use loops to get multiple user inputs, print multiple rows or columns
- RUSHING through the easier problems
- Not writing ANYTHING
- At least declare, scan and print your variable
- Not sleeping
ECE15review
By Grant R. Vousden-Dishington
ECE15review
Review slides for UCSD ECE 15 Midterm, Fall 2013
- 636