unsigned int Queue::size() {
unsigned int size=0;
Node* n = _head;
if(!isEmpty()){
size =1;
while(n!=_tail) {
size++;
n = n->_next;
}
}
return size;
}
Saturday, 1 December 2012
Week 9_int& operator[](unsigned int index)
int& Queue::operator[] (unsigned int index){
unsigned int size = this->size();
Node* idx = _head;
for(int i=0; i<int(index%size); i++){
idx = idx->_next;
}
return idx->_data;
}
unsigned int size = this->size();
Node* idx = _head;
for(int i=0; i<int(index%size); i++){
idx = idx->_next;
}
return idx->_data;
}
Thursday, 29 November 2012
Week13_Display the environment variable path
#include <iostream>
using namespace std;
int main(int argc, char* argv[], char* env[]) {
int len = strlen(env[0]);
int pos = 0;
int i=0;
while(env[0][i] != '='){
i++;
}
for(pos = i+1;pos<len;pos++){
cout <<env[0][pos];
}
cout<<endl;
return 0;
}
using namespace std;
int main(int argc, char* argv[], char* env[]) {
int len = strlen(env[0]);
int pos = 0;
int i=0;
while(env[0][i] != '='){
i++;
}
for(pos = i+1;pos<len;pos++){
cout <<env[0][pos];
}
cout<<endl;
return 0;
}
Week13_StrCat function - Concatenate optional strings
#include <iostream>
#include <cstdarg>
#include <string.h>
using namespace std;
char* StrCat(char* des, char* src1, ...);
int main() {
char str[500] = "Fred";
StrCat(str, "Soly", "Moly", 0);
cout << str << endl;
return 0;
}
char* StrCat(char* des,char* src1, ...) {
strcat(des, src1);
va_list varg;
va_start(varg, src1);
char* i =va_arg(varg, char*);
while(i!=0) {
strcat(des, i);
i = va_arg(varg, char*);
}
va_end(varg);
return des;
}
output is :
FredSolyMoly
#include <cstdarg>
#include <string.h>
using namespace std;
char* StrCat(char* des, char* src1, ...);
int main() {
char str[500] = "Fred";
StrCat(str, "Soly", "Moly", 0);
cout << str << endl;
return 0;
}
char* StrCat(char* des,char* src1, ...) {
strcat(des, src1);
va_list varg;
va_start(varg, src1);
char* i =va_arg(varg, char*);
while(i!=0) {
strcat(des, i);
i = va_arg(varg, char*);
}
va_end(varg);
return des;
}
output is :
FredSolyMoly
Monday, 12 November 2012
Week11_prnBitOneLn(unsigned int Val)
Writing print bit function in one line as follows:
void prnBitsOneLn(unsigned int Val){
for(unsigned int bitNo = sizeof(Val)*8; bitNo > 0; printf("%d", !!(Val&(1<<(bitNo-1)))),bitNo --);
}
void prnBitsOneLn(unsigned int Val){
for(unsigned int bitNo = sizeof(Val)*8; bitNo > 0; printf("%d", !!(Val&(1<<(bitNo-1)))),bitNo --);
}
Sunday, 11 November 2012
Week11_void bitDump(void* memAddress, unsigned int memSize)
By given memory address and the size, output what the given memory has on the screen:
void bitDump(void* memAddress, unsigned int memSize) {
unsigned char* p = (unsigned char*) memAddress;
int i;
for (i=0; i < memSize; i++) {
unsigned int n = 1 << (sizeof(p)*8 -1);
while(n) {
printf("%c", !!(p[i] && n));
n = n >> 1;
}
}//end forloop
}
void bitDump(void* memAddress, unsigned int memSize) {
unsigned char* p = (unsigned char*) memAddress;
int i;
for (i=0; i < memSize; i++) {
unsigned int n = 1 << (sizeof(p)*8 -1);
while(n) {
printf("%c", !!(p[i] && n));
n = n >> 1;
}
}//end forloop
}
Week11_char *bits(unsigned int Val)
Output bits as a string:
char *bits(unsigned int Val){
// write the above function so it works like this:
// cout<<bits(2345)<<endl;
char* bitstr = new char[sizeof(Val)*8];
unsigned int m = 1 << (sizeof(Val)*8 - 1);
int i =0;
while (m) {
bitstr[i] = 48 + (int)!!(Val & m);
m = m >>1;
i++;
}
bitstr[i]='\0';
return bitstr;
}
char *bits(unsigned int Val){
// write the above function so it works like this:
// cout<<bits(2345)<<endl;
char* bitstr = new char[sizeof(Val)*8];
unsigned int m = 1 << (sizeof(Val)*8 - 1);
int i =0;
while (m) {
bitstr[i] = 48 + (int)!!(Val & m);
m = m >>1;
i++;
}
bitstr[i]='\0';
return bitstr;
}
Week11_setBit(unsigned int val, unsigned int bitNo, bool bitVal)
Sets the "bitNo" bit of val to the "bitVal" value:
unsigned int setBit(unsigned int val, unsigned int bitNo, bool bitVal){
if(bitVal) {
val = val | (1 << bitNo);
}
else {
val = val & ~(1 << bitNo);
}
return val;
}
unsigned int setBit(unsigned int val, unsigned int bitNo, bool bitVal){
if(bitVal) {
val = val | (1 << bitNo);
}
else {
val = val & ~(1 << bitNo);
}
return val;
}
Monday, 10 September 2012
Starting
I am very excited on creating my blog for the first time. I will record every progress and share my experience in software development here and look forward to rapid growth.
Subscribe to:
Posts (Atom)