Need help with understanding concepts for upcoming quiz.

I don;t want to come off as lazy, but since my professor doesn't really answer any of my questions without yelling I have come here in need of help.

She gave us an assignment that was due the pastg week, and this assignment is supposed to be out study guide for an upcoming quiz If you guys could show me how you guys were answering the questions would really be helpful, and clarify it a lot to my own answers.


Problems

********************************************************
int i = 0;
while (i < 7)
{
cout << 2 * i;
++i;
}
**************************************************************
Figure 1

1. When the code in Figure 1 runs, what does it output to the console? (5 pts)








********************************************************
for (int i = 0; i < 7; ++i)
{
cout << 2 * i;
}
********************************************************
Figure 2

2. Rewrite the code in Figure 2 so that it uses a while loop rather than a for
loop to accomplish the same output. (5 points)




********************************************************
int k = 100;
for (int i = 0; i < k; ++i)
{
// This is the body of the for loop.
cout << i;
}
********************************************************
Figure 3

3. How many times does the body of the for loop run in the code in Figure 3? (5
pts)



********************************************************
int i = 11;
while (i <= 99)
{
i = i + 3;
} cout << i;
********************************************************
Figure 4

4. When the code in Figure 4 runs, what does it output to the console? (5
points)




********************************************************
cout << 1 / 2 << ", " << 1 / 2.0;
********************************************************
Figure 5
5. When the code in Figure 5 runs, what does it output to the console? (5 points)




********************************************************
int i = 2;
int k = i++ * 2;
cout << ++i * 2 + k;
********************************************************
Figure 6

6. When the code in Figure 6 runs, what does it output to the console? (5 points)


7. Write a C++ program that computes the sum of integers 0 through n, where n is an integer
entered by the user. (5 points)

8. Write a C++ program that prints every number between 330 and 550, inclusive. (5 points)

9. Write a program that prompts the user to enter a number between 3 and 12, inclusive. If the
user enters a number inside [3, 12], the problem displays good number, otherwise the program
displays bad number. (5 points)


********************************************************************************
int i;
cin >> i;
if (i % 2 == 1) {
cout << "odd number"
}
*********************************************************************************
Figure 10

10. The code in Figure 10 prints odd number when the user enters an odd number and does not
print anything when the user enters an even number. Rewrite the code so that it prints even
number when the user enters an even number and does not print anything when the user
enters an odd number. (5 points)



If you could explain how you guys were able to answer the questions would be helpful.


The first four I have down, but the latter questions especially #9 and #10 would help me out a lot.


Thanks.
What part of 9 are you having a hard time with?

It's a fairly basic concept.

Prompt the user to enter a number [3-12] if it is within the range output "good number" else output "bad number"

Looking at the other questions you should have a good grasp on input, output , and conditional statements.

Question 10 is about the same concept. though this one basically wants to change from odd to even. So we can invert the comparison equal to not equal which would mean if its not odd instead of is odd. Then simply change the output from odd to even. You could also simply change the 1 to a 0.


Also if you could provide code/answers for the questions you answered we could provide more help. Or even the exact parts that you are not understanding like "what does the % operator mean?" and we could say something like: It is the modulo operator that returns the remainder of the division of two values for example: 12 % 5 = 2. So when you think about it if a number is not divisible by 2 evenly ( has no remainder ) then it is odd otherwise if it does divide into it evenly it is even.
These were my answers, but I don't have a program to run it I was trying dev c++ and code blocks and I cannot run it.



1. The output is:

0020406081012


2.

#include <iostream>

using namespace std;

int main ()



int i = 0;
while (i < 7 )
++i

{

cout << 2 * i;



return 0;

}

3.


The loops will run 100 times.

4.

It outputs the number 101.

5. 0 , 0.5


6. 12


7.
#include <iostream>

using namespace std;


int main ()

{
int n

cin >> " Please enter a value for n " << endl;


cin >> n;


double sum = n * (n+ 1)/ 2.0;


cout << sum;



return 0;

}











