answersLogoWhite

0

📱

Computer Programming

A category for questions about computer programming and programming languages.

10,506 Questions

What is a quick sort?

Quicksort is a recursive algorithm.

Given an array and indices to the lower and upper bounds of a subset within the array, divide the subset into two subsets around a pivot value then recursively apply the algorithm to each of these subsets until a subset has fewer than 2 elements.

The algorithm begins by checking the size of the given subset. If there are fewer than 2 elements then the subset is sorted. This represents the end condition for this instance of the algorithm since a subset of 0 or 1 elements can always be regarded as being sorted.

If the set has 2 or more elements, however, a pivot element is selected . the pivot is typically the mode of the first, last and middle elements. The pivot is then swapped with the last element so it's out of the way.

Two indices are then instantiated, left and right, one referring to the first element (the lower bound), the other referring to the penultimate element (we ignore the pivot at the end for now).

While the left index is less than the right index, repeat:

1. While left is less than right and the value referred to by left is less than the pivot, increment left. (In other words, search for the first value from the left but no further than the right that is not less than the pivot).

2. While right is greater than left and the value referred to by right is not less than the pivot, decrement right. (In other words, search for the first value from the right but no further than the left index that is less than the pivot).

3. If the left and right indices are not equal, swap the values at those indices.

When the indexes are the same, or left crosses right, the loop ends.

Swap the value at the left index with the last element (the pivot).

At this point, the pivot is now in place and all values less than the pivot are to its left, with all others to its right. The values on either side are not yet sorted, thus we recursively invoke the same algorithm to sort each of these two subsets (excluding the pivot, of course).

Since the set is divided into two smaller subsets upon each invocation of the algorithm, each recursion takes less time because there are fewer elements to divide. The recursions effectively form a binary tree with one recursion per node. Some nodes will terminate earlier than others so the tree will probably be unbalanced. But since the sorting is done in-place, each recursion will unwind automatically leaving behind a sorted subset within the set. When the initial instance of the algorithm terminates, the entire set is sorted.

Menu driven program for selection sort bubble sort and insertion sort in c?

#include<iostream>

#include<time.h>

#include<iomanip>

#include<string>

void swap(int& x, int& y)

{

x^=y^=x^=y;

}

void bubble_sort(int* A, int size)

{

while(size)

{

int n=0;

for(int i=1; i<size; ++i)

{

if(A[i-1]>A[i])

{

swap(A[i-1], A[i]);

n=i;

}

}

size=n;

}

}

void insertion_sort(int* A, int size)

{

for(int i=1; i<size; ++i)

{

int value=A[i];

int hole=i;

while( hole && value<A[hole-1] )

{

A[hole]=A[hole-1];

--hole;

}

A[hole]=value;

}

}

void selection_sort(int* A, int size)

{

for(int i=0; i<size-1; ++i)

{

int j=i;

for(int k=i+1; k<size; ++k)

if(A[k]<A[j])

j=k;

if( i!=j )

swap(A[i],A[j]);

}

}

void sort(int* A, int size, int sort_type)

{

switch(sort_type)

{

case(0): bubble_sort( A, size );

case(1): insertion_sort( A, size );

case(2): selection_sort( A, size );

}

}

int* copy_array(int* A, int size)

{

int* copy=new int[size];

memcpy(copy, A, size*sizeof(int));

return(copy);

}

void print_array(int* A, int size, char* prompt)

{

std::cout<<prompt<<"\t";

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

std::cout<<std::setw(2)<<A[i]<<" ";

std::cout<<std::endl;

}

int get_rand(int range_min=0, int range_max=RAND_MAX)

{

return((int) ((double)rand() / (RAND_MAX + 1) * ((range_max + 1) - range_min) + range_min));

}

int input_char(std::string prompt, std::string input)

{

char ch;

do

{

std::cout<<prompt<<": ";

std::cin>>ch;

}

while(input.find(ch)==std::string::npos);

return(input.find(ch)%(input.size()/2));

}

int main()

{

srand((unsigned) time(NULL));

int size = get_rand( 10, 80);

if( int* A = new int[size] )

{

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

A[i]=get_rand( 1, size );

int choice=input_char("Please select a sorting method:\n[B]ubble, [I]nsert, [S]election", "bisBIS");

std::cout<<"You chose ";

switch(choice)

{

case(0): std::cout<<"bubble"; break;

case(1): std::cout<<"insertion"; break;

case(2): std::cout<<"selection"; break;

}

std::cout<<" sort...\n"<<std::endl;

print_array( A, size, "Before sorting" );

sort(A, size, choice);

print_array( A, size, "After sorting" );

delete [] A;

}

return(0);

}

