Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Just started programing in C

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
reiko
Posts: 100
Joined: 2015-09-02 22:40

Just started programing in C

#1 Post by reiko »

hey i picked C as my first programming language and i stuck at trying to find the nth position of fibonaci sequence
this how far i got

Code: Select all

#include <stdio.h>

int fibonaci(int x)
{
int i;
if(x == 0 || x == 1);
    return x;

    else  {
        int a[10];
        a[0] = 0;
        a[1] = 1;
        for(i = 2; i<10; i++)
    {
        a[i] = a[i-1]+a[i-2];
    }

   x = a[x];
   return x;
}



int main ()
{
int a , b;

printf("What position in fibonaci you want from 0 to 10");
scanf("%d",&a);
b = fibonaci(a);
printf("this position in fibonaci is : %d" , a);
return 0;
}
i kinda stuck this is my first program so pls dont kill me yet
i using code::blocks v 13.12 and i cant pinpoint the compiler error

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Just started programing in C

#2 Post by GarryRicketson »

i kinda stuck this is my first program so pls dont kill me yet
You really should learn how to do some searches, and use a search engine , before you start learning how to write programs, Hint: Use the search engines , they are the most valuable and use full tools available , one can learn anything, but especially in relation to programming, real programmer, and students were and still are using search engines as a means to track down data and information, It was "programmers" that developed the search tools, in order to have a way to find the data they need.
Copy these key words, :
paste them into the little search box window.

This is couple, of many I found,
http://www.sanfoundry.com/c-program-fib ... recursion/
This one even has a example you could use,

http://www.programmerinterview.com/inde ... ci-number/

On the left side of the page , it has selections, for various programming languages, C or C+ is included, but if that site does not have what you are looking for , there are 100s of other examples,..all on the same subject.
"" trying to find the nth position of fibonacci sequence"


User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Just started programing in C

#4 Post by GarryRicketson »

But any way, there is no problem, asking about these things here also,..that is what the forum is for,
-------------------------------------------EDITED: -------------------
But we are not here to do peoples home work, if this is a home work problem,
you need to solve it your self.
That gets back to what I said in my first reply,..before you do anything, you need to learn how to do some searches,use the search engines to find your solutions, or possible solutions.
------------------End of edit.------------------
This might be a use full one to study too, it will help you learn to make a program that can help you search for solutions,..
http://www.sanfoundry.com/c-programming ... g-sorting/
Another important factor, Do you really know and understand what the "fibonacci-sequence" is , and how that works ?
http://cs.stackexchange.com/questions/3 ... definitive
Another important factor, Do you really know and understand what the "fibonacci-sequence" is , and how that works ?
http://www.maths.surrey.ac.uk/hosted-si ... rmula.html

https://en.wikipedia.org/wiki/Fibonacci_number
Last edited by GarryRicketson on 2015-09-20 15:33, edited 1 time in total.

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Just started programing in C

#5 Post by GarryRicketson »

Last but not least (should have been first )
using code::blocks v 13.12 and i cant pinpoint the compiler error
So, what you need to do : Use the search engine , to find out how to debug the compiler, and pin point errors, etc,..
Key words:
using code::blocks v 13.12 how can I pinpoint the compiler error
Some of the results: http://wiki.codeblocks.org/index.php/De ... de::Blocks
From the above link: Make sure that the project is compiled with the -g (debugging symbols) compiler option on, and the -s (strip symbols) option off. This ensures that the executable has debug symbols included.
Compiler optimization switches should be turned off, stripping symbols (-s) must be turned off.
Keep in mind that you may have to re-build your project as up-to-date object files might not be re-compiled with -g otherwise. Please be aware that in compilers other than GCC, -g and/or -s might be a different switch (-s might not be available at all).

Menu => Project => Build Options
And this :
http://wiki.codeblocks.org/index.php/Debugger_scripts
Have you followed all the guides, and documentation, on setting up "code::Blocks" and using it, ? I have a feeling this is where you are having problems,..

User avatar
dasein
Posts: 7680
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: Just started programing in C

#6 Post by dasein »

GarryRicketson wrote:...that is what the forum is for,...
No. This is very obviously a homework problem, and the OP needs to learn to debug his/her own code.

@OP: The whole point of this assignment, or any other, is to help you develop your own problem-solving and troubleshooting skills. Obtaining the "right answer" isn't the goal; going through the process is. That is: the way one learns to debug a 30,000-line program is by first learning to debug a 30-line program.

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Just started programing in C

#7 Post by GarryRicketson »

dasein wrote:
GarryRicketson wrote:...that is what the forum is for,...
No. This is very obviously a homework problem, and the OP needs to learn to debug his/her own code.

@OP: The whole point of this assignment, or any other, is to help you develop your own problem-solving and troubleshooting skills. Obtaining the "right answer" isn't the goal; going through the process is.
Your right, I should have been more clear, the forum is NOT to here for doing peoples "home work", we are here though to try to help, any one, that is using Debian, and then as Dasein says, very well put "to help you develop your own problem-solving and troubleshooting skills.",... As said in my previous post, that is the biggest problem for the OP, at this point, not knowing how to do that, I assume, or un-willing ?
So to clarify, please don't feel we are being "mean", or trying to
so pls dont kill me yet
we are not trying to discourage or "kill" you, but trying to help you, that is what the forum is for, but it is not , for doing peoples home work,.. I do think if the OP takes the time to study the pages I showed links to they can find the answer, to the question,..Actually if the OP gets the "debugging' and his/her compiler set up correctly, they should get error messages that are pretty clear,..

User avatar
dasein
Posts: 7680
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: Just started programing in C

#8 Post by dasein »

GarryRicketson wrote:Actually if the OP gets the "debugging' and his/her compiler set up correctly, they should get error messages that are pretty clear,..
Nononononono. The "problem" is not a syntax error. It's a logic error.

("Problem" in quotes because the thing that's preventing the program from displaying the numerically correct answer is purely mechanical--equivalent to a typo. But there are at least two other suboptimal solutions hiding in this code, including one that, if remedied, would provide an average performance improvment of 500%. And that is what this particular homework can teach the OP.)

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Just started programing in C

#9 Post by GarryRicketson »

Nononononono. The "problem" is not a syntax error. It's a logic error.
Oh, ok, ...that seems like a good "clue", to be honest I don't know enough about C programming, nor math to help him/her, much.
But that is a good "clue", .. I kind of was thinking that might be part of it , that is why I asked if the OP fully understood
myself> Do you really know and understand what the "fibonacci-sequence" is , and how that works ?
I don't,.. math is one of my weakest points, then also there is a lot of "logic" involved it writing any code, and that gets into why I kind of stay away from C or C++, it seems like a very "illogical" programming language, in fact I have a hard time understanding why anyone would want to try to do this using C , when it would be more "logical" to write a simple bash script, ...

runfrodorun
Posts: 202
Joined: 2013-06-19 05:09

Re: Just started programing in C

#10 Post by runfrodorun »

You guys are right, but learning sometimes comes with a bit of mentoring. These skills don't come with spoonfeeding, but they don't come with baning your head against a wall for 8 hours solving a problem that's not that helpful for a beginner. It takes some exposure to the range of problems you need to solve as you are developing these skills, and those 8 hours could be spent doing things that are a lot more productive for developing these skills. Now I hope the OP has spent some significant time trying to solve this before asking for help on the other hand. Think back to when you first started. The issue with this code is not an easy one to catch for a beginner, as the syntax problem is likely not where the compiler would point the user to, and in fact, it would be pretty darn hard to see the relationship between the actual problem and the error without some knowledge about what certain things mean in C that are not so obvious in C, like the fact that an isolated ; represents a no-op and counts as a single line of code. *hint* OP, check your work on line 6, and for the love of god please style your code. If you're still stuck, look up if statements online and see the different syntactic use cases.

Even if we don't give people hints on their homework here, it doesn't take 6 posts to say that.

Also, I'd assume the OP does understand how the fibonacci sequence works, apart from some syntactic errors in his/her code.

-RJ
Much opinionated.
Some abrasive.
No systemd.
Wow.

User avatar
dasein
Posts: 7680
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: Just started programing in C

#11 Post by dasein »

Ok, two logic errors ;)

runfrodorun
Posts: 202
Joined: 2013-06-19 05:09

Re: Just started programing in C

#12 Post by runfrodorun »

I see it now! :D

-RJ
Much opinionated.
Some abrasive.
No systemd.
Wow.

BowCatShot
Posts: 959
Joined: 2006-07-15 12:08

Re: Just started programing in C

#13 Post by BowCatShot »

In your fibonaci function, the if statement is not closed properly
with a curly brace. In particular, look at your else clause. You need
a terminating curly brace before the one marking the function end.

Caitlin
Posts: 329
Joined: 2012-05-24 07:32
Has thanked: 3 times
Been thanked: 2 times

Re: Just started programing in C

#14 Post by Caitlin »

Hi reiko,

In addition to the aforementioned missing closing brace (or curly bracket if you prefer), I notice that there is a semicolon after the first IF statement. This has the effect of saying, "if x is equal to 0 or 1, do nothing". The following RETURN statement is unconditional because it is not in the scope of the IF.

Furthermore, if you remove THAT semicolon, the semicolon after the RETURN has the effect of pushing the ELSE out of the scope of the IF. Perhaps you meant something like this:

Code: Select all

if (x == 0 || x == 1)
    {return x;}
else
    {etc;
     etc;
     etc;};
Note that semicolons INSIDE of braces won't end the scope of the IF. (My positioning of those is a bit unconventional but you get the point.)

You use scanf to input the value of A, then printf to display the value of A. Do you mean to display the value of B instead?

Also, this program will obviously fail if you enter an A of more than 9. Your attempt to access the A'th entry of the array will use an out-of-range subscript, and as my old IBM manuals like to say, "results are unpredictable". C has no protection against out-of-range subscripts, and it is expected the programmer will take great pains to defend against them.

Since you don't really need to save ALL the Fibonacci numbers, I would have done it something like this:

Code: Select all

p = 0;
q = 1;
<begin loop here>
r = p + q;
<exit loop when done>
p = q;
q = r;
<repeat loop>
This code only saves the last two Fibonacci numbers, and by running through the loop an arbitrary number of times, you can get to any number you wanted (until the numbers get so big they won't fit into an INT anymore).

Good luck learning C programming!

Caitlin

runfrodorun
Posts: 202
Joined: 2013-06-19 05:09

Re: Just started programing in C

#15 Post by runfrodorun »

Or we could just tell people the answers I guess that's fine too...

-RJ
Much opinionated.
Some abrasive.
No systemd.
Wow.

BowCatShot
Posts: 959
Joined: 2006-07-15 12:08

Re: Just started programing in C

#16 Post by BowCatShot »

runfrodorun wrote:Or we could just tell people the answers I guess that's fine too...

-RJ
Of course that's fine too. Matter of fact it's preferable. It takes about 6 lines to give the guy a hint or an answer. It takes about 6 paragraphs of unpleasant shithead vitriol to try to admonish the guy for daring to ask a question on this sacred forum. What a bunch of jackasses.

runfrodorun
Posts: 202
Joined: 2013-06-19 05:09

Re: Just started programing in C

#17 Post by runfrodorun »

You should read my post, I was not on that side of things. I think I may have even come just short of calling people jackasses, but didn't quite get there. You basically said everything I said in my long post in two sentences. No hate bro.

-RJ
Much opinionated.
Some abrasive.
No systemd.
Wow.

Post Reply