Thursday, 3 October 2013

string size is equal to number of characters

string size is equal to number of characters

I was making a basic program of strings and did this. there is a string
int this way
#include<stdio.h>
int main()
{
char str[7]="network";
printf("%s",str);
return 0;
}
It prints network.In my view it should not print network or some garbage
value should be printed because '\0' does not end this character array. So
how it got printed?there were no warning or errors too.

Wednesday, 2 October 2013

Setting a knockout observable using sammy for routing

Setting a knockout observable using sammy for routing

I have a SPA using knockout JS for data binding and sammy for routing. I
have a deck of cards that I am trying to have a dynamic routing to. My
problem is that it doesn't work when I try to set a knockout observable
from the routing function in sammy.
My HTML, where I try to bind the name of the deck, looks like this:
<!-- Create Deck -->
<div id="createDeck" class="page" style="display:none;">
<input type="text" class="form-control" placeholder="Untitled
Deck..." data-bind="value: $root.deck.name" />
</div>
<script type="text/javascript" src="lib/jquery-1.9.1.js"></script>
<script type="text/javascript" src="lib/knockout-2.3.0.js"></script>
<script type="text/javascript" src="lib/bootstrap.min.js"></script>
<script type="text/javascript" src="lib/sammy.js"></script>
<script type="text/javascript" src="js/Models/Deck.js"></script>
<script type="text/javascript" src="js/Models/Card.js"></script>
<script type="text/javascript" src="js/ViewModels/DeckViewModel.js"></script>
<script type="text/javascript" src="js/ViewModels/CardViewModel.js"></script>
<script type="text/javascript" src="js/routing.js"></script>
The Deck.js and DeckViewModel.js looks like below
function Deck(deckid, name, cards) {
var self = this;
self.id = deckid;
self.name = name;
self.cards = cards;
}
function DeckViewModel(deck, cards) {
var self = this;
self.deck = ko.observable(deck);
self.cards = ko.observableArray(cards);
self.goToCard = function (card) { location.hash = card.deckid + '/' +
card.id };
}
// Bind
var element = $('#createDeck')[0];
var deckView = new DeckViewModel(null, null);
ko.applyBindings(deckView, element);
Finally, in my routing I try to create a new Deck, like this:
// Client-side routes
(function ($) {
var app = $.sammy('#content', function () {
this.get('#deck/:id', function (context) {
showPage("createDeck", ": Create Deck");
console.log(this.params.id);
deckView.deck = new Deck(1, "test", null);
console.log(deckView.deck);
});
});
$(function () {
app.run('#/');
});
})(jQuery);
function showPage(pageID, subHeader) {
// Hide all pages
$(".page").hide();
// Show the given page
$("#" + pageID).show();
// change the sub header
$("#subHeader").text(subHeader);
}
As you can see, I'm trying to create a test deck with the name 'test', but
the binding <input type="text" class="form-control" placeholder="Untitled
Deck..." data-bind="value: $root.deck.name" /> seems to bind the letter
'c'.
I'm at a loss, please help.
I tried to make a jsfiddle to demonstrate my problem

Maximum volume of parallelepiped

Maximum volume of parallelepiped

Find the dimensions of the parallelepiped of maximum volume circumscribed
by a sphere of radius R.
I would normally be familiar with this using lagrange multipliers, but how
do I do this? It probably helps that I do not know the volume of a
parallelepiped. Thanks!

Automatically resize JInternalFrame according to the screen resolution

Automatically resize JInternalFrame according to the screen resolution

I am developing the MDI application. JFrame as main window and
JInternalFrame as child windows. How can I make sure that my internal
frame has to re-size automatically when I increase/decrease the screen
resolution.
Scenario: Internal frame is in restore mode and occupied the complete
desktop size.
Now when I increase the screen resolution, I am able to see the additional
desktop area that is not occupied by the internal frame.
Is there any way to auto re-size the internal frame according to resolution?

Print a sequence of numbers using recursion - javascript

Print a sequence of numbers using recursion - javascript

I have this function which prints the numbers from 1 to n in a triangle
like way
function printNumbers(n){
var result = "";
var counter = 1;
while (counter <= n) {
result += counter;
console.log(result);
counter = counter + 1;
}
}
console.log(printNumbers(4));
the result looks like this
1
12
123
1234
I need pointer on how to do this using recursion, because I am new to
programing an I don't have a clue on how to do it.

Tuesday, 1 October 2013

CakePHP select field does not populate

CakePHP select field does not populate

I'm Using cakePHP 2.3.8
I have two tables: application, computer_application. The relationship is
one application hasMany computer_application, foreign key is
application_id
in my ComputerApplication model:
class ComputerApplication extends AppModel{
public $name = "ComputerApplication";
public $useTable = "computer_application";
var $belongsTo = array(
'Computer' => array(
'className' => 'Computer',
'foreignKey' => 'computer_id',
'dependent' => true
),
'Application' => array(
'className' => 'Application',
'foreignKey' => 'application_id',
'dependent' => true
)
);
}
In my ComputerApplication controller. HERE I INITIALIZE THE POPULATION OF
DROPDOWN in **add** function
public function add($id=null) {
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
$this->set('computerApplications',
$this->ComputerApplication->Application->find('list',
array('fields' => array('description') ) ) );
}
Now In my Add View
echo $this->Form->create("computerApplication");
echo $this->Form->input('application_id',array('empty'=>''));
echo $this->Form->end('Save Post');
My problem is that it won't populate the select input. This is the first
time I used 2 words in table [computer_application] using cake since I
don't have problem populating other table with just one word. Just help me
identify which I need to tweak for it to populate.

Angularjs: directive defined controllers communicating with child directives

Angularjs: directive defined controllers communicating with child directives

I think I'm missing something when it comes to directives interacting with
internal controllers. The API documentation is pretty poor on the Angular
site. As far as I can tell, in order to access a controller located within
a parent directive you simply need to set require: '^parentDirective' and
add a fourth argument to the link: attribute on the child directive.
I am unable to access the controller with either $scope or the fourth
argument (I'm using $ctrls).
I wish to be able to push data to an injected service on the parent
directive.
Service:
app.service( 'injectedService', function() {
var service = {
spies: [],
addSpy: function( spy ) {
service.spies.push( spy );
}
};
return service;
});
Parent directive:
app.directive ( 'parentDirective', [ '$window', 'injectedService',
function( $window, injectedService ) {
return {
restrict: 'A',
controller: function( $scope ) {
$scope.test = 'hello';
$scope.addSpy = injectedService.addSpy;
},
link: function( $scope, $element, $attrs ) {
console.log( $scope.test ); //hello
}
};
}]);
Child directive:
app.directive( 'childDirective', [ function() {
return {
restrict: 'A',
require: '^parentDirective',
link: function( $scope, $element, $attrs, $ctrls ){
console.log( $scope.test ); //undefined
console.log( $ctrls.test ); //undefined
console.log( $ctrls.$scope.test ); //undefined
$scope.addSpy({
id: 'test'
}); //error: $ctrls/$scope.addSpy not a function
}
};
}]);