What is the objective of a structured walkthrough?

The advantage of using a structured approach is that the work and information will remain structured. The information will be stored in such an organized manner there will be little room for mistakes.

Can a Structure contain a Pointer to itself?

Yes, it is quite common.

Example:

struct List {

struct List *Next;

int value;

}

typedef struct List List;

Example2:

typedef struct Tree Tree;

struct Tree {

Tree *left,*right;

int value;

};

How do you decompile a compiled program?

Basically, you can't. It would be like trying to take a baked cake and turn it back into its ingredients. It is maybe not that severe, but nevertheless very difficult to break a program and find some of its original components. That is why we keep the programs themselves in the form of source code.

But for some technologies it is possible. For example .Net programs can be decompiled.

And there are special tools which allow to do that - .net decompilers.

For an example look at the one at the related link below.

How do you write a program in Python to find the first n prime numbers?

One way to do this is to write a function that checks if a number is prime:

def isPrime(number):

for i in range(2, number):

if number%i == 0:

return False

return True

Then create variables to track how many primes have been found and a variable to track which number is being tested for being prime. Increment that variable and test it, and if it is prime print it out (or save it somewhere) and increment the variable being used to track how many primes have been found:

targetPrimes = 10 #number of primes to find

primesFound = 0 #number of primes found

i = 1

while (primesFound < targetPrimes):

i += 1 #first number tested is 2

if isPrime(i):

print(i)

primesFound += 1

What are the different types of technology available for inputting formatting and editing text?

keyboard scanner word prosseser and to give copies to printer The standard technology to use for input of text is a keyboard and more recently, a scanner. The keys are depressed on the keyboard and whatever the user depresses is transferred to the word processor, built into the office application in use. Using a scanner enables one to manipulate text after converting it from jpeg to Word. A scanner can also save in PDF format and there are programs to convert this to Word as well.

What is a criminal hacker?

Originally the term referred to people who used a razor blade and/or fingernail clippers to "hack" or roughly cut parts in electronics such as the traces on circuit boards, legs of diodes etc. Police scanners were "hacked" in order to add more memory, remove CPU processors , speed up scanning by "hacking" the crystal oscillator and replacing with faster ones. Thes methods are directly related to why computers now have more RAM, and are faster. Some of the old (and I do mean old) "hackers" devised a system in the 1960s to simultaneously send voice and data over a phone line using frequency phase shifting and filtering. A similar process used later by DSL.

"Hacking" and "Modding" evolved into things such as examining the programming of a thing. Beginning with noticing things such that by pressing certain keys or combinations of keys in certain secquences on a device could produce certain results. Following the era of IBM's "Big Blue" chess tournament, AT&T introduced the "unbeatable Tic Tac Toe Computer, and exhibited it. I, I mean a "hacker" back then, was able to beat the computer with a sequence of key presses in order to "cheat" the computer out of a move. On many Shortwave radios, Police Scanners, even cell phones etc, certain key presses are used in the programming, and could be used to alter that programming.

The keys on a keyboard of a device are a "Matrix" Keyboards use a matrix with the rows and columns made up of wires. Each key acts like a switch. When a key is pressed, a column wire makes contact with a row wire and completes a circuit. The keyboard controller detects this closed circuit and registers it as a key press to the software.

I'm going to try to keep this understandable to readers ...

Software can emulate or act like hardware.

Example: a hardware modem versus a DSP/Digital Signal Processor modem, where a chip is programmed to act like the hardware....

So, later, the focus turned even more from "hacking" the actual electronics and hardware to software "hacking" through PROGRAMMING.

A "black hat" hacker is often referred to as a microcomputer user who attempts to gain unauthorized access to proprietary computer systems. Although that could be a "cracker" ... a person cracking passwords etc.

Most often people who call themselves "hackers" are called "wannabes" by the old-timers, and nowadays are often adolescents using a program they didn't write, by clicking a button they didn't create, to do what don't understand, in an attempt to disrupt something they don't know how to fix.

"White Hat" hackers are the "good guys" like ... Like the old-timers who vastly improved what computers are capable of doing, and the ones currently trying to improve security by locating and preventing weaknesses.

This Answer is dedicated to the memory of Bill Cheek, author, The Scanner Modification Handbook series, and his BBS and Fidonet terminal (prior to "The Internet") Good man, Good freind to many... May he R.I.P.

Is c a block structured language?

Structured programming (sometimes known as modular programming) is a subset of procedural programming that enforces a logical structure on the program being written to make it more efficient and easier to understand and modify.

ref:structured programming

Hope this will help u.!!! :)

stucured language mean dividing program in to blocks or parts and executing where one block differs from other so that thr reader can understand the program easy.its just like top-down design approach

Write A program to add and multiply two very large numbers using doubly linked list?

class Node

002

{

003

private:

004

int data;

005

006

public:

007

friend class List;

008

friend class stack;

009

010

Node* next;

011

012

Node(){data = 0;}

013

014

Node(int x)

015

{

016

next = 0;

017

setdata(x);

018

}

019

020

int getdata()

021

{

022

return data;

023

}

024

025

void setdata(int x)

026

{

027

data = x;

028

}

029

030

void print()

031

{

032

cout<<data;

033

}

034

035

~Node(){}

036

};

037

038

class List

039

{

040

private:

041

Node* Head;

042

Node* Tail;

043

044

public:

045

046

friend class stack;

047

List(){Head = 0; Tail = 0;}

048

049

void addnode(int x)

050

{

051

Node *current = new Node(x);

052

053

if(Head 0)

197

{

198

return true;

199

}

200

else

201

{

202

return false;

203

}

204

}

205

206

Node* topEl()

207

{

208

return (L.top());

209

}

210

211

void printstack()

212

{

213

L.printlist();

214

}

215

216

~stack()

217

{

218

L.deletelist();

219

}

220

221

};

222

223

int main()

224

{

225

stack operand1;

226

stack operand2;

227

stack carry;

228

stack result;

229

int tempsum;

230

int a,b,c;

231

int carry1;

232

Node *n1 = NULL;

233

Node *n2 = NULL;

234

Node *n3 = NULL;

235

236

operand1.push(3);

237

operand1.push(8);

238

operand1.push(0);

239

operand2.push(5);

240

operand2.push(3);

241

operand2.push(5);

242

243

244

245

while(!(operand1.isEmpty()) && !(operand2.isEmpty()))

246

{

247

n1 = operand1.topEl();

248

a = n1->getdata();

249

n2 = operand2.topEl();

250

b = n2->getdata();

251

tempsum = a + b;

252

253

if(tempsum > 9)

254

{

255

c = 0;

256

if(!(carry.isEmpty()))

257

{

258

n3 = carry.topEl();

259

c = n3->getdata();

260

carry.pop();

261

}

262

carry1 = tempsum / 10;

263

tempsum = tempsum % 10;

264

carry.push(carry1);

265

result.push(tempsum + c);

266

}

267

else

268

{

269

if(!(carry.isEmpty()))

270

{

271

n3 = carry.topEl();

272

c = n3->getdata();

273

result.push(tempsum + c);

274

carry.pop();

275

}

276

else

277

{

278

result.push(tempsum);

279

}

280

}

281

operand1.pop();

282

operand2.pop();

283

}

284

285

n1 = operand1.topEl();

286

a = n1->getdata();

287

n2 = operand2.topEl();

288

b = n2->getdata();

289

tempsum = a + b;

290

if(carry.isEmpty())

291

{

292

result.push(tempsum);

293

}

294

else

295

{

296

n3 = carry.topEl();

297

c = n3->getdata();

298

result.push(tempsum + c);

299

carry.pop();

300

}

301

operand1.pop();

302

operand2.pop();

303

304

result.printstack();

305

306

system("pause");

307

return 0;

308

}

Is there any electrical current storage device?

Yes, once current starts going through an inductor, it tends to continue going through that inductor in the same direction -- even if that inductor is completely disconnected from everything else.

Unfortunately, all available inductors ( even superconducting magnetic energy storage systems -- see link below ) store relatively little energy.

You can, however, store the means to produce a current. A battery is such a storage device: when charged, it can produce a current in a conductor.

Also, a capacitor stores an electric charge, which when released will produce a current.

How you can find a factorial of a number without using multiple operator?

If you really wanted to do this, you could simulate multiplication with repeated addition.

Why do computers work with bits rather than decimal digits?

Ever wonder what the real numbers are? Numbers are artificial things invented by human, and the same applied to computers. So, the inventors of computers storing human readable numbers (decimal, Roman numerals, etc...) as computer readable numbers (binary). Binary fit very well with the electrical pulses (on and off, as 1 and 0)

