1 |
h09 |
CS16 S17 |
Name: | ||||
---|---|---|---|---|
(as it would appear on official course roster) | ||||
Umail address: | @umail.ucsb.edu | |||
Optional: name you wish to be called if different from name above. | ||||
Optional: name of "homework buddy" (leaving this blank signifies "I worked alone") |
h09: Homework 9: Functions, Command-Line Args, Numerical Conversions
ready? | assigned | due | points |
---|---|---|---|
true | Thu 05/04 12:30PM | Tue 05/09 12:30PM |
You may collaborate on this homework with AT MOST one person, an optional "homework buddy".
MAY ONLY BE TURNED IN IN THE LECTURE LISTED ABOVE AS THE DUE DATE. There is NO MAKEUP for missed assignments.
In place of that, we drop the two lowest scores (if you have zeros, those are the two lowest scores.)
PLEASE MARK YOUR HOMEWORK CLEARLY, REGARDLESS OF IF YOU WRITE IT OUT IN INK OR PENCIL!
-
1.(2 pt) Can you define a function in the body of another function in C++?
2.(2 pt) Can you call a function in the body of another function in C++?
3.(8 pts) What is the output of the program below (write it in the space to the right)?
#include <iostream>
using namespace std;
void times(int y) {
y = y * 9;
cout << "y=" << y << endl;
}
int main() {
int b = 7;
times(b);
cout << "b=" << b << endl;
return 0;
}
int main(int argc, char *argv[])
~jimbo/cs16/myprog 12 dozen eggs
Answer the following questions carefully, using the proper quotation marks (where applicable).
What is the value of argc?
What is the value of argv[2][2]?
What is the value of argv[1] and what data type is it?
What is the value of argv[0]?
What is the value of argv[1]/4?
5.(8 pts) Write a void-function definition for a function called "zero_both" with 2 parameters, both which are variables of type int, and sets the value of both variables to 0. Describe if you picked this function to be a call-by-reference or a call-by-value AND WHY?
6.(2 pts) Convert the hexadecimal (base 16) number, 6A into decimal. SHOW YOUR WORK.
7.(4 pts) Convert the binary (base 2) number, 10010101 into both hexadecimal and decimal. SHOW YOUR WORK.
8.(4 pts) Show where you would insert an assert statement in this code in order to make sure that you would not get a run error:
#include <iostream>
#include <cassert>
using namespace std;
void foo(int x, int y) {
y = 5 / x;
cout << "Result = " << y << endl;
}
int main() {
int a, b;
cin >> a >> b;
foo(a, b);
return 0;
}