views:

313

answers:

1

Okay, so I need to have the output look like a diamond of asterisks, with each row increasing by 2 asterisks until the middle has 9 asterisks...and then the rows decrease. function main has to be:

int main (void){  
   int i;    
   i = 0;  
   while (i < 5){  
      printline (4-i, i*2+1);  
      i = i + 1;  
   }  
   i = 3;  
   while (i >= 0) {  
      printline (4-i, i*2+1);  
      i = i - 1;  
   }  
   return 0;  
}

Now I am supposed to define function printline to print a single line of the figure each time it is called. It takes 2 arguments, the number of spaces and the number of asterisks that should be printed on the line. Use counter controlled repetition to print the appropriate number of spaces and again to print the appropriate number of asterisks.

char print_line (int spaces, int stars){  
   for (int i = 4; i>=spaces; i--){  
      printf(" ");  
   }  
   for (int i = 1; i<=stars; i+=2){  
      printf("*");  
   }  
   printf("\n");  
}

Woohoo! I'm almost done! The outputs kinda right, except that instead of a line with 4 spaces with a star, a line with 3 spaces with 3 stars, and so on up to no spaces and 9 stars, (and then reverses)...I get a line with a star, a line with a space and 2 stars, a line with two spaces three stars, etc, up to 4 spaces five stars (and then reverses)...
The reason printline is different is because I have problems writing with this sometimes and I kept on getting italics whenever I tried to write it...

+5  A: 

Some hints:

  • Your function need only print characters, it does not return anything (there's a specific return type for this)
  • The number of spaces and the number of stars to print are probably integers
  • You can print a single space using printf(" ");
  • You can print a single star using printf("*");
  • You can print a "newline" (which goes to the next line) using printf("\n");
  • If you say int i=0; for(i=0; i<n; i++) { printf("X"); } you will print the letter X a total of n times (you may not have learned for loops at this point; if not, use the next hint)
  • If you say int i=0; while(i<n) { printf("X"); i++; } you will also print the letter X a total of n times
  • You will be much happier if you use parameter names other than a and b. Try to think of a name that corresponds with what the parameter represents.

Response to your edit:

  • You will want to use two separate loops; one that prints the spaces, and then one that prints the stars
  • for loops are always constructed with one initializer, one check condition, and one step/increment
  • You should not need to assign values to stars or spaces; they are numbers given to you by whoever calls your printline function (i.e. they already have a value)

Response to your second edit:

  • In your for loops, you probably want to use an index variable other than the parameter being passed in (e.g. for(i=0; i<spaces; i++), where i is just a counter you declare at the top of your function, like in the code from the assignment) In your current construction, it will try to print 4 spaces, no matter what the caller specified when they called your function.
  • You only need to output the newline once, after you're done with all the spaces and stars (i.e. outputing the newline doesn't belong inside a for loop)
  • You probably only need to increment your counter by 1 each time you output a star.
  • If the assignment says to make a function called printline, you can't call it print_line; that isn't the same thing (neither is PrintLine)

Response to your third edit:

  • Don't forget "You probably only need to increment your counter by 1 each time you output a star."
  • Don't forget "Your function need only print characters, it does not return anything (there's a specific return type for this)" (i.e. it should not be returning a char)
  • I think you want the code that prints spaces to look more like the code that prints stars
Daniel LeCheminant
Why is the compiler saying that there are too many arguments to function printline?
Kaity
It will say this if you are passing too many parameters to the function called "printline". For example, in the example you gave above, printline has two formal parameters. If you called it like this "printline(1, 2, 3)" you would be calling it with too many parameters
1800 INFORMATION
The number of parameters you pass in has to equal the number of formal parameters
1800 INFORMATION
@Kaity: Make sure that your definition for printline has two arguments, and that when you call it, you give it two values!
Daniel LeCheminant
I know I sound pathetic...but I keep getting a block of stars that just go on repeating....
Kaity
@Kaity: Don't worry :] Consider editing your original post with your current code; it will be much easier for folks to get an idea of what is wrong...
Daniel LeCheminant
@Kaity: Also, if you are using a while block, make sure that the condition actually occurs (e.g. that you are incrementing the counter, and it will eventually cause the condition in the while to be false)
Daniel LeCheminant
I got it....finally lol! Thanks so much for your help, I know you probably had something better to do than take pity on me...but thanks so much!
Kaity
@Kaity: No problem. I was also a student finishing CS homework at 3am once ...
Daniel LeCheminant
@Daniel L. lol, I hate this class...I'm always on here getting help bc the teacher doesn't teach what we need for the program assignments...it's like he's off in his own little world! But for some reason since I'm a math major I have to have this class...
Kaity
A lot of pure math is done on computers these days you know
1800 INFORMATION