Tuesday, December 17, 2019

The multi threading concept in C++ with an example

The multi threading concept in C++ with an example:

#include
#include
using namespace std;
// function to be used in callable
void func_dummy(int N)
 {

  for (int i = 0; i < N; i++) {
  cout << "Thread 1 :: callable => function pointer\n";
   }

 }
 
// A callable object

class thread_obj {

 public:

   void operator()(int n) {

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

           cout << "Thread 2 :: callable => function object\n";

   }

};

int main()
{
// Define a Lambda Expression
auto f = [](int n) {
   for (int i = 0; i < n; i++)
   cout << "Thread 3 :: callable => lambda expression\n";
   };
//launch thread using function pointer as callable
thread th1(func_dummy, 2);
// launch thread using function object as callable
thread th2(thread_obj(), 2);
//launch thread using lambda expression as callable
thread th3(f, 2);
// Wait for thread t1 to finish
 th1.join();
// Wait for thread t2 to finish
th2.join();
// Wait for thread t3 to finish
th3.join();
return 0;
}

Output:

Thread 1 :: callable => function pointer
Thread 1 :: callable => function pointer
Thread 3 :: callable => lambda expression
Thread 3 :: callable => lambda expression
Thread 2 :: callable => function object
Thread 2 :: callable => function object

Programming language : Ruby

Programming language : Ruby


Ruby is an interpreted, high-level, general-purpose programming language.

It was designed and developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan.
A dynamic, open source programming language with a focus on simplicity and productivity.
It has an elegant syntax that is natural to read and easy to write.


Platform support
Matsumoto originally did Ruby development on the 4.3BSD-based Sony NEWS-OS 3.x, but later migrated his work to SunOS 4.x, and finally to Linux.
Ruby versions and implementations are available on many operating systems, such as Linux, BSD, Solaris, AIX, macOS, Windows, Windows Phone,[104] Windows CE, Symbian OS, BeOS, and IBM i.

Features of Ruby:

1.Thoroughly object-oriented with inheritance, mixins and metaclasses
2.Dynamic typing and duck typing
3.Everything is an expression (even statements) and everything is executed imperatively (even declarations)
4.Succinct and flexible syntaxthat minimizes syntactic noise and serves as a foundation for domain-specific languages
5.Dynamic reflection and alteration of objects to facilitate metaprogramming
6.Lexical closures, iterators and generators, with a block syntax
7.Literal notation for arrays, hashes, regular expressions and symbols
8.Embedding code in strings (interpolation)
9.Default arguments
10.Four levels of variable scope (global, class, instance, and local) denoted by sigils or the lack thereof
11.Garbage collection
12.First-class continuations
13.Strict boolean coercion rules (everything is true except false and nil)
14.Exception handling
15.Operator overloading
16.Built-in support for rational numbers, complex numbers and arbitrary-precision arithmetic
17.Custom dispatch behavior (through method_missing and const_missing)
18.Native threads and cooperative fibers (fibers are a 1.9/YARV feature)
19.Support for Unicode and multiple character encodings.
20.Native plug-in API in C
21.Interactive Ruby Shell (a REPL)
22.Centralized package management through RubyGems
23.Implemented on all major platforms
24.Large standard library, including modules for YAML, JSON, XML, CGI, OpenSSL, HTTP, FTP, RSS, curses, zlib and Tk

Repositories and libraries:
RubyGems is Ruby's package manager. A Ruby package is called a "gem" and can easily be installed via the command line. Most gems are libraries, though a few exist that are applications, such as IDEs.
There are over 10,000 Ruby gems hosted on RubyGems.org.
Many new and existing Ruby libraries are hosted on GitHub, a service that offers version control repository hosting for Git.
The Ruby Application Archive, which hosted applications, documentation, and libraries for Ruby programming, was maintained until 2013, when its function was transferred to RubyGems.

Date and time java script program

<!DOCTYPE html>
<title>Time and Date</title>


<time id="msg"></time>

<script>
  document.getElementById("msg").innerHTML = new Date().toLocaleString();
</script>

JavaScript program to create a JavaScript object to show the current time

<!DOCTYPE html>
<title>Current time</title>

<time id="time"></time>
<script language="javascript">
  /*Create a JavaScript object for the current time,then extract the desired parts, then join them again in the desired format.*/

  var currentTime = new Date(),
      hours = currentTime.getHours(),
      minutes = currentTime.getMinutes();
 
 
  if (minutes < 10) {
    minutes  = "0" + minutes;
  }

 
var suffix = "AM";
if (hours >= 12) {
    suffix = "PM";
    hours = hours - 12;
}
if (hours == 0) {
hours = 12;
}


  time = hours + ":" + minutes + " " + suffix;
     
  // Output
  document.getElementById("time").innerHTML = time;
</script>

JavaScript program for multiplication table

