Jump to content

Loops

- - - - -

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

#1
ksample23

ksample23

    Newbie

  • Members
  • Pip
  • 3 posts
Im trying to write a program that will ask the user to input a character and an integer.

Once it has read the number and sybmol, i need it to output in a pattern like this:

Example:
user inputs *
user inputs 5

//I need it to print the 5 stars then go to the next line and print 5 spaces and the 5 stars. And keep going up until there are 5 lines. So on line 3 there would be 10 spaces and then the 5 stars.

// so it would look like this:

*****
(5 spaces)*****
(10 spaces)*****
(15 spaces)*****
(20 spaces)*****

Here what I got so far:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int j;/*row*/
int i;
int m;
int N;/*Positive integer*/
char character = ' ';/*Symbol from user*/

printf("Enter a character representing the symbol to display: ");
scanf("%c", &character);
printf("Enter a value: ");
scanf("%d", &N);

for(j=1; j<=N; j++){
for(i=1; i<=N; i++){
printf("%c", character);
if(i%N == 0)
printf("\n");
}
for(m=1; m<=N; m++){
printf(" ");
}
}
system("PAUSE");
return 0;
}


Any help?

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
What's your problem? (And please edit your post to wrap code tags around your code. Highlight it and click the # button.)

A couple of things:
int N;/*Positive integer*/
Change it to unsigned int and use %u instead of %d. Less chance of an error there.

printf("%c", character);
...
printf(" ");
I'd suggest putchar(character) and putchar(' ') instead.

system("PAUSE");
Mmm...I don't condone this. My teacher would've docked me points for this, don't know about yours. I suggest you use this instead:
printf("Press any key...");
getchar();
A system call has way too much overhead for what it does, is vulnerable to attack, and won't always work, especially for more complicated commands.
sudo rm -rf /

#3
ksample23

ksample23

    Newbie

  • Members
  • Pip
  • 3 posts
Thanks for your help, but those didn't fix my problem, maybe I didn't give a good explanation, sry :( But I do notice what your saying about those issues there and I changed alot of them.

Here I will try again.

the program is suppose to ask the user for a character and the program will take that character and print a pattern on the screen. After, it reads a positive integer (x) and displays (x) lines of that symbol. Each line will have (x) symbols.

The first line should just have (x) symbols.
The second line should first have (x) spaces and then display the (x) amount of symbols.
The third line with skip 2 * (x) spaces and then display the symbols. This continues until the (x)th line is reached.
So it should look like stars. :)

My program so far will print the (x) amount of symbols and (x) amount of lines but I can't get the spacing right.
So mine looks like this:

Character input: &
Value: 5

&&&&&
(5 spaces)&&&&&
(5 spaces)&&&&&
(5 spaces)&&&&&
(5 spaces)&&&&&

Im having trouble sliding the 3rd line over 5 more spaces, the 4th 10 more spaces, and the 5th 15 more spaces...

Hope this helps make more clear.

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
for(j=1; j<=N; j++){
    for(i=1; i<=N; i++){
        printf("%c", character);
        if(i%N == 0)
            printf("\n");
    }
    for(m=1; [COLOR="red"]m<=N;[/COLOR] m++){
        printf(" ");
    }
}
Try m <= N*j.
sudo rm -rf /

#5
ksample23

ksample23

    Newbie

  • Members
  • Pip
  • 3 posts
Thanks :) that did it. I can't believe it was something that simple. I knew it was right in front of me.

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
We all do that. Especially me. :D
sudo rm -rf /