|
This thread is locked; no one can reply to it. |
1
2
|
Please Help me. |
Doctor Cop
Member #16,833
April 2018
|
#include <stdio.h> int main() { char name[100], ch; This program returns segmentation errors.
|
amarillion
Member #940
January 2001
|
You're attempting to pass a single char to atoi. But atoi takes a const char *. It seems to me that this is causing the crash. A better way to convert a char to a numeric value is to simply substract the ascii code of '0', something like: int idx = (int)(name[j] - '0') Where you write if(name[j] < '9' && name[j] > '0') You probably mean if(name[j] <= '9' && name[j] >= '0') If I may say so, your code is very messy, and there are a number of things you could improve. When posting code in the forums, use < code > tags to format the code. Why is the variable called 'name' when it takes a string of digits? What is the intention of the variable named 'cout'? Better variable naming is the first step to better code and fewer bugs. There is a variable 'ch' that is unused. Code hygiene is the second step to better code. You are often trying to do a lot of operations on a single line. This is not helpful if you're a beginner and you're trying to understand the way code works. Therefore I would write out a line like cout[atoi(name[j])]++; in multiple steps, until you know what you're doing. It will also help to print intermediate values in your calculation and check that they are correct. Also, you're calling strlen in a loop, which has O(N^2) complexity -- |
Doctor Cop
Member #16,833
April 2018
|
1#include <stdio.h>
2#include <string.h>
3#include <math.h>
4#include <stdlib.h>
5
6int main() {
7
8 char string[1000];
9 int i = 0, count[10]={0};
10
11 gets(string);
12
13 for(int j=0; j<strlen(string); j++)
14 {
15 if(string[j] <= '9' && string[j] >= '0')
16 count[(int)(string[j]-'0')]++;
17 }
18
19 for(int l=0; l<10; l++)
20 printf("%d ",count[l]);
21 return 0;
22}
Thanks man, my code runs now.
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
He was using Big O Notation. O(n^2) means the complexity of your code is n^2. A for loop like this : Move the strlen call in front of the for loop. It only needs to be calculated once. https://upload.wikimedia.org/wikipedia/commons/7/7e/Comparison_computational_complexity.svg My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Doctor Cop
Member #16,833
April 2018
|
Edgar Reynaldo: Thanks man, you people are really good. I will keep your advice.
|
amarillion
Member #940
January 2001
|
Glad to see you're learning. Edgar Reynaldo said: Move the strlen call in front of the for loop. It only needs to be calculated once. Indeed, that's what I meant. It's not going to make any noticeable speed improvement in this example, but it's good to start thinking about efficiency early on. At my job we recently had an example where a programmer carelessly wrote a quadratic algorithm where linear was possible. This problem wasn't noticed during testing because we only tested with small numbers. But the customer sure was noticing when the program crashed repeatedly and then wouldn't start. Oops. Doctor Cop said: can you send me links to any website from where I can learn optimization techniques. Well, there is a lot out there, and optimization is a big topic. If I may be so bold, I recommend you my own tutorial series: https://www.helixsoft.nl/articles/circle/sincos.htm It's not exactly about optimization but you may find it useful anyway. -- |
Edgar Reynaldo
Major Reynaldo
May 2007
|
Get the basics of C programming down first, and then try doing some reading on algorithms. Basic ones like sort and search. https://en.cppreference.com/w/ https://www.khanacademy.org/computing/computer-science/algorithms The large part of optimization is in picking algorithms that are suitable for your purposes, and don't take longer than they need to. amarillion was mentioning the difference between a quadratic complexity and linear. Linear beats quadratic all the time. In fact, I'm sure I lost an opportunity to work at Google because I didn't optimize my algorithm. I used two for loops instead of one, which is inefficient. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Doctor Cop
Member #16,833
April 2018
|
Thanks amarillion and Edgar Reynaldo.
|
GullRaDriel
Member #3,861
September 2003
|
Moreover, learn how to profile your code to know where optimization really needs to be done. http://www.network-theory.co.uk/docs/gccintro/gccintro_80.html "Code is like shit - it only smells if it is not yours" |
Audric
Member #907
January 2001
|
The first step of optimization is knowing what your code does, don't bother too much about the rest too early. Ie. in my opinion you shouldn't bother trying profiling by yourself, because the results are not meaningful when you're measuring very small amounts (Such as strlen() of strings under 1000 characters, or the comparison between one addition and one square root) Sometimes arrays are more readable (and thus fool-proof), sometimes pointers are. for(int j=0; j<strlen(string); j++) { if(string[j] <= '9' && string[j] >= '0') count[(int)(string[j]-'0')]++; } can be replaced quite nicely with : for(char* c=string; *c != '\0'; c++) { if(*c <= '9' && *c >= '0') count[(int)(*c-'0')]++; } You should read this code as : "c is a pointer that initially points at the first character. *c is the value of the pointed character. c++ moves the pointer, and we loop until we reach the NULL character." |
Edgar Reynaldo
Major Reynaldo
May 2007
|
There are C lib functions that already do the kind of things you are wanting to do.
My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
bamccaig
Member #7,536
July 2006
|
Donald Knuth said: Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%. In this case, the for-loop is easily improved by moving the strlen() to the initialization step (but only because the length of the string doesn't change during the loop) and storing it. And that's a very common thing that programmers screw up, and an easy way to save some CPU cycles. Often it won't significantly impact the performance, but it's just as easy if not easier to read the program when you restructure it so it's an easy win. Just today I was working on some scheduling code at work and checking database state through an ORM layer to determine which UI to render on each day of the month (and later, which functions should be permitted on each day, versus rejected). I looped through the dates visible on the calendar and checked each one. I did my best to preload the memory cache with the database records, but the framework isn't so reliable at obeying your command. I'm not even certain if my function was querying the database again or loading from cache, but I suspect cache. It was very fast in development, but the database is small and hosted on the same machine so it's possible it'll be worse in production. Nevertheless, if it's good enough then it's good enough. Often adding more hardware to run slow or bloated code is cheaper than maintaining micro-optimized code. Pretty much don't solve the problem until it becomes a problem (unless the better solution is obvious, and better, such as above). Also, isdigit is not strictly 0-9. It is defined according to the selected locale, which means it might act strangely in certain locales, and in particular since we're translating the character into a memory address offset that's a pretty dangerous assumption to make. This is sort of like using \d in regular expressions. Depending on the language, that can sometimes include many other types of characters than strictly 0-9. You're much better off to be explicit and guarantee that the program will do what you intend unless the documentation/specification is explicit about the meaning of the functionality you're using. -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
Ariesnl
Member #2,902
November 2002
|
Perhaps one day we will find that the human factor is more complicated than space and time (Jean luc Picard) |
Doctor Cop
Member #16,833
April 2018
|
Now I have completed C language and now I want to work on some projects. Please help me get started. Please help me.
|
GullRaDriel
Member #3,861
September 2003
|
It takes understanding the already exising code ^^ "Code is like shit - it only smells if it is not yours" |
Edgar Reynaldo
Major Reynaldo
May 2007
|
Doctor Cop said: Now I have completed C language Making this kind of statement implies you don't actually know C as well as you think you do. Go to https://en.cppreference.com/w/c and see how much you actually know. Then learn C++. Then C#. Then come back to C. If you want to contribute to an open source project, you need to follow their coding conventions such as formatting and indentation and naming schemes. Then you need to understand what their current code does before you start trying to make contributions. Start small and submit patches. Knowing how to use diff and patch is essential. Unless you've been brought up spoiled and you only know how to use a Github pull request. Learn git. Versioning control systems (VCS) allow you to save and modify your work without fear of losing work you've already committed. But I suggest you start small and work your way up to larger projects. Learn interactive console programming first. After that, make some simple graphical applications using your favorite library (Allegro of course!). Then move on to bigger projects, but only after you've gained experience making smaller applications. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
jmasterx
Member #11,410
October 2009
|
Doctor Cop said: Now I have completed C language Care to explain: Agui GUI API -> https://github.com/jmasterx/Agui |
bamccaig
Member #7,536
July 2006
|
The naysayers make valid points, but that's all beside the point. If you're serious about contributing to a project, or doing anything at all in life, you should go for it balls out and try. Nothing will be accomplished by giving up. Think you can fix a bug or make something better? Allegro is open source. Download the code or better yet clone the Git repo so you have the full history and a mechanism to stay up to date. Learn to build from source and learn to use Git to fetch updates and branch changes. And go ahead and make your changes in a new branch. Test it. Test it again. Try to break it. If it works and it's as good as you can do, and you think it's valuable to the project, then research how the developers prefer contributions to be submitted. Often projects have a CONTRIBUTE or HACKING file in the root of the repo that details that. If not, check the README, the Web site, and wherever else you can think of. If you can't find an answer then ask somewhere. Here, on the liballeg.org forums, on the mailing list, or in IRC. Somebody will know and guide you toward it. Submit your patch for consideration, review, comments, and criticism. Expect it to be rejected, but take the suggestions to heart and make the changes requested by the developers. You can always continue working on your change and resubmit it after addressing the concerns. That's basically what the process is on anything you want to accomplish in life. Try it. Fail. Try again. Repeat. Eventually, you will achieve. It can take weeks or months to familiarize yourself with a new nontrivial codebase. You should aim low to start out. If you set your goal too high you won't be able to reach it without a lot of dedicated work over a long time. If you are passionate about it then you can do it so go for it. But if you aren't passionate about it and just want to contribute immediately then look for little things you can do. Submitting small documentation improvements is an easy way to get involved. Over time, if you keep at it, you'll learn a lot and your experience will prepare you for better things. -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
Edgar Reynaldo
Major Reynaldo
May 2007
|
All of what bambams is saying is true. However, you lack both knowledge, and experience. Start with knowledge, and then gain experience using that knowledge. Allegro 4 was hard enough to follow, let alone Allegro 5. There are a ton of A) vtables (virtual function lookup tables), B) Opaque structs, C)Platform specific drivers, D) Addons, E)Private headers and private implementations, etc... Learn how to use grep. That is your single biggest ally in debugging errors other than a debugger. Which means, you also need to learn GDB, or MSVC's debugger. grep allows you to search for specific string expressions inside groups of files. A debugger allows you to step through code, and observe the state during crashes by getting a backtrace and examining local variables. You need to learn basic data structures, or at least what their attributes are, and when to use them. You need to understand basic algorithms like search and sort and their complexity requirements. By all means contribute to Allegro. But don't expect it to be as easy as you might think it would be at first. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Doctor Cop
Member #16,833
April 2018
|
jmasterx: I get it what you want to say, yes I have completed most of the concepts you mentioned. 1. pointers. Others I will, thank you for pointing out. Please tell me more so I can improve.
|
jmasterx
Member #11,410
October 2009
|
bamccaig said: The naysayers Thanks BamBam Now that you've mastered the basics, you'll mostly just want to get out there and start coding something. Most of a dev's time is spent reading code, not writing it. So really focus on coming up with clear ways to structure your code. Master the art of automated testing (Unit, integration, component, end to end) early on because testing gives you confidence in the code you write and it documents your software's behavior. Even if the project you're contributing to does not have tests, make the code you modify testable and test your code. If you can write clear, modular, well tested, and well documented code (not comments in the code, automated tests, and meaningful function names), you're sure to be successful. Agui GUI API -> https://github.com/jmasterx/Agui |
Doctor Cop
Member #16,833
April 2018
|
Thaks jmasterx. Not at ones, one by one.
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
I don't know what the state of profilers on Linux is these days. Last time I tried to use gprof it was horribly borked it just didn't work. EDIT My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Doctor Cop
Member #16,833
April 2018
|
Can anybody tell me how can I change my DP?
|
|
1
2
|