<html>
<head>
  <title>Multiplication Table</title>
  <script type="text/javascript">
    var rows = prompt("How many rows for your multiplication table?");
    var cols = prompt("How many columns for your multiplication table?");
    if(rows == "" || rows == null)
    rows = 10;
    if(cols== "" || cols== null)
    cols = 10;
    createTable(rows, cols);
    function createTable(rows, cols)
    {
      var j=1;
      var output = "<table border='1' width='500' cellspacing='0'cellpadding='5'>";
      for(i=1;i<=rows;i++)
      {
    output = output + "<tr>";
        while(j<=cols)
        {
    output = output + "<td>" + i*j + "</td>";
      j = j+1;
    }
    output = output + "</tr>";
    j = 1;
    }
    output = output + "</table>";
    document.write(output);
    }
  </script>
</head>
<body>
</body>
</html>

Monday, December 16, 2019

C program solved practical slips of F.Y.B.Sc. Computer Science Laboratory Course I & II

C program solved practical slips of F.Y.B.Sc. Computer Science Laboratory Course I & II:

C solved practical slips of F.Y.B.Sc. Computer Science

C solved practical slips of F.Y.B.Sc. Computer Science:

Study testing conditions: if, elif, else in python

Testing conditions: if, elif, else :


In Python, we have the if, elif and the else statements for decision making to execute a certain line of codes if a condition is satisfied,and a different set of code in case it is not.


This is the simplest example of a conditional statement. The syntax is:

if(condition):

      indented Statement Block


The block of lines indented the same amount after the colon (:) will be executed whenever the condition is TRUE.


For example, lets say you are recording the score for a certain course.
The total score is 100, with 50 points for the theoretical work and 50 for practical.
You want to display an error message or warning if the score exceeds 100.

score_theory = 40
score_practical = 45
if(score_theory + score_practical > 100):
    print("Invalid score. Please check the input.")
The colon (:) is important because it separates the condition from the statements to be executed after the evaluation of the condition.


The above if statement checks for the 'if' condition and determines the statement
(40 + 45 = 85) > 100 to be FALSE and thus, will not print the warning.
Lets make the statement FALSE and see what happens:

score_theory = 50
score_practical = 55
if(score_theory + score_practical >= 100):
    print("Invalid score. Please check the input.")

Output:
Invalid score. Please check the input.




if-else Statement

 The syntax for the if-else statement is:

if(condition):

      Indented statement block for when condition is TRUE

else:

      Indented statement block for when condition is FALSE


example:
score_theory = 40
score_practical = 45
if(score_theory + score_practical > 100):
    print("Please check the input. Score exceeds total possible score.")
else:
    print("Score validated. Your total is: ", score_theory + score_practical)




Output:

Score validated. Your total is:  85



Multiple tests: if-elif-else Statement:


The syntax followed by the if-else-if statement is:

if(Condition1):
      Indented statement block for Condition1
elif(Condition2):
       Indented statement block for Condition2
else:
       Alternate statement block if all condition check above fails



Example:
coursework = "English"
score_theory = 53
score_practical = 35

if(coursework == "Science" or coursework == "science"):
    if(score_theory > 50):
        print("Please check the input score for 'Science: Theory'.")
    elif(score_practical > 50):
            print("Please check the input score for 'Science: Practical'.") 
    else:
        print("Score validated for Science. Your total is: ",score_theory + score_practical)           
elif(coursework == "English" or coursework == "english"):
    if(score_theory > 60):
        print("Please check the input score for 'English: Theory'.")
    elif(score_practical > 40):
            print("Please check the input score for 'English: Practical'.") 
    else:
        print("Score validated for English. Your total is: ",score_theory + score_practical)
else: print("Coursework not recognized. Please enter score for either Science or English.")

Output:
Score validated for English. Your total is:  88

Variables and objects in python

Learn variables and objects in python:

In Python, values are stored in objects.
If we do
d = 10.0
a new object d is created. As we have given it a floating point value (10.0) the object is of type floating point.
If we had defined d = 10, d would have been an integer object.
In other programming languages, values are stored in variables.
This is not exactly the same as an object, as an object has "methods", that means functions that belong to the object.

There are many object types in Python.

The most important to begin with are:

Object type: Integer
Type class name: int
Description: Signed integer, 32 bit
Example: a = 5

Object type: Float
Type class name: float
Description: Double precision floating point number, 64 bit
Example:b = 3.14

Object type: Complex
Type class name: complex
Description: Complex number
Example: c = 3 + 5j
  c= complex(3,5)


Object type: Character
Type class name: chr
Description:  Single byte character
Example: d = chr(65)
d = 'A'
d = "A"

Object type: String
Type class name: str
Description: List of characters, text string
Example: e = 'LTAM'
e = "LTAM"

A python program to ask the user for his name and greets him

Write a python program to ask the user for his name and greets him:

Answer:
s = raw_input("What is your name?")
print "HELLO ", s

Output:
What is your name?Tom
HELLO Tom

Featured posts

Happy Independence Day August 15th

 Here's a message for India's Independence Day (August 15th): "शुभ स्वतंत्रता दिवस! आजादी की 79वीं वर्षगांठ पर, आइए हम अपने देश...

Popular posts