Sets of Numbers and Graphing With Asterisks

advertisement
09/15/13
Week 3 Assignments
Table of Contents
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 <stdio.h>
int main(void) {
int
int
int
int
a;
b;
c;
d;
//list
//list
//list
//list
a
b
c
d
variable
variable
variable
variable
printf("List A: ");
for( a = 1; a <= 7; ++a){
printf("%d, ", a);
make list readable
}
printf("\n");
printf("List B: ");
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
//return to next line for next list
//shows the list being printed
for( b = 3; b <= 23; b+=5){
printf("%d, ", b);
space to make list readable
}
printf("\n");
printf("List C: ");
//set b = 3 and increment by 5 until b = 23
//print b integers with a comma and a
for( c = 20; c >= -10; c-=6){
printf("%d, ", c);
space to make list readable
}
printf("\n");
printf("List D: ");
//set c = 20 and decrement by 6 until c = -10
//print c integers with a comma and a
for( d = 19; d <= 51; d+=8){
printf("%d, ", d);
space to make list readable
}
printf("\n");
continue" 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
//return to next line for next list
//shows the list being printed
//return to next line for next list
//shows the list being printed
//return to next line so "Press any key to
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 <stdio.h>
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("Enter 5 whole numbers (between 1 and 30) to build a graph.\n");
//instructions for user
printf("Decimal values will be rounded down to the nearest whole number.n");
//instructions for user
printf_s("Enter first number: "); //instructions for user
scanf_s("%f", &num1);
//read in first number
while (num1 <=0 || num1 >=31) {
//check to see that number is greater
than 0 and less than 31
printf_s("Negative numbers and numbers greater than 30 are not
allowed.\n"); //error message for user
printf_s("Please re-enter first number: ");
//explain to user to
enter a new number
scanf_s("%f", &num1);
//take in new number to memory
}
printf_s("Enter second number: "); //instructions for user
scanf_s("%f", &num2);
//read in second number
while (num2 <=0 || num2 >=31) { //check to see that number is greater than
0 and less than 31
printf_s("Negative numbers and numbers greater than 30 are not
allowed.\n."); //error message for user
printf_s("Please re-enter second number: "); //explain to user to
enter a new number
scanf_s("%f", &num2); //take in new number to memory
}
printf_s("Enter third number: "); //instructions for user
scanf_s("%f", &num3);
//read in third number
while (num3 <=0 || num3 >=31) { //check to see that number is greater than
0 and less than 31
printf_s("Negative numbers and numbers greater than 30 are not
allowed.\n"); //error message for user
printf_s("Please re-enter third number: "); //explain to user to
enter a new number
scanf_s("%f", &num3); //take in new number to memory
}
printf_s("Enter fourth number: "); //instructions for user
scanf_s("%f", &num4);
//read in fourth number
while (num4 <=0 || num4 >=31) { //check to see that number is greater than
0 and less than 31
printf_s("Negative numbers and numbers greater than 30 are not
allowed.\n"); //error message for user
printf_s("Please re-enter fourth number: "); //explain to user to
enter a new number
scanf_s("%f", &num4); //take in new number to memory
}
printf_s("Enter fifth number: "); //instructions for user
scanf_s("%f", &num5);
//read in fifth number
10 | P a g e
09/15/13
Week 3 Assignments
Daniel Sharpe
while (num5 <=0 || num5 >=31) { //check to see that number is greater than
0 and less than 31
printf_s("Negative numbers and numbers greater than 30 are not
allowed.\n"); //error message for user
printf_s("Please re-enter fifth number: "); //explain to user to
enter a new number
scanf_s("%f", &num5); //take in new number to memory
}
printf_s("\n");
//space for
readability
printf_s("Here is your graph\n"); //text to inform user what is being
displayed
printf_s("\n");
//space for
readability
for (a=1; a<=num1; ++a){
//take a and count it
number entered
printf_s("*", a);
//print
is counted
}
printf_s("\n");
//skip a line for next graph
for (b=1; b<=num2; ++b){
//take b and count it
number entered
printf_s("*", b);
//print
is counted
}
printf_s("\n"); //skip a line for next graph
for (c=1; c<=num3; ++c){
//take c and count it
number entered
printf_s("*", c);
//print
is counted
}
printf_s("\n"); //skip a line for next graph
for (d=1; d<=num4; ++d){
//take d and count it
number entered
printf_s("*", a);
//print
is counted
}
printf_s("\n"); //skip a line for next graph
for (e=1; e<=num5; ++e){
//take e and count it
number entered
printf_s("*", e);
//print
is counted
}
printf_s("\n");
}
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
Download