Write a program to perform a binary search on an unsorted list of n integers?
#include<iostream>
#include<iomanip>
#include<vector>
#include<random>
// The node class is used to store a value from the
// unsorted list, along with its index in the list.
// The left node points to a tree of nodes where
// all values are less than this node's value. The
// right node points to a tree of nodes where all
// values are greater than (or equal to) this node.
struct node
{
unsigned value;
unsigned index;
node* left;
node* right;
node (unsigned val, unsigned idx)
: value (val), index (idx),
left(NULL), right (NULL) {}
~node ()
{
delete left;
delete right;
}
void insert (node& n)
{
if (n.value<value)
{
if (left)
left->insert (n);
else
left = &n;
}
else
{
if (right)
right->insert (n);
else
right = &n;
}
}
node* find (unsigned val)
{
if (val == value)
return this;
else if (val < value)
return left ? left->find (val) : NULL;
else
return right ? right->find (val) : NULL;
}
};
// The tree class maintains the root node of a binary
// search tree (BST). The default constructor builds
// a BST from a vector of values. All other methods
// delegate to the root node.
struct tree
{
node* root;
tree (const std::vector<unsigned>& v)
: root (NULL)
{
for (unsigned i=0; i<v.size(); ++i)
{
node* n = new node(v[i], i);
if (!root)
root = n;
else
root->insert (*n);
}
}
~tree ()
{
delete root;
}
node* find (unsigned value)
{
return root ? root->find (value) : NULL;
}
};
// A helper function to determine if a value exists
// in a vector or not.
bool exists(std::vector<unsigned> vect, unsigned num)
{
for (unsigned i=0; i<vect.size(); ++i)
if (vect[i] == num )
return true;
return false;
}
int main()
{
// random number generator (range: 1 to 15)
std::default_random_engine generator;
std::uniform_int_distribution<unsigned> distribution (1, 15);
// create an unsorted list with 10 random numbers and print them
// in the order they are generated
std::vector<unsigned> vect;
std::cout << "Unsorted list: ";
unsigned nums = 10;
while (nums--)
{
do
{
unsigned num = distribution (generator);
// ensure num is unique!
if (!exists (vect, num))
{
vect.push_back (num);
std::cout << std::setw(3) << vect.back();
break;
}
} while (true);
}
std::cout <<std::endl;
// create a binary search tree from the vector
tree bst (vect);
// now go find all the numbers in the tree and show where they
// reside in the list (include all the missing numbers)
for (unsigned num=1; num<=15; ++num)
{
if( node* n = bst.find (num))
{
std::cout << num << " was found in the tree";
std::cout << " and is at index " << n->index;
std::cout << " in the list" << std::endl;
}
else
std::cout << num << " could not be found in the tree" << std::endl;
}
}
How do you store a number of integers entered by the user in an arra with C?
You can use dynamic arrays. Malloc, realloc and free play crucial roles. You can also substitute calloc for malloc. Below is a sample code:
#include
#include
int
main(){
int
n = 10
, noofnumbers = 0
, num, i;
int
*arr = (
int
*)malloc(n*
sizeof
(
int
));
//first creating an array of size 10 - you can even skip this part and directly go for expanding an array every time an element is entered
if
( !arr ){
printf(
"No space in memory\n");
return
(1
);
}
while
( scanf("%d"
, &num) != EOF){
if
( noofnumbers < n){
arr[noofnumbers] = num;
noofnumbers++;
}
else
{
//when more than 10 numbers are added increasing the size accordingly
int
*temp = (int
*) realloc(arr, (++n)*sizeof
(int
));//realloc for reallocating the memory
if
( temp == NULL
){
printf("Not enough space\n"
);
break
;
}
else
{
arr = temp;
arr[noofnumbers] = num;
noofnumbers++;
}
}
}
printf(
"the numbers are:\t");
for
( i = 0
; i
, arr[i]);
printf("\n"
); free(arr);//freeing the memory space
return
0
;
}
What Linked list uses.type of memory allocation?
Linked lists use dynamic memory allocation (also called "heap memory allocation", as the linked list is stored in heap memory).
When no semicolon is used after printf statement what will be the output?
A semi-colon denotes the end of a statement. If you omit the semi-colon, the statement will extend into the next expression and this will result in a compiler error because a semi-colon will have been expected at the end of the first expression.
What is the difference between directions and instructions?
Directions direct you in a direction. Like directions to grandma's house.
Instructions explain how to do something. Like an instructor instructs how to solve a math problem.
If you want to take it literally it seems quite simple.
C program that converts the input dollars into its equivalent peso exchange rate?
void main() { float dlr; float peso(float d); clrscr(); printf("Enter the amount of dollors with you: "); scanf("%f",&dlr); printf("\nYour dollars are equivalent to %.2f peso",peso(dlr)); getch(); } float peso(float x) { return x*51.60; }
Why far pointer is not there in Linux?
Pointer types are very specific to the compiler used and oftentimes subject to the target architecture. The concept of a near/far differentiation may not have any meaning on the memory model of your target. This has nothing to do with Linux or whatever OS you choose to use. More information should be provided with the documentation of your compiler.
How you run C plus plus compiler in windows 2007?
There is no operating system known as "Windows 2007". The only Microsoft operating system released in 2007 was Microsoft Windows Server 2008. Microsoft Windows Vista was released earlier in 2006 and has since been superseded by Microsoft Windows 7 (released in 2009), Microsoft Windows Server 2012, Windows 8 (2012), Microsoft Windows Server 2012 R2 and now Microsoft Windows 8.1 (2013).
As to how you run a C++ compiler on Microsoft Windows (regardless of the version), you first have to download or buy a compiler as well as a linker and a resource compiler. However, compilers and linkers are not generally available separately, they usually come as part of an Integrated Development Environment (IDE), which includes project management facilities and code editors. Your IDE documentation will tell you how to run the built in compiler from the command line, however in an IDE this is seldom necessary as all compilation and linking can be achieved from within the IDE itself.
Why is cross-platform connectivity important to many computer users?
Users want to communicate with one another, without being restricted by incompatibility between different types of computers.
Use the control variable during the execution of the loop?
Control variable which is used in control structures.
e.g. for(int i=0;i<10;i++)
{
// body of loop
}
In above example i is a control variable. The scope of i is only in body of loop.
Out of body the scope of all control variables are lost.
A computer or other device that will communicate with the WWW (world wide web)
Dry run technique in c language?
dry run is nothing but manual compilation of code with an example for written steps to check whether the code for particular problem is solving or not.....if it not happens change code accordingly...........
Is there scared straight programs n Delaware?
There is no specific "Scared Straight" program in Delaware.
However there are similar programs for troubled youths in Delaware that may be worth looking into.
A company called Winggate hosts wilderness boot camps to therapise troubled youths.
There is also a facility called New Castle Region Detention Center, which houses youths and teaches them the correct paths to follow.
You can ask your local authorities for advice and information on such programs in your local area.
How do you read one dimensional array of integers for highest integer?
Sample code is as follows:
#include <stdio.h>
void main()
{
int i = 0;
int final_number = 0;
int array[] = {-2,-6,2,4,1,6,8,20,-55};
for(i = 0; i < (sizeof(array)/sizeof(array[0])); i++)
{
if(array[i] > array[i + 1])
final_number = array[i];
}
printf("%d", final_number);
}
Tried in MinGw:
Output: 20
Why do you need translator according to compiler design?
the PC or computer ONLY UNDERSTAND 0 and 1 (Binary Code) which is a switch between on and off in electric current. if we do not use translators or compilers we will have a hard time trying to program the PC to do something so we use something more understandable(command syntax) to "tell" the PC what to do
Singly list program using data structures in C?
//Singly linked list:
#include<stdio.h>
#include<stdlib.h>
typedef struct list
{
int data;
struct list *next;
}node;
node *head = NULL;
node* create_node()
{
node *new1 =malloc(sizeof(node));
printf("\nEnter data:");
scanf("%d",&new1->data);
new1->next = NULL;
return(new1);
}
void insert_beg()
{
node *new1;
if(head 1)
insert_beg();
else
insert_after();
break;
case 3: search();
break;
case 4: display();
break;
case 5: printf("\nEnter the data to be found");
scanf("%d",&x);
find_prev(x);
break;
case 6: deletion();
break;
case 7: exit(0);
}
printf("\n\n");
}while(ch != 7);
getchar();
return 0;
}
Write a c program to sort an unsorted stack?
A stack is implicitly sorted by hierarchical nested order. It does not make sense to sort a stack.
Do you mean a list? If so, please ask the question again.
Canonical signed digit number representation of a binary number?
is it possible to apply CSD to bough wooley multiplier
What is involved in CEA testing?
Determination of the CEA level is a laboratory blood test. Obtaining a specimen of blood for the study takes only a few minutes. CEA testing should be covered by most insurance plans.
Powers that only Congress can exercise are called what?
Legislative Powers are exclusive to Congress. The Constitution separates the powers of government so that no branch becomes too powerful.
What do you do if it gets ocward between you and your girlfriend?
Tell her that you want to improve your relation ship with her and that you love her . . . . ( but only if you truly do of course )
int maxprod (int n)
{
return n/2;
}
int main (void)
{
int n, a, b;
n= 7; /* for example */
a= maxprod (n);
b= n-a;
printf ("%d+%d=%d, %d*%d=%d\n", a, b, a+b, a, b, a*b);
}
Write algorithm of a largest number in three numbers?
1. Read the 3 nos a,b,c
2. Let larget = a
3. if b > largest then largest = b
4. if c > largest then largest = c.....
If you have to process more nos, read all of them in an array. Assume the first element be largest, do comparison through all elements of the array....
Similar algorithm can be developed for finding the lowest also.
/*lab practice 2 damithguruge question 4 */
#include<stdio.h>
int main()
{
int num1,num2,num3,num4;
int smallest;
printf("Please enter a number1");
scanf("%d%*c", &num1);
printf("Please enter a number2");
scanf("%d%*c" ,&num2);
printf("Please enter a number3");
scanf("%d%*c", &num3);
Printf("Please enter a numbe4r");
scanf("%d%*c", &num4);
/* num1 set as the smallest */
smallest=num1;
if(smallest > num2) then
smallest=num2;
else
if smallest >num3 then
smallest=num3;
else
if smallest>num4 then
smallest=num4;
printf("smallest number:%d\n,smallest");
return(0);
endif
endif
endif
}
What is recursive epistemology?
· Knowledge is produced on the basis of a non-reflexive epistemology (Bateson's "bad" epistemology).
· The knowledge produced is thus assumed to be "objective" and "true" (i.e. independent of the means of its production; ignorant of its genesis).
· This tends to have a psychologically limiting effect with respect to alternative knowledge and especially alternative modes of knowledge production. One's viewpoint becomes ossified.
· The unreflective assumption of the "truth" of the knowledge becomes justification for its projection, often forcefully, onto other people and processes in the environments surrounding the knower. A sense of conviction that the world is "like this" leads to inflexible protocols in our institutions and in our modes of interaction with others. In other words, outer processes are also ossified; they become sclerotic.
Calibration and Self-Calibration.But what does it mean to have a self-reflexive epistemology? The difference between the two types of epistemologies, non-self-relfexive and self-reflexive, can be described by the first and second order difference. A non-self-reflexive epistemology is a first-order epistemology, a process of creating knowledge that operates in a linear fashion. The knowledge it generates is not explicitly connected to the process of its generation, and thus does not act as a potential corrective to its mode of production. Epistemology is a tool for knowing; but with a non-self-reflexive epistemology, the tool's operation does not change the tool, so that no matter what job it is called to do, it re-instances any new creations in the manner and style of its past processes-regardless of what might be new in the situation it encounters. The kind of newness that comes from a linear epistemology is innovative, but not radical. It is well-suited to the kinds of knowledge domains that work towards technical, but not paradigmatic, advances.To use a term from cybernetics, a linear epistemology is not open to calibration. But this lack of openness to calibration is often not simply passive, but active: attempts at calibration are often either discarded or met with increasing rigidity, with a sort of "doubling-down" on the knowledge already produced by the epistemology, and an increasing unwillingness to change the process by which knowledge is produced. We could therefore describe this kind of epistemology as willfully ignorant. This can actually be quite beneficial in producing new knowledge (within the parameters already accepted by the epistemology), because it minimizes the recursive complexity and is more amenable to simplification. Reductionisms of all types, where all phenomena, regardless of their complexity, are explained only in terms of more simple (and often more abstract) entities, are linear epistemologies. Said differently, a linear epistemology allows one to ignore second-order alternatives. Most of modern scientific thinking rests on the back of linear epistemology, to which it is well-suited.
On the other hand, a self-reflexive epistemology is a second-order epistemology (see Table 1 below for a comparison of qualities). The process allows itself to be changed by the content it produces. It is thus an open epistemology that operates on the basis of a recursion between process and content. It is open to calibration, and not simply in a passive way, but actively: it seeks calibration of its own processes through an active monitoring of what it is generating. Whereas a linear epistemology tends to minimize calibrative influences (changes in the way it operates at a second-order level), a recursive epistemology actively embodies them at its heart; it is not merely open to calibration, but is self-calbrative. The linking of process and content in this recursive way is akin to the creation of a new type of sensitivity; we can metaphorically say that operating with a recursive epistemology is the development of a new type of higher sense-organ for a knowing system. This is another way of saying that what a system distinguishes distinguishes its distinguishing. Recursive epistemology is open to self-revision not only at the content level (which is also true of linear epistemologies, although there may still be a difference in their relative inertia to this change), but also at the process level. This means that the kind of newness it can potentially yield includes radical, as well as technical shifts. Radical shifts restructure the further possibilities that are available to the knowing system; they are paradigmatic shifts.
What is important to consider is that the recursion between knowledge and knowing (between content and process) actually applies to allepistemologies, even first-order epistemologies. It is simply that in the case of a first-order epistemology, the epistemology is not systemically inclusive of this fact; we could say that it is not sensitive to its own sensitivity. A second-order epistemology is sensitive to its sensitivities: it includes processes whose content is other processes. The most tightly recursive epistemologies have processes whose content is itself.
The Unknown and Unknowing.Every epistemology has its boundary, a place where it meets a kind of threshold of what it can so far distinguish to itself. Here again we have a first and second-order difference. The first-order level to this threshold is the possible content that could be revealed there, if only our knowing could continue across the threshold. This is the unknown, as a content. The second-order level to the threshold is the ongoing activity of the unknowing.A first-order epistemology meets its boundary only by virtue of the loss of what is seen as its potential content. The unknown is assumed to be a specific content that is just like the known in every way except that has yet to be brought to light, discovered, or found. These metaphors work from the assumption that knowledge is somehow waiting "out there" to be had, and the protocols for knowledge production are thus geared towards the transformation of the unknown into the known, often with forceful manipulation and through maximization of control procedures. The unknown is precisely what needs to be minimized, and processes that generate unknowing are therefore seen as barriers to further knowing, leading only to confusion and unclarity. The unknown is valued only negatively, as the not-yet-known. The not-yet-known is pulled through from the other side of the threshold and brought "down" to re-instance the same reality that the epistemology already operates within, as further proof of its existence. The first-order epistemology focuses on the content of what it produces, and derives from this content the very justification for its truth; it invests its validity in its facts.
A second-order epistemology meets its boundary also by virtue of what is not known, but in addition it values the unknowing as an active and potentially transformative process. What for the first-order epistemology is only a potential new content is for the second-order epistemology a potential new way of being. This is a higher-order content, and is not simply "out there" to be discovered but must be enacted to exist; it must be brought into being. The unknowing is thus taken to be an invitation, a doorway, and instead of taking the unknown and making it known through the same epistemological patterns, it opens the door to more unknowing. Enacting a second-order epistemology is to allow one's way of knowing to change, not just the content of what is known. Unknowing is thus valued as a transformative agent, as a source of not only new knowledge but new ways of living forward. For a second-order epistemology, unknowing is a feature to be actively worked with, even developed, rather than a bug to be squashed. Said another way, a linear epistemology does not know that it does not know, while a recursive epistemology does, and makes of this something new of itself. The active incorporation of processes that yield states of unknowing is a primary way that a recursive epistemology self-calibrates.
Table 1. A comparison of the qualities of linear and recursive epistemologies.
Linear Episetmology
Recursive Epistemology
Non-self-reflexive
Self-reflexive
First-order
Second-order
Process kept independent of content
Process changes in response to content
Truth is objective and "out there"
Truth is a way of relating process and content
Content-focused
Process-focused
Validation reflected by its content
Validation as ongoing content-process recursions
Tends toward technical innovation
Can yield paradigmatic shifts
Knowledge produced as "pieces"
Knowledge produced as "wholes"
Unknown as potentially known content
Unknowing as potentially
transformative process
Observer split from environment
Observer and environment are mutually constituted in one process