EE160
In the blank below, write the comparison / relational operator for "not"
!
In the blank below, write the comparison / relational operator for "not equal to"
!=
The symbol that indicates a directive to the C pre-processor is
#
Header files <> make your own #Include library file: "_____.h"
#include compiler director in a .c file. Include system or user header files. The brackets say that is a SYSTEM FILE. Where headers are stored.
The format conversion specifier for character data type is
%c
In order to print the ASCII decimal value of the letter A, the format conversion specifier should be
%d
In the blank below, write the comparison / relational operator for "and"
&&
Fill in the blanks to declare the variable var as a character data type and define it to be the letter A. You are to define it two different ways (hence the "or" separating the entries).
'A'|65, 'A'|65
cast operator example
(float) x It does not permanently change the data type or re-declare it. ___ printf("%d\n",3/5); =0 but with printf("%f\n",(float)3/5); is like saying... float x = 3; float y = 5; printf("%f\n",x/y);
Choose all the ways in which a comment can be written in C
//This is a comment /* This is a comment */
What numeric value does the C language assign to "true"
1
#include <stdio.h> int main (void) { int numerator= 5; __(1)__ denominator = 3; __(2)__ quotient; __(3)__ = numerator / denominator; print('The quotient of __(4)__ and __(5)__ is: __(6)__", numerator, denominator, quotient); return 0; }
1. float 2. float 3. quotient 4. %f 5. %f 6. %f
# ___1_____ <stido.h> _____2____ () //begin program { ____3_____ number; //declare a variable type of int ___4_____ = 7; //set value of number to seven __5____ ("The number is: ___6___ \n ___7___, number); //print "The number is:" and the number
1. include 2. int main/ main 3. int 4. number 5. printf 6. %d 7. " The number is: 7
What is the decimal value of 'C' - 'A'
2 Feedback: C is 67 in ASCII and A is 65 in ASCII so 67 - 65 = 2
In the blank below, type EXACTLY what is printed to the screen in the following code: #define THE_MEANING_OF_LIFE 42 int main(void) { printf("%d",THE_MEANING_OF_LIFE);
42
In the blank below, write the comparison / relational operator for "less than"
<
In the blank below, write the comparison / relational operator for "less than or equal to"
<=
In the blank below, write the comparison / relational operator for "equal to"
==
In the blank below, write the comparison / relational operator for "greater than"
>
What is conditional compilation?
A method with which a programmer can instruct a compiler to only compile portions of the code.
A design approach in which a simplified version of a problem is first solved and tested before adding more features is called a
Bottom-up design approach
Comparing Characters
Character data is represented by using standardized numeric codes. • ASCII (American Standard Code for Information Interchange) is the most common. • Here are ASCII characters and their decimal code values • Note 0 through 31 are non-printable control characters • Note also that numbers and letters (uppercase and lowercase) are in a contiguous increasing sequence • 'A' is 65 in ASCII • 'B' is 66 in ASCII • We can assign character values either with the ASCII value or with single quotes (or with the octal or hexadecimal equivalent)
What is the second step in the design process?
Devise a plan
What is the fourth step of the design process?
Evaluate
A macro is a variable.
F
An extra line of white space in C will cause a compilation error.
F
Every computer understands the same machine language.
F
Macros are declared with key words such as 'int', 'float', 'char', etc in front of the macro name
F
A macro is a different type of variable.
False
The symbol '5' has a decimal equivalent value of 5.
False Feedback:'5' has a decimal value of 53 in the ASCII table
getchar() and putchar() are functions
False Feedback:They act like functions but they're actually macros
At the hardware level, CPUs carry out operations as sequences of zeros and ones.
False Hardware carries out operations by applying different voltages across electronic circuits known as gates so that the gates are either "open" or "closed" These two states are represented as "true" or "false" or, numerically by 0 and 1.
In C, the following two lines function identically: sum = a + b; a + b = sum;
False Remember that the "=" sign is NOT an equals sign. It is the ASSIGNMENT OPERATOR. You can't have two things to the left of the assignment operator because the compiler doesn't know which variable the value should be assigned to.a + b = sum; will result in a compile error
A language which is English-like and easy to read and understand is considered a
High Level Language
Code::Blocks, Xcode, and repl.it are all examples of
IDE
What is the third step in the design process?
Implement
IDE (as it's used in this class) stands for
Integrated Development Environment
Typical character set
Like all other data, characters are assigned a unique bit pattern for the computer to interpret. • Characters computers can typically understand are: • Alphabetic lower case: 'a', 'b', ..... , 'z' • Alphabetic upper case: 'A', 'B', ..... , 'Z' • Digit symbols: '0', '1', .... '9' • Punctuation: ' . ', ' , ', ' ; ', etc. • Space: ' ' • Special Symbols: '@', '#', '$', '%', etc. • Control characters (used in escape sequences)
int main (void){ int x=2, y=4, z=6; if (x == 1) { if ( x == 2) { if (y == 4) { if ( z == 6) {print("Kauai"); } } } } if ( y == 4) { if ((x==2) && (z == 4)) { printf("Oahu"); } } if ((x == 2) && (y == 4) && (z ==4)) { printf("Maui"); } if ((x == 1) || (y == 4) || (z ==4)) { printf(Molokai"); } else printf("Hawaii"); return 0; }
Molokai
Uppercase to lowercase Lowercase to uppercase
Note we can also convert from lowercase to uppercase (and viceversa) by noting both upper and lowercase letters (and numeric representations) are contiguous increasing. • This means the difference between the lower and upper case of any letter will always be the same: 'b' - 'B' = 'a' - 'A' = 'q' - 'Q' etc... _______________ • So if ch is any upper case letter, we can convert it to lower case by: ch - 'A' + 'a' _________________ Similarly, if ch is any lower case letter, we can convert it to upper case by: ch - 'a' + 'A' • Note we do have to first consider if ch is upper or lower case since these two operations are different
Macros are defined with a pre-processor directive
T
The source code for a program written in the C language (colloquially referred to as a "c" file since the file extension is ".c") can be written with any text editor software such as notepad.
T
C is a programming language
TRUE
getchar() and putchar() are macros defined in stdio.h
They operate like functions so we may call them functions • They aren't as robust as scanf and printf but they don't have to be formatted so they can be convenient. • Remember the "f" in scanf and printf stands for "formatted" • Need conversion specifiers (%c, %d, etc.) and quotation marks • getchar() and putchar don't need to be formatted but they only scan or print one character at a time. • Scanning or printing a sentence requires several getchar() or putchar() commands
A common design approach in which a problem is modularized by breaking it into smaller parts which are later put back together is known as a
Top-down design approach.
The pre-processor directive #ifdef checks if a macro has been defined but it doesn't matter if the macro has been assigned a value - it only matters if it has been defined with #define
True
To change the value of a macro after it is defined, it must be undefined then redefined.
True
Generally speaking, int data type requires more space (storage) than char data type.
True Feedback: char is a minimum of 8 bits whereas int is a minimum of 16 bits. You can check C data types in Wikipedia.
What is the first step in the design process?
Understand the Problem
int test = 0, score = 0; if (test ==1) { score = 5; } else if (test == 5) { score = 1; }
Value score 0
int test = 0, score = 0; if (score == 0) { score = 5; } if (score == 5) { score = 10; }
Value score 10
#include <stdio.h> int main { int x=1; if (x==2) printf("hello"); printf("world"); return 0; }
World Since there are no curly braces only the statement immediately following the if statement is tied to it. Thus, "World" will print regardless of whether or not the if() is true. Since the if() is FALSE here though, "Hello" will not print.
A macro is most like
a find and replace command
When we refer to "declaring variables" we mean:
allocate a certain amount of memory associated with a particular type of data for each variable
cast operator
allows us to temporarily change the data type of a variable while it's being used in an expression or calculation. The format is: (<data type identifier>) <variable name>
What is the name of the function below? float area( int radius);
area
If ch is a lowercase letter which of these will convert it to uppercase.
ch = ch - 'y' + 'Y'
Select the proper use of getchar to scan a character from the user and assign it to ch.
ch = getchar()
To declare a character data type, use
char
software tells the ________ what to do
computer hardware
The executables in a if statement go inside
curly braces { }
In flowcharts, if statements are generally written inside
diamonds
What data type is returned by the function below? float area( int radius);
float|floating-point|floating point|real floating-point|real floating point
Two tools we have discussed that can help devise a plan are
flowchart and pseudocode
The if statement is really a
function (like main)
float area( int radius);
function prototypes
The pre-processor command which is used to indicate an external file (such as a library or header) should be linked with the source code when building an executable is
include
What data type is passed to the function below? float area( int radius);
int|integer|signed int|signed integer|decimal integer
Machine language refers to
is a low-level language comprised of binary digits... The natural language of a particular computer.
This line of code: #define PI 3.14159
is perfectly fine
The name of the function where execution begins for all C programs is called
main
The logical test (or condition) that determines whether or not the statements within the if are executed goes inside
parentheses ( )
What function have we learned so far that allows us to write information to the standard output (the terminal)?
printf
Select the proper use of putchar to print a variable of character data type named ch to the screen.
putchar(ch)
In the blank below write one line of code which will scan a value from the user and store it as a float data type in a variable named var. Assume the variable has already been declared.
scanf("%f",&var);
Characters are specified by:
single quotes strings (which are character arrays) are specified by double quotes
The benefit(s) of using macros is/are (check all that apply)
they make the code easier to change / edit they make the code easier to read
The executables in an if statement are only executed if the condition is
true
int test = 0, score =0; if (test ==1) { score = 5; } else { score = 1; }
value score 1
while (condition) { statements; }
while loop structure
In the blank below, write the comparison / relational operator for "or"
||
what is the pre-processor command for "else"
#else
what is the pre-processor command to end a pre-processor "if" statement
#endif
what is the pre-processor command for "if defined"
#ifdef
what is the pre-processor command for "if not defined"
#ifndef
The conditional operator is a shortcut way of writing if and else statements. The format is:
<expression1> ? <expression2> : <expression3> This can be read as "if expression 1 is true then execute expression 2. Otherwise, execute expression 3." Any conditional expression (which is an expression containing a conditional operator) can be written with if and else statements. The above conditional expression could be written as: if (expression1) { expression2; } else { expression3; }
In the blank below, write the comparison / relational operator for "greater than or equal to"
>=
C is considered an assembly language
F high-level language
It's ok to start variable names with a digit ?
NO
The if statement is a function within another function
T The if statement (which is a function) must be placed within main() - which is also a function.
In order to execute a ".c" file, a program called a _______ is used to translate source code to machine language and another program called a _______ is often needed to call in supporting code from external libraries.
compiler, linker
#Include <stido.h> #define MAGIC(x,y) (x)*(y) int main (void) { int z =1; if (z >2) { Pritnf("Hello"); } else if (z < 2) { printf("%d",MAGIC(4,5)): } return 0; }
since z < 2, it goes to the else if statement of (x) * (y) = 20
math functions basic: takes in a double and returns double/float use double variable; %lf;
sqrt (x): square root i.e. int sqaure_root; sqaure_root = sqrt(25); print("Pi=%f\n",PI); Pi is 5 _______ log (x); exp (x) : expos pow (x,y) : x raised to power of y https://www.tutorialspoint.com/c_standard_library/math_h.htm
The file that must be included in order to use the formatted print function is
stdio.h
What file is getchar() and putchar() defined in?
stdio.h
#include <stdio.h> int main(void) { //#define DEBUG int x,y,z; x = 3; y = 5; #Ifdef DEBUG printf ("DEBUG: x=%d\ny = %d\n",x,y); #endif if (x == y) { printf("They are the same\n"); } if ( x != y) { printf("the product is %d,x*y); } return 0; }
the product is 15 Since x DOES NOT EQUAL (!=) to y, it goes to the next if the statement of x*y. if #define was not commented out it would be, DEBUG: x=3 y = 5 the product is 15
A certain function has the variable "test", which is of data type double, passed to it. The body of the function definition only contains the following one line of code: printf("The output is: %lf",test*test); When writing the function prototype, what data type should be returned?
void
Translate the following logical comparison to the C language:"x is not equal to y and y is not equal to z"
x != y && y!=z
int x = 10; int y = 20; int z; if (x > 5) { if (y < 10) { z = 1; } else { z = 2; } } else { if (y > 10) { z =3; } else { z = 4; } } //what is the value of z at this point
z = 2 FOLLOW THE BRACKETS else statements only follow "if" statements were false.