Lecture Note
University
Virtual High SchoolCourse
ICS2O | Introduction To Computer StudiesPages
2
Academic year
2023
anon
Views
22
ICS2O Grade 10 Computer Science – Printing “Triangle” Letters Full source available below. This note addresses a typical query from newcomers to computer science: “Write a program to print out a word in a triangle of different sorts” I’ve created C++ code to print out an input phrase in various ways. C++ code can easily be ported over to Java, VBA, or Python code. 1) Print in left right angle NNONOTNOTENOTES Code: void leftAngle(string phrase) { for (int x = 0; x < phrase.length()+1; ++x) { cout << phrase[y]; } cout << endl; }} 2) Print in right right angle S ES TES OTESNOTES Code: void rightAngle(string phrase) { int spaceLength = (int)phrase.length(); int wordIndex = (int)phrase.length(); for (int x = 0; x < phrase.length(); ++x) { for (int y = spaceLength; y > 0; --y) { cout << " "; } for (int y = wordIndex; y < phrase.length(); ++y) { cout << phrase[y]; }
--spaceLength; --wordLength; cout << endl; }} 3) Print Equilateral Triangle N N O N O T N O T EN O T E S Code void equilateralAngle(string phrase) { int numberOfSpaces = (int)phrase.length()-1; int wordLength = 1; for (int x = 0; x < phrase.length(); ++x) { for (int y = 0; y < numberOfSpaces; ++y) { cout << " "; for (int y = 0; y < wordLength; ++y) { cout << phrase[y] << " "; } cout << endl; ++wordLength; --numberOfSpaces; } }
ICS2O Grade 10 Computer Science – Printing “Triangle” Letters
Please or to post comments