943,948 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 792
  • Python RSS
Dec 29th, 2008
-1

print no

Expand Post »
print 1 to 10 numbers with array in c
Similar Threads
Reputation Points: 7
Solved Threads: 0
Newbie Poster
nrupparikh is offline Offline
5 posts
since Dec 2008
Dec 29th, 2008
0

Re: print no

See the answer from this thread
http://www.daniweb.com/forums/thread164796.html

Modify as appropriate.
Reputation Points: 16
Solved Threads: 35
Junior Poster
mn_kthompson is offline Offline
148 posts
since Nov 2007
Dec 31st, 2008
0

Re: print no

... and here is how you would do this with Python:
python Syntax (Toggle Plain Text)
  1. arr = [12, 7, 9, 25, 87]
  2.  
  3. for item in arr:
  4. print(item)
Just to compare the syntax, here is the same thing in C:
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. int arr[] = {12, 7, 9, 25, 87};
  6. int i;
  7.  
  8. for(i = 0; i < sizeof(arr)/sizeof(int); i++)
  9. {
  10. printf("%d\n", arr[i]);
  11. }
  12.  
  13. return 0;
  14. }
Last edited by Ene Uran; Dec 31st, 2008 at 10:47 am. Reason: see c
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Jan 2nd, 2009
0

Re: print no

There is "no beauty" in even simple C code! Let's all thank the Python development team for giving us a readable syntax. Come to think of it, they all were very fluent in C since the core of Python is writtten in C.

I guess the true beauty of C is somewhat hidden, it can do low level stuff more readable than assembly language.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Jan 3rd, 2009
0

Re: print no

Its true ! Thanks Python Dev team. I saw one example from Ene where there was no indentation, I whoop it was horrible!
Anyway it is more readable than assembly but wait a minute snee, Assembly is more readable than 10000100101111, I mean a turbulent of machine code language!
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Jan 3rd, 2009
0

Re: print no

Click to Expand / Collapse  Quote originally posted by sneekula ...
There is "no beauty" in even simple C code!
"Beauty is only in the eye of the beholder".
Those that know, find a well written piece of code in C very motivating and easy to work with.
The comparison of languages as it has been described here, only shows the lack of knowledge.

for(i = 0; i < sizeof(arr)/sizeof(int); i++) There's no need to evaluate sizeof(arr)/sizeof(int) every single time in the loop, since it never changes.
Last edited by Aia; Jan 3rd, 2009 at 3:54 pm.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jan 3rd, 2009
0

Re: print no

I don't agree, because the compiler can see that sizeof(arr)/sizeof(int) never changes, so it certainly generates the code with a constant value. Isn't it a lack of knowledge Aia ? Knowledge, beauty, great philosophical problems to think about !
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Jan 3rd, 2009
0

Re: print no

I don't agree, because the compiler can see that sizeof(arr)/sizeof(int) never changes, so it certainly generates the code with a constant value. Isn't it a lack of knowledge Aia ? Knowledge, beauty, great philosophical problems to think about !
The compiler doesn't miraculously change that expression into a constant. It evaluates each time through the loop. An unnecessary waste of CPU cycles when making a single call and keeping the value would have been sufficient.

[As a way of clarification]
The comment about sizeof() was not the reference to lack of knowledge, but the comparison of languages base on syntax, which is why I said, "The comparison of languages as it has been described here".
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jan 3rd, 2009
0

Re: print no

I tried comparing the assembly code produced when you remplace sizeof(arr)/sizeof(int) by the constant 5. The diff between the 2 programs is
Python Syntax (Toggle Plain Text)
  1. < cmpl $4, -8(%ebp)
  2. < jle .L3
  3. ---
  4. > movl -8(%ebp), %eax
  5. > cmpl $4, %eax
  6. > jbe .L3
So the difference is only one movl instruction.
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Jan 3rd, 2009
0

Re: print no

>So the difference is only one movl instruction.

cmpl $4, -8(%ebp) is not the same that cmpl $4, %eax

jle .L3 in not the same that jbe .L3

movl -8(%ebp), %eax is in a loop by virtue of "Jump if Below or Equal" Having the opportunity to execute many times. So it is not "only one mov1 instruction" executed only once.
Last edited by Aia; Jan 3rd, 2009 at 10:17 pm.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Trouble implmenting Minheap algorithm!
Next Thread in Python Forum Timeline: Indentation?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC