Presentation Material for Class Meeting #8 on Feb 11

Download Report

Transcript Presentation Material for Class Meeting #8 on Feb 11

CS 108 Computing Fundamentals
Notes for Thursday, February 11, 2016
Let's Review GHP #8
• A sample of a "good" submission is posted
Let's Review GHP #9
• Let's review last semester's Exam #1
• Notice how I take the exam… I give myself the best opportunity for
partial credit and I also reduce my chances of making a mistake
Practice
• Write a program to prompt a user to enter a positive integer and
then list all factors of that positive integer.
What is a "factor" and how can we test for a factor?
A Loop and a Selection Statement
1.
2.
3.
4.
5.
6.
7.
Create variables ("input" initialized to 0 and "index" initialized to 0)
Introduce the program
Ask the user to enter an integer
Record the user's input
Display a message indicating the list to follow are factors of the user's input
Test every integer between 1 and input/2 inclusive
i. Test to determine if ( input mod index ) is equal to 0
1) If true
1) Display index (it's a factor of input)
2) Go to 6 (back-tracking requires a loop)
2) If false
1) Go to step 6 (back-tracking requires a loop)
ii. If false, go to step 7
Terminate
#include <stdio.h>
// My file 1.c
int main(void)
{
int input = 0, index = 0;
printf("\n This program will determine the factors of a positive integer. \n ");
printf("\n Enter a positive integer: ");
scanf("%d", &input);
printf("\n Integer factors of %d are: " , input);
for ( index = 1; index <= input / 2 ; index = index + 1)
{
if ( input % index == 0 )
{
printf(" %d \n", index);
}
}
printf("\n\n\n");
return 0;
}
Practice
• Modify the previous algorithm and program so that only the
greatest factor other than the input itself is displayed to the screen.
A Loop and a Selection Statement
1.
2.
3.
4.
5.
6.
7.
Create variables ("input" initialized to 0 and "index" initialized to 0)
Introduce the program
Ask the user to enter an integer
Record the user's input
Display a message indicating the list to follow are factors of the user's input
Test to every integer between 1 and input/2
i. Test to determine if ( input mod index ) is equal to 0
1) If true
1) Display index (it's a factor of input)
2) Go to 6 (back-tracking requires a loop)
2) If false
1) Go to step 6 (back-tracking requires a loop)
ii. If false, go to step 7
Terminate
A Loop and a Selection Statement
1.
2.
3.
4.
5.
6.
7.
Create variables ("input" initialized to 0 and "index" initialized to 0 and "greatest"
initialized to 0)
Introduce the program
Ask the user to enter an integer
Record the user's input
Test every integer between 1 and input/2 inclusive
i. Test to determine if ( input mod index ) is equal to 0
1) If true
1) Store input to greatest
2) Go to 5 (back-tracking requires a loop)
2) If false
1) Go to step 5 (back-tracking requires a loop)
ii. If false, go to step 6
Display a message indicating the greatest factor of the user's input
Terminate
#include <stdio.h>
// My file 2.c
int main(void)
// GCF is Greatest Common Factor
{
int input = 0, index = 0 , greatest = 0;
printf("\n This program determines the GCF of a positive integer. \n ");
printf("\n Enter a positive integer: "); scanf("%d", &input);
for (index = 1; index <= input/2 ; index = index + 1)
{
if (input%index == 0)
{
greatest = index ;
}
}
printf("\n The GCF of %d other than %d is %d \n\n\n" , input ,
input, greatest);
return 0;
}
Practice
• Write a program to display a "multiplication table" for
numbers 1 to 12.
What are the steps necessary to accomplish this?
1.
2.
3.
4.
5.
6.
Create a place to track "row" information (initialize to 1)
Create a place to track "column" information (initialize to 1)
Set "column" to 1
Test to determine if "row" is less than or equal to 12
1. If true
1. Display a linefeed
2. Set "column" to 1
3. Test to determine if "column" is less than or equal to 12
1) If true
1) Multiply row * column
2) Display the result
3) Increment column
4) Go to step 4.1.3
2) If false
1) Increment row
2) Go to step 4
2. If false
1. Go to step 5
Display 3 linefeeds
Terminate
#include <stdio.h> // My file 3.c
int main(void)
{
int row = 0, column = 0 ;
for (row = 1; row <= 12; row = row + 1)
{
printf("\n");
for (column = 1; column <= 12; column = column + 1)
{
printf(" %5d", row * column); // %5d facilitates alignment
}
}
printf("\n\n\n");
return 0;
}
Fun With Loops
#include <stdio.h>
int main(void)
{
char input = ' ' ;
input = 32 ;
while(input)
{
printf(" %c " , input ) ;
input = input + 1 ;
}
return 0;
}
// My file 4.c
// Demonstrating the characters are really 8 bit
// unsigned integers
Fun With Loops
#include <stdio.h>
int main(void)
{
int input = 0 ;
// My file 5.c
// Demonstrating the break statement in a loop
for ( input = 0 ; input < 666 ; input = input + 1 )
{
printf(" %d\n " , input ) ;
if ( input == 357 )
break ;
}
return 0;
}
Fun With Loops
#include <stdio.h>
int main(void)
{
// My file 6.c
// Demonstrating the break statement in a nested loop
// Let's use nested loops to find prime numbers
// between 2 and 1,000
int number = 0 , index = 0 ;
for ( number = 2 ; number < 1000 ; number = number + 1 )
{
for ( index = 2 ; index <= number / 2 ; index = index + 1 )
{
if ( ! ( number % index ) )
break ;
}
if ( index > (number / 2 ) )
printf ("\n%d is prime. \n" , number ) ;
}
return 0;
}