SophiaPretty

(5)

$14/per page/Negotiable

About SophiaPretty

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Accounting,Algebra See all
Accounting,Algebra,Applied Sciences,Architecture and Design,Art & Design,Biology,Business & Finance,Calculus,Chemistry,Communications,Computer Science,Economics,Engineering,English,Environmental science,Essay writing Hide all
Teaching Since: Jul 2017
Last Sign in: 314 Weeks Ago
Questions Answered: 15833
Tutorials Posted: 15827

Education

  • MBA,PHD, Juris Doctor
    Strayer,Devery,Harvard University
    Mar-1995 - Mar-2002

Experience

  • Manager Planning
    WalMart
    Mar-2001 - Feb-2009

Category > Computer Science Posted 06 Jan 2018 My Price 8.00

programs are starting to get a little more complex

For all exercises:

As always, all your code must follow the class style guide - correct indentation, spaces around all operators, meaningful variable names, etc.


In particular, now that your programs are starting to get a little more complex, you must pay attention to clearly commenting your code: each “conceptual block” of code should have a brief comment describing its purpose.

Exercise 1: for loops

Write three for loops to produce the numeric sequences below.
Your output should be as displayed, with numbers separated by a comma and space. There should also be a single blank line between each sequence. 

Hint: To generate all three sequences, you should only have to change the for loop parameters (i.e. the initialization, the conditional, and the update). Once this simple change occurs in the for loop header, a line (at most two) inside the body may need to be changed to accommodate proper output formatting with commas.

Before coding, write down in words the algorithm behind each sequence


User input has been bolded and underlined for emphasis:

Which exercise? 1

Enter beginning and ending numbers, separated by space: 0 20

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20

Enter beginning and ending numbers, separated by space: 1 35

1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35

Enter the beginning and largest positive numbers, separated by space: 2 128

2, -4, 8, -16, 32, -64, 128, -256


----------- Another sample run of the same program -----------------

Which exercise? 1

Enter beginning and ending numbers, separated by space: 1 5

1, 2, 3, 4, 5

Enter beginning and ending numbers, separated by space: 2 13

2, 4, 6, 8, 10, 12

Enter the beginning and largest positive numbers, separated by space: 1 256

1, -2, 4, -8, 16, -32, 64, -128, 256


As you can see, the for-loop structure is particularly effective at handling iterations where we know in advance (or can easily calculate) where to start counting, and where to stop; “counter-controlled” loops are a typical use.


Exercise 2: Sentinel-controlled loops

Write a program that will read in from user input the scores of an exam (possible scores: 0 to 100). We do not know in advance the number of students, so the user will have to type in a sentinel value when they have finished entering scores - i.e a value that could not be mistaken for an actual score. In this case, it’s pretty clear that  a negative number would be a good choice for our sentinel. 
We will utilize -1 as our sentinel value.


Instead of outputting anything within the loop, you will keep track of how many A's, B's, C's, D's and F's there are. Output the letter grades and the count for each only after you exit the loop (i.e. after the user has entered the sentinel).


Use the scale: 90 - 100 => A, 80 - 89 => B, 70 - 79  => C, 60 - 69  => D and 0 - 59  => F.


Hints:
You will need a separate int variable to keep track of the number receiving each letter grade; 
increment that variable within a branch of an if / else if / else structure;
make sure the SENTINEL isn't accidentally entered as a grade.


Since we do not know how many iterations the loop will execute, the while or do-while loop is the best choice for this exercise.



User input has be bolded and underlined for emphasis:

Which exercise? 2


Enter one or more grades, or -1 to stop:

45

78

99

92

45

67

-1


The grades breakdown is:

As: 2

Bs: 0

Cs: 1

Ds: 1

Fs: 2



Exercise 3: Coin Flip

Write a program that will simulate a coin toss.


First, write this program to try out the rand function (remember to #include <cstdlib>)

for (int i = 0; i < 10, ++i)

{

  int random_number = rand();

  cout << random_number << endl;

}


Run the program a few times. What do you observe?


To prevent this repetition of the same sequence of random numbers whenever the program is run, we have to “prime” the rand function with a different “seed” each time:

// place this statement just ONCE at the start of your main:

srand(time(0));


When submitting on hypergrade do not use srand, only use rand();


Now run the program a few times, as you did before: what do you observe this time?



Now simulate the coin toss: for this, you will need to restrict your random numbers to just two values: Let’s use 0 to represent heads, and 1 for tails.


What integer operator will you use to do this - i.e. turn the huge range of the “raw” rand function into a range of just 2 possible values?


Use a while loop to ask the user if they wish to toss the coin, and keep on doing so while the user answers “yes”; for each toss output the word “heads” or “tails” according to the random value obtained.


User input has been bolded and underlined for emphasis:

Toss the coin? yes


heads

Toss the coin? yes


heads

Toss the coin? yes


tails

Toss the coin? yes


heads

Toss the coin? no

Answers

(5)
Status NEW Posted 06 Jan 2018 02:01 PM My Price 8.00

-----------  ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly

Not Rated(0)