Download What will the output of the following code segment

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Collatz conjecture wikipedia , lookup

Line (geometry) wikipedia , lookup

Elementary mathematics wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Addition wikipedia , lookup

Transcript
CSC 2100: Introduction to Problem Solving and Computer
Programming
Fall 2008, Test 1, October 10, 2008
Time: 55 minutes 100 Points
Name ______________________
1. [10 Points] Assuming x = 10, y = 20, and z = 30, determine the value of the
following expressions.
x == 10 && y<=20
(x + y) != 40 || 60 < = (x + z – 7)
!((x + y*2) > z * 2)
!(x == 10 || y == 20 || z == 30)
(y+10) >= (z +10 –x)
1
1
1
0
1
2. [20 Points] What will be the output of the following code segments?
a. int x ;
for (x =5; x <14; x = x+3)
cout << x << endl;
cout << x << endl;
5
8
11
14
b. int y = 10;
do
cout << “ Hello World\n”;
while (y< 10);
Hello World
c. int v = 0;
while ( v < 5)
cout << v;
0 0 0 0 …… indefinitely
d. int x=5, y;
y = x++;
cout << x “ “ << y << endl ;
y = ++x ;
cout << x « « << y << endl ;
65
77
3. [15 Points] Write a program (using if/else if) that asks the user to enter the length
and width of two rectangles. The program should tell the user which rectangle has
greater area, or if the areas are the same.
#include <iostream>
using namespace std;
int main()
{
double len1, len2, width1, width2, area1, area2;
cout << "Enter length and width of rectangle 1: ";
cin >> len1 >> width1;
area1 = len1 * width1;
cout << "Enter length and width of rectangle 2: ";
cin >> len2 >> width2;
area2 = len2 * width2;
if( area1 > area2)
cout << " Rentangle 1 has greater area";
else if( area1 < area2)
cout << " Rentangle 2 has greater area";
else
cout << " Rentangles have same area";
}
4. [15 Points] Write a program that will ask the users to enter 50 integers. The
program will count the number of odd integer entered by the user and then display
the count.
#include <iostream>
using namespace std;
int main()
{
int number, odd = 0, i;
for ( i = 0; i < 20 ; i++)
{
cout << "Please enter an integer: ";
cin >> number;
if (number % 2 == 1)
odd++;
}
cout << "You have entered "<< odd << " odd integers" << endl;
}
5. [20 Points] Write a program that will ask the users to enter 20 numbers between
100 and 5000. Make sure that your program forces the users to enter a valid
number. The program should calculate and display the sum and the average of the
numbers.
#include <iostream>
using namespace std;
int main()
{
double number, sum = 0, avg;
int i;
cout << "Please enter 50 integers between 100 and 5000\n";
for ( i = 0; i < 50 ; i++)
{
cin >> number;
while (number < 100 || number > 5000)
{
cout << "Invalid number, enter a number between 100 and
5000 ";
cin >> number;
}
sum = sum + number;
}
avg = sum / 50;
cout << "The sum is " << sum << " and the average is " << avg << endl;
}
6. [20 Points] Write program that uses nested loops to display the following pattern.
******
*****
****
***
**
*
#include <iostream>
using namespace std;
int main()
{
int i, j;
for ( i = 6; i > 0 ; i--)
{
for( j = 0; j < i; j ++)
{
cout << "*";
}
cout << endl;
}
}