CS 13A
Prof. Sturm
HW #2 - variable types, loops, functions
Hand in Part II only.
Part I. Whats the output?
1.
int n,m; out
char c=p;
char d=s;
n=c;
m=d;
cout<<m-n;
2.
int m=3,n=5: out
while(m+n<14){
if (m<n)
m=n;
else
n=n+1;
m=m+1;
cout<<m<< <<n<< <<endl;
}
3.
for(j=0;j<100;j++){ out
if (j%24!=0)
continue;
cout<<j<<endl;
}
4.
for(s=0;s<3;s++){ out
for(t=0;t<3;t++)
cout<<*;
cout<<\n;
}
cout<<endl;
5.
int prod(int a,int b,int c){ out
cout<<hello<<endl;
return(a*b*c);
}
int prod(int a,int b){
cout<<hi<<endl;
return(a*b);
}
void main(){
cout<<prod(5,6)<<endl;
cout<<prod(2,3,4)<<endl;
}
6.
void printchar(char c, int n = 10); out
void main(){
printchar(*,5);
printchar(&);
}//end main
void printchar(char c, int n){
for(j=0;j<n;j++)
cout<<c;
cout<<endl;
}//end printchar
7.
/* global vs local variables */
int x = 5;
void func1(){
x=x+3;
cout<<x<<endl;
return;
}
void func2(int x){
x=x+1;
cout<<x<<endl;
}
void main(){
int x=10;
func2(5);
x=x+1;
func1();
func1();
cout<<x<<endl;
}
8.
/* static variables */
void func(){ out
int a=0;
static int b=0;
a=a+1;
b=b+1;
cout<<a<< <<b<< <<endl;
}
void main(){
for(int i=1;i<3;i++){
func();
}
}
Part II Write the three functions: sqrt(), sqrt2(), and sqrt3(). Write a main procedures that call each
function. Hand in the source code and
the output.
1. Write
a void function sqrt(double num) that finds and
displays the square root of num. Write a main procedure that calls the
function.
2. Write
a double function sqrt2(double num). Function sqrt2() finds and returns the square root of
num. Write a main procedure that calls
the function and displays the square root.
3. Write
a void function sqrt3(double num,double &sqrt).
Function sqrt3 places the square root of num in reference parameter sqrt. Write a main procedure that calls sqrt3() and
then displays the square root.