Computer Science Final

Pages: 12
1. Given an integer ‘a’ and an integer ‘b’ write an IF statement that executes when ‘a’ is
less than 5 and ‘b’ is greater than or equal to 22.
2. Write a for loop that iterates through all elements of a vector named “list” which contains
strings. Print out each element in “list” within the for loop.
3. Given a string named “word” and an integer ‘a’. Write a while loop that will stop looping
when “word” is equal to “quit” or ‘a’ does not equal 10.
4. Declare a vector of strings and add the words “hello” and “world” to it. You may due this
in any way you wish.
5. Create a struct named Student. Student should have a first name, last name, a student
id number, a list of course names, and current GPA.
6. Write a function called CalculateMortgagePayment. It should return the monthly
payment. It will take in 3 parameters: the loan amount, the interest rate, and the number
of years. (Note: you only need to write the function declaration and not the logic to
calculate a monthly payment.)

What is the correct answers to these problems?? thanks
The correct answer is to try it yourself first, post what you think and then ask.
We could give you the answers, but then you would not learn.

Giving you the answers without you making an effort is cheating.
you may due this? FAIL.
1. int main() {
int a;
int b;
if (a < 5) {
b ≥ 22;
}
else {
b = 0
return 0;
}

Is this correct? can someone edit it for me if necessary and explain
No headers, FAIL.

Using uninitialized variables, FAIL.

NOT using code tags, EPIC FAIL.

PLEASE learn to use code tags.

http://www.cplusplus.com/articles/jEywvCM9/
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
int main () {
int a;
int b;
if (a<5) {
b ≥ 22;
}
else {
b = 0
return 0;
}



Is this correct now???
Last edited on
For #1, they don't specify what statement should execute when the if statement is true. I suggest you execute cout << "Hello World\n";

Having this info might help you.

Note that you just have to write the statement, not a full program.
I don't think the exam wants a full program.
1) says write the if statement, not a program.
so it wants ONE if statement that checks TWO conditions.
and, its not right. line 7 isnt even code, it uses a nonaccessible symbol for G.E. do you know c++ for greater/equals? Even if you had it right, conditon; is a do-nothing statement:
x<3; //does nothing.

so no, it isnt right.

just write only the if statement.

if ( a... ?? b... )

^^ if you can't do these, you have not been doing your homework/reading the book/etc. These questions are very basic 'did you attend the class and do the homework' questions.

Last edited on
cout << "Hello World" << endl;
if (a < 5 b > = 22) {
}
else {
cout << "Goodbye World" << endl;
write an IF statement that executes when ‘a’ is
less than 5 and ‘b’ is greater than or equal to 22.

you are still not following directions, even after I spelled it out for you.
your expression is still wrong. Its like you have gone thru a whole class all the way to the final and never written a line of code at all. Or you are trying to be funny. Either way, from the looks of it, you need to repeat this class.

FYI I used to tutor jocks. I am immune to the 'guess and make crap up until helper gives up and does it for me' tactic.
Last edited on
if (expression) {
// Statements that execute when expression is true (first branch)
}
else {
// Statements that execute when expression is false (second branch)
}

// Statements that execute after the branches

isn't this the format for if statements??? I'm just looking for help, no need to be rude
that is one format.
you do not need an else here. it serves no purpose, and would be difficult to get correct (it is possible to do it that way).

executes when ‘a’ is
less than 5 and ‘b’ is greater than or equal to 22.

you only need one statement.

if(something and something)
c++ has an 'and' keyword that I haven't ever used: most coders use && for and (|| is or, ! is not, and those 3 are the backbone of boolean expressions)

expression can be compound, you can do anything that evaluates to a boolean, and in fact, anything that evaluates or can be implicitly cast to a zero / nonzero integer type will also work.

so one freebie since you got very close.

1) if(a < 5 && b >= 22)

you can also do it these ways, which are not good answers, but get you to the same coded result.
if(a<5)
if(b>= 22)
... anything inside second if block passed the tests. if you understand this, the 'and' is implicit because the if's are nested. if the first if is false, the second is skipped as a statement. if the first is true, the second needs to be true to get to the action.

if(a>=5) //if it fails this, a < 5 is true. You have to reverse the logic to get the else to fire.
else
if(b>=22)
..anything here also passes, but its weird. You would only write this to annoy someone.

Last edited on
Ok. I was overcomplicating it.
2.
for (int i=0; i<list.size(); i++)
{
Cout << list[i]<< endl;
}

is this correct?
almost. c++ is case sensitive so you have a bad cout.

with what you saw in #1, #3 is going to be similar. you need a compound expression again, this time uses 'or'.
Last edited on
Hello bethmayweather,

For the most part NO.

The concept is correct, but it will not work.

"i" is defined as an "int", but "list.size()" returns a "size_t" type.so you are comparing an "int" to an "unsigned int". Not enough to stop the program, but enough to generate a warning.

Now the "Cout" will generate an error and stop the compile.

I would agree with jonnin.

jonnin wrote:
you are still not following directions, even after I spelled it out for you.
your expression is still wrong. Its like you have gone thru a whole class all the way to the final and never written a line of code at all. Or you are trying to be funny. Either way, from the looks of it, you need to repeat this class.


I do not mean to be mean or rude and I do realize that this may not have been your only class, but if it is important your need to put more time in the class. It is a bit late to be learning the basics.

Andy
Hey, Beth, you have been very patient. I think many of the answers on this thread have been unnecessarily harsh to you.

There is a reason for that, however. Programmers are not typically your normal kind of social type, and they can be very rude, especially when they perceive that you have not been trying to use your brain or that you are simply begging solutions.

Like any discipline, programming requires study and effort, and in particular: exactness.

That is part of the conflicting things people have posted here, such as complaining about lack of headers or errors with size_t and the like. You really don’t have to care about that — just follow the question exactly.

For example:

1. Given an integer ‘a’ and an integer ‘b’ write an IF statement that executes when ‘a’ is less than 5 and ‘b’ is greater than or equal to 22.


I know that an IF statement looks like:

    if (some_condition)

I can check that ‘a’ is less than 5 with the ‘<’ operator: (a < 5).

Likewise, I can check that ‘b’ is greater-than or equal-to 22: (b >= 22).

I can check that both are true (make note of the use of the word “and” in your problem statement) with the ‘&&’ operator: (a < 5) && (b >= 22).

Put this condition into your IF statement and you are done:

    if ((a < 5) && (b >= 22))

Final answer.


The purpose of these kinds of homework are to help you study basic materials so that you can pass your exams, proving that you understand the material.

If you have not been paying attention in class, or have been overwhelmed, take a night or two and just work your way through the book, studying examples and using your very excellent brain to figure out what the examples mean and how to express the ideas in the C++ language.

As an anecdote, when I took my first semester of calculus, I wasn't sure I quite understood everything. I was doing the homework, playing with limits (not really understanding them, but hey, trying to keep up), and I remember one day in the middle of the week sitting in class when the conversation suddenly went over my head. I wondered if I had fallen asleep for five minutes or something. I had to take the course over.

Turns out that calculus is actually really easy (really!), but it requires some effort to bend your brain around the concepts. (Which I think are often taught in a mind-bogglingly unintuitive way, BTW.)

The same thing happens in CS. It takes a bit to get your brain to wrap around the concepts and thinking in a much more strict, logical way. But you can do it. Your brain is more powerful than any computer, anywhere. (Just not as fast, which is why we like computers. --And we can’t do telepathy, so computers help there, too... with wireless communication, and stuff...)


All the things you have been asked to do are very basic concepts. Look through your notes. Peruse the first few chapters of your textbook — variables and expressions and loops — and if your final exam is in a few weeks you’ve got time to put in a few hours a week to get it.

Good luck!

[edit] Fixed typo
Last edited on
1
2
3
4
5
6
7
8
9
int main() {
vector<string> list
list.push_back(“Rock”)
list.push_back(“Paper”)
list.push_back(“Scissors”)
for(int i = 0; i<list.size(); i++) {
      cout << list_vec[i] << endl;
   }
}


is this correct for #2
also#include vector & iostream
and using namespace std;
Pages: 12