8. #include <iostream>

using namespace std;


int main ()


{

int k = 550

for (int i = 330 k < = 550 ++i)

cout << i;

return 0;

}




9.

#include <iostream>

using namespace std;

int main ()

int n

cin >> " Please enter a value between 3 and 12 " endl;

cin >> n;

{

if (n >= 3) && (n <= 12)
}
{
else ( cout << "Bad Number. ") endl;

}
cout << " Good number.

}

return 0;

10.
#include <iostream>

using namespace std;

int main ()


{

int i;


cout << " Please enter a value for i. " endl;

cin >> i;

if (i % 2 == 2)
{

cout << "even number"
}


return 0;

}


Last edited on
How did you get this as an output for #1?

0020406081012


The correct output should be


024681012


2) You should go back and check where the output statement was. Anyways the answer is the code from question 1 :P

3) correct

4) correct

5) correct

6) correct

7) The first cin >>[code] should be [code]cout <<. Also for the sum you should make it an integer because think about it if you are adding only integers the sum will always be an integer.

8) You may want to have another look at your for loop
for (int i = 330 k < = 550 ++i) not sure what the k is supposed to be and you are missing a few semi colons

9 & 10) look like you just stopped caring.
Could you you tell me how to do numbers 9 and 10? I honestly have no idea, the professor not much help, this is my first time using c++.
Hi @noobplusminus,
i think that your
answer 1 is not
right

1. The output is:

0020406081012


The correct answer
should be


024681012



1
2
3
4
5
6
int i=0;
while(i<7){
cout<<2*i; //2*0=0,2*1=2,2*2=4,2*3=6,2*4=8,2*5=10,2*6=12
++i;
}


if i am
missing something
let me know!

answer 2
should be

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

int main(){

int i=0;

while(i<7){
cout<<2*i;
++i; //increment by 1
}//end while loop <--

return 0;
}//end of main <-- 


answer 7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

using namespace std;


int main ()

{
int n

//console input object is for "input" information
//cin >> " Please enter a value for n " << endl;

cout<<"Please enter a value for n"<<endl;

cin >> n;


double sum = n * (n+ 1)/ 2.0; //this is clever ;D


cout << sum;



return 0;

}


answer 8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;


int main ()


{

int k = 550

for (int i = 330 k < = 550 ++i)//hint: you are missing something ";"  
//and k<=550? you are incrementing i not k
//k is equal to 550 already 
//you'll get an infinite loop 
cout << i;

return 0;

}


answer 9
is wrong
this is an
example using
if-else

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;


int main(){

int n;

cout<<"Enter a non-negative number: ";
cin>>n;

if(n>=0)
cout<<"good"<<endl;
else
cout<<"Wrong number"<<endl;

return 0; //indicates success
}//end of main 


you have a good advice
for Question 10
from @giblit

regards.
I really appreciate it guys. Hopefully I can do this tomorrow during the quiz.

For number 9 how would I run multiple if statements since N has to be a number between 3 and 12.

and just for reference could you guys type out the answer for number 10. (I'm not being lazy I have no idea what the output will be since I can't run them, that is why i got number 1 wrong.)

Edit: If you guys have any windows programs that can compile and uses <iostream> would greatly be appreciated.
Last edited on
just use www.ideone.com it is an online compiler. Also for number 9 you want to use the conditional && operator
1
2
3
4
5
6
7
8
if( n >= 3 && n <= 12 )
{
    //do stuff
}
else
{
    //do other stuff
}
Thanks!
Download visual studio express edition from visualstudio.com (is free)
(free like free beer)

You have to correct Question 8

Answer 10

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;


int main(){


int i;
cout<<"Enter number: ";
cin>>i;

if(i%2==0)
cout<<"Even"<<endl;

return 0; //indicates success
}//end of main 
I got all of it except for question 8.
Topic archived. No new replies allowed.