473,799 Members | 3,077 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tutorial 1 - What Is Programming?

Banfa
9,065 Recognized Expert Moderator Expert
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

"Programmin g 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
Expand|Select|Wrap|Line Numbers
  1. #include "stdio.h"
  2.  
  3. #define MAX (10000UL)
  4.  
  5. int main(int argc, char **argp)
  6. {
  7.     unsigned long sum = 0;
  8.     unsigned long number;
  9.  
  10.     for(number=1; number<=MAX; number++)
  11.     {
  12.         sum += number;
  13.     }
  14.  
  15.     printf("The sum of all integers from 1 - %lu is: %lu\n", MAX, sum);
  16.  
  17.     return 0;
  18. }
  19.  
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:
  1. Wet hair
  2. Rub in shampoo to create a lather
  3. Rinse Hair
  4. 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
Nov 29 '06 #1
17 79400
samliddicott
1 New Member
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
Jun 2 '08 #2
londonrastan
1 New Member
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>
Jun 2 '08 #3
BrentAllsop
1 New Member
And we have almost developed a plan for solving the problem of how to do programming just like nature did right?
Jun 2 '08 #4
taerb
1 New Member
personally i prefer to write my code like this...

Expand|Select|Wrap|Line Numbers
  1. x=0
  2. (1..10000).each{ |i| x+=i }
  3. puts "The sum of all integers from 1 - 10000 is: #{x}"
  4.  
name that language!
Jun 2 '08 #5
KillsTheWeak
1 New Member
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
Jun 2 '08 #6
Banfa
9,065 Recognized Expert Moderator Expert
* 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.
Jun 2 '08 #7
Banfa
9,065 Recognized Expert Moderator Expert
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.
Jun 2 '08 #8
Banfa
9,065 Recognized Expert Moderator Expert
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
Jun 2 '08 #9
lukeh
1 New Member
personally i prefer to write my code like this...

Expand|Select|Wrap|Line Numbers
  1. x=0
  2. (1..10000).each{ |i| x+=i }
  3. puts "The sum of all integers from 1 - 10000 is: #{x}"
  4.  
name that language!
I may be wrong, but this looks like ruby to me.
Jun 2 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
2431
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 learn, object oriented, clear syntax, etc...). Can anyone assist in getting me started with learning to program and Python? Recommended reading material? Online tutorials? Recommended development tools (wxpython, pythonwin, etc...)? I am a...
11
3219
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 like to hear feedbacks from other users. I do not have much experience with template myself so I am sure there are many problems or even mistakes in this material. Comments of all types are welcomed. and I also hope this tutorial/review is helpful...
6
2399
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 networking capabilities by writing a simple instant messenger program. I only have a few problems: 1. I know squat about Python network Programming 2. I know nothing about networks
156
7704
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 people in this group give it a try and tell me if I am saying nonsense somewhere. Beware that I am not very orthodox, hence my tutorial
11
2937
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 the web. I think 'A Byte of Python' by Swaroop C H is a good beginners tutorial, and 'Dive Into Python' by Mark Pilgrim is a good tutorial for more experienced programmers.
14
2834
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 well as programs that use OpenGL. My new tutorial is posted at http://www.bushidohacks.com/tutorials/gcc.php The tutorial is informative for anyone who has a REAL interest in object-oriented programming. It is NOT some phoney balogna sales pitch...
2
8555
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 huge amount of thread programming also, so I wanted a good tutorial which can give me good understanding of all the functions used for Network Programming using Standard C++ (and not using any Network Library other than Winsocks and those available in...
37
8267
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 online to my computer. (The one that can not get on the internet.) I don't need an absolute beginner tutorial, as I have used Python before. Thanks! - Ian Shearin
2
19354
Banfa
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 we set about doing that. Every program starts with a specification, this may be a several hundred page document from your latest client or one small paragraph from your professor and pretty much anything in-between. The specification is very...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10473
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10249
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10219
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10025
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5461
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5584
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.