Examination #2 solution

advertisement
CIS 180
Fall ’08
EXAMINATION #2
70 points
NAME:
1. [3 points] Write a for statement to display the odd numbers from 13 through 25.
Answer
for(int j=13; j <= 25; j+=2)
cout<<j<<endl;
2. [6 points] Write a C++ code to display the following output. (You are allowed to use
cout twice in your solution)
1
12
123
1234
12345
123456
1234567
12345678
123456789
12345678910
1234567891011
123456789101112
12345678910111213
1234567891011121314
123456789101112131415
12345678910111213141516
1234567891011121314151617
123456789101112131415161718
12345678910111213141516171819
1234567891011121314151617181920
Answer:
#include<iostream.h>
void main()
{ for (int i =1; i <= 20; i ++)
{
for (int j = 1; j <= i; j++)
cout <<j;
cout<<endl;
}
}
1
3. [8 points] Write a complete program that inputs 100 integer numbers from the
keyboard and accumulates only the even input numbers. The program will output the
accumulated result either the input number is 5 or 100 integer numbers has been entered.
Answer:
#include<iostream.h>
void main( )
{ int num, sum = 0;
for(int i=0; i < 100; i++)
{
cin >> num;
if
(num == 5)
break;
else if (num %2 == 0)
sum += num;
}
cout<<sum<<endl;
}
4. [5 points] What is the output of the program below?
#include<iostream.h>
int test(int = 2, int =4,int =6);
int main()
{cout<<test()<<endl;
cout<<test(10)<<endl;
cout<<test(10,20)<<endl;
cout<<test(10,20,30)<<endl;
return 0;
}
int test(int x,int y, int z)
{return x+ y + z;
}
Answer
12
20
36
60
5. [5 points] What is the output of the program below?
2
#include<iostream.h>
void test();
void test(int);
void test(float, int);
void test(int, float);
int main( )
{test(5);
test();
test(10,4.5);
test(10.0,4);
return 0;
}
void test()
{cout<<"Hi"<<endl;}
void test(int x)
{cout<<"Hi"<<x<<endl;}
void test(int x, float y)
{int sum;
sum = x + y;
cout<<sum<<endl;
}
void test(float x, int y)
{int dif;
dif = x - y;
cout<<dif<<endl;
}
Answer
Hi5
Hi
14
6
3
6. [9 points] Write a complete program by defining the following functions:
a. whole - returns the integer part of any real number passing to the function.
b. fracpart - returns the fractional part of any real number
passing to the function. For example, when a number 256.879 is passed to the
fracpart( ), the number .879 should be returned. Require to call the function whole
in the function fracpart.
c. main - tests the function fractpart.
Answer:
#include<iostream.h>
int whole(double);
double fracpart (double);
void main() //2 points
{ cout<<fracpart(256.879)<<endl;
}
int whole(double x) //3 points
{ int a;
a=x;
return a;
}
double fracpart(double y) //3 points
{return ( y - whole(y));
}
7. [7 points] What is the output of the program below?
#include<iostream.h>
void add10(int);
void add5(int [],int);
int main()
{int a[]={10,20,30,40};
int x = 15;
add10(a[2]);
cout<<a[2]<<endl;
add10(x);
cout<<x<<endl;
add5(a,4);
cout<<a[1]<<endl;
return 0;
}
void add10(int x)
{x = x + 10;
cout<<x<<endl;
4
}
void add5(int b[ ],int size)
{ for(int i =0;i<size; i++)
b[i] = b[i] + 5;
}
Answer
40
30
25
15
25
8. [ 7 points] Write a program that prompts the user to make a choice for pizza size – S,
M, L or X – and then displays the price as $ 7.00, $9.00, $12.0 and $15.00, respectively.
You are not allowed to use any the following decision structures: switch, if/else and if/
else if. You can use if statement once in your solution.
Hint: Use one-dimensional array.
Output sample
What size pizza do you want? M
Your pizza is $8.99
Answer
#include<iostream>
using namespace std;
int main()
{ char pizzaSizes[] = {'S','M','L','X'};
double price[]= {6.99, 8.99, 12.50, 15.00};
int x;
char requestedSize;
cout<<"What size pizza do you want? ";
cin.get(requestedSize);
for(x = 0; x < 4; ++x)
{
if(requestedSize == pizzaSizes[x])
cout<<"Your pizza is $"<<price[x];
}
}
5
9. [10 points] Write a complete C++ program that gets 30 test scores from the user and
displays the average score. You are required to define the following functions:
inputScores – This function accepts an integer one-dimensional array and its size as
argument. The function gets 30 test scores from the user and store them in the onedimensional array.
averageScores – This functions accepts an integer one-dimensional array and its size as
argument. The function returns an average value of all the array elements.
Answer
#include<iostream>
using namespace std;
void inputScores(int [],int);
int averageScores(int[],int);
int main()
{int scores[30];
inputScores(scores,30);
cout<<averageScores(scores,30);
return 0;
}
void inputScores(int a[],int size)
{ cout<<"Input 30 scores:\n";
for(int i=0; i < size;i++)
cin>>a[i];
}
int averageScores(int a[],int size)
{ int sum =0;
for(int i =0; i<size;i++)
sum += a[i];
return sum/size;
}
6
10. [10 points] Write a code that that calculates the sums of the rows and columns in an
integer two-dimensional matrix named A with three rows and three columns. Store the
sums of the rows and columns separately into one-dimensional arrays called row and col
respectively. Input the two-dimensional array values from the user.
Hints:
If the input two-dimensional array A is defined as
10
20
30
40
50
60
70
80
90
The arrays rows and columns will be
row[0] = 60
row[1] = 150
row[2] = 240
col[0] = 120
col[1]= 150
col[2]= 180
Answer:
int a[3][3],row[3],col[3],i,j;
cout<<"Array input:"<<endl;
for( i = 0; i<3;i++)
for( j= 0; j<3;j++)
cin>>a[i][j];
int r,c;
for (r =0; r < 3; r++)
{ row[r]=0;
for(c = 0;c<3;c++)
row[r] += a[r][c];
}
for (c =0; c < 3; c++)
{ col[c]=0;
for(r = 0;r<3;r++)
col[c] += a[r][c];
7
Download