Why is searching in a sorted list faster than searching in an unsorted list?
With an unsorted list you have to search from the beginning to locate the item you're looking for. This is a linear search which takes linear time O(n) for n items. That is, the best case is constant time O(1) if the item you're looking for is the first item, it is O(n) if it is the last item (or the item doesn''t exist). Thus, on average, searches will take O(n/2) if the item exists, but always takes O(n) if it doesn't.
With a sorted list you start in the middle of the list. If the item you're looking for is less than this item, you can safely discard the upper half of the list but if it is greater you can discard the lower half. In other words you reduce the number of items to be searched by half. You then repeat the process with the remaining half. If the item you're looking for is equal to the middle item, you're done, but if the remaining half has no items the item does not exist. This is a logarithmic search with a best case of O(1), a worst case of O(log n) and an average case of O((log n)/2).
Logarithmic searches are clearly faster, on average, than linear searches. However, logarithmic searches require two separate comparison operations (equal to and less than) while linear searches require just one (equal to). Thus for a list of three or four items, a linear search is arguably quicker. But for larger lists, logarithmic search is quicker. Note that we don't need to test for greater than since it can be assumed that if an item is neither less than or equal to another item, then it must be greater.
What language is pseudocode written in?
Pseudocode is more a process description than actual code. Since it is just describing the logic and processes of a proposed program it can be written in ANY written language, note that while it could theoretically be written in a "computer language", that sort of defeats the purpose of the pseudo code which intended for human reading rather than machine reading.
How would you write an algorithm to calculate the average number of vowels in a passage?
String passage= new String("blah blah blah");
int vowelSum=0;
for(int i=0;i<passage.length();i++)
if(passage.charAt(i)=='a' passage.charAt(i)=='e' ...etc....
vowelSum+=1;
System.out.println(vowelSum);
That is a basic idea and in no way the best possible way to do it.
JBuck
When do you use the nested if?
we use "nested if" if we have to test a large number of possibilities and trials i an if statement.
Tech republic is cool.
You can go for google or yahoo alerts.
Hang around on various forums.
The POST is a program essentially and it's "located" on the motherboard. The software gets run when you first turn the computer on.
In the binary system what is the decimal number 2 represented by?
I'm pretty sure binary is just 1's and 0's
What is polynomail time algorithm?
That means, roughly speaking, that for any input of size "x", the algorithm will take no longer than xn for some constant "n".
What do you mean by parity in RAID?
In computers, parity (from the Latin paritas: equal or equivalent) refers to a technique of checking whether data has been lost or written over when it's moved from one place in storage to another or when transmitted between computers.If you lose any one of the "N+1" pieces of data, you can recreate it from the "N" that remain, ... This property is exploited for parity calculation under RAID. ... This means a hardware controller that performs these calculations is required for high.
Asheesh jain
New Delhi
How do you write an output without using printf function in c?
Yes. However, C is a type-sensitive language thus PRINTF and printf would need to be defined as separate functions. However, names using all caps are conventionally used to denote macros in C thus you can easily define PRINTF as an alias for printf:
#define PRINTF printf;
int main (void) {
PRINTF ("%d", 42);
}
The C precompiler will substitute all instances of the symbol PRINTF with printf, thus the code seen by the compiler will become:
int main (void) { printf ("%d", 42);
}
// Set up a Scanner class to read from standard input
final Scanner in = new Scanner(System.in);
// Prompt for input and read the next integer
System.out.print("Please enter a positive integer: ");
int n = in.nextInt();
// Since we want to print out n lines, iterate n times
for (; n > 0; --n) {
// On each iteration, print out the numbers [1,n]
for (int i = 1; i <= n; ++i) {
System.out.print(i + " ");
}
System.out.println();
}
// Remember to close open resources when we're done.
in.close();
Is the First Generation Language machine dependent?
Yes. Both first and second generation languages are machine-dependent. The first generation of languages were machine code, while the second were assembly languages. Non-machine dependency came about with the advent of the third-generation of languages, all the high-level languages.
How do you create a desktop shortcut for Paint?
In order to create a desktop shortcut for Paint in Windows XP, you will need to go to: Start > Accessories and Right Click on Paint. From the new window, click on "Send to Desktop (Create Shortcut)". Paint should now be available from the desktop.
You are probably referring to a resource leak, however a resource leak specifically relates to programs that allocate memory on the heap (the free store) but fail to keep track of it. If we lose track of a heap allocation, the programmer cannot release that memory back to the system until the program terminates. The following is an example of a resource leak:
void f (unsigned x) {
void* p = malloc (x); // allocate x bytes // ... use memory
}
Each time we call this function we allocate another s bytes on the heap. But the function never releases that memory and because p is local to the function, we have no way of knowing where that memory was allocated (p will fall from scope when we return from the function). If we call this function often enough, we will eventually run out of memory.
To fix the problem, we must release the memory as soon as we are finished with it:
void f (unsigned x) {
void* p = malloc (x); // allocate x bytes // ... use memory
free (p); // release the memory
p = NULL; // zero the pointer
}
It is not necessary to zero a pointer when the pointer will fall from scope anyway, but it is considered good programming practice. What is important is that we release the memory it refers to before it falls from scope. Keeping our scopes as narrow as possible helps us to manage our resources more easily; the larger the scope, the more easily we can inadvertently introduce resource leaks, particularly when our code is maintained by several different programmers.
The problem you describe isn't necessarily a resource leak but it is clearly wasteful to allocate memory that is left unused even if we don't create a resource leak. Good programming practice dictates that we release memory as soon as we no longer require it, however allocating memory on an as-required basis can impact upon performance so it is not unusual for a programmer to allocate more memory than he needs, thus creating a reserve that can be used as and when required. Not using a reserve isn't in itself a problem, however we should endeavour to keep our reserves to a minimum and return all reserved memory to the system in a timely manner -- as soon as we no longer require it.
Algorithm
Step1: Read A, B, C
Step2: If A > B is True, then check whether A > C, if yes then A is greatest otherwise C is greatest
Step3: If A > B is False, then check whether B > C, if yes then B is greatest otherwise C is greatest
Give the Flowchart Answer
Write a program to implement tower of hanoi in openGL?
/*Answer by weerdhaval chowgule this is the new edited version of the previous certain errors in push function and headers are included */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<iostream>
#include <glut.h>
using namespace std;
double WIDTH = 800;
double HEIGHT = 800;
GLboolean motion = GL_TRUE;
GLboolean back_wall = GL_FALSE;
GLint xangle = 0, yangle = 0;
GLint xlangle = 0, ylangle = 0;
#define other(i,j) (6-(i+j))
#define wallz -(WIDTH/2)
#define DISK_HEIGHT 20
int NUM_DISKS = 5;
#define CONE NUM_DISKS+1
#define WALL CONE + 1
#define HANOI_SOLVE 0
#define HANOI_QUIT 1
#define HANOI_LIGHTING 2
#define HANOI_WALL 3
#define HANOI_FOG 4
GLfloat lightOneDirection[] =
{0, 0, -1};
GLfloat lightOnePosition[] =
{200, 100, 300, 1};
GLfloat lightOneColor[] =
{1.0, 1.0, 0.5, 1.0};
GLfloat lightTwoDirection[] =
{0, 0, -1};
GLfloat lightTwoPosition[] =
{600, 100, 300, 1};
GLfloat lightTwoColor[] =
{1.0, 0.0, 0.3, 1.0};
GLfloat lightZeroPosition[] =
{400, 200, 300, 1};
GLfloat lightZeroColor[] =
{.3, .3, .3, .3};
GLfloat diskColor[] =
{1.0, 1.0, 1.0, .8}, poleColor[] =
{1.0, 0.2, 0.2, .8};
typedef struct stack_node {
int size;
struct stack_node *next;
} stack_node;
typedef struct stack {
struct stack_node *head;
int depth;
} stack;
stack poles[4];
void
push(int which, int size)
{
int i;
stack_node *n = (stack_node* ) malloc(sizeof(stack_node));
if (!n) {
fprintf(stderr, "out of memory!\n");
exit(-1);
}
n->size = size;
n->next = poles[which].head;
poles[which].head = n;
poles[which].depth++;
}
int
pop(int which)
{
int retval = poles[which].head->size;
stack_node *temp = poles[which].head;
poles[which].head = poles[which].head->next;
poles[which].depth--;
free(temp);
return retval;
}
typedef struct move_node {
int t, f;
struct move_node *next;
struct move_node *prev;
} move_node;
typedef struct move_stack {
int depth;
struct move_node *head, *tail;
} move_stack;
move_stack moves;
void
init(void)
{
int i;
for (i = 0; i < 4; i++) {
poles[i].head = NULL;
poles[i].depth = 0;
}
moves.head = NULL;
moves.tail = NULL;
moves.depth = 0;
for (i = 1; i <= NUM_DISKS; i++) {
glNewList(i, GL_COMPILE);
{
glutSolidTorus(DISK_HEIGHT / 2, 5 * i, 15, 15);
}
glEndList();
}
glNewList(CONE, GL_COMPILE);
{
glutSolidCone(10, (NUM_DISKS + 1) * DISK_HEIGHT, 20, 20);
}
glEndList();
}
void
mpop(void)
{
move_node *temp = moves.head;
moves.head = moves.head->next;
free(temp);
moves.depth--;
}
void
mpush(int t, int f)
{
move_node *n = (move_node *) malloc(sizeof(move_node));
if (!n) {
fprintf(stderr, "Out of memory!\n");
exit(-1);
}
n->t = t;
n->f = f;
n->next = NULL;
n->prev = moves.tail;
if (moves.tail)
moves.tail->next = n;
moves.tail = n;
if (!moves.head)
moves.head = moves.tail;
moves.depth++;
}
/* ARGSUSED1 */
void
keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27: /* ESC */
case 'q':
case 'Q':
exit(0);
}
}
void
update(void)
{
glutPostRedisplay();
}
void
DrawPost(int xcenter)
{
glPushMatrix();
{
glTranslatef(xcenter, 0, 0);
glRotatef(90, -1, 0, 0);
glCallList(CONE);
} glPopMatrix();
}
void
DrawPosts(void)
{
glColor3fv(poleColor);
glLineWidth(10);
glMaterialfv(GL_FRONT, GL_DIFFUSE, poleColor);
DrawPost(WIDTH / 4);
DrawPost(2 * WIDTH / 4);
DrawPost(3 * WIDTH / 4);
}
void
DrawDisk(int xcenter, int ycenter, int size)
{
glPushMatrix();
{
glTranslatef(xcenter, ycenter, 0);
glRotatef(90, 1, 0, 0);
glCallList(size);
} glPopMatrix();
}
void
DrawDooDads(void)
{
int i;
stack_node *temp;
int xcenter, ycenter;
glColor3fv(diskColor);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diskColor);
for (i = 1; i <= 3; i++) {
xcenter = i * WIDTH / 4;
for (temp = poles[i].head, ycenter = DISK_HEIGHT * poles[i].depth - DISK_HEIGHT / 2; temp; temp = temp->next, ycenter -= DISK_HEIGHT) {
DrawDisk(xcenter, ycenter, temp->size);
}
}
}
#define MOVE(t,f) mpush((t),(f))
static void
mov(int n, int f, int t)
{
int o;
if (n == 1) {
MOVE(t, f);
return;
}
o = other(f, t);
mov(n - 1, f, o);
mov(1, f, t);
mov(n - 1, o, t);
}
GLfloat wallcolor[] =
{0, .3, 1, 1};
void
DrawWall(void)
{
int i;
int j;
glColor3fv(wallcolor);
for (i = 0; i < WIDTH; i += 10) {
for (j = 0; j < HEIGHT; j += 10) {
glBegin(GL_POLYGON);
{
glNormal3f(0, 0, 1);
glVertex3f(i + 10, j, wallz);
glVertex3f(i + 10, j + 10, wallz);
glVertex3f(i, j + 10, wallz);
glVertex3f(i, j, wallz);
}
glEnd();
}
}
}
void
draw(void)
{
int t, f;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (back_wall) {
glMaterialfv(GL_FRONT, GL_DIFFUSE, wallcolor);
DrawWall();
}
glPushMatrix();
{
glTranslatef(WIDTH / 2, HEIGHT / 2, 0);
glRotatef(xlangle, 0, 1, 0);
glRotatef(ylangle, 1, 0, 0);
glTranslatef(-WIDTH / 2, -HEIGHT / 2, 0);
glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition);
}
glPopMatrix();
glPushMatrix();
{
glTranslatef(WIDTH / 2, HEIGHT / 2, 0);
glRotatef(xangle, 0, 1, 0);
glRotatef(yangle, 1, 0, 0);
glTranslatef(-WIDTH / 2, -HEIGHT / 2, 0);
DrawPosts();
DrawDooDads();
}
glPopMatrix();
if (motion && moves.depth) {
t = moves.head->t;
f = moves.head->f;
push(t, pop(f));
mpop();
}
glutSwapBuffers();
}
void
hanoi_menu(int value)
{
switch (value) {
case HANOI_SOLVE:
motion = !motion;
if(motion) {
glutIdleFunc(update);
} else {
glutIdleFunc(NULL);
}
break;
case HANOI_LIGHTING:
if (glIsEnabled(GL_LIGHTING))
glDisable(GL_LIGHTING);
else
glEnable(GL_LIGHTING);
break;
case HANOI_WALL:
back_wall = !back_wall;
break;
case HANOI_FOG:
if (glIsEnabled(GL_FOG))
glDisable(GL_FOG);
else {
glEnable(GL_FOG);
glFogi(GL_FOG_MODE, GL_EXP);
glFogf(GL_FOG_DENSITY, .01);
}
break;
case HANOI_QUIT:
exit(0);
break;
}
glutPostRedisplay();
}
int oldx, oldy;
GLboolean leftb = GL_FALSE, middleb = GL_FALSE;
void
hanoi_mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON) {
oldx = x;
oldy = y;
if (state == GLUT_DOWN)
leftb = GL_TRUE;
else
leftb = GL_FALSE;
}
if (button == GLUT_MIDDLE_BUTTON) {
oldx = x;
oldy = y;
if (state == GLUT_DOWN)
middleb = GL_TRUE;
else
middleb = GL_FALSE;
}
}
void
hanoi_visibility(int state)
{
if (state == GLUT_VISIBLE && motion) {
glutIdleFunc(update);
} else {
glutIdleFunc(NULL);
}
}
void
hanoi_motion(int x, int y)
{
if (leftb) {
xangle -= (x - oldx);
yangle -= (y - oldy);
}
if (middleb) {
xlangle -= (x - oldx);
ylangle -= (y - oldy);
}
oldx = x;
oldy = y;
glutPostRedisplay();
}
int
main(int argc, char *argv[])
{
int i;
glutInit(&argc, argv);
for(i=1; i<argc; i++) {
if(!strcmp("-n", argv[i])) {
i++;
if(i >= argc) {
printf("hanoi: number after -n is required\n");
exit(1);
}
NUM_DISKS = atoi(argv[i]);
}
}
glutInitWindowSize(WIDTH, HEIGHT);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Hanoi");
glutDisplayFunc(draw);
glutKeyboardFunc(keyboard);
glViewport(0, 0, WIDTH, HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WIDTH, 0, HEIGHT, -10000, 10000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0, 0, 0, 0);
glClearDepth(1.0);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
/* glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1); */
glLightfv(GL_LIGHT1, GL_POSITION, lightOnePosition);
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightOneColor);
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 10);
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, lightOneDirection);
glEnable(GL_LIGHT1);
glLightfv(GL_LIGHT2, GL_POSITION, lightTwoPosition);
glLightfv(GL_LIGHT2, GL_DIFFUSE, lightTwoColor);
/* glLightf(GL_LIGHT2,GL_LINEAR_ATTENUATION,.005); */
glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, 10);
glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, lightTwoDirection);
glEnable(GL_LIGHT2);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glutMouseFunc(hanoi_mouse);
glutMotionFunc(hanoi_motion);
glutVisibilityFunc(hanoi_visibility);
glutCreateMenu(hanoi_menu);
glutAddMenuEntry("Solve", HANOI_SOLVE);
glutAddMenuEntry("Lighting", HANOI_LIGHTING);
glutAddMenuEntry("Back Wall", HANOI_WALL);
glutAddMenuEntry("Fog", HANOI_FOG);
glutAddMenuEntry("Quit", HANOI_QUIT);
glutAttachMenu(GLUT_RIGHT_BUTTON);
init();
for (i = 0; i < NUM_DISKS; i++)
push(1, NUM_DISKS - i);
mov(NUM_DISKS, 1, 3);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
You used a picture from the web on your personal webpage how do you cite it?
First of all, it's important to know that you can't always take a picture and use it without the permission of the author. Some websites ask that you not reuse their images. Be careful that you are not violating someone's copyright by using their image without their permission. Of course, citing a photo you use (with permission) is just like citing any other source in any other medium: the citation depends on the context and usage: == If you are using an image in a formal context, like an academic work, it's in your best interest to cite the image formaly... for example: "Description or title of image. [Online image] Available http://address/filename, Date." == If you are using an image less formally, a casual mention of the owner or site can be appropriate: "Thanks to Soandso for the image." or "Here's a picture of Soandso that Whatshisface took yesterday." == If you are using a picture as part of the design of a page or in a location where an explicit citation is inconvenient (and you have the author's permission), a brief note in the source code of the page is often appreciated. In any case, you should link to the author's page or provide a link to the picture in its original context whenever possible.
What does .long instruction in powerpc assembly indicate?
.long doesn't really do anything. It just indicates the 32-bit value stored at that instruction's address. So if you have the instruction ".long 0xdeadbeef" at 0x8000, then memory looks like this (assuming big-endian):
0x8000: 0xde
0x8001: 0xad
0x8002: 0xbe
0x8003: 0xef
What is the purpose of SQLPLUS?
SQLPLUS, also written as SQL+ or SQL*Plus and pronounced "sequel plus" is a type of non-compiled (scripted) computer language that is used to write programs for accessing relational databases. The SQL acronym stands for "Simple Query Language" or "Structured Query Language" depending on who you ask. The original language developed by IBM was called SEQUEL (Structured English Query Language) but later changed to SQL due to the name SEQUEL being the trademark of another company. SQL+ is a variant of the original language developed by Oracle Corporation.
GCC is the GNU Compiler Collection (Originally GNU C Compiler) See the related link.
It is a collection of "compiler software", that is used to convert human readable source code into binary programs the computer can actually understand and run. it supports several programming languages like C, C++, Java, FORTRAN, etc.