Help writing a C program using for loop

October 14, 2010 at 11:58:28
Specs: Windows XP
My teacher assigned us a question on a quiz, graded it, and returned it, without giving us the correct answer or response. It is: Write a program using for loop that generates the following output: 1 10 2 9 3 8 4 7 5 6 6 5 7 4 8 3 9 2 10 1

I see that the pattern is subtracting even numbers and adding odds until you get to 0, and then it switches until you get back up to 9. This is what I have so far:

#include<stdio.h>

int main (void)
{
int i;
for( i=0; i<10;
{
printf("%d", i);

}
}

If you could please help that would be great!


See More: Help writing a C program using for loop

Report •


#1
October 14, 2010 at 12:39:22
I see that the pattern is subtracting even numbers and adding odds until you get to 0, and then it switches until you get back up to 9.
Actually, it looks like a count from 1 to 10, intermixed with a count from 10 to 1.

How To Ask Questions The Smart Way


Report •

#2
October 17, 2010 at 08:04:07
#include<stdio.h>
#include<conio.h>

main()
{
int i,j;
j=10;

for(i=1,j=10;i<=10,j>=1;j--,i++)

printf("%d %d ", i,j);


getch();
}

Here's a code that will show you your output given by your teacher using FOR loop.


Report •

#3
October 18, 2010 at 10:25:01
laughingsin, three things:
1) We strongly discourage people from doing other's homework for them.
2) You probably don't want to use two exit conditions, especially when one will do.
3) In C++, the pre-increment/pre-decrement operators are preferred over their post counterparts. While the cost is roughly the same in C, the post operators can be very expensive when you use C++ objects.

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
October 18, 2010 at 10:51:30
Thank you everyone for your help. Actually it was not a homework assignment, it was a pop quiz that was given in class, and I was trying to figure out how to solve it for a midterm. The teacher hasn't been there for 2 and a half weeks, so I was not able to get him to solve it for us (he left the class that it was handed back). I was able to work it out though, similar along the lines of the example given.

Report •


Ask Question