What is increment and decrement operators?

increment operator increments the variable by 1 at a time and decrement operator decrements by 1 in this we have two types increments pre_increment and post increment.

In pre_increment the original value is incremented by 1 and assign the new value

n=10

i=++n

then i =11

In post increment the original value is assigned and after it increments value by 1.

n=10

i=n++

then i=10

example:

k=5

i=k++ + ++k

i=?

ans:

in first k++ value is 5

second ++k value is 7

i=5+7=12

What are the different symbols used in a program flow chart?

The most widely used symbols are:

1. Start and end

2. Arrows

3. Processing steps

3. Conditional Other symbols are:

4. document symbol

5. Manual Operation

6. Data file

Hypothetical decimal computer?

Hypothetical Decimal Computer A hypothetical computer is introduced, a decimal one which can operate also in four modes. The machine has 16 bit numbers, with the first bit as sign bit. The exponent is stored as a binary integer and the coefficient is stored as a sequence of decimal digits. In all modes the exponent base is 10. So in fact this computer is a multi-exponent computer with base 10. In listing the four modes are: Exponent coefficient Mode size bits size structureBCD 3 14-12 12 3 BCD-digits of 4 bits each DPD-1 5 14-10 10 3 digits in 10 bit DPD-code DPD-2 3 14-12 12 3 digits in 10 bit DPD-code preceded by one 'digit' of 2 bits PDE value=0, 1, 2 10+3 4 digits, 3 of them in 10 bit DPD-code and one combined with exponent field *****************************************************************Submitted by ADARSH.S.V.NAIR*******************************************************************

How does a high level language relate to the microprocessor's instruction set?

Generally speaking, it doesn't relate at all. If it did, it wouldn't be high-level, it would be machine-dependent. The relationship between the high-level code and the machine-code is ultimately determined by the language translator (compiler and/or interpreter) but, unlike assembly language which maps 1:1 with the microprocessor instruction set, compilers and interpreters are code generators and there is seldom a 1:1 relationship between the high-level source code and the machine code.

What computer language they use to create autocad?

Originally, Computer-Aided Design systems was developed in computer languages such as Fortran, but with the advancement of object-oriented programming methods this has radically changed. Typical modern parametric feature based modeler and freeform surface systems are built around a number of key C (programming language) modules with their own APIs. A CAD system can be seen as built up from the interaction of a graphical user interface (GUI) with NURBS geometry and/or boundary representation (B-rep) data via a geometric modeling kernel. A geometry constraint engine may also be employed to manage the associative relationships between geometry, such as wireframe geometry in a sketch or components in an assembly.

Why is BASIC considered to be a user friendly language?

It's English-like syntax is much easier to learn than many other languages.

What are registers in assembly language?

Registers are memory locations on the microprocessor itself (not in main memory). In RISC architectures generally most operations (add, multiply, etc) must take there input from registers and write their output to a register. Since registers are located directly on the microprocessor, they represent the fastest form of memory in the computer, and also the type of memory available in the least quantity.

What is the advantage of a binary search tree over an array based structure?

In a binary search tree, insertion, deletion and lookup are O(log n) (i.e. fast) when balanced.
With unsorted arrays, insertion and deletion are O(1) (i.e. very fast) but lookup is O(n) (i.e. slow).
With sorted arrays, insertion and deletion are O(n) (i.e. slow) and lookup is O(log n) (i.e. fast).

Binary search trees are good if you do all three operation (insertion, deletion, lookup) often and have enough data to justify the added burden of more complex structures and algorithms.

What are the features of OOP?

The primary features of an object oriented language are:

* Encapsulation - This allows every object to have its own properties and behaviors separate from others, and its own little "capsule" to keep its properties in. * Inheritance - This allows an object to inherit the properties of its ancestors. * Polymorphism - The allows an object to be treated like one of its ancestors, i.e. if Truck was a subclass or descendant of an object called Automobile, it can redo behaviors that Automobile had, like Automobile has a suspension feature that has it set to 10 inches tall, Truck overwrites it and says its 30 inches tall.

* Abstraction - This idea is related to encapsulation in that you don't care how the object works, but you can make it behave in certain manners.

Difference between almost complete binary tree and complete binary tree?

A full tree is a tree where all nodes except the leaves have the maximum number of children. For a BST, that would be two children per node.

A complete tree is the same thing, except that the bottom level does not need to be full. It can be missing leaf nodes, however the ones present must be shifted to the left.