hello,
im new here ... i wanna ask... how to create a program using C language .... with the output like this ...
using for loop statement
1 10 2 9 3 8 4 7 5 6 6 5 7 4 8 3 9 2 10 1
or
1 5 2 4 3 3 4 2 5 1
as you can observed its altenate ...
please help to code with this .. im new with this language ...
thank you and more power
30 replies to this topic
#1
Posted 26 January 2012 - 05:10 AM
|
|
|
#2
Posted 26 January 2012 - 06:18 AM
The trick here is simple: Find a pattern that can be represented by a combination of a loop and a mathematical formula.
In your case, what is the pattern here? If I supply 10 as the input to your algorithm, it prints 1 first, then what's the 2nd number?
I'll give you a hint. Your algorithm could be easily done in one loop. You'd be printing out pairs of numbers in the body of this loop. Given i, which is your loop iterator (in other words, it will have a value from 0 to n-1, where n is the input number), how can you calculate those pairs of numbers?
In other words, you have:
i, n
So you need to find a formula to calculate:
x1, x2
in terms of i and n, where x1 and x2 correspond to pairs of numbers in your sequences above.
In your case, what is the pattern here? If I supply 10 as the input to your algorithm, it prints 1 first, then what's the 2nd number?
I'll give you a hint. Your algorithm could be easily done in one loop. You'd be printing out pairs of numbers in the body of this loop. Given i, which is your loop iterator (in other words, it will have a value from 0 to n-1, where n is the input number), how can you calculate those pairs of numbers?
In other words, you have:
i, n
So you need to find a formula to calculate:
x1, x2
in terms of i and n, where x1 and x2 correspond to pairs of numbers in your sequences above.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
#3
Posted 27 January 2012 - 05:12 PM
sir,
i was new in this language ... i can code with the output like this 0 2 4 6 8 10 or 1 3 5 7 9 11 ...
but with this 1 10 2 9 3 8 4 7 5 6 6 5 7 4 8 3 9 2 10 1 it give me a hard time to think because its alternate output... im not good in programming ...
we were in for loop discussion ...
soo i came here for help ...
thanks for helping me sir,
i was new in this language ... i can code with the output like this 0 2 4 6 8 10 or 1 3 5 7 9 11 ...
but with this 1 10 2 9 3 8 4 7 5 6 6 5 7 4 8 3 9 2 10 1 it give me a hard time to think because its alternate output... im not good in programming ...
we were in for loop discussion ...
soo i came here for help ...
thanks for helping me sir,
#4
Posted 28 January 2012 - 11:39 AM
please help me
#5
Posted 28 January 2012 - 06:29 PM
What do you have so far? Also, is the C or C#? They are completely different languages.
#6
Posted 30 January 2012 - 06:42 AM
Think of it like this: You want to print out pairs of characters like this: (without the brackets, obviously. They are just for visual clarity.)
[1 10] [2 9] [3 8] ... [10 1]
If you group all the numbers into pairs like this, you start to see a clear pattern. Let's look at them again, only vertical, so we can compare the first numbers and the 2nd numbers with each other:
Let's call the 1st number in a pair 'A', and the 2nd number 'B'. In the list above, we can see that for each loop iteration, A is increasing by 1, and B is decreasing by 1. Now, all we need to do is find a formula to calculate any pair of numbers based on the value of 'i', which is your loop iterator.
So for starters, let's say we want to calculate a series from 1 to n, where 'n' is any positive integer. In the listing above, n is 10. So we need to store 'n' in a variable so we have access to its value in our formula.
Secondly, we need a loop. In pseudocode, it will look something like this:
So you see, we can do more than 1 thing inside a for loop, thus, we have the interleaving of two patterns.
Now then, all you need to do is find a function which maps the values of 'i' to the correct values of 'A' and 'B', and simply plug those functions into the pseudocode on the two lines that begin with the word 'Calculate'. Here is the table of correct mappings for n=10:
Can you figure it out from there?
[1 10] [2 9] [3 8] ... [10 1]
If you group all the numbers into pairs like this, you start to see a clear pattern. Let's look at them again, only vertical, so we can compare the first numbers and the 2nd numbers with each other:
[ 1 10] [ 2 9] [ 3 8] [ 4 7] [ 5 6] [ 6 5] [ 7 4] [ 8 3] [ 9 2] [10 1]
Let's call the 1st number in a pair 'A', and the 2nd number 'B'. In the list above, we can see that for each loop iteration, A is increasing by 1, and B is decreasing by 1. Now, all we need to do is find a formula to calculate any pair of numbers based on the value of 'i', which is your loop iterator.
So for starters, let's say we want to calculate a series from 1 to n, where 'n' is any positive integer. In the listing above, n is 10. So we need to store 'n' in a variable so we have access to its value in our formula.
Secondly, we need a loop. In pseudocode, it will look something like this:
get value of 'n' for 'i' from 0 to less than 'n': // for n=10, this is 10 iterations: i=0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Calculate first number, 'a'. Print 'a'. Calculate second number, 'b'. Print 'b'. next 'i'.
So you see, we can do more than 1 thing inside a for loop, thus, we have the interleaving of two patterns.
Now then, all you need to do is find a function which maps the values of 'i' to the correct values of 'A' and 'B', and simply plug those functions into the pseudocode on the two lines that begin with the word 'Calculate'. Here is the table of correct mappings for n=10:
n := 10 i := 0 --> A(0) = 1 B(0) = 10 i := 1 --> A(1) = 2 B(1) = 9 i := 2 --> A(2) = 3 B(2) = 8 i := 3 --> A(3) = 4 B(3) = 7 i := 4 --> A(4) = 5 B(4) = 6 i := 5 --> A(5) = 6 B(5) = 5 i := 6 --> A(6) = 7 B(6) = 4 i := 7 --> A(7) = 8 B(7) = 3 i := 8 --> A(8) = 9 B(8) = 2 i := 9 --> A(9) = 10 B(9) = 1
Can you figure it out from there?
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
#7
Posted 31 January 2012 - 06:11 AM
im thinking for using IF ELSE statement ...
but i cant get the formula ...
sorry again ..
this is my first time using the c language ...
thats why im still noob ...
thanks for the help then
---------- Post added at 06:11 AM ---------- Previous post was at 05:32 AM ----------
i try to code this problem but the output was ... 11111522222433333444442555551
i try to use to FOR LOOP statement ...
i guess the only one side works ...
but i cant get the formula ...
sorry again ..
this is my first time using the c language ...
thats why im still noob ...
thanks for the help then
---------- Post added at 06:11 AM ---------- Previous post was at 05:32 AM ----------
i try to code this problem but the output was ... 11111522222433333444442555551
i try to use to FOR LOOP statement ...
i guess the only one side works ...
#8
Posted 31 January 2012 - 06:30 AM
grafix said:
im thinking for using IF ELSE statement ...
I don't think an if-else statement would help you much here.
Just look at the table I made. Let's look at the first formula you'll need: A(i). Consider the first case: A(0) = 1. How do you make 1 from 0? (Hint: 0 + x = 1, solve for x.)
Now, consider the 2nd case: A(1) = 2. How do you make 2 from 1?
How do you make 3 from 2?
How do you make 4 from 3?
You should begin to see a pattern developing here.
Now, consider the B formula, B(i, n) for the first case: B(0,10) = 10. How do you make 10 from 10 and zero? (Hint: 10 - 0 = 10)
Now, B(1, 10) = 9. How do you make 9 from 10 and 1?
B(2, 10) = 8. How do you make 8 from 10 and 2?
And from there you should see the pattern.
Once you have those two formulas (which I pretty much gave you above), just place them both within the body of the loop statement. You have 'n', the max number of loop iterations to perform, and you have 'i', the current iteration that you're on. The rest is right at your fingertips.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
#9
Posted 31 January 2012 - 07:23 AM
your really great in analyzing problems ...
ill try to think about it ...
its really hard for me to make a code ...
but ill try ...
---------- Post added at 07:23 AM ---------- Previous post was at 06:51 AM ----------
#define p printf
#define s scanf
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
p("enter number: ");
s("%d"&n);
for(i=0,i>=n,i++)
for(i=10,i<=n,i--)
p("No. in alternating : "n);
getch();
}
this is my first try ...
ill try to think about it ...
its really hard for me to make a code ...
but ill try ...
---------- Post added at 07:23 AM ---------- Previous post was at 06:51 AM ----------
#define p printf
#define s scanf
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
p("enter number: ");
s("%d"&n);
for(i=0,i>=n,i++)
for(i=10,i<=n,i--)
p("No. in alternating : "n);
getch();
}
this is my first try ...
#10
Posted 31 January 2012 - 07:51 AM
You only need 1 for loop, not 2.
That won't do anything.
That will loop forever. (for all n >= 10) For all n < 10, it won't do anything at all.
Just write 1 for loop, for i from 0 to less than n, incrementing.
grafix said:
for(i=0,i>=n,i++)
That won't do anything.
grafix said:
for(i=10,i<=n,i--)
That will loop forever. (for all n >= 10) For all n < 10, it won't do anything at all.
Just write 1 for loop, for i from 0 to less than n, incrementing.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
#11
Posted 31 January 2012 - 07:56 AM
this one is correct?
i think this only prints from 0-10?
#define p printf
#define s scanf
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
p("enter number: ");
s("%d"&n);
for(i=0,i<=n,i++)
p("No. in alternating : "n);
getch();
}
i think this only prints from 0-10?
#define p printf
#define s scanf
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
p("enter number: ");
s("%d"&n);
for(i=0,i<=n,i++)
p("No. in alternating : "n);
getch();
}
#12
Posted 31 January 2012 - 08:03 AM
Less than n, not less than or equal to.
Less than will run for n iterations:
Also:
Less than will run for n iterations:
for (i = 0; i < 10; i++) {
printf("%d ", i);
}
The above code will print:0 1 2 3 4 5 6 7 8 9Count the numbers in the above list to see how many times the loop ran.
for (i = 0; i <= 10; i++) {
printf("%d ", i);
}
The above code will print:0 1 2 3 4 5 6 7 8 9 10Now count the numbers in the list to find out how many times the loop ran.
Also:
s("%d"&n);
and
p("No. in alternating : "n);
are syntactically incorrect. They are each missing a comma between the function parameters.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









