Banfa 9,065
Expert Mod 8TB
I felt that this was a good point to start a tutorial on C/C++ programming because clearly we need to have some idea of what we are trying to achieve before we start out. I recently found this definition on the web which I rather like "Programming is planning how to solve a problem. No matter what method is used -- pencil and paper, slide rule, adding machine, or computer -problem solving requires programming. Of course, how one programs depends on the device one uses in problem solving."
This is taken from the ROYAL PRECISION, Electronic Computer PROGRAMMING MANUAL for the LGP-30. For those of you that have not heard of the LPG-30 you may be forgiven because it was first made in 1956 and has long since gone out of production. But it has the rather auspicious claim of being the type of computer that Edward Lorenz was using when he first noted the chaotic nature of weather systems.
Anyway back to what is programming... "planning how to solve a problem", note we are not actually solving a problem, the computer is going to do that for us. If we could solve the problem ourselves we would have no need to write the program. The premise for a program is that we don't have the time, tenacity or memory capabilities to solve a problem but we do know how to solve it so can instruct a computer to do it for us.
A simple example of this is what is the sum of all integers from 1 - 10,000. If you wanted to you could sit down with a pencil and paper or a calculator and work this out however the time involved plus the likelihood that at some point you would make a mistake makes that an undesirable option. However I can write and run a program to calculate this sum in less than 5 minutes -
#include "stdio.h"
-
-
#define MAX (10000UL)
-
-
int main(int argc, char **argp)
-
{
-
unsigned long sum = 0;
-
unsigned long number;
-
-
for(number=1; number<=MAX; number++)
-
{
-
sum += number;
-
}
-
-
printf("The sum of all integers from 1 - %lu is: %lu\n", MAX, sum);
-
-
return 0;
-
}
-
This gives the result 50005000. As it happens I can verify this because I know that the sum of integers from 1 - N can be calculated as
(N+1)*(N/2)
(10000 + 1)*(10000/2) = 10001*5000 = 50005000
So I have solved the problem of how to calculate the sum of all integers from 1 - 10000 and the computer has solve the problem of calculating the sum of all integers from 1 - 10000.
And this is the crux of all computer programs; you can not program a computer to solve a problem unless you know how to solve that problem. There is no point even sitting at your computer with the intention of programming until you have the knowledge, be that a formula from a text book or a design document or a print out from a web page, of how you are going to set about solving the problem.
So programming is the production of a set of instructions that describe how a problem can be solved. There are many languages in which these instructions may be written, for instance on the back of a bottle of shampoo you will often find the instructions describing how to solve the problem of making dirty hair clean: - Wet hair
- Rub in shampoo to create a lather
- Rinse Hair
- Repeat
Note that because this set of instructions is aimed at human beings it makes a couple of assumptions. In step 4 for instance it makes the assumption that normal English is in use and that the instruction will actually be read as “Repeat Once”. It is also likely that an human running these instructions will not repeat step 1 because their hair will already be wet so they will judge it unnecessary to repeat that step.
And that is one of the major differences between humans and computers. Humans have judgement and free will and will not run any instruction they deem not required or nonsensical where as a computer will do exactly what it is told with no judgement on the need or sanity of the instruction. Give the instructions above to your computer and it will never get out of the shower. Tutorial 2: How to Program 17 79279
Thats a pretty cool write-up; very clearly put.
Arguably programming is expressing the plan in a way that the computer can accept; not merely making the plan, however what you wrote is so good and clear, I won't actually argue the point.
Nice!
Sam
sorry, but your example is very sloppy code.
* you return 0, why not RETURN_SUCCESS?
* why don't you check the return code of printf()?
* if your unsigned long types were 16 bit, your total would overflow
* printf("%lu", MAX) is not correct. your #define is replaced by a signed integer
* printf("%lu", MAX) is not correct if your unsigned long type is larger than an integer (eg. 32-bit int, 64-bit long)
* most compilers will take #include "stdio.h" to mean looking local directory first, then system/compiler flag listed directories. your should use #include<stdio.h>
And we have almost developed a plan for solving the problem of how to do programming just like nature did right?
personally i prefer to write my code like this... -
x=0
-
(1..10000).each{ |i| x+=i }
-
puts "The sum of all integers from 1 - 10000 is: #{x}"
-
name that language!
Looks like a type of old Basic, i remember puts, was in Apple, TI, Commodore or TRS-80, not sure which one, been awhile :P
Banfa 9,065
Expert Mod 8TB
* you return 0, why not RETURN_SUCCESS?
Returning 0 is defined as an acceptable indication of success by the C standard, it is guaranteed to work for a conforming compiler and I see no reason not to use it. Also I think you mean EXIT_SUCCESS.
* why don't you check the return code of printf()?
And do what if it has failed? I suppose return EXIT_FAILURE but for the purposes of this small example program that seems like over engineering to me for a function that in such a simple program really should be expected to work on any platform with a stdout.
* if your unsigned long types were 16 bit, your total would overflow
As it would if the unsigned long where 7 bit, 13 bit or any other uncommon number. However in my years of experience of programming 8, 16, 32 and 64 bit machines I have yet to see a platform where unsigned long was not 32 bits (although it arguably should be on todays 64 bit PCs). I am not saying it is not possible or that such machines do not exist but I get the impression they are quite rare and I suspect anyone trying this code on such a machine is likely to be aware this.
* printf("%lu", MAX) is not correct. your #define is replaced by a signed integer
* printf("%lu", MAX) is not correct if your unsigned long type is larger than an integer (eg. 32-bit int, 64-bit long)
I concede both points, I have changed my #define to define the constant as a unsigned long constant to make all the types match.
* most compilers will take #include "stdio.h" to mean looking local directory first, then system/compiler flag listed directories. your should use #include<stdio.h>
A bit of a nit pick but I have change the code to fix this.
Banfa 9,065
Expert Mod 8TB
name that language!
Nope I can't and Google is letting me down badly, I have to admit to having my curiosity piqued and would like to know the answer.
Banfa 9,065
Expert Mod 8TB
Arguably programming is expressing the plan in a way that the computer can accept; not merely making the plan, however what you wrote is so good and clear, I won't actually argue the point.
Nice!
Sam
Thanks Sam :D
personally i prefer to write my code like this... -
x=0
-
(1..10000).each{ |i| x+=i }
-
puts "The sum of all integers from 1 - 10000 is: #{x}"
-
name that language!
I may be wrong, but this looks like ruby to me.
personally i prefer to write my code like this... -
x=0
-
(1..10000).each{ |i| x+=i }
-
puts "The sum of all integers from 1 - 10000 is: #{x}"
-
name that language!
Yes, that's Ruby definitely.
Yes, that's Ruby definitely.
It's not good Ruby though! (Sorry...can't resist.) -
x = (1..10000).inject(0) {|total, i| total + i }
-
puts "The sum of all integers from 1 - 10000 is: #{x}"
-
Or, if you want to abstract out the sum: -
module Enumerable
-
def sum
-
inject(0) {|total, i| total + i }
-
end
-
end
-
-
puts "The sum of all integers from 1 - 10000 is: #{(1..10000).sum}"
-
And this is the crux of all computer programs; you can not program a computer to solve a problem unless you know how to solve that problem.
I disagree with this statement. There are lots of built in functions and libraries that are able to do things that i might never learn how to do. personally i think your logic is flawed from either side of the opposing argument. If you're saying that we're limited to what we know, then why can't i just apply my binary skills to create any imaginable form of data and or programs?
i belive what you're saying is partly true, but i think that you miss an important and powerful topic when you present it like this.
As to the specific statement above
(Regarding the difference between computers and people)
This statement is perfectly wrong:
"Humans have judgment and free will and will not run any instruction they deem not required or nonsensical where as a computer will do exactly what it is told.”
It can be readily and consistently observed that a vast majority of people accept, obey and repeat all kinds of irrelevant and nonsensical instructions-
Both created within themselves and all the more received from the outside world
With the advent of programming modeled after neural networks, I'd say the computer has in the very least, the potential to surpass the majority of human beings as far as comprehension
Once that potential is packaged and fulfilled by going mainstream, the only judgment/free will left to the majority people will be the choice to ignore/override the computer's perfect suggestion and choose the wrong result instead
-A concept well mastered already
One note to add about programming of a computer vs human instructions, is that with computers we have the full machine level instruction set known to us, and access to that level. We don''t have that in humans (yet).
One might define programming as specifying transitions in a state machine, considering all data in memorry and everywhere as one state. i.e. given machine state #38742, what instruction # will take me to desired state. Planning how to solve a problem may involve no programming at all.
One note to add about programming of a computer vs human instructions, is that with computers we have the full machine level instruction set known to us, and access to that level. We don''t have that in humans (yet).
Yuh-huh we do-
Well...I don't know about 'we'
People operate on four drives
-Learn
-Bond
-Defend
-Acquire
Try to think of a fifth drive- There isn't one
-THAT means we have just defined a fixed parameter to what it is to be a human
A fixed parameter is a measurement...a measurement is an amount...an amount can be represented by a number...a number can form an equation...equations form the basis of code...code dictates a program...a program determines behavior...
Know how to modify the code and you modify the program- You modify the program and you've modified the behavior
People behave
Very nice writeup, good job. In school people often are taught math is just memorizing multiplication tables, but we know it is so much more awesome than that
This is great, thanks.
I think this has changed my life forever.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Mark |
last post by:
Hello. I am new to programming and Python and was wondering if someone
could help get me started. I picked Python to start learning to prgram
because of some things I have read about it (easy to...
|
by: Guotao Luan |
last post by:
Hello All:
I notice that there have been frequent questions being asked about template
here so I guess it is okay to post this message here. I just wrote a c++
tempalte tutorial/review, I would...
|
by: John Walton |
last post by:
Hello, everyone. I just began school, and they
already assigned us science fair. Since I'm in 8th
grade, I get to do demonstrations for our projects.
I'm probably going to demonstrate Python's...
|
by: jacob navia |
last post by:
There is a C tutorial at
http://www.cs.virginia.edu/~lcc-win32
It is written to go with the compiler, available
at the same URL.
I have added quite a bit of material, and I would be
glad if...
|
by: Magnus Lycka |
last post by:
While the official Python Tutorial has served its
purpose well, keeping it up to date is hardly anyones
top priority, and there are others who passionately
create really good Python tutorials on...
|
by: Bushido Hacks |
last post by:
My name is Bushido Hacks. I am a computer science major from the St.
Louis, Missouri area. I wrote a new software installation tutorial for
C and C++ programmer who want to create programs as...
|
by: ppuniversal |
last post by:
Hello,
Can someone tell a good Tutorial on Network Programming in C++, except Beej's Guide and Vijay Mukhi's tutorials.
I want to make a Client Server application in C++ which will involve...
|
by: Zabac |
last post by:
I am attempting to learn C on a computer that does not have internet
access. Can anyone find a C tutorial that can be completely
downloaded? I wish to transfer it from a computer that can get...
|
by: Banfa |
last post by:
Posted by Banfa
The previous tutorial discussed what programming is, what we are trying to achieve, the answer being a list of instructions constituting a valid program. Now we will discuss how...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |