Chapter 1 - An Introduction to Computers and Visual Basic

advertisement
CS-102 Final Exam
Name _________________
Hardin D. CS 102
Part I. (15 points) General Information: Short answer or Multiple Choice
Know the format specifiers for printf and scanf:
Type
int
unsigned int
float
double
char
char [ ]
printf
%Wd
%Wu
%W.Pf
%W.Plf
%Wc
%Ws
scanf
%d
%u
%f
%lf
%c
%s
W is the field width – Total number of print positions e.g. %5d or %12f
P is the precision – Number of digits right of decimal e.g. %12.4lf or %0.4f
REMEMBER: In printf %f and %lf will have a precision of 6 by default.
e.g. printf(“%f”, 123.67); will print 123.670000
REMEMBER: The number of non- precision digits will always be printed even if the field
width (W) is set too small. e.g. printf (“%2d”, 5678); will still print 5678 even though the
field width is set to 2.
Given printf and scanf statements know how different types of data are written to the screen or
read in.
Know the following functions and their libraries:
<stdlib.h>
<math.h>
<stdio.h>
rand
sqrt, pow, floor, ceiling
printf, fprintf, scanf, fscanf, getchar, putchar
Know how to declare array variables, initialize their values and how to access a specific element:
Array syntax ex
int numbers[5] = {1, 2, 3, 4, 5};
indices go from 0 to 4
int numbers[5] ={0}; sets all elements to 0
printf(“%d”, numbers[2]); Prints out the number 3
Know how to evaluate a computation given the precedence rules:
1. ( )
2. multiplication *, and division /
3. addition +, and subtraction x = 8/2 + 4 * (2 + 3)
-
2 * 2;
Know how to use the pow function for exponentiation:
answer
x = 4
x = pow(4,3) sets x to 4 cubed
Part II. (10 points) Assignment Operators, Increment & Decrement Operators
Fill in the blank
Assignment
Operators
x += 1;
x -= 1;
x *= 1;
x /= 1;
x%= 4;
same as x = x + 1;
same as x = x – 1;
same as x = x * 1;
same as x = x/1;
same as x = x %4; (Modulus operator %)
Increment and Decrement Operators
a++
++a
a---a
Post Increment. Use variable a in the calculation then add 1
Pre Increment. Add 1 to a then perfom the calculation
Post Decrement. Use variable a in the calculation then subtract 1
Pre Decrement. Subtract 1 from a then use in calculation
Given some integer variables and a series of operations using the Assignment Operators,
Increment & Decrement Operators give the new values for those variables
Example int x = 1, y = 2, z = 3;
x += 2 ;
y ++ ;
-- z ;
x = _____
y = _____
z = _____
y = _____
z = _____
x = (y + z++) ;
x = _____
Part III (10 Points) Find the syntax or logical errors
Find and correct the syntax errors in the following if statements and loops
Know the syntax of the if statement, for loop, while loop, do – while loop and switch statements.
Part IV (15 Points) Show Output
Show the output produced by some for loops, while loops and do – while loops
Study the Loops homework assignment (number 4).
Know how to write a switch statement that will replace a series of if statements.
Example:
if (day = = 1 ){
printf(“Sunday\n”);
}
else if (day = = 2 ) {
printf(“Monday\n”);
}
else if (day = = 3 ) {
printf(“Tuesday\n”);
printf(“Tuesday\n”);
}
switch(day){
case 1: printf(“Sunday\n”);
break;
case 2: printf(“Monday\n”);
break;
case 3:
break;
}
Part V (5 Points) Local & Global Variables
Given a main program with functions identify the global variables and the local variables.
Be able to give the output of simple calculations involving local and global variables.
Example:
#include <stdio.h>
Sample Questions
Answer
int addTwo(int a, int b);
What are the global variables?
What are the local variables in main?
What are the local variables in addTwo?
What global variable is used in addTwo?
What value is returned by addTwo?
x and y
m and p
s, t, sum
x
11 ( 2 + 4 + 5)
int x = 5;
int y = 6;
int main(void)
{
int m= 2, p = 4;
printf(‘%5d\n”, addTwo(m, p))
system(“pause”);
}
// Functions
int addTwo(int s, int t)
{
int sum = 0;
sum = s + t + x;
return sum;
}
Programs: (50 Points) Complete the following programs on your workstation. Turn in only the
C Code for each program.
There will be three programs, one will require all three C loops to perform some calculation, one
will involve a function and one will require an array.
Be able to generate a table using all three looping constructs. Study homework assignment 4,
plus the body mass index (BMI) program.
Be able to write a program that involves one or more functions. Study homework assignment 5
– functions, and homework assignment 3 – simple calculations plus the body mass index
(BMI) program. Study midterm.
Arrays
Be able to write a program that will initialize an array with random numbers and then
print out the array. Study the in class program “Array Operations” and final homework
assignment.
Declare a variable as an array:
int values[10] = {0};
Initialize an array with random numbers between 0 and 99:
for (i = 0, i < 10; i++) {
values [i] = rand ( ) % 100;
}
Know that the indices of an array start at 0
Print our the contents of an array
for (i = 0, i < 10; i++) {
printf(“%d\n”, values[i]);
}
Functions
Be able to write a function, specify the function prototype, and call the function
Example: Write a function that takes two real numbers as input, adds them and returns the sum
as a real number.
The prototype for this function:
float add_two( float,
float);
Main program with call to the function:
#include <stdio.h>
int main(void) {
float answer = 0;
// Call the function
answer = add_two (10.5,
8.3);
printf(“The sum is %6.2f\n”, answer);
system(“pause”);
}
The Function:
float add_two ( float num1, float num2) {
float sum = 0.0;
sum = num1 + num2;
return (sum);
}
Download