# Sets of Numbers and Graphing With Asterisks

09/15/13
Week 3 Assignments
Problem 4.7 (Sets of Numbers) – Page 2
Pseudocode – Page 3
Flowchart – Page 4
Code – Page 5
Problem 4.18 (Graphing with Asterisks) – Page 6
Pseudocode – Page 7
Flowchart – Page 9
Code – Page 10
1|Page
Daniel Sharpe
09/15/13
Week 3 Assignments
Problem:
Write for statements that print the following sequences of values:
a. 1, 2, 3, 4, 5, 6, 7
b. 3, 8, 13, 18, 23
c. 20, 14, 8, 2, -4, -10
d. 19, 27, 35, 43, 51
2|Page
Daniel Sharpe
09/15/13
Week 3 Assignments
Pseudocode
Print 4 lists of numbers that increment and decrement by a declared value.
Initialize Integers
Initialize a
Initialize b
Initialize c
Initialize d
For a = 1, while a is less than or equal to 7, increase a by 1
Print each variable
For b = 1, while b is less than or equal to 23, increase b by 5
Print each variable
For c = 20, while c is more than or equal to -10, decrease c by 6
Print each variable
For d = 8, while d is less than or equal to 51, increase d by 8
Print each variable
3|Page
Daniel Sharpe
09/15/13
Week 3 Assignments
Flowchart
4|Page
Daniel Sharpe
09/15/13
Week 3 Assignments
Daniel Sharpe
Program Code
#include &lt;stdio.h&gt;
int main(void) {
int
int
int
int
a;
b;
c;
d;
//list
//list
//list
//list
a
b
c
d
variable
variable
variable
variable
printf(&quot;List A: &quot;);
for( a = 1; a &lt;= 7; ++a){
printf(&quot;%d, &quot;, a);
}
printf(&quot;\n&quot;);
printf(&quot;List B: &quot;);
integer
integer
integer
integer
//shows the list being printed
//set a = 1 and increment by a (1) until a = 7
//print a integers with a comma and a space to
//shows the list being printed
for( b = 3; b &lt;= 23; b+=5){
printf(&quot;%d, &quot;, b);
}
printf(&quot;\n&quot;);
printf(&quot;List C: &quot;);
//set b = 3 and increment by 5 until b = 23
//print b integers with a comma and a
for( c = 20; c &gt;= -10; c-=6){
printf(&quot;%d, &quot;, c);
}
printf(&quot;\n&quot;);
printf(&quot;List D: &quot;);
//set c = 20 and decrement by 6 until c = -10
//print c integers with a comma and a
for( d = 19; d &lt;= 51; d+=8){
printf(&quot;%d, &quot;, d);
}
printf(&quot;\n&quot;);
continue&quot; isn't in List D
}
//set d = 19 and increment by 8 until d = 51
//print d integers with a comma and a
5|Page
//shows the list being printed
//shows the list being printed
09/15/13
Week 3 Assignments
Daniel Sharpe
Problem:
One interesting application of computers is drawing graphs and bar charts (sometimes
called histograms). Write a program that first reads and stores five numbers (each
between 1 and 30). Then, for each number read, your program should print a line
containing that number of adjacent asterisks (e.g. for the number seven, it should print
*******), so that it displays five rows of asterisks.
6|Page
09/15/13
Week 3 Assignments
Pseudocode
Input 5 numbers and check to see if they’re between 1 and 30. For numbers that are
Initialize Variables
num1, num2, num3, num4, num5
a, b, c, d, e
Input first number
While number is less than 1 or greater than 30
Print Error code
Allow user to re-input number
Input second number
While number is less than 1 or greater than 30
Print Error code
Allow user to re-input number
Input third number
While number is less than 1 or greater than 30
Print Error code
Allow user to re-input number
Input fourth number
While number is less than 1 or greater than 30
Print Error code
Allow user to re-input number
Input fifth number
While number is less than 1 or greater than 30
Print Error code
Allow user to re-input number
For variable a = 1, ++1 until a equals num1
Print one asterisk
For variable b= 1, ++1 until a equals num2
7|Page
Daniel Sharpe
09/15/13
Week 3 Assignments
Print one asterisk
For variable c = 1, ++1 until a equals num3
Print one asterisk
For variable d = 1, ++1 until a equals num4
Print one asterisk
For variable e = 1, ++1 until a equals num5
Print one asterisk
Flowchart
8|Page
Daniel Sharpe
09/15/13
Week 3 Assignments
Code
9|Page
Daniel Sharpe
09/15/13
Week 3 Assignments
Daniel Sharpe
#include &lt;stdio.h&gt;
int main(void) {
float num1, num2, num3, num4, num5;
int a, b, c, d, e;
use float in case user enters decimal
//numbers to be read into memory
//integers to build each graph,
printf(&quot;Enter 5 whole numbers (between 1 and 30) to build a graph.\n&quot;);
//instructions for user
printf(&quot;Decimal values will be rounded down to the nearest whole number.n&quot;);
//instructions for user
printf_s(&quot;Enter first number: &quot;); //instructions for user
scanf_s(&quot;%f&quot;, &amp;num1);
while (num1 &lt;=0 || num1 &gt;=31) {
//check to see that number is greater
than 0 and less than 31
printf_s(&quot;Negative numbers and numbers greater than 30 are not
allowed.\n&quot;); //error message for user
//explain to user to
enter a new number
scanf_s(&quot;%f&quot;, &amp;num1);
//take in new number to memory
}
printf_s(&quot;Enter second number: &quot;); //instructions for user
scanf_s(&quot;%f&quot;, &amp;num2);
while (num2 &lt;=0 || num2 &gt;=31) { //check to see that number is greater than
0 and less than 31
printf_s(&quot;Negative numbers and numbers greater than 30 are not
allowed.\n.&quot;); //error message for user
printf_s(&quot;Please re-enter second number: &quot;); //explain to user to
enter a new number
scanf_s(&quot;%f&quot;, &amp;num2); //take in new number to memory
}
printf_s(&quot;Enter third number: &quot;); //instructions for user
scanf_s(&quot;%f&quot;, &amp;num3);
while (num3 &lt;=0 || num3 &gt;=31) { //check to see that number is greater than
0 and less than 31
printf_s(&quot;Negative numbers and numbers greater than 30 are not
allowed.\n&quot;); //error message for user
printf_s(&quot;Please re-enter third number: &quot;); //explain to user to
enter a new number
scanf_s(&quot;%f&quot;, &amp;num3); //take in new number to memory
}
printf_s(&quot;Enter fourth number: &quot;); //instructions for user
scanf_s(&quot;%f&quot;, &amp;num4);
while (num4 &lt;=0 || num4 &gt;=31) { //check to see that number is greater than
0 and less than 31
printf_s(&quot;Negative numbers and numbers greater than 30 are not
allowed.\n&quot;); //error message for user
printf_s(&quot;Please re-enter fourth number: &quot;); //explain to user to
enter a new number
scanf_s(&quot;%f&quot;, &amp;num4); //take in new number to memory
}
printf_s(&quot;Enter fifth number: &quot;); //instructions for user
scanf_s(&quot;%f&quot;, &amp;num5);
10 | P a g e
09/15/13
Week 3 Assignments
Daniel Sharpe
while (num5 &lt;=0 || num5 &gt;=31) { //check to see that number is greater than
0 and less than 31
printf_s(&quot;Negative numbers and numbers greater than 30 are not
allowed.\n&quot;); //error message for user
printf_s(&quot;Please re-enter fifth number: &quot;); //explain to user to
enter a new number
scanf_s(&quot;%f&quot;, &amp;num5); //take in new number to memory
}
printf_s(&quot;\n&quot;);
//space for
printf_s(&quot;Here is your graph\n&quot;); //text to inform user what is being
displayed
printf_s(&quot;\n&quot;);
//space for
for (a=1; a&lt;=num1; ++a){
//take a and count it
number entered
printf_s(&quot;*&quot;, a);
//print
is counted
}
printf_s(&quot;\n&quot;);
//skip a line for next graph
for (b=1; b&lt;=num2; ++b){
//take b and count it
number entered
printf_s(&quot;*&quot;, b);
//print
is counted
}
printf_s(&quot;\n&quot;); //skip a line for next graph
for (c=1; c&lt;=num3; ++c){
//take c and count it
number entered
printf_s(&quot;*&quot;, c);
//print
is counted
}
printf_s(&quot;\n&quot;); //skip a line for next graph
for (d=1; d&lt;=num4; ++d){
//take d and count it
number entered
printf_s(&quot;*&quot;, a);
//print
is counted
}
printf_s(&quot;\n&quot;); //skip a line for next graph
for (e=1; e&lt;=num5; ++e){
//take e and count it
number entered
printf_s(&quot;*&quot;, e);
//print
is counted
}
printf_s(&quot;\n&quot;);
}
11 | P a g e
up till it equals first
an asterisk every time a
up till it equals second
an asterisk every time b
up till it equals third
an asterisk every time c
up till it equals fourth
an asterisk every time d
up till it equals fifth
an asterisk every time e