Click here to Skip to main content
15,894,291 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is my question:
Write C code (using a for loop) to create and print an array GS that stores the first 10 terms of the geometric sequence that halves each time:
{1/2, 1/4, 1/8, 1/16, ... 1/1024 }

My Semi-solution
is to use a for loop from 1:10
and use the formula 1/(2)^2
then print the array?
Posted
Comments
Sergey Alexandrovich Kryukov 9-Dec-15 22:33pm    
What have you tried so far?
Just one hint: beware of integer division: if the value is supposed to be less than one, it returns zero, that is, integer part of result.
—SA
phil.o 9-Dec-15 23:02pm    
You can proceed :) Trial and error is a necessary process if you want to become a developper.
Start from your pseudo-code -> code -> compile -> test -> debug -> code -> compile, etc.
While at this stage, if you do have a specific issue with the code you wrote, we will be glad to help.
Tip: the variable of your intended for-loop should appear in the formula.
[no name] 9-Dec-15 23:26pm    
"the variable of your intended for-loop should appear in the formula" - You cannot categorically assert this.
phil.o 9-Dec-15 23:31pm    
In the context described in the question, I think I can, yes, but I'll be glad you explain me why not :)
[no name] 9-Dec-15 23:39pm    
There is more than one way of implementing the 1/(2)^2 in a loop and I would suggest avoiding any that employ the loop variable. Let's call that a hint.

1 solution

so this is my solution now:

int Initial=0

Initial=input('Enter initial value: ')
scanf("%d", Initial);

for i =1:10 {
y(i)=(Initial)*(0.5);
}
end
 
Share this answer
 
Comments
[no name] 10-Dec-15 3:28am    
Have you read the comments? I would suggest you do as phil.o suggests, code it up, run it and see what happens. You will learn by this. You can do it all with only integers - no floating points.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900