Vectors ,the Dynamic Arrays “D & A episode #2 ”

Leave a comment

what’s a vector ?

simply not precisely we can say that the vector is a Dynamic array of variables, struct or objects in which you Insert data at the end ; and this shows a Great difference with arrays in which arrays was created with a fixed size , this is simply why would we use a vector

declaring vectors :

  • vector <int> myvector ;   >> declaring an empty vector of integers
  • vector <float > myvector (5)  >> declaring a vector of  floats having 5 spaces
  • vector <int> myvector (10,1) >> declaring a vector of  integers having 10 spaces all initialized with value 1

Accessing elements in a vectors :

we will focus on two ways to access elements inside the vector

the normal method ” indexing ” []  :

each element in the vector has an index beginning with index 0 for the first element  1 for the second and …etc till the final element and we use  square brackets to write the index inside it

myvector[1] ; // to select the second element
myvector[n-1] ; // to select the final element among n elements

the function   at ()   :

the vector class has a member method called at()  it takes the index of the element and returns the element having this element

myvector.at(1) ;  // to index the second element in the vector 

Are both  the same ? :

actually no  because in the first method  no index range checking happens in the standard library’s vector so an access out of  the array  index bounds is not caught by the compiler for example

vector<int> myvector(5,1) ; cout<< myvector[6] ;

this might be caught by compiler as an error and might not , maybe rubbish is printed

(Range check- ing can be done by using at method ;

ie : myvector.at ( i  )  is the same as a  [  i  ] ,  except that an exception is thrown  if  i  is out-of-bounds

How to fill  a vector :

dealing with vectors is the same as dealing with arrays so to fill a certain element in a vector you say
myvector[5] = 13 ;

Inserting and Deleting Elements inside the vector :

  • push_back() method is used to insert a new row in the vector  // it takes the value of the row as an argument  ex : myvector.push_back(15) ;

  • pop_back() method is used to remove the last row in the vector

click the image for larger view

 

Getting the size of  a vector  & changing it :

  • size() method is used to return the size of the vector = the number of elements in the vector
  • resize () takes two arguments  the new size as and the value of the new added elements  , if you didn’t add the second argument the new elements will have a value of zero by default

Advertisements

Getting Started with Jquery Ajax #1

Leave a comment

if you don’t already know what’s AJAX you can check my previous post about : what’s AJAX ?

Starting Jquery AJAX

jquery framework , offers an easy ways to make AJAX calls , alot easier than normal Javascript AJAX

1. “$.get” to handle Get request :

beginning with  some HTML to apply AJAX on  write this in a file called index.html

<body>
<button id="ajaxButton">Click me </button>
<div id = "datahere" style="width:200px; height:200px; background-color:Blue ; ">
</div></body>

make a php Page and call it default.php  , containing this code the response of the PHP page is a Text Box

<?php  echo"<input type="text" /> ; ?>

now to the Jquery Part in the file index.html

$(document).ready(function () {  
  $("#ajaxButton").click(function () {              
     $.get("default.php", function (data) {        
       $("datahere").html(data);  
       $("datahere").html(data);              
       alert(" request is sent sucessfully ");          
     });          
   });      
});
  • . ready is used to execute the code below after the document is Ready
  • attaching a click event on the button
  • $.get  takes two arguments -till now-

  1. the link to the server page to which the request will be sent
  2. call back function executed after the response comes  this call back function takes an argument called data which contains , the response of the server page  (ie : data variable is a string contains the response -the text box in this case – )
  • $(“datahere”).html(data);   // to load the write into the DIV

BY EXECUTION , THE TEXT box will appear inside the Blue box

 

 

 

 

 

 

 

 

till now you can download the Source Files from here : download the source code #1

NEXT PHASE , Sending data in the AJAX Request :

changing alittle bit to the Javascript :

$(document).ready(function () {  
  $("#ajaxButton").click(function () {              
     $.get("default.php",{"name":"hady","age":"20"} , function (data) {        
       $("datahere").html(data);  
       $("datahere").html(data);              
       alert(" request is sent sucessfully ");          
     });          
   });      
});

and changing the PHP code

<?php 
echo "name is".$_GET['name']."<br/> age is : ".$_GET['age'] ;
 ; ?>
  • now we noticed here is that the $.get function takes a middle argument which is a JSON object
  • we can get the value of the sent Json attributes by function  $_GET[”the name of the attribute”] in the PHP code lines

the output will be like this :

 

 

 

 

 

 

 

 

you can download the code lines till now from here : download the source code #2

2. “$.POST” to handle POST request :

actually $.post is simillar to $.get  in everything so no need for repetition  the only difference it it’s responsible for post request to the server and $.get is responsible for the get request

if you don’t know the difference between post and get check this link .

those are not all the Jquery methods to send an AJAX request  acutally there are others that ease sending and requesting of a special kind of data ( JSON , FORM , Javascript …etc )

more will be Explained in Next posts so Stay tuned

What’s AJAX ?

1 Comment

AJAX :

it’s shorthand to ” Asynchronous JavaScript and XML ”

Historical Background :

in the early web stages , websites where all HTML based so any small change in the content of the web page needs the webpage to be all reloaded , this SUCKS  and adds loads to the bandwidth and the server , after Java applets are introduced , an asynchronous data reloading was available also Microsoft introduced in internet explorer 5 the  XMLHTTP control  then later on google deployed the AJAX when it used it in GMAIL and GOOGLE maps

check this out when Ajax what introduced to the public Ajax: A New Approach to Web Applications

\

src : http://www.adaptivepath.com/ideas/essays/archives/000385.php

AJAX Main achievements :

the transfer of data between servers and browsers became asynchronous no need to reload the page for each single change in the page ; you can find something like  suggested links in search Engines  , Google and Bing instance ,

however XML is not the only used for data interfered  often JSON or although other formats such as preformatted HTML or plain text can also be used.instead .

some Drawbacks :

  • clicked links are not saved into the browser history also it’s difficult to bookmark certain page at a certain position , because the pages changes without changing the URL of the main page so if you are building an Ajax gallery for example you can’t bookmark a single page , or go back or forward but alot of developers are Creating techniques to over come this , as by using the Hash Object

 

Ajax frameworks:

Ajax Frame works are  frame works Built to help users to Develop ajax applications Easily AJAX has different Frame works in different languages

  • Javascript AJAX  frameworks : like Jquery , MooTools , dojo toolkit , ASP.net AJAX
  • JAVA Frameworks for server side AJAX :  Google webkit , BackBase , Apache webkit
  • C++ : wt  webkit
  • ASP.NET AJAX : Microsoft Atlas formerly
  • Pyjamas , a widget library with a Python to JavaScript compiler

 

 

 

 

Hello world program in C++ & VS2010 “D&A episode #1”

1 Comment

before going into code we need first to settle some important informations

C++ is originally called C with classes so it’s really a subset of C  , however C++ have a lot more Features than C ; including some basic things like inputs and outputs

do you mean that Scanf () and printf() and getche()  works in C++ ? yeah they do  =)  actually all C functions do work on C++

Great  so that means we don’t need to read about functions , arrays and all the C stuff ,,, yeah however we will elaborate some important things and offcourse things related to data structure

so let’s  code :

in the begining this is a let’s describe how to configure visual studio for a hello world program ,and a sample code to make a simple hello world program

the source code project  from here : Download

  • open new project  cntrl + shift + N
  • click new win 32 console application
  • click on console application  and empty project
  • then on the right click on the solution explorer
  • if you cant find the solution explorer click show it from View next to file in the menu  or click  cntrl +alt + L
  • and write the following in the  main file

  • #include <iostream>
    using namespace std ; 
    void main () { 
     cout << "hello world " ; 
    }
  • click cntrl +f5 to run

NOTES :

  • #include <iostream>    // this to include the libraries for console output  .. has alot of similarities with stdio.h “standard input and output” library in C
  • using  .. this keyword in C++ is like we say assume that we are using , the following so we write using namespace std  , in order not to say  std::cout  and write cout  alone , (we will explain what’s the name space later on )
  • cout is like printf  it takes a string and prints it into the console

How to get a free Microsoft Visual studio 2010

2 Comments

microsoft visual studio 2010

Hello to my new  series of  lessons  “data structure and algorithms using C++ and Ms VisualStudio2010 “

before going through algorithms and Data Structure or even C++ we need First to install an IDE : “integrated development environment ”

is a software application that provides comprehensive facilities to
 computer programmers for software development    src : wikipedia 

the most of  IDEs  mainly consists of  :

  • text editor
  • compiler
  • debuggerr
  • design tools

the IDE main benefit is it’s integrates all the processes of writing code in one program which is the “IDE” as well as configuration to the files and writing a preparation code to let developers start developing at once

some examples of an IDE ,

in the early ages  people used  the command line tools

lately :

Microsoft visual studio  on windows platform  , Eclipse , Netbeans , ActiveState Komodo .

how to get a free microsoft visual studio :

for any user :

you can download visual studio 2010  express version it’s for free but it has  less feature than the other version but for a beginner developer , it doesn’t matter  : http://www.microsoft.com/express/Downloads/#2010-Visual-CPP

and here’s a little comparison about different visual studio version

for students  :

if you have a  live@edu email , -if you don’t have one contact your campus –  you can logi nto Microsoft Dream Spark then you could download a punch of developers stuff including microsoft visual C++ express which is already free as well as Microsoft visual studio 2010  professional edition which is not .

for students in computer science and computer engineering   :

contact your campus MSP –microsoft student partnersfor an MSDNAA account this MSDNAA account provides alot of Free Microsoft stuff only for students that helps them in their study like : windows 7 professional , Ms visual studio professional , expression studio version 3 & version 4  ….etc . if you already have one you could login through here : bit.ly/MSDNAA_account   (only for ASU , engineering Campus  )

….. to be contiued

Newer Entries