Arimaa Forum (http://arimaa.com/arimaa/forum/cgi/YaBB.cgi)
Arimaa >> Off Topic Discussion >> My homework
(Message started by: hanzack on Jun 11th, 2010, 4:40am)

Title: My homework
Post by hanzack on Jun 11th, 2010, 4:40am
Please tell me about programing. help meee!  :'(
I want to plot next programmings on emacs.

1,  when you input a positive number from your keyboard,
making the program plot only multipuls of 3 in numbers from 0 to the number you input

2,  making the program add an indentation every time it plots 5 numbers

3,  making the program plot the sum total of the plotted numbers.

and I wrote like this

#include <stdio.h>
int main()
{  int x, y = 0;  
   printf("x ? ");
   scanf("%d", &x);
   if(x < 0){
   printf("Input positive number");
    }else{
   while(3 * y <= x){
    printf("%3d ", 3 * y); y++, x--;
  }
   return 0;
}  

when i inputted the number "12" , it plotted this

"x ? 15
 0  3  6  9 12 15"


awww help meee!!! awawawa :-/

Title: Re: My homework
Post by ocmiente on Jun 11th, 2010, 8:29am
Since the topic is "My Homework", I'm guessing this is a homework assignment.  So, I won't give you the answer, but will try to give a couple of ideas.

1.  Include error checking.  Make sure the user can't enter a negative number. <EDIT> I reread your post and see you already have that.
2.  Instead of a while loop, consider a for loop.  with for loops, you can do something like this:

Code:
int counter;
for(counter = 0; counter <= num; counter += 2)
{
/* do something */
}

That would do something with the numbers from 0 to num, incrementing by 2 rather than 3
3.  for part 2 you could add another counter, increment by 1 in the loop, and each time that new counter % 5 == 0, print out a tab or something
OR... you could use some math and use the original for loop counter % 15 to determine when to print out the tab.  The math I'm suggesting here is not complete - it is homework after all.  However, if I were the instructor, I'd probably like the mathematical solution better - depends no your instructor, I suppose.
4.  For part 3, as for part 2 you could declare another variable and add the for loop counter value to it each time through the loop, then print that value at the end.  
OR... you could probably use something like the mathematical formula for summing numbers from 1 to n (http://mathforum.org/library/drmath/view/57919.html).  and maybe multiply that by 3... not sure since I haven't worked through the math on that one... but it seems like something like that would work.  

Best of luck with that!

Title: Re: My homework
Post by FireBorn on Jun 11th, 2010, 9:58am
1. Yeah, use a for loop, like ocmiente said. And get rid of the x--, which I don't understand what it's for and doesn't seem to be executing anyway.

2. For the tab thing, the character is "\t", FYI, and to do it you could declare another variable and use it to count to 5 in the loop, and when it gets to 5 output "\t" and then reset the counter to 0 and count up again.

3. For the sum total, use an accumulator (a variable that holds a running sum) in the for loop and keep adding the y to it (use the "+=" operator).

Make sure you declare those variables outside of the loop though or it might cause problems.

Title: Re: My homework
Post by hanzack on Jun 16th, 2010, 6:52am
Thank you very much! Ocmiente and FireBorn! I made it! :-*



Arimaa Forum » Powered by YaBB 1 Gold - SP 1.3.1!
YaBB © 2000-2003. All Rights Reserved.