format problem


 
Thread Tools Search this Thread
Top Forums Programming format problem
# 1  
Old 10-07-2006
format problem

hey everyone, Im making a program that prints different patterns using *
like this:
*
***
*****
*******
*****
***
*

i am letting the user decide what the number of rows can be, i want the shape to appear on the left (60 spaces from the right) if it is even and on the right if they choose an odd number. Any suggestions on how to get them to print in that format?

thanks

Last edited by bebop1111116; 10-08-2006 at 01:28 AM..
# 2  
Old 10-08-2006
this seems to be from a question/exercise in a book.
What have you got so far?
# 3  
Old 10-08-2006
Well, my program is set up so that after the user enters in a number then it goes through an if statement. The If statement correctly display the right number of stars per line, but right now now matter if the number is even or else the number is odd it only displays on the right side of the page. Also the stars all display in a straight line against the right. I want to for a dimond shape.

inside the if else statement i have a for loop in both the if and the else. which is what gets the *'s displayed.
# 4  
Old 10-09-2006
Post ur code.
# 5  
Old 10-09-2006
heres what i have.

#include <stdio.h>
int main()
{
int r, rs, astr;
rs=2; /*i am hardcoding the rs value here*/

if(rs/2%0){
/*here i want to have it right aligned*/
for(r=1, r<=rs, r++){
for(astr=1, astr<=rs, astr++)
printf("*");}

else
/*here i want to have it left aligned*/
for(r=1, r<=rs, r++){
for(astr=1, astr<=rs, astr++)
printf("*");}

}
return EXIT_SUCCESS;
}
# 6  
Old 10-10-2006
code tags for code please. like {code} stuff {/code} except with [ ] instead of { }. And align your code so people can read it.
Code:
#include <stdio.h>
int main()
{
  int r, rs, astr;
  rs=2; /*i am hardcoding the rs value here*/

  if(rs/2%0)
  {
    /*here i want to have it right aligned*/
    for(r=1, r<=rs, r++){
    for(astr=1, astr<=rs, astr++)
      printf("*");
  }
  else
  /*here i want to have it left aligned*/
  for(r=1, r<=rs, r++)
  {
    for(astr=1, astr<=rs, astr++)
      printf("*");
  }
}
  return EXIT_SUCCESS;
}

Aligning it reveals you have too many brackets.

To right-align your output, print spaces in front of things. The number of spaces will be inversely proportional to the number of asterisks you need to print.

You can simplify the if(rs/2%0) thing into if(rs&1)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date Format Problem

I have written a code in Linux environment which compares two dates and print "correct" if 1st date is less than 2nd date. But when I'm running the same code in SunOS environment, "date -d is an illegal format" is the error it is throwing. How different should my code be so that it executes well in... (18 Replies)
Discussion started by: Chandan_Bose
18 Replies

2. Shell Programming and Scripting

Problem with numbers in exponential format

Hi I have a shell scribt with some numbers in exponential format, for example, "1.23456789E +01" Now I would like to bring these numbers into a format without the E. Can someone help me Thanks Flo ---------- Post updated at 10:07 AM ---------- Previous update was at 09:14 AM... (1 Reply)
Discussion started by: sbfly
1 Replies

3. Shell Programming and Scripting

Problem with format numbers

Hello Everyone! I hope you can help me!! I have this little problem: I executed oracle query and the output of the result are in a text file called "DATAFILE.txt", and the value of file is: 97.37 Well, the script compare the result in text file with a condition: ... (5 Replies)
Discussion started by: bobbasystem
5 Replies

4. Shell Programming and Scripting

Format problem

can any one know that how can i get perfect format of phone number my function is phone_number=`echo $name_number | awk '{print $NF}'` check=`echo $phone_number | egrep -c "--"` i get the format but if i need perfect format like xxx-xxx-xxxx. if i enter not in this format it still... (1 Reply)
Discussion started by: tarunsavalia
1 Replies

5. Shell Programming and Scripting

having problem in date format

if ( $opt{ww} ) { my $week = $opt{ww} ? $opt{ww} : $lot_list->{cal}->getWW(); print "\nStarting at $week\n"; while ( $week > 200430 ) { print localtime() . ": Working on $week\n"; my ($start,$end) = $lot_list->getWeekDates($week); print localtime() . ":... (3 Replies)
Discussion started by: vahnski
3 Replies

6. Shell Programming and Scripting

change format problem

Someone can help for the following fromat change can done by script? i.e. insert the perivous colurm's first variable if below line don't have the varaible start from AXXX.... From A123,B123,C123 B123,C123 A234,B234,C234 A345,B345,C345 B345,C34 B345,C345 B345,C345 A456,B456,C456 ... (2 Replies)
Discussion started by: bleach8578
2 Replies

7. Shell Programming and Scripting

mail format problem:

i need to send 2 diff mail in a single script:Below the script #!/bin/ksh echo "enter the number" read num if then mail -s "test" unix@yahoo.com mail -s "test" 9886767000@nma.vodafone.in,9916138003@nma.vodafone.in else echo "test again" fi... (5 Replies)
Discussion started by: ali560045
5 Replies

8. Shell Programming and Scripting

CSV format problem

studentNo=$1 tmp=/tmp/acc2courses$ID.$$ grep -w "$studentNo" CSE1001 >/dev/null 2>&1 && echo CSE1001 >>$tmp grep -w "$studentNo" CSE1002 >/dev/null 2>&1 && echo CSE1002 >>$tmp grep -w "$studentNo" CSE9001 >/dev/null 2>&1 && echo CSE9001 >>$tmp This is my code, The output in tmp file... (10 Replies)
Discussion started by: ccwq
10 Replies

9. UNIX for Dummies Questions & Answers

serial no. format problem

Hello guyz.. just want to know whether my doubt can be solve or not..maybe it my company using yyyymmddxx for serial no format..and it cause problems.. first, my adjustments for moving n deleting domain name exceeded xxx per day, and it will cause and effect the yyyymmdd digits. can... (6 Replies)
Discussion started by: unknown2205
6 Replies

10. Programming

Format Problem with vim

Hi, there. Everyone had a great Thanksgiving? I have a slice format problem when I am using vim to edit c++ source code. I don't know how to discribe the problem exactly. You see, usually when I use the command "vi hello.cpp" to write a code, it will have the format like this: #include... (4 Replies)
Discussion started by: HOUSCOUS
4 Replies
Login or Register to Ask a Question