trouble with homework completely destroyed

So I have gotten a new homework assignment and I have been working on it non stop for 4 days now. I have read the book and watched videos but im having trouble knowing what to do. Could someone walk me through it and just explain how to do the step? Please help me out


Page 1
CPSC 1375 Programming I (Block letters via functions 4 procedures) This exercise is designed to give you practice in input/output in C++, to introduce the data type char, and also to introduce you to using functions and procedures (that is, void functions) to modularize your code. Firstly, write a function void line (char ch , int num) which outputs the character ch num times in a row on a given line. For example, line( 'A' ,10) should output: AAAAAAAAAA Secondly, write a function void rectangle (char ch , int x , int y) which writes the character ch in a rectangular pattern of x rows and y columns. For example, rectangle('X',3,5) should output: XXXXXXXXXXXXXXX [593$ Function "rectangle" should invoke function "line" to accomplish its job ll Thirdly, write a main program body to read Qne character at a time from the data file "animals.dat" , each record of which is the name of an animal. Your program should produce a rectangular pattern for each letter in the animal's name: the size of the rectangular pattern depends on the letter and on its position in the animal name, the rule being: if the x th letter in the name is the y th letter of the alphabet, the rectangle should be size 1-: by y. E9; example, if the inputted animal name is CAT, your program should produce the output pattern: CCC { l by 3, since the 1 st letter in CAT is the 3 rd letter of the alphabet } A A { 2 by 1, since the 2 nd letter in CAT is the 1 st letter of the alphabet} TTTTTTTTTTTTTTTTTTTT TTTTTTTTTTTTTTTTTTTT 'I'TTT'I‘TT'I‘TTTTTTTTTTT'I' { 3 by 20, since the 3 rd letter in CAT is the 20 tb letter of the alphabet} (continued on back)
Page 2
page 2 Note that you should skip one line between each block letter pattern, to separate them. You should also skip sgleral lines between animal names, to separate each animal name from the next one. Programming notes: (1) . Of course, your main program should call your procedure"rectangle" to produce the letter patterns! (2) . This program's structure is really a "single control break" one. You should read a character (while not EOF) and react appropriately depending on whether or not the character read is an end-of-record symbol '\n' . You will, of course, have to keep track of where in the particular animal name you are! (3) . Unlike most computer languages, C++ will let you dogitmgtig with characters. This is particularly useful here,since you will want to somehow "convert" the character 'A' to the number 1, the character '3' to the number 2, etc. (For simplicity, you can assume all letters in the data file are capital.
1.)You'll write a void function "line" which takes one character and one integer parameter.

void line(char ch, int num);

This function should print the character
ch
num
times.

If I call:
line('A', 3);

The output should be:
AAA



2.)You'll write a void function "rectangle" which takes one character and two integer parameters.

void rectangle(char ch, int x, int y);

This function should print a rectangle composed of the character
ch
. The x and y integer parameters define the dimensions of the rectangle.
In addition, it should do all this by calling the line() function several times.

3.) You'll want to have your main() function open a file. This file should be named "animals.dat", which contains a list of animal names. You'll open the file, and read one character at a time. For each character extracted from the file, print a rectangle.

Each rectangle:
a.) Is composed of the letter that's currently being handled.
b.) Has a height that's equal to this letter's position in the animal name.
c.) Has a width that's equal to this letter's position in the alphabet.

Example:

DOG



DDDD


OOOOOOOOOOOOOOO
OOOOOOOOOOOOOOO


GGGGGGG
GGGGGGG
GGGGGGG

Last edited on
how do you pull out only one character at a time from the file. Not an entire word?
Use std::cin.get() to extract a single character.
is that using the namspace std; or is that how you type it no matter what. That might not have made any sense
ok so i have line.
1
2
3
4
5
6
void line(char ch, int num)
{ int i; //local variable 
	for(i=1;i<=num;i++)
	cout<<ch;
	cout<<endl;
}

but im having trouble with rectangle any ideas?
void rectangle(char ch, int x, int y)

make a for loop that iterates up to the height of the rectangle, then call line() inside it
owenkmc wrote:
is that using the namspace std; or is that how you type it no matter what. That might not have made any sense

No, that is how you type it 'no matter what'. As a general rule, I feel that using namespace std; should be avoided, as it can create problems. I feel its better to just get used to it. In fact, I think I read somewhere that using namespace was only added in to ease porting from pre-standard C++ programs (i.e. before namespaces, in the days of <iostream.h> and the like).

EDIT:
isocpp wrote:
The using-directive exists for legacy C++ code and to ease the transition to namespaces, but you probably shouldn’t use it on a regular basis, at least not in your new C++ code.

See http://isocpp.org/wiki/faq/coding-standards#using-namespace-std
Last edited on
Topic archived. No new replies allowed.