•

Check out our Community Blogs

Register and join over 40,000 other developers!

### Recent Blog Entries

• phi

I love this community !

• JackJames

hi i am jack i am seo expert jack james would love you to read new post

# [SOLVED] ASCII. Advice for a Student

ascii

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

### #1 Chanito

Chanito

CC Newcomer

• Member
• 14 posts

Posted 17 January 2013 - 10:19 AM

Hey! I have an assignment that i need to understand a bit better.

The assignment is as follows:

Write a program that asks for the number of rows and plots the figure below, depending on the integer you have entered. The question of the number of rows to be repeated after printing to new character to be drawn. If you enter 0 as the number of rows then the program ends.

Examples from running:

Enter the number of lines (enter 0 to exit): 3
*
**
***

Enter the number of lines (enter 0 to exit): 6
*
**
***
****
*****
******

Enter the number of lines (enter 0 to exit): 10
*
**
***
****
*****
******
*******
********
*********
**********

I need advice on how to achieve a solution to this problem, i dont want the entire code, maby just some Pseudocode to get me started so i can solve this myself?

The problem im having right now is that when i assign "Char a = 42; and multiply it by 1, it just jumps to the next ASCII character and it doesen't add a similar *.
Should i even use ASCII to solve this? or is there a better way?

Any advice to get me started is much appreciated

Thanks!

### #2 speculatius

speculatius

• 106 posts

Posted 17 January 2013 - 10:33 AM

Hi Chanito,

you dont have to use ASCII codes at all. You need to use two nested cycles. Outer cycle "iterates" over lines, inner cycle prints stars.

```for(int i = 0; i < n; ++i) {
for(int j = 0; j < i; ++ j) {
putchar('*');
}
putchar('\n');
}
```

Edited by speculatius, 17 January 2013 - 10:35 AM.

### #3 Chanito

Chanito

CC Newcomer

• Member
• 14 posts

Posted 17 January 2013 - 10:42 AM

Thank you speculatius,
I will start trying to solve this now, thanks for not saying to much!

Edited by Chanito, 17 January 2013 - 10:49 AM.

### #4 Chanito

Chanito

CC Newcomer

• Member
• 14 posts

Posted 17 January 2013 - 12:23 PM

```#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int n, i = 0, j = 0;
cout << "Input number of rows (ENTER 0 to exit) " << endl;
cin >> n;

for(int i = -1; i < n; ++i){

for(int j = -1; j < i; ++j)
putchar('*');
putchar('\n');
}
if (n <= 0){
return 0;
}
if (i == j){
main();
}
getch();
return 0;
}
```

So this is what i've come up with so far.
My question is this, why do i have to use "-1" for the program to display 5 rows when i enter 5, if i input "0" it only displays 4 rows.

My other problem is that when i "Input 0" the program is supposed to exit immediatly, right now i have to input "0" and then press a button again to exit it.

Thanks!

### #5 BlackRabbit

BlackRabbit

CodeCall Legend

• Expert Member
• 3871 posts

Posted 17 January 2013 - 12:47 PM

About the number of rows, I would suggest you do two changes in the for statement. 1. replace -1 with 0 and second replace ++j and ++i for j++ and i++, that will do the trick.
The extra button press is due to the getch() at the end, remove or comment it and the issue will be solved.

you don't need the ifs either.

### #6 object

object

CC Regular

• Member
• 35 posts

Posted 17 January 2013 - 08:04 PM

So long as we're spoonfeeding, I propose that we spoonfeed the least horrible substance.
```void write_star_pyramid(size_t height) {
size_t width = height;
char stars[width];
memset(stars, '*', width);
while (width > 0) {
fwrite(stars, 1, width--, stdout);
putchar('\n');
}
}```

Edited by object, 17 January 2013 - 08:10 PM.

### #7 speculatius

speculatius

• 106 posts

Posted 17 January 2013 - 11:42 PM

Hi Chanito,

you are missing opening bracket in second cycle

```for(int j = -1; j < i; ++ j) {
```

After correcting this, you should be able to iterate from 0 instead of -1. Also move new line (putchar('\n')) to outer cycle. And the if statements are not needed.

@BlackRabbit: It is possible to use prefix increment with no side effect. I like it more than postfix, because postfix suppose that I want to use the previous value of variable (and I dont in that case).

EDIT: Oh I see. Forget about the first advice with missing bracket This should work... Notice the <= operator.

```for(i = 0; i < n; ++ i) {
for(j = 0; j <= i; ++ j) {
putchar('*');
}
putchar('\n');
}
```

Edited by speculatius, 17 January 2013 - 11:51 PM.

### #8 object

object

CC Regular

• Member
• 35 posts

Posted 18 January 2013 - 03:31 AM

@BlackRabbit: It is possible to use prefix increment with no side effect. I like it more than postfix, because postfix suppose that I want to use the previous value of variable (and I dont in that case).

According to the standard, any operator that changes the value of an object produces a side-effect. You seem to be confusing the prefix and postfix operators. The prefix operators go before their operand, while the postfix operators go after their operand. For example, the ++ in ++x is a prefix operator, while the ++ in x++ is a postfix operator. Technically, it doesn't matter which of these you use in the context of the "expression-3" of for loops, because that expression is evaluated as a void expression for it's side-effects alone. Here's an example which actually makes use of the behavioural difference of the prefix versus postfix:
```void print_star_pyramid(size_t n) {
size_t x = n;
if (n == 0) { return; }
do {
if (x-- > 0) { putchar('*'); continue; }
putchar('\n');
x = --n;
} while (x > 0);
}```

Edited by object, 18 January 2013 - 03:36 AM.

### #9 Chanito

Chanito

CC Newcomer

• Member
• 14 posts

Posted 19 January 2013 - 03:24 AM

Thanks you speculatius, that solved the problem!

### #10 BenW

BenW

CC Devotee

• Community Manager
• 976 posts

Posted 19 January 2013 - 08:20 PM

This topic has been marked as SOLVED. If you have a similar question or topic, you can go back to the subforum and start a new topic to continue discussions.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download