d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Paying Fg For Hw Help > This
Add Reply New Topic New Poll
Member
Posts: 14,537
Joined: Feb 13 2007
Gold: 3,241.00
Oct 26 2013 06:05pm
CS 111 Lab 9
1. Consider the following C++ program. What is the output from the program in
response to the following user inputs?
#include <iostream>
using namespace std;
int main () {
int n;
cout << "Please give me an integer: ";
cin >> n;
if (n < 10) {
cout << "Integer is too small." << endl;
if (n < 0) return 0;
}
if (n % 2 == 0) cout << 3 * n / 2 << endl;
else if (n % 4 == 1) cout << 3 * ((n - 1) / 4) + 1;
else cout << 3 * ((n + 1) / 4) - 1;
cout << endl;
return 0;
}
(a) The user enters: -9
(b) The user enters: 9
(c) The user enters: 10
(d) The user enters: 11
(e) The user enters: 21
2. The following C++ program is supposed to ask a user to enter three different
integers. It then prints the middle value of the three input numbers. (For example,
if the user types 10 5 15, the program should print 10.) The program has several
errors. Rewrite the program to fix the errors and arrange the program so that it is
easier for a human to read.
# <iostream>;
using namespace std;
int main
{
int x, y, z;
cout << "Enter three different integers: " endl;
cin >> "x" >> "y" >> "z" endl;
if ((x > y > z) && (z > y > x)); cout << y;
if ((y > x > z) && (z > x > y)); cout << x;
if ((z > y > x) && (x > y > z)); cout << y; return; };

3. Write a function multiPrint(int n, char c) that prints a n copies of a character c.
Apply this function to print an upright triangle that may look like the image
below:
c
c c
c c c
c c c c

4. Write a function add3 which calculates the sum of three integer numbers.
Member
Posts: 6,192
Joined: Dec 13 2010
Gold: 6,669.99
Oct 26 2013 07:50pm
You should at least try them before, then if you need help with a specific question, ask it here.
You'll end up not understanding anything and it will screw you over big time during your whole semester.



Anyways, if you don't want to follow my advice, here are the answers:





1. a. return 0 (end the program, no output)
b. Integer is too small
c. 15
d. 10
e. 16

2.

#include "stdafx.h"
#include <iostream> // old - # <iostream>;
using namespace std;
int main() // old - int main
{
int x, y, z;
cout << "Enter three different integers: " << endl; // forgot << before endl;
cin >> x ; // old - cin >> "x" >> "y" >> "z" endl;
cin >> y ;
cin >> z ;
cout << endl;
if (x < y && z > y || z < y && x > y) cout << y; // removed ";" after if() for all 3 and fixed inside the if
if (z < x && y > x || y < x && z > x) cout << x;
if (y < z && x > z || x < z && y > z) cout << z; // old - cout << y;
return 0; // old - return ;
} // no ; at the end of a function


3.

Code
void multiPrint(int n, char c) {

for(int i = 0; i < n; i++){
for(int j = 0; j <= i; j++){
cout << c << " ";
}
cout << endl;
}
}


4.

Code
int add3r(int a,int b,int c) {
int sum;
sum = a + b + c;
return sum;
}


Or at least try to understand the answers. (hopefully I didn't make any stupid mistakes)
Member
Posts: 6,192
Joined: Dec 13 2010
Gold: 6,669.99
Oct 26 2013 10:07pm
oops, I screwed up a and b for the first question lol
I won't give the answers tho, try them by yourself.
Member
Posts: 1,732
Joined: Sep 3 2007
Gold: 5,991.00
Oct 29 2013 12:36am
for question 1:
a)
Integer is too small.
b )
Integer is too small.
7
c)
15
d)
8
e)
16

For question 2:
comments // indicate what was wrong with the code, the code itself is corrected and tested

Code
#include <iostream> //didn't have include,; at end of statement
using namespace std;
int main(){ // no parenthesis
int x, y, z;
cout << "Enter three different integers: " << endl; // no << between the string and endl
cin >> x >> y >> z; //variable names put into quotes which makes them strings, not variables
if((y>x&&x>z)||(z>x&&x>y)) cout << x; //invalid booleans, you can only compare 2 variables using signs at given time, you have to combine multiple using &&
if((z>y&&y>x)||(x>y&&y>z)) cout << y;
if((y>z&&z>x)||(x>z&&z>y)) cout << z;
cout << endl; //lack of print a new line, so output will be screwed up by the command line printing next to it
return 0; // main is an integer function so we have to return an integer, in C and Java where void main() is allowed we can just return
} //; at end of the whole thing


for the 3rd one:

Code
void multiPrint(int n, char c){
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++) cout << c;
cout << endl;
}
}


for 4th:

Code
int add3(int a, int b, int c){
return a+b+c;
}


This post was edited by NeX_1337 on Oct 29 2013 12:49am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll