Introduction to Programming - Urząd Miasta Łodzipolrola/strony/1617z-wdprog/wdprog1617-w05e… ·...

50
Loops - exercises Introduction to Programming lecture 5 Agata Pólrola Faculty of Mathematics and Computer Science, Lódź University 2016/2017 Agata Pólrola Faculty of Mathematics and Computer Science, Lódź University Introduction to Programming

Transcript of Introduction to Programming - Urząd Miasta Łodzipolrola/strony/1617z-wdprog/wdprog1617-w05e… ·...

Loops - exercises

Introduction to Programminglecture 5

Agata PółrolaFaculty of Mathematics and Computer Science, Łódź University

2016/2017

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Exercises - developing algorithms

Exercise 1

We have a group of 9 students, and should select a two-personsubgroup. Write a program which prints:

all the possible subgroups in which one person is a “leader”and the second one a “subordinate”, and the number ofpossible ways in which such a subgroup can be chosen,

all the the possible subgropus consisting of two “equal”’members, and the number of possible ways in which such asubgroup can be chosen.

(the students are identified by numbers from 1 to 9

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Pairs of “non-equal” members (ordered pairs)Consider people of names listed below...

the scheme:

choosing a “leader”

creating all the possible pairs of the form: this leader + a subordinate (eachstudent different from the leader can be his/her subordinate)

implemention:for (int leader=1; leader<=9; leader++)

for (int subord=1; subord <=9; subord++)if (leader!=subord) {

// print the pair}

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Pairs of “non-equal” members (ordered pairs)Consider people of names listed below...

the scheme:

choosing a “leader”

creating all the possible pairs of the form: this leader + a subordinate (eachstudent different from the leader can be his/her subordinate)

implemention:for (int leader=1; leader<=9; leader++)

for (int subord=1; subord <=9; subord++)if (leader!=subord) {

// print the pair}

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Pairs of “non-equal” members (ordered pairs)Consider people of names listed below...

the scheme:

choosing a “leader”

creating all the possible pairs of the form: this leader + a subordinate (eachstudent different from the leader can be his/her subordinate)

implemention:

for (int leader=1; leader<=9; leader++)for (int subord=1; subord <=9; subord++)

if (leader!=subord) {// print the pair

}

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Pairs of “non-equal” members (ordered pairs)Consider people of names listed below...

the scheme:

choosing a “leader”

creating all the possible pairs of the form: this leader + a subordinate (eachstudent different from the leader can be his/her subordinate)

implemention:for (int leader=1; leader<=9; leader++)

for (int subord=1; subord <=9; subord++)if (leader!=subord) {

// print the pair}

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Pairs of “non-equal” members (ordered pairs)Consider people of names listed below...

the scheme:

choosing a “leader”

creating all the possible pairs of the form: this leader + a subordinate (eachstudent different from the leader can be his/her subordinate)

implemention:for (int leader=1; leader<=9; leader++)

for (int subord=1; subord <=9; subord++)if (leader!=subord) {

// print the pair}

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Pairs of “non-equal” members (ordered pairs)Consider people of names listed below...

the scheme:

choosing a “leader”

creating all the possible pairs of the form: this leader + a subordinate (eachstudent different from the leader can be his/her subordinate)

implemention:for (int leader=1; leader<=9; leader++)

for (int subord=1; subord <=9; subord++)if (leader!=subord) {

// print the pair}

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Pairs of “non-equal” members (ordered pairs)Consider people of names listed below...

the scheme:

choosing a “leader”

creating all the possible pairs of the form: this leader + a subordinate (eachstudent different from the leader can be his/her subordinate)

implemention:for (int leader=1; leader<=9; leader++)

for (int subord=1; subord <=9; subord++)if (leader!=subord) {

// print the pair}

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Example (exercise 1 - part 1)

#include <iostream >using namespace std;int main(){int numOfPairs =0, groupSize;cout << "give the size of the group " ; cin >>groupSize;

for (int leader =1; leader <= groupSize; leader ++)for (int subor =1; subor <= groupSize; subor ++)if ( leader !=subor){cout << " leader: "<< leader << ",subordinate: " <<subor << endl;

numOfPairs ++;}

cout << "there is " << numOfPairs << " possiblepairs" << endl;

return 0;}

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Pairs of “equal” members (unordered pairs)

the scheme:

choosing the first person to the pair (A)

choosing the second person to the pair (B), omitting the people being alredy themember A (the pair consisting of these two persons has been considered before)

implemention:for (int person1=1; person1<=9; person1++)

for (int person2 = person1; person2<=9; person2++)if (person1!=person2 ){

// print the pair}

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Pairs of “equal” members (unordered pairs)

the scheme:

choosing the first person to the pair (A)

choosing the second person to the pair (B), omitting the people being alredy themember A (the pair consisting of these two persons has been considered before)

implemention:for (int person1=1; person1<=9; person1++)

for (int person2 = person1; person2<=9; person2++)if (person1!=person2 ){

// print the pair}

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Pairs of “equal” members (unordered pairs)

the scheme:

choosing the first person to the pair (A)

choosing the second person to the pair (B), omitting the people being alredy themember A (the pair consisting of these two persons has been considered before)

implemention:

for (int person1=1; person1<=9; person1++)for (int person2 = person1; person2<=9; person2++)

if (person1!=person2 ){// print the pair}

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Pairs of “equal” members (unordered pairs)

the scheme:

choosing the first person to the pair (A)

choosing the second person to the pair (B), omitting the people being alredy themember A (the pair consisting of these two persons has been considered before)

implemention:for (int person1=1; person1<=9; person1++)

for (int person2 = person1; person2<=9; person2++)if (person1!=person2 ){

// print the pair}

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Pairs of “equal” members (unordered pairs)

the scheme:

choosing the first person to the pair (A)

choosing the second person to the pair (B), omitting the people being alredy themember A (the pair consisting of these two persons has been considered before)

implemention:for (int person1=1; person1<=9; person1++)

for (int person2 = person1; person2<=9; person2++)if (person1!=person2 ){

// print the pair}

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Pairs of “equal” members (unordered pairs)

the scheme:

choosing the first person to the pair (A)

choosing the second person to the pair (B), omitting the people being alredy themember A (the pair consisting of these two persons has been considered before)

implemention:for (int person1=1; person1<=9; person1++)

for (int person2 = person1; person2<=9; person2++)if (person1!=person2 ){

// print the pair}

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Pairs of “equal” members (unordered pairs)

the scheme:

choosing the first person to the pair (A)

choosing the second person to the pair (B), omitting the people being alredy themember A (the pair consisting of these two persons has been considered before)

implemention:for (int person1=1; person1<=9; person1++)

for (int person2 = person1; person2<=9; person2++)if (person1!=person2 ){

// print the pair}

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Example (exercise 1 - part 2)

#include <iostream >using namespace std;int main(){int numOfPairs =0, groupSize;cout << "give the size of the group " ; cin >>groupSize;

for (int person1 =1; person1 <= groupSize; person1 ++)for (int person2=person1; person2 <= groupSize;person2 ++)if (person1 != person2){cout << "person1: "<< person1 << ",person2: " <<person2 << endl;

numOfPairs ++;}

cout << "there is " << numOfPairs << "possiblepairs" << endl;

return 0;}

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Exercise 2

Write a program which reads numbers until giving the zero value,and printing the greatest number given (the final 0 is not takeninto account)

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

some elements of the scheme:

reading numbers until giving 0:

while(true){cout << "give a number: "; cin >> number;if (number==0) break;//here the value given is handled

}

searching for the maximal value:

we need a variable storing the “max value found so far” (Max)if the number given is greater than “temporary max” Max we change Max

note that after giving the first number Max has to became equal to this

value (one should either choose an appropriate initial value, or “notice”

that the first value was given)

the result printed has to be correct even if the first number given was 0

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

some elements of the scheme:

reading numbers until giving 0:

while(true){cout << "give a number: "; cin >> number;if (number==0) break;//here the value given is handled

}

searching for the maximal value:

we need a variable storing the “max value found so far” (Max)if the number given is greater than “temporary max” Max we change Max

note that after giving the first number Max has to became equal to this

value (one should either choose an appropriate initial value, or “notice”

that the first value was given)

the result printed has to be correct even if the first number given was 0

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

some elements of the scheme:

reading numbers until giving 0:

while(true){cout << "give a number: "; cin >> number;if (number==0) break;//here the value given is handled

}

searching for the maximal value:

we need a variable storing the “max value found so far” (Max)if the number given is greater than “temporary max” Max we change Max

note that after giving the first number Max has to became equal to this

value (one should either choose an appropriate initial value, or “notice”

that the first value was given)

the result printed has to be correct even if the first number given was 0

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

some elements of the scheme:

reading numbers until giving 0:

while(true){cout << "give a number: "; cin >> number;if (number==0) break;//here the value given is handled

}

searching for the maximal value:

we need a variable storing the “max value found so far” (Max)if the number given is greater than “temporary max” Max we change Max

note that after giving the first number Max has to became equal to this

value (one should either choose an appropriate initial value, or “notice”

that the first value was given)

the result printed has to be correct even if the first number given was 0

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Example (exercise 2 - version 1)

#include <iostream >#include <limits >using namespace std;int main(){int a, max; bool numsWereGiven;numsWereGiven=false; // no numbers yetmax=std:: numeric_limits <int >:: min();while (true) {

cout << "give a number : "; cin >> a;if ( a==0 ) break;numsWereGiven=true; //a number was givenif (a>max) max=a;

}if (numsWereGiven ==true)

cout << "the greatest value:"<< max << endl;elsecout << "no numbers given , no greatest one ";return 0; }

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Example (exercise 2 - version 2)

#include <iostream >using namespace std;int main(){

int a,max ,counter;counter =0;while(true) {

cout << "give a number "; cin >> a;if (a==0) break;counter ++;if (counter ==1) max = a;else {

if (a>max) max = a;}

}if (counter > 0)

cout << "the greatest value is " <<max << endl;

return 0; }

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Exercise 3

Write a program which reads integers until 0 is given, and printsthe information whether an even number was provided (the final 0is not taken into account)

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Algorithms “with a belief”:

we have a “belief” about the result

(there was no even number so far)

seeing an apropriate value changes this belief

(if an even number was given - the belief changes to “therewas an even number”)

seeing an “inappropriate” value does not cause belief change

(if an odd number was given - the opinion whether an evennumber was given earlier does not change)

the final result is the “belief” we have after reading all thenumbers

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Algorithms “with a belief”:

we have a “belief” about the result

(there was no even number so far)

seeing an apropriate value changes this belief

(if an even number was given - the belief changes to “therewas an even number”)

seeing an “inappropriate” value does not cause belief change

(if an odd number was given - the opinion whether an evennumber was given earlier does not change)

the final result is the “belief” we have after reading all thenumbers

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Algorithms “with a belief”:

we have a “belief” about the result

(there was no even number so far)

seeing an apropriate value changes this belief

(if an even number was given - the belief changes to “therewas an even number”)

seeing an “inappropriate” value does not cause belief change

(if an odd number was given - the opinion whether an evennumber was given earlier does not change)

the final result is the “belief” we have after reading all thenumbers

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Algorithms “with a belief”:

we have a “belief” about the result

(there was no even number so far)

seeing an apropriate value changes this belief

(if an even number was given - the belief changes to “therewas an even number”)

seeing an “inappropriate” value does not cause belief change

(if an odd number was given - the opinion whether an evennumber was given earlier does not change)

the final result is the “belief” we have after reading all thenumbers

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Algorithms “with a belief”:

we have a “belief” about the result

(there was no even number so far)

seeing an apropriate value changes this belief

(if an even number was given - the belief changes to “therewas an even number”)

seeing an “inappropriate” value does not cause belief change

(if an odd number was given - the opinion whether an evennumber was given earlier does not change)

the final result is the “belief” we have after reading all thenumbers

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Algorithms “with a belief”:

we have a “belief” about the result(there was no even number so far)

seeing an apropriate value changes this belief

(if an even number was given - the belief changes to “therewas an even number”)

seeing an “inappropriate” value does not cause belief change

(if an odd number was given - the opinion whether an evennumber was given earlier does not change)

the final result is the “belief” we have after reading all thenumbers

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Algorithms “with a belief”:

we have a “belief” about the result(there was no even number so far)

seeing an apropriate value changes this belief(if an even number was given - the belief changes to “therewas an even number”)

seeing an “inappropriate” value does not cause belief change

(if an odd number was given - the opinion whether an evennumber was given earlier does not change)

the final result is the “belief” we have after reading all thenumbers

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Algorithms “with a belief”:

we have a “belief” about the result(there was no even number so far)

seeing an apropriate value changes this belief(if an even number was given - the belief changes to “therewas an even number”)

seeing an “inappropriate” value does not cause belief change(if an odd number was given - the opinion whether an evennumber was given earlier does not change)

the final result is the “belief” we have after reading all thenumbers

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Example (exercise 3)

#include <iostream >using namespace std;int main() {

int a;bool evenGiven = false;while(true){

cout << "give a number "; cin >> a;if (a==0) break;if (a%2==0)

evenGiven = true;}if (evenGiven ==true)

cout << "there was an even value\n";else

cout << "no even number\n";return 0;}

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Exercise 4

Write a program which reads integers until 0 is given, and printingthese pairs of sequentially given numbers in which the first elementis smaller than the second one (the final 0 is not taken intoaccount)

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

some elements of the scheme:

notice that we read single numbers, not pairs of numbers

pairs we consider “mesh together”:

after reading 1 - no pair exists yetafter reading 2 - the pair considered is: 1,2after reading 3 -the pair considered is: 2,3after reading 4 - the pair considered is: 3,4after reading 5 - the pair considered is: 4,5

after reading 0 - the end

implementation:

we need two variables: first el ofPair, second el ofPairpreparation to creating a new pair:

the second element of the “old”pair has to became the first elementof the “new” pair (first el ofPair=second el ofPair)the second element of the pair is read from the input

one need to avoid considering the pair consisting of a random value and

the first number given

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

some elements of the scheme:

notice that we read single numbers, not pairs of numbers

pairs we consider “mesh together”:

after reading 1 - no pair exists yetafter reading 2 - the pair considered is: 1,2after reading 3 -the pair considered is: 2,3after reading 4 - the pair considered is: 3,4after reading 5 - the pair considered is: 4,5

after reading 0 - the end

implementation:

we need two variables: first el ofPair, second el ofPairpreparation to creating a new pair:

the second element of the “old”pair has to became the first elementof the “new” pair (first el ofPair=second el ofPair)the second element of the pair is read from the input

one need to avoid considering the pair consisting of a random value and

the first number given

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

some elements of the scheme:

notice that we read single numbers, not pairs of numbers

pairs we consider “mesh together”:

after reading 1 - no pair exists yet

after reading 2 - the pair considered is: 1,2after reading 3 -the pair considered is: 2,3after reading 4 - the pair considered is: 3,4after reading 5 - the pair considered is: 4,5

after reading 0 - the end

implementation:

we need two variables: first el ofPair, second el ofPairpreparation to creating a new pair:

the second element of the “old”pair has to became the first elementof the “new” pair (first el ofPair=second el ofPair)the second element of the pair is read from the input

one need to avoid considering the pair consisting of a random value and

the first number given

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

some elements of the scheme:

notice that we read single numbers, not pairs of numbers

pairs we consider “mesh together”:

after reading 1 - no pair exists yetafter reading 2 - the pair considered is: 1,2

after reading 3 -the pair considered is: 2,3after reading 4 - the pair considered is: 3,4after reading 5 - the pair considered is: 4,5

after reading 0 - the end

implementation:

we need two variables: first el ofPair, second el ofPairpreparation to creating a new pair:

the second element of the “old”pair has to became the first elementof the “new” pair (first el ofPair=second el ofPair)the second element of the pair is read from the input

one need to avoid considering the pair consisting of a random value and

the first number given

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

some elements of the scheme:

notice that we read single numbers, not pairs of numbers

pairs we consider “mesh together”:

after reading 1 - no pair exists yetafter reading 2 - the pair considered is: 1,2after reading 3 -the pair considered is: 2,3

after reading 4 - the pair considered is: 3,4after reading 5 - the pair considered is: 4,5

after reading 0 - the end

implementation:

we need two variables: first el ofPair, second el ofPairpreparation to creating a new pair:

the second element of the “old”pair has to became the first elementof the “new” pair (first el ofPair=second el ofPair)the second element of the pair is read from the input

one need to avoid considering the pair consisting of a random value and

the first number given

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

some elements of the scheme:

notice that we read single numbers, not pairs of numbers

pairs we consider “mesh together”:

after reading 1 - no pair exists yetafter reading 2 - the pair considered is: 1,2after reading 3 -the pair considered is: 2,3after reading 4 - the pair considered is: 3,4

after reading 5 - the pair considered is: 4,5

after reading 0 - the end

implementation:

we need two variables: first el ofPair, second el ofPairpreparation to creating a new pair:

the second element of the “old”pair has to became the first elementof the “new” pair (first el ofPair=second el ofPair)the second element of the pair is read from the input

one need to avoid considering the pair consisting of a random value and

the first number given

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

some elements of the scheme:

notice that we read single numbers, not pairs of numbers

pairs we consider “mesh together”:

after reading 1 - no pair exists yetafter reading 2 - the pair considered is: 1,2after reading 3 -the pair considered is: 2,3after reading 4 - the pair considered is: 3,4after reading 5 - the pair considered is: 4,5

after reading 0 - the end

implementation:

we need two variables: first el ofPair, second el ofPairpreparation to creating a new pair:

the second element of the “old”pair has to became the first elementof the “new” pair (first el ofPair=second el ofPair)the second element of the pair is read from the input

one need to avoid considering the pair consisting of a random value and

the first number given

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

some elements of the scheme:

notice that we read single numbers, not pairs of numbers

pairs we consider “mesh together”:

after reading 1 - no pair exists yetafter reading 2 - the pair considered is: 1,2after reading 3 -the pair considered is: 2,3after reading 4 - the pair considered is: 3,4after reading 5 - the pair considered is: 4,5

after reading 0 - the end

implementation:

we need two variables: first el ofPair, second el ofPair

preparation to creating a new pair:

the second element of the “old”pair has to became the first elementof the “new” pair (first el ofPair=second el ofPair)the second element of the pair is read from the input

one need to avoid considering the pair consisting of a random value and

the first number given

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

some elements of the scheme:

notice that we read single numbers, not pairs of numbers

pairs we consider “mesh together”:

after reading 1 - no pair exists yetafter reading 2 - the pair considered is: 1,2after reading 3 -the pair considered is: 2,3after reading 4 - the pair considered is: 3,4after reading 5 - the pair considered is: 4,5

after reading 0 - the end

implementation:

we need two variables: first el ofPair, second el ofPairpreparation to creating a new pair:

the second element of the “old”pair has to became the first elementof the “new” pair (first el ofPair=second el ofPair)the second element of the pair is read from the input

one need to avoid considering the pair consisting of a random value and

the first number given

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

some elements of the scheme:

notice that we read single numbers, not pairs of numbers

pairs we consider “mesh together”:

after reading 1 - no pair exists yetafter reading 2 - the pair considered is: 1,2after reading 3 -the pair considered is: 2,3after reading 4 - the pair considered is: 3,4after reading 5 - the pair considered is: 4,5

after reading 0 - the end

implementation:

we need two variables: first el ofPair, second el ofPairpreparation to creating a new pair:

the second element of the “old”pair has to became the first elementof the “new” pair (first el ofPair=second el ofPair)

the second element of the pair is read from the input

one need to avoid considering the pair consisting of a random value and

the first number given

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

some elements of the scheme:

notice that we read single numbers, not pairs of numbers

pairs we consider “mesh together”:

after reading 1 - no pair exists yetafter reading 2 - the pair considered is: 1,2after reading 3 -the pair considered is: 2,3after reading 4 - the pair considered is: 3,4after reading 5 - the pair considered is: 4,5

after reading 0 - the end

implementation:

we need two variables: first el ofPair, second el ofPairpreparation to creating a new pair:

the second element of the “old”pair has to became the first elementof the “new” pair (first el ofPair=second el ofPair)the second element of the pair is read from the input

one need to avoid considering the pair consisting of a random value and

the first number given

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

some elements of the scheme:

notice that we read single numbers, not pairs of numbers

pairs we consider “mesh together”:

after reading 1 - no pair exists yetafter reading 2 - the pair considered is: 1,2after reading 3 -the pair considered is: 2,3after reading 4 - the pair considered is: 3,4after reading 5 - the pair considered is: 4,5

after reading 0 - the end

implementation:

we need two variables: first el ofPair, second el ofPairpreparation to creating a new pair:

the second element of the “old”pair has to became the first elementof the “new” pair (first el ofPair=second el ofPair)the second element of the pair is read from the input

one need to avoid considering the pair consisting of a random value and

the first number given

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Example (exercise 4 - version 1)

#include <iostream >using namespace std;int main(){int number1=0, number2 =0; /* assignment to avoidprinting junk pair

zero is the only possible value hereas it is never assigned in the program */while (true){

number1=number2;cout << "give a number : "; cin >> number2;if (number2 ==0) break;if (number1 !=0) // junk pair is not printedif (number1 <number2)cout << number1 << " " << number2 << endl;

}return 0; }

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Loops - exercises

Example (exercise 4 - version 2)

#include <iostream >using namespace std;int main(){

int number1 , number2 , counter;counter = 0;while (true){number1=number2;cout << "give a number : "; cin >> number2;if (number2 ==0) break;counter ++; // we count numbersif (counter > =2) //if there were at least2, the pair is not a junk one

if (number1 <number2)cout << number1 << " " << number2 <<endl;

}return 0;}

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming