CIS 1.5 2nd ExamName:
10 points total:
1.
Write prototypes for the following. Do not write the functions.
A)A function called update gets an integer and returns a double.
B)A function called name gets no parameter, but returns a string.
C)A function called sort gets an array of integers and returns nothing.
D)A function called change that gets a reference parameter to an integer, and returns nothing.
E)A function call average that receives an array of integer values and returns the integer average.
12 Points
2.
What would be the output by the following section of C++ code?
int main() {
int c[4];
int t;
c[0]=0;
c[1]=10;
c[2]=20;
c[3]=30;
for(int i=0;i<=3;i++)
cout < c[i] < endl;
cout < endl;
for(int i=0;i<=3;i++) {
if(c[i]<10 || c[i]==20)
c[i]+=5;
else if (c[i]>5 & c[i]<15)
c[i]=100;
else c[i]=300;
}
for(int i=0;i<=3;i++)
cout < c[i] < endl;
}
12 Points
- Fix the following pieces of code:
A. I want to have the program display all the elements in the array. However something went wrong. What does my code do, and what should I do to fix it.
int main() {
int a[]={1,2,3,4,5,6};
for(int i=1;i<6;i++)
cout < a[i] < endl;
}
B. I'm trying to find the SMALLEST value in an array. But again, I cannot figure out what went wrong. Tell me what my code does, and what should I do to fix it.
int main() {
int a[]={10,20,30,1,25,5};
int min;
min=a[0];
for(int i=0;i<6;i++) {
if(a[i]==min)
min=a[i];
}
cout < min < endl;
}
12 points
4.
What would be the output by the following section of C++ code?
int main() {
string word="boat";
string sentence="flake";
string temp;
int position;
cout < word[2];
cout < word[3] < endl;
position=sentence.find("ake",0);
cout < position < endl;
cout < sentence.length() < endl;
word.replace(0,1,"g");
cout < word < endl;
temp=word.substr(1,3);
sentence.replace(2,3,temp);
cout < sentence < endl;
sentence.erase(0,2);
cout < sentence < endl;
sentence.insert(3,"meal");
cout < sentence < endl;
}
12 points
5.
What would be the output by the following section of C++ code?
void popit(int, int &, int &);
int main() {
int i=10, j=15, k=25;
cout < i < " " < j < " " < k < endl;
popit(i,j,k);
cout < i < " " < j < " " < k < endl;
popit(i,j,k);
cout < i < " " < j < " " < k <endl;
return 0;
}
void popit(int a, int &b, int &c) {
if(c==a+b)
b+=a;
else c=a;
}
12 points
6.
What would be the output by the following section of C++ code?
void bah(int [], int [], int);
int boo(int);
int boo(int item){
return item*10;
}
void bah(int array[], int value, int size) {
for(int i=0;i<size;i++) {
array[i]=i+value;
int k=boo(array[i]);
cout < k < endl;
}
}
int main() {
int a[3];
bah(a,10,3);
return 0;
}
12 points
7.
What would be the output by the following section of C++ code?
int main() {
string text="B4N23O52";
string foo="12345";
int k=0;
for(int i=0;i<text.length();i++){
if(isdigit(text[i])) {
foo[k]=text[i];
k++;
}
}
cout < foo < endl;
return 0;
}
18 points
8.
What would be the output by the following section of C++ code?
Write a program that has 2 functions:
A function called fill will get an array to hold integer values, and the size of the array. The function will ask the user to enter in integer values. Assume that the user will not enter in more than the size provided.
A function called limitarray that gets the array of integers, and the size of the array. It will go through all the elements in the array and sets any values over 100 to become 100. And any values under 0 to become 0.
For example: 10, 105, -5, 5
becomes 10, 100, 0, 5
Create an array to hold 100 integers. Write a main program which calls the above functions. There are no need for prompts, and no need to display the results.
Page 1 of 8