Chat with us, powered by LiveChat assignment c++ - Credence Writers
+1(978)310-4246 [email protected]

P5: Loop

1. Write programs using looping to print the following patterns:
Your program ask the user to enter the size of a pattern from keyboard.
NOTE that you will earn extra points if you work on rectangular patters instead of square patterns. See some samples 
here

(1) A square of zeros of any size;

Enter the size of the square:

16

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Press any key to continue . . .

(2) A square of zeros of any size with 1’s on the first diagonal;

Enter the size of the square:

20

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1

Press any key to continue . . .

(3) A square of zeros of any size with 1’s on the second diagonal;

Enter the size of the square:

18

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Press any key to continue . . .

(4) A square of zeros of any size with 1’s on both diagonals;

Enter the size of the square:

20

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1

0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0

0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0

0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0

0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0

0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0

0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0

0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0

0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0

0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0

0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0

0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0

0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1

Press any key to continue . . .

(5) A rectangular flag.

Enter the width and height of a rectangle:

20 10

* * * * * * * * * * – – – – – – – – – –

* * * * * * * * * * – – – – – – – – – –

* * * * * * * * * * – – – – – – – – – –

* * * * * * * * * * – – – – – – – – – –

* * * * * * * * * * – – – – – – – – – –

– – – – – – – – – – * * * * * * * * * *

– – – – – – – – – – * * * * * * * * * *

– – – – – – – – – – * * * * * * * * * *

– – – – – – – – – – * * * * * * * * * *

– – – – – – – – – – * * * * * * * * * *

Enter the width and height of a rectangle:

30 10

* * * * * * * * * * * * * * * – – – – – – – – – – – – – – –

* * * * * * * * * * * * * * * – – – – – – – – – – – – – – –

* * * * * * * * * * * * * * * – – – – – – – – – – – – – – –

* * * * * * * * * * * * * * * – – – – – – – – – – – – – – –

* * * * * * * * * * * * * * * – – – – – – – – – – – – – – –

– – – – – – – – – – – – – – – * * * * * * * * * * * * * * *

– – – – – – – – – – – – – – – * * * * * * * * * * * * * * *

– – – – – – – – – – – – – – – * * * * * * * * * * * * * * *

– – – – – – – – – – – – – – – * * * * * * * * * * * * * * *

– – – – – – – – – – – – – – – * * * * * * * * * * * * * * *

2. Write a program using loops to print out counting numbers in a triangle pattern with the height chosen by the user (the height < 13). Hint: Use setw(3) to align the numbers vertically.

3. Enter the height of the triangle:

4. 6

5. 1

6. 2 3

7. 4 5 6

8. 7 8 9 10

9. 11 12 13 14 15

10. 16 17 18 19 20 21

11. Press any key to continue . . .

12.

13. Enter the height of the triangle:

14. 12

15. 1

16. 2 3

17. 4 5 6

18. 7 8 9 10

19. 11 12 13 14 15

20. 16 17 18 19 20 21

21. 22 23 24 25 26 27 28

22. 29 30 31 32 33 34 35 36

23. 37 38 39 40 41 42 43 44 45

24. 46 47 48 49 50 51 52 53 54 55

25. 56 57 58 59 60 61 62 63 64 65 66

26. 67 68 69 70 71 72 73 74 75 76 77 78

27. Press any key to continue . . .

28. Write programs using looping to find out
(1). The largest and second largest integers from a data file containing integers:

29. 8 7 19 14 21 22 6 19 45 8

30. 36 7 43 42 9 30 11 45 45 26

31. 14 45 45 6 30 45 12 45 31 47

32. 3 40 18 20 12 48 22 33 26 27

Hint: To find the second largest number in a data file, try the following steps: Read the first number from the file. Set the first number in the file as both the largest and second largest numbers initially on the assumption that the largest and second largest are two distinct numbers in the file. Then use a loop to read numbers one by one from the file. If a number read from the file is larger than the current largest number, then update the largest with the new number and the “previous” largest becomes the new second largest. Otherwise, check the newly read number from the file is larger than the second largest, if so, update the second largest number with the number.
(2). The sum of the integers from the same file.

//Sample skeleton code

int main()

{

//Declare an ifstream variable, say, inData

//Open a text file containing those integers

//Read the first integer into a variable, say, num

//inData >> num;

//while(inData)

//{

//Find the largest integer

//Find the sum of the integers

//}

//Print out the largest integer and sum.

return 0;

}

33. At the top section of your program you need to use C++ comments to include your name, class name, programming assignment name, and date the program was written.

34. Your programs should be properly formatted using indentation and empty lines (spaces). Use “camel” notation for variable naming.

35. Upon completion, save all programs and the required output into a single text file using notepad (or notepad++). The file should be named in this format: P5_YourName.txt, for example, P5_JohnDoe.txt. Then submit to the Blackboard (Bb) as attachment by the deadline. The submission link on the Bb will be closed automatically after the deadline. Assignments that fail to follow the instructions will NOT be graded.

error: Content is protected !!