Author |
Topic: My homework (Read 1133 times) |
|
hanzack
Forum Senior Member
lalala...I'm singing a song of arimaa.
Gender:
Posts: 35
|
|
My homework
« on: Jun 11th, 2010, 4:40am » |
Quote Modify
|
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
|
|
IP Logged |
|
|
|
ocmiente
Forum Guru
Arimaa player #3996
Gender:
Posts: 194
|
|
Re: My homework
« Reply #1 on: Jun 11th, 2010, 8:29am » |
Quote Modify
|
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. 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!
|
« Last Edit: Jun 11th, 2010, 8:56am by ocmiente » |
IP Logged |
|
|
|
FireBorn
Forum Guru
Arimaa player #1832
Gender:
Posts: 123
|
|
Re: My homework
« Reply #2 on: Jun 11th, 2010, 9:58am » |
Quote Modify
|
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.
|
« Last Edit: Jun 11th, 2010, 9:59am by FireBorn » |
IP Logged |
|
|
|
hanzack
Forum Senior Member
lalala...I'm singing a song of arimaa.
Gender:
Posts: 35
|
|
Re: My homework
« Reply #3 on: Jun 16th, 2010, 6:52am » |
Quote Modify
|
Thank you very much! Ocmiente and FireBorn! I made it!
|
|
IP Logged |
|
|
|
|