Hello, I just have a few questions. The first one be how would you print a pattern. I could use the if else, but I remember my teacher talking about something like -
for(i=1;i<=size;i)
-
printf($);
-
Code below will take the size of what I input and change the size of the pattern by adding more or less $
So if the user type number 6 it would print -
6 $ $ $ $ $
-
$ 6 $ $ $ $
-
$ $ 6 $ $ $
-
$ $ $ 6 $ $
-
$ $ $ $ 6 $
-
-
#include <stdio.h>
-
-
#define QUIT 5
-
-
// Prototype Definitions
-
void printMenu (void);
-
int getOption (void);
-
int getSize (void);
-
void printPattern( int size, int option );
-
void printOne ( int size );
-
void printTwo ( int size );
-
void printThree( int size );
-
void printFour ( int size );
-
-
int main (void)
-
{
-
// Local Definitions
-
int option;
-
int size;
-
-
// Statements
-
printf("\nBeginning of Program.\n");
-
-
option = getOption();
-
while (option != QUIT)
-
{
-
size = getSize();
-
printPattern( size, option );
-
option = getOption();
-
}
-
-
printf("\nEnd of Program.\n");
-
-
return 0;
-
}// main
-
-
/* ==================== printMenu ====================
-
This function prints a menu
-
Pre nothing
-
Post nothing
-
*/
-
void printMenu (void)
-
{
-
-
printf("\n*********************");
-
printf("\n* M E N U *");
-
printf("\n* *");
-
printf("\n* 1. Pattern One *");
-
printf("\n* 2. Pattern Two *");
-
printf("\n* 3. Pattern Three *");
-
printf("\n* 4. Pattern Four *");
-
printf("\n* 5. Quit *");
-
printf("\n* *");
-
printf("\n*********************");
-
-
return;
-
}
-
/* ==================== getOption ====================
-
This function shows a menu and reads the user option.
-
Pre nothing
-
Post returns a valid option
-
*/
-
int getOption (void)
-
{
-
// Local Definitions
-
int option;
-
-
// Statements
-
printMenu();
-
printf("\n\n\nPlease type your choice ");
-
printf("and press the return key : ");
-
scanf("%d", &option);
-
while (option < 1 || option > 5)
-
{
-
printf("\nInvalid option. Please re-enter.\n\n");
-
printMenu();
-
printf("\n\n\nPlease type your choice ");
-
printf("and press the return key : ");
-
scanf("%d", &option);
-
}
-
-
return option;
-
}// getOption
-
-
-
/* ==================== getSize ====================
-
This function gets the size of the pattern
-
Pre nothing
-
Post returns a valid size
-
*/
-
int getSize (void)
-
{
-
// Local Definitions
-
int size;
-
-
// Statements
-
printf("\n\n\nPlease type the size of the pattern [2 - 9]: ");
-
scanf("%d", &size);
-
while (size < 2 || size > 9)
-
{
-
printf("\nInvalid size. Please re-enter.\n");
-
scanf("%d", &size);
-
}
-
-
return size;
-
}// getSize
-
-
/* ==================== printPattern ====================
-
This function receives a pattern's size and the
-
user's option and prints the corresponding pattern.
-
Pre size
-
option
-
Post pattern printed
-
*/
-
void printPattern( int size, int option )
-
{
-
// Local Definitions
-
-
// Statements
-
switch (option)
-
{
-
case 1: printOne(size);
-
break;
-
case 2: printTwo(size);
-
break;
-
case 3: printThree(size);
-
break;
-
case 4: printFour(size);
-
break;
-
}
-
-
return;
-
}// printPattern
-
-
/* ==================== printOne ====================
-
This function receives the size of pattern one and
-
prints it. For instance, if size is 5, output would
-
be:
-
5 $ $ $ $
-
$ 5 $ $ $
-
$ $ 5 $ $
-
$ $ $ 5 $
-
$ $ $ $ 5
-
Pre size
-
Post pattern one printed
-
-
*/
-
void printOne ( int size )
-
{
-
// Local Definitions
-
-
// Statements
-
for(i=!;i<=n;i
-
printf("%d",size);
-
-
return ;
-
}// printOne
-
-
/* ==================== printTwo ====================
-
This function receives the size of pattern two and
-
prints it. For instance, if size is 3, output would
-
be:
-
-
-
(Took most of the code out)
Sorry if this is confusing. Not sure how else I can explain it.
11 2566
I didn't really look at the code but basically the logic would be:
(in non-specific pseudocode -
loop (i from 0 to size)
-
loop (j from 0 to size)
-
if( i = j)
-
print size;
-
else
-
print symbol
-
endif
-
endloop
-
print newline;
-
endloop
-
I think!!!
I didn't really look at the code but basically the logic would be:
(in non-specific pseudocode -
loop (i from 0 to size)
-
loop (j from 0 to size)
-
if( i = j)
-
print size;
-
else
-
print symbol
-
endif
-
endloop
-
print newline;
-
endloop
-
I think!!!
Hmm, how about if you wanted to use for
my code is non language specific...
so the loop statement could be replaced with something like -
for(int i = 0; i< size; i++)
-
{
-
doThings()
-
}
-
same goes for the rest.......
my code is non language specific...
so the loop statement could be replaced with something like -
for(int i = 0; i< size; i++)
-
{
-
doThings()
-
}
-
same goes for the rest.......
How would I control it so if I only wanted to hardcore that there only can print four $ only and no more no less.
I assunme you mean -
5 $ $ $ $
-
$ 5 $ $ $
-
$ $ 5 $ $
-
$ $ $ 5 $
-
$ $ $ $ 5
-
set size to 5 and inlcude the nested for loops.
If you want 4 $ signs without the 5 down the centre, try two nested loops (at size 4) without the if statement (ie remove "if i=j")
if you want just 4 in a row you can either print them literally:
printf(" $ $ $ $");
or for a single loop from 1 to 4......
-
/* ==================== printOne ====================
-
This function receives the size of pattern one and
-
prints it. For instance, if size is 5, output would
-
be:
-
5 $ $ $ $
-
$ 5 $ $ $
-
$ $ 5 $ $
-
$ $ $ 5 $
-
$ $ $ $ 5
-
Pre size
-
Post pattern one printed
-
-
*/
-
void printOne ( int size )
-
{
-
// Local Definitions
-
int i;
-
// Statements
-
printf("\n\n\t\t");
-
printf("%d",size);
-
for(int i = 1; i< size; i++)
-
{
-
printf(" $");
-
-
}// first line
-
-
-
printf("\n\t\t$ ");
-
printf("%d",size);
-
for(int i = 2; i< size; i++)
-
{
-
printf(" $");
-
-
}// second line
-
-
printf("\n\t\t$ $ ");
-
printf("%d",size);
-
for(int i = 3; i< size; i++)
-
{
-
printf(" $");
-
-
}// third line
-
-
printf("\n\t\t$ $ $ ");
-
printf("%d",size);
-
for(int i = 4; i< size; i++)
-
{
-
printf(" $");
-
-
}// fourth line
-
-
printf("\n\t\t$ $ $ $ ");
-
printf("%d",size);
-
for(int i = 5; i< size; i++)
-
{
-
printf(" $");
-
-
}// fifth line
-
-
printf("\n\n\n");
-
-
return ;
-
}// printOne
-
-
The result when I enter pattern 5, I get -
Please type the size of the pattern [2 - 9]: 5
-
-
-
5 $ $ $ $
-
$ 5 $ $ $
-
$ $ 5 $ $
-
$ $ $ 5 $
-
$ $ $ $ 5
-
-
-
However, if I push pattern 3 or 4 I get a messed up pattern. -
-
-
3 $ $
-
$ 3 $
-
$ $ 3
-
$ $ $ 3
-
$ $ $ $ 3
-
-
4 $ $ $
-
$ 4 $ $
-
$ $ 4 $
-
$ $ $ 4
-
$ $ $ $ 4
-
-
I know its the way I coded it, but how would you do it?
I'm thinking I would have to do something like this -
void printOne ( int size )
-
{
-
// Local Definitions
-
int i;
-
// Statements
-
-
printf("%d",size);
-
for(int i = 1; i< size; i++)
-
{
-
printf(" $");
-
printf("\n");
-
-
}// first line
-
Which would print.
5
$
$
$
$
Is the way I'm thinking correct? Thanks in advance.
pyut two gfor loops inside each other: -
for(int i=0; i< size i++)
-
{
-
for(int k=0; < size; k++)
-
{
-
//do your printing here, print size if i=k and $ otherwise
-
}
-
//print newline here
-
}
-
[quote]=DeMan]pyut two gfor loops inside each other: -
for(int i=0; i< size i++)
-
{
-
for(int k=0; < size; k++)
-
{
-
//do your printing here, print size if i=k and $ otherwise
-
}
-
//print newline here
-
}
-
[/QUOTE
Ahh, I see. Another question how would you put a number in there?
5 $ $ $ $
$ 5 $ $ $
$ $ 5 $ $
$ $ $ 5 $
$ $ $ $ 5
This is what I'm getting -
-
5 $5 $5 $5 $
-
5 $5 $5 $5 $
-
5 $5 $5 $5 $
-
5 $5 $5 $5 $
-
-
void printOne ( int size )
-
{
-
// Local Definitions
-
int i;
-
int k;
-
// Statements
-
-
-
for(int i=1; i<=4; i++)
-
{
-
for(int k=1; k< size; k++)
-
{
-
printf("%d",size);
-
printf(" $");
-
}
-
printf("\n");
-
}
-
return ;
-
As per the pseudocode originally posted, you need some sort of test....
We have different things tio print on different conditions...
if i=j we want to print the number, otherwise we can print the $ so -
if( i==k) //need to equals signs if we're testing
-
{
-
printf("%d", Size);
-
}
-
else
-
{
-
printf("$");
-
}
-
How about if I wanted
5 $ $ $ $
$ 5 $ $ $
$ $ 5 $ $
$ $ $ 5 $
$ $ $ $ 5
I figured I would change the ++ to -- and make the >=, but that doesn't seem to work for me.
I don't really understand how that is different to what we are doing (although I notice in your code you have a hard coded 4 where you may have meant Size -> 1st for loop)
Do you mean how to get the numbers to follow throyugh the other way?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Markus Elfring |
last post by:
Hello,
I try to use alternative delimiters for a regular expression.
When will it be supported?
www@mike:/home/www > /usr/local/bin/php -a
Interactive mode enabled
<?php...
|
by: David Horne |
last post by:
I forgot to mention in my other posting that the program is suppose to
figure out the minimum amount of different dollars and cents that
would be required for a given amount.
|
by: Spike |
last post by:
Hello!
Im going to make a javascript for changing alot of images. But im not
sure how to do it., where to start..
Ok, first.. this is the isue.
I have 3 images(I call them 1a-3a).
when u...
|
by: Sebastiao Pereira Filho |
last post by:
When I bind a server control to a repeater it renders one copy of this
control for each item in the datasource collection.
By doing this it changes the name of the dynamically created controls, to...
|
by: Petr Jakes |
last post by:
Hello,
I am trying to study/understand OOP principles using Python. I have
found following code http://tinyurl.com/a4zkn about FSM (finite state
machine) on this list, which looks quite useful for...
|
by: Gary Wessle |
last post by:
Hi
import string
import re
accumulator =
pattern = '(\S*)\s*(\S*)\s*(\S*)'
for each text file in dir
openfile and read into text
|
by: mirandacascade |
last post by:
O/S: Win2K
Vsn of Python: 2.4
Here is copy/paste from interactive window of pythonwin:
>>> x = "Joe's desk"
>>> y = 'Joe\x92s desk'
>>> type(x)
<type 'str'>
>>> type(y)
|
by: Andrew Savige |
last post by:
Python beginner here.
For a string 'ABBBCC', I want to produce a list .
That is, break the string into pieces based on change of character.
What's the best way to do this in Python?
Using...
|
by: zhangsonglovexiaoniuniu |
last post by:
Dear all,
I need you help.
here the program:
char a = 0x91;
printf("%x",a);
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |