CS31 Worksheet 4
CS31 Worksheet 4
This worksheet is entirely optional, and meant for extra practice. Some problems will be more
challenging than others and are designed to have you apply your knowledge beyond the examples
presented in lecture, discussion or projects. All exams will be done on paper, so it is in your best interest
to practice these problems by hand and not rely on a compiler.
Concepts
Functions, Parameter Passing Arrays
a. #include <iostream.h>
void dolocal();
void doref(int&);
void doval(int);
int main()
{
x = 15;
doref(x);
cout << "x = " << x << " after the call to doref\n";
x = 16;
doval(x);
cout << "x = " << x << " after the call to doval\n";
x = 17;
dolocal();
cout << "x = " << x << " after the call to dolocal\n";
x = 18;
return 0;
}
void doref(int& a)
{
a = 3;
}
void doval(int b)
{
b = 4;
}
void dolocal()
{
int x;
x = 5;
}
b. #include <iostream.h>
void one(int num);
void two( );
void three(int & num);
void four(int & num);
int main()
{
int num = 1;
cout << "At start of main num = " << num << endl;
one( num );
cout << "After call to one num = " << num << endl;
two( );
cout << "After call to two num = " << num << endl;
three( num );
cout << "After call to three num = " << num << endl;
four( num );
cout << "After call to four num = " << num << endl;
two( );
cout << "After call to two num = " << num << endl;
one( one );
cout << "After call to one num = " << num << endl;
four(.num );
cout << "After call to four num = " << num << endl;
one( num );
cout << "After call to one num " << num << endl;
}
void one( num )
{
cout << " At the start of one num = " << num << endl;
num = 5;
cout << " At the end of one num = " << num << endl;
}
void two( )
{
int num = 100;
cout << " At the start of two num = " << num << endl;
num = 200;
cout << " At the end of two num = " << num << endl;
}
void three()
{
cout << " At the start of three num = " << num << endl;
num = 25;
cout << " At the end of three num = " << num << endl;
}
void four(int& num)
{
cout << " At the start of four num = " << num << endl;
num = 3;
cout << " At the end of four num = " << num << endl;
}
c. #include <iostream.h>
void triple(int);
int main(void)
{
int x;
for (x = 1; x <= 5; x++)
triple(x);
}
2. Declare a function named scan that reviews an array of int and returns the largest and smallest
number found in the array. HINT #1: You’ll need to pass an array argument and a companion size
parameter. HINT #2: Since you are returning more than one value from your function, you’ll need to use
reference parameters. Implement this function and then write statements to call this function with an
array of size 5.
#include <iostream.h>
int main()
{
int a[100], b[100], j, m;
int suma = 0, sumb = 0, sumdiff = 0;
cin >> m;
for (j = 0 ; j < m ; j++)
{
cin >> a[j] >> b[j];
suma = suma + a[j];
sumb += b[j];
sumdiff = sumdiff + (a[j] - b[j]);
}
for (j = m - 1 ; j >= 0 ; j--)
cout << a[j] << " " << b[j] << " " << a[j] - b[j] << endl;
cout << suma << " " << sumb << " " << sumdiff << endl;
}
DATA:
5
11 15
19 14
4 2
17 6
1 3
4. Given: int h = 6, p = 2, m = 3;
int values[7];
Suppose values contains: -4 0 2 6 -2 -1 14
Show the contents of the array values after:
for (; m <=5; m++)
values[m] = values[h] + values[p] * values[m];
int main()
{
int i, count[10];
cout << "please enter 10 numbers: ";
for (i = 1; i <= 10; i++)
cin >> count[i];
}
int main()
{
int nums[10];
int i;
for (i = 9 ; i >= 0 ; i--)
{
nums[i] = 5 * (i + 1);
cout << nums[i] << " ";
}
cout << endl;
for (i = 0 ; i < 9 ; i++)
cout << nums[i] << " ";
cout << endl;
for (i = 8 ; i >= 0 ; i--)
nums[i+1] = nums[i];
for (i = 0 ; i < 9 ; i++)
cout << nums[i] << " ";
cout << endl;
}