Saturday, June 5, 2021

Repeating Things

Repeating any task many times is a boring stuff. And error may be introduced while repeating for a longer time period.

But computer can repeat any stuff over and over again without any error, and never get bored!

Let’s write a sequence which makes computer to repeat a task five times for us:

OK Computer[main()]:

Repeat 5 times

Display “Awesome!”

 

Output:

     Awesome! Awesome! Awesome! Awesome! Awesome!

In c language, if programmer wants to implement above task into his code form then below is the details:

main()

{

    for(int i=0;i<5;i++)

        puts("Awesome!");

}           

Output:

     Awesome! 

     Awesome!

     Awesome!

     Awesome!

     Awesome!

It would take a lot of time and effort to perform repetitive tasks if computers couldn't repeat them for us.

Where to use repeat

Imagine Tom wanted to prepare burger for him and his friend. Sounds simple but it requires a few repetitive steps.

Get four Buns

Put vegetables on Bun 1

Put vegetables on Bun 2

Put chicken on Bun 3

Put chicken on Bun 4

Place Bun 1 & 3 over each other

Place Bun 2 & 4 over each other

Looks tricky?

Now imagine Tom had to make these burgers for 20 more friends. Tom say does let’s actually make 20 burgers with the shortcut:

Repeat 20 times

Get two Buns

Put vegetables on first Bun

Put chicken on second Bun

Place the Buns over each other

 

Loops

This repetition of steps is known as loops.

As a computer programmer, one should be able to identify repetitive tasks and wrap them up in loops.

Some of the repetitive tasks are

  • counting one to hundred
  • issuing movie tickets at counter
  • filling water bottles at a tap

Count based loops

In such loops, exactly how many times the loop has to be repeated is known before starting the task.

In other words, in many situations one might have to repeat a particular task a set number of times.

For example, if a teacher has to do the following task repeatedly for 50 students:

  • from mark sheet add individual subject marks
  • display total marks

 

Repeat for 50 times

Get the marks sheet

Add individual subject marks

Display the total marks

Condition controlled loops: while loop

If programmer doesn't know how many times a sequence has to be repeated? In such situations programmer use the until condition loop or while condition loop.

Counting the balls in a bag is a good example for this type of loop.

Open the bag

Repeat until the bag is empty

Put your hand in the bag

Draw one ball out

The until loop or while loop is condition controlled loop which repeat until the condition is reached.

Summary

The repetitive tasks can be handled better by using loops.

The loops can have fixed number of repetitions.

The loops can have conditioned number of repetitions [until a condition is met].

PS: I have composed this post-series based on my pdf eBook on playstore:

Basics of Computer, Programming and C

The series is posted continuously one each next day, and reader can found any post by referring blogs’ time line tab.

--till next post, bye-bye and take care.

 

Friday, June 4, 2021

Storage and Variables

The Human’s usually forgot something to do, due to busy schedule or work load.

We may don’t remember to pay bill?

We may forget to mention last-minute tip in our presentation!

We may forget to switch off veranda light.

But with computer, it doesn’t happen. Because they don’t forgets anything, if properly programmed.

Asking the computer to remember

Let us try telling the computer to remember Tom’s name.

OK Computer:

Remember Tom as my_name

Display my_name

 

Output:

     Tom

 

In this oversimplified example, we asking the computer to remember Tom’s name and display it finally.

The computer will remember Tom’s name until we tell it to store a different name.

Variables

To remember things, computers use memory boxes called variables.

Like boxes, variables have a label and store content in them.

The sequence for storing an animal name in a variable [memory box]

OK Computer [main()]:

Create my_pet

Remember “Pug” as my_pet

Display my_pet

 

Output:

     Pug

Here the label of variable is my_pet and its content is Dog.

In c language, if programmer wants to implement above task into his code form then below is the details:

main()

{

     char *my_pet;

     my_pet = “Pug”;   

puts(my_pet);

}

Output:

     Pug 

 

In computer language, labels have to be named with some rules, such as:

  • Labels have to be single letter or single word.
    • I, x, my_pet, Rocky_Handsome etc. are valid labels
  • Labels may contain or start with underscore and not with any other symbols.
    • _animal, height_ etc. are valid labels
    • My_Quest?, /_age etc. are invalid labels
  • Labels may contain numbers, but not start with it.
    • student_1, box27 etc. are valid labels
    • 1969, 10100,1_must etc. are invalid labels

In simple terms, labels named with a-z, A-Z and underscore symbol. They don’t have to be real words, but it’s smart to use meaningful labels.

Changing values of variables

Can variables change values?

Let’s find out:

OK Computer[main()]:

Create my_name

Remember “Rocky Handsome” as my_name

Remember “Tom” as my_name

Display my_name

 

Output:

     Tom

Here my_name is the label of variable, which initially stores Rocky Handsome then Tom. So when display command is called computer prints Tom as my_name.

In c language, if programmer wants to implement above task into his code form then below is the details:

main()

{

     char *my_name;

     my_pet = “Rocky Handsome”;   

puts(my_name);

my_pet = “Tom”;   

puts(my_name);

 

}

Output:

     Rocky Handsome

     Tom 

As we saw here, the variables are capable of changing values as the program runs. So we can use variables to assign values and keep track of things.

When we use storage?

During the programming, programmer needs to store some information for future manipulation. In such times storage is a necessary option.

Usually we need to store different kinds of or different types of information. Most widely used information types are numbers and words.

Using variables

Most of the computers are great in carry out arithmetic tasks.

OK Computer [main()]:

Crete my_number

Remember 10 as my_number

Add 50 to my_number

Subtract 10 from my_number

Multiply my_number by 3

Divide my_number by 5

Display result

 

Output:

     30

In c language, if programmer wants to implement above task into his code form then below is the details:

main()

{

     int my_number;

     my_number = 10;   

my_number = my_number + 50;

my_number = my_number - 10;

my_number = my_number * 3;

my_number = my_number / 5;   

printf("%d", my_number);

 

}           

Output:

     30

    

Note that even if we were mathematicians, it would be hard to beat the speed and accuracy of computers.

Storing text

Unlike numbers, words or strings have (" “) double quotation around them and can't be used for arithmetic operations. They are used to store text like information.

Hey Computer:

Remember my_pet as “Dog”

Add “Cat” to my_pet

Display my_pet

 

Output:

     DogCat  [not Dog Cat]

Here the two structures are combined without space. As spaces are also part of strings we have to add them if we want to see them in the result.

Hey Computer:

Remember my_Greet as “Hello ”

Add “world!” to my_Greet

Display my_Greet

 

Output:

     Hello world!

Lists

As we saw, variables are like memory boxes which store values. What if we want to store multiple values? This is where we use boxes with space to store multiple values: also called as lists.

Hey Computer:

Create a list

Add “Dog” to the list

Add “Pug” to the list

Display list

 

Output:

     Dog

Pug

In c language, if programmer wants to implement above task into his code form then below is the details:

#include <string.h>

main()

{

    char *my_pet[2];

    my_pet[0]="Pug";

    my_pet[1]="Tug";

    puts(my_pet[0]);

    puts(my_pet[1]);

 

}           

Output:

     Pug    

     Tug

Notice that? Lists are suitable to store multiple values, particularly when they are of the same type.

For example list of phone numbers which is numbers list, and list of students name which is text list.

Summary

What are variables?

  • They are memory boxes with labels
  • Computers use them to store and remember stuff
  • They can change their values

 

PS: I have composed this post-series based on my pdf eBook on playstore:

Basics of Computer, Programming and C

The series is posted continuously one each next day, and reader can found any post by referring blogs’ time line tab.

--till next post, bye-bye and take care.

 

Thursday, June 3, 2021

Getting started with C

How compiled program works? a old-traditional “hello world!”

Since C is a compiler based programming language, executable program must be created in two stages:

1. First, create source code by writing instructions in it and save it as .c file.

2. Second, compile this source file and generate binary file or executable file.

 

The C is called as POP or Procedural Oriented Programming language. It means it contains procedures or functions as main building blocks. One such function is called main(), a zero level or driver function.


Always in C, program is addressed to the driver function main(). Hence the pseudo code is as follows:

OK Computer [main()]:

Do nothing, no tasks

 

Output:

     [Nothing happens, no output]

is written in c language as:                                         

 main()

{

//Do nothing, no tasks

     }

Output:

     [Nothing happens, no output]

Here ‘//’ indicates comment, used for documentation of the code. It will not take part in actual program execution. So nothing prints here.

 

Suppose programmer wants to greet his user a “Hell World!” message, then he can write it as follows:

OK Computer (main()):

Display “Hello World!”

 

Output:

     Hello World!

is written in c language as                                          

 main()

{

    puts("Hello World!");

}

Output:

     Hello World!  

Here the puts() function is another one supplied by C language for putting string on display window. Before using it programmer needs to add the or include the library file stdio.h where puts()is declared.

So the actual implementation c code for above example is as follows:

#include <stdio.h>

void main()

{

    puts("Hello World!");

}

Output:

          Hello World!

Copy above code and paste it in new code file called main.c [or type it], compile it and run it to see the greeting on console window.

The above programs code line explanation is as follows:

-including library file or header file stdio.h from c compilers include folder.

-addressing to main() function which returns nothing [void] to operating system.

-code block starts of main()function.

-calling puts()function with argument “Hello World!” once.

-code block ends of main()function.

 

PS: I have composed this post-series based on my pdf eBook on playstore:

Basics of Computer, Programming and C

The series is posted continuously one each next day, and reader can found any post by referring blogs’ time line tab.

--till next post, bye-bye and take care.

 

Wednesday, June 2, 2021

Think like a programmer

The program means to solve the problems by giving list of instructions to computers!

What are the steps in solving the problem?

  1. Define
  2. Break the problem into smaller parts and analyze.
  3. Plan the steps for solution
  4. Implement the solution.

This list of instructions is called as sequences.

For our all day-to-day situations, we create sequences and then implement it automatically.

The sequences are the way human brain works or forms when it finds any problem with its solution.

For example Tom’s feeling hungry?

Tom’s steps or sequences:

  1. Open the fridge
  2. Get the food out
  3. Close the fridge
  4. Eat the food

Done!

We use similar sequence of steps in programming.

Let us see some more examples or tasks where sequences are needed:

  1. Visiting a General Store
  • Ask the Store owner: Grocery is there?
  • If yes, ask for the listed items are available and their prices.
  • If satisfied, ask store owner to pack them all.
  • Pay the final bill and carry the Grocery to home.

 

  1. Paying School Fees of a Child
  • Visit the School Office and ask child’s fees details to clerk.
  • Go to Cash counter, pay the fees and get the receipt.
  • Show the receipt to the clerk, and ensure the paid entry

 

  1. Saying what’s the Time now?
  • Person asks the time please?
  • Open the mobile, tap the screen
  • See the time and tell him or show the mobile screen to him.

 

  1. Changing TV Channel
  • Take the TV Remote in hand.
  • Check presently which channel is running on TV.
  • Decide how to move to your desired channel.
  • Directly press the desired channel number on TV Remote
  • Or press menu button, navigate to desired channel and press OK.

Finally the simple, yet famous sequence to make a burger what would be the steps?

  1. Take two buns
  2. Place Patty and Veggies over a bun
  3. Place the other bun over the first bun.

Summary

The computers works using programs and programs are sequence in which set of instructions are involved.

So, programs are nothing but sequence of instructions.

The programming is solving a problem with steps of instructions.

PS: I have composed this post-series based on my pdf eBook on playstore:

Basics of Computer, Programming and C

The series is posted continuously one each next day, and reader can found any post by referring blogs’ time line tab.

--till next post, bye-bye and take care.

 

Tuesday, June 1, 2021

C for Everyone: Introduction

21st century is ruled by digital devices or computers of many sizes. In an average, for each person there are five digital devices working around him. So it is mandatory to learn computer programming fundamentals as basic skill.

Why learn any programming language?

The digital devices needs code or program to work, and such standard code lines are written by skilled programmers.

Since this is digital age and digital devices are observed everywhere, it is mandatory to learn any one programming language to survive.

Why to learn C?

Because it is simple, easy to learn, old language and found everywhere from embedded to robotics, desktop to main frame computers. It is base of many languages! So it teaches or helps to understand any/all programing languages basic in one effort.

Who this C programming topic is for:

-Those who want to start a career in Computer Programming or study Computer Science stream.

-Those who want to know how a computer performs a task in a simple and easy way.

-Teachers, Parents, Students, Educators, Anyone Interested in Real Coding [not just block coding].

Benefits

-Reader understands important concepts of C programming and its fundamentals in simple terms.

-Learning C, the most popular and used programming language with any means [mobile app, online C IDE or Computer IDE].

Requirements

Computer or Android mobile device

Internet connection

Motivation to see reader fall in love with coding

 

If you are new to programming domain, ask help of any college going Student, or Computer Lab instructor or Internet cafe people [for search and install proper C tool in your device, and its operation] environment setup. Or search You tube for “how to learn C” tutorial videos.

All the discusses c codes are tested on Code::Blocks 20.03 IDE, DevC++ 6.3, Cygwin gcc 10.02, mobile GCC Editor Cxxdroid App and any online IDE. You can try any one tool of them for practice.

This post series has following contents and read them in any order:

Think like a programmer

Getting started with C

Storage & Variables

Repeating Things

Making Decisions

Creating Lists

Grouping Instructions

Avoiding Mistakes

 

PS: I have composed this post-series based on my pdf eBook on playstore:

Basics of Computer, Programming and C

The series is posted continuously one each next day, and reader can found any post by referring blogs’ time line tab.

--till next post, bye-bye and take care.