C Programming - Midterm 1
Syntax to start a C program
#include <stdio.h> int main(void) { program ; return 0; }
What is the boolean convention for 0 and 1 using <stdbool.h>?
0 = False 1 = True
Which of the following are constant expressions: 1) 50/2 + 8%6 2) (d+24) / 3 3) '?' - 5
1 and 3
What do these represents: 1) %c 2) %i 3) %f 4) %x 5) \n
1) display character 2) display integer 3) display float 4) display value in hex 5) insert carriage return and newline
Which of the following numbers represent numbers in C and what type are they: 1) 056 2) 325,000 3) 0x76
1) octal and 3) hex
Convert 34 in decimal to binary
100010
Take 2's complement of 01011101
10100011
Convert C6 in hexadecimal to its equivalent in binary
11000110
Which of the following pairs are equivalent when compiled: 1) B=45; and b=45; 2) a=5+b; and a = 5 + b; 3) //comment and /*comment*/ 4) e=f%g; and e=f%g
2 and 3
Which are valid variable names: 1) !myApple 2) _HeartRate 3) double 4) int45
2 and 4
Convert 23 in decimal to hexadecimal
35
Convert 110001 in binary to decimal
49
Convert 0101 0111 in binary to hexadecimal
57
Convert 3B in hexadecimal to decimal
59
Computer program
A sequence of instructions in a given language that solves a problem (or accomplishes a task)
Algorithm
A sequence of steps needed to solve a problem
What data type is: char
A single character
What are constants?
Any number, single character, or character string
Variables that are: 1) created each time the function is called declaring the variable 2) initialized each time the function is called 3) has no default initial value 4) disappears when function ends
Automatic variables
Instruction set
Basic operations of a computer system
High-level languages
English-like commands
What is the % arithmetic expression stand for?
Getting the remainder of the 2 numbers around it being divided
Variables that are: 1) accessed by any function in the program 2) defined outside of all functions 3) value can be changed by any function 4) default initial value is 0
Global variables
What data type is: int
Integer
Variables that are: 1) defined within a function 2) only available within a function 3) do not have a default initial value
Local variables
What is the range associated with a particular data type dependent on, the computer or language?
Machine-dependent
Function declaration
Must include a semicolon at the end of the header Must include argument types int is assumed if not type of return value isn't included.
Is 01010101 positive or negative if you're in 2's complement notation?
Positive because it begins with 0, the significant number
Write a print statement to display a float f consisting of at least 4 digits.
Printf("%4f\n",f);
What data type is: double
Same as float but with twice the precision
Variables that are: 1) not created when the function is called 2) doesn't disappear when the function ends 3) values that when the function ends, are the same values next time the function is called. 4) initialized only once and only as a constant or constant expression 5) have a default initial value of 0
Static variables
What data type is: _Bool
The Boolean values 0 or 1
What is the difference between the following two declarations: float mynums[500] = {3.6,4.8,5.2}; float mynums[]= {3.6, 4.8, 5.2};
The first makes an array that is 500 by 500 but give values to only the first 3. The second array is only made up of the 3 values given with no empty spaces.
What is a type cast operator used for?
To get the number type that you'd like after using integer arithmetic, which can only result between
What can 'short' be used for?
To indicate an integer is shorter than normal
What can 'long' be used for?
To indicate than an integer is longer than normal
What can 'unsigned' be used for?
To indicate that the variable will only contain positive numbers
Compiler
Translates from high level language to machine language
What data type is: float
Values containing decimal places
Machine language
What computers actually understands
When do you use a while loop?
When you want to a program that immediately checks for the expression and works through repeatedly after.
When do you use a do loop?
When you want to a program to go through once before running through the program. Usually ends with a while loop expression.
How do you add the escape character for a double quotation mark?
\"
How do you add the escape character for a question mark?
\?
How do you add the escape character for a horizontal tab?
\t
Write a command to declare the Boolean variable istrue.
_Bool is true
Write the function isslower that returns 1 if the character passed to the routine is a lower case letter.
_Bool islowera(char c) { if (c>='a' && c<='z') return 1; else return 0; }
Write four ways to decrement b by 1
b-- ,--b, b=b-1, b-=1
Set the variable c of type character to the character a
c = 'a'
Declare and initialize a character array named letters to the characters abcdefg. End the string with the null character
char letters [] = {'a','b', 'c', 'd', 'e', 'f', 'g', '\0'};
Declare a character array and initialize it to the null string
char myarray[30] = ""
Define a constant maxvals and set it equal to 256
const int maxvals = 256
Write a for loop that initialized x to 8, loops while x is less than 30, and increments x by 1 and y by 2 each time the loop is executed.
for (x=8; x<30; x++, y+=2);
Write a command to pass matrix m to function handlematrix
handlematrix(m)
Declare an array of 100 integers, name array intlist, set them all to 0, except the one with index 50, which should be set to 5.
int intuits[100] = {[50]=5};
Write a function equivalent to the sum function in Matlab
int sum (int vector[],int len) { int total,cnt; total=0; for (cnt=0;cnt<len;cnt++) total+=vector[cnt]; return total; }
Declare and initialize a matrix with 2 rows and 3 columns. Set the values to 6.
matrix [2][3] = {{6,6,6},{6,6,6}};
Set the 3rd value in the 5th mevent structure to 9
mevent[4].value[3] = 9;
Write a command to pass the structure patient to a function called getmap. The function will accept the structure as the only argument. It returns an integer and stores it in a field in patient called map.
patient.map = getup(patient);
Write a statement that accepts an input as time in hour:minute:second format
scanf("%i:%i:%i", &hour, &minute, &second);
Write a command to read in the time in a person's first and last name
scant("%s %s", fname, lname);
How is a character constant identified by?
single quotation marks ie. 'a'
Declare a variable my patient of type structure bloodpressure
struct bloodpressure mypatient
Define structure blood pressure that has two integer values: diastolic and systolic
struct bloodpressure { int diastolic; int systolic; } ;
Write a single command to set a variable timestamp (of type time which has three fields: hour, minute, second) to the time 10:23:34
timestamp = (struct time) {10, 23, 34};
How would set element a in vector1 to 9
vector1[a] = 9;
Write a function header for a function named find that accepts values for x, a, b, and c. It does not return any arguments.
void findy (int x, int a, int b, int c)
Write a command to send the date of the 8th mevent to a function called lookatdate. Lookatdate accepts only 1 argument and does not return anything.
void lookatdate(mevent[8].date)
Write a line of code to set z to the remainder when a is divided by b
z = a%b;