5 Why Searching is slower in Linked-List than in Trees?
Searching is slower for linked-lists rather than (ordered) trees because you need to iterate through each element serially, whereas for an ordered (binary) tree you use a "divide and conquer", otherwise known as binary search, method.
Think of it this way. Think of a number between 1 and 128. Perhaps the number is 97. With an ordered list, it would take 97 comparisons to find the item. With an unordered list, it would take an average of 64 comparisons - if the order was random. With a binary tree, i.e. a binary search, you only need 7 comparisons. In fact, you never need more than 7 comparisions for a tree size of 128, and on average, you would use less than that.
How do you print the following pattern in C language X X X X X X X X X?
There are a few ways of going about this. The easiest way would be through a literal string:
printf("X X X X X X X X X");
If you want to print an arbitrary number of X's (in this case, 9), a for() loop would work:
for (count=0; count<9; count++) printf("X ");
This code would generate a final space at the end. To fix this and only use spaces to separate X's:
for (count=0; count<9; count++) printf("%sX", count?" ":"");
How do you write a program to find whether the given number is prime or not and show output?
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a=2;
clrscr();
printf("\n enter n");
scanf("%d",&n);
if(i<=a-1)
{
if(a%i==0)
{
printf("\n the given number is not a prime number");
break;
}
i++;
if(a==i)
{
printf("\n the given number is a prime number");
}
getch();
output:
enter the value of n:2
the given number is prime number
What is substr function does in c?
it will give the substring of length(as per the user) from the actual string,and the starting length from where it has to copy the substring from actual should be given by the user.
Which part of the for loop is resposible for assigning a value to a variable?
Typically, the variable is initialized in the first clause of the for statement, and incremented in the third clause. However, the language does not require any particular type of statement in these places.
The following is a typical example:
for (int i=0; i
In this example, the variable i is initialized in the first clause, and incremented in the third.
The same effect could be realized by putting the initialization and increment in other places:
int i=0; for (/* */; i++
but this would be more difficult to read.
Design of the database (Database Design) refers to a given application environment, optimize the structure of the database, the database and applications, which can efficiently store data to meet the application needs of various user information needs and processing requirements). At www. myelibrary.net.In you can clearly understand what is the database design.
Advantages of having no type in a language?
A type system is generally very useful. It prevents you from making many mistakes. For example adding 1 to "cool" would result in a garbage answer but you might do it accidentally without types.
However types are sometimes constraining and inflexible. Perhaps I really do want to add 1 to 'c' because I know exactly what new ascii value it would give me. Type systems also increase compile time, run time, or both.
Note: You may be confusing no typing with dynamic typing. With dynamic typing you generally do not label a variable with a type in its declaration. Scripting languages like python are often like this. However they remain strongly typed languages. Assembly is an example of a untyped or one type language.
One instruction in high level language corresponds to one instruction in machine language?
No. Generally, one instruction in a high level language corresponds to many instructions in machine language.
in dynamic list after adding a new element size of the list disturbed so deletion and insertation is necessary to add a new element
1. 'Explain' is not a question, but here you are:
unsigned char: a number between 0 and 255
signed char: a number between -128 and 127
unsigned short: a number between 0 and 65535
signed short: a number between -32768 and 32767
2. sizeof (type) will tell you
Yes, please do. Here is an example:
#include <stdio.h>
int main (void)
{ puts ("Hello, World"); return 0; }
Advantages of multiple inheritance in UML?
dvantages of multiple inheritances:
· Multiple inheritance allows a class to inherit the functionality of more than one base class thus allowing for modeling of complex relationships
· You categorize classes in many ways. Multiple inheritance is a way of showing our natural tendency to organize the world. During analysis, for example, we use multiple inheritance to capture the way users classify objects.
· By having multiple super-classes, your subclass has more opportunities to reuse the inherited attributes and operations of the super-classes.
Disadvantages of multiple inheritances:
· Some programming languages (such as Java) do not allow you to use multiple inheritances. You must translate multiple inheritance into single inheritance or individual java interfaces. This can be confusing and difficult to maintain because the implemented code for categorizing objects is quite different fro the way the user organizes those objects. So, when the user changes their mind or adds another category, it is difficult to figure out how to program the new sub classes.
· The more super classes your sub class inherits from the more maintenance you are likely to perform. If one of the super classes happens to change, the sub class may have to change as well.
· When a single sub class inherits the same attribute or operation form different super classes, you must choose exactly which one it must use.
Multiple inheritances can cause a lot of confusion, say when both the classes from which you want the child class to inherit from, has a method with same
Muhammad Shahbaz
Linux Administrator
Other Responses:
The Eiffel language and method was designed from the ground up to have multiple inheritance without ANY of the `gotchas' of languages like Java, C++ and others. The `disadvantages' listed above have nothing to do with MI, but with the technique and technologies listed (e.g. Java or C++). When MI is designed thoughtfully into a language and method, then the `dangers' or `disadvantages' are not there. As an Eiffel engineer, we use MI extensively, safely and beneficially all the time.
What is a library file and give examples?
A library file on a computer is the user's personal file. It could be a series of pictures downloaded to the computer, documents scanned into the computer, or a music file.
C is ideally suited to low-level programming, but has been used to write operating system kernels, device drivers, subsystems programming and applications software of all types. C++ now dominates across all these domains as it is capable of producing far more efficient machine code much more easily. However, even C++ programmers will make use of C-style programming in combination with its object-oriented and generic programming paradigms.
Declaration of two dimentional array in functions?
Possible.
void foo (void)
{
int array [10][20];
...
}
What does this mean to copy data from a temporary file to a more permanent location?
Temporary files are used a lot in computers. Most of the time, a temporary file is created when changes to a permanent file cannot be made until a certain process is complete.
A simple analogy might be painting a wall. Before you can paint the wall, anything hanging on the wall must be moved to a temporary holding area while the wall is being painted. After the painting process is complete, you move the items from their temporary location back to the permanent location - the wall that they hang on.
Computers actually follow a very similar process whenever a file is updated. Often times, the original file is copied to a temporary folder where the computer will make any changes needed to the file. When all the changes are complete, the computer might request your approval before the information is permanently stored in the permanent location where the file belongs.
Many modern word processing software programs (like Microsoft Word, Corel Wordperfect, or OpenOffice Writer) will create temporary files for new documents before the user actually saves the document to its permanent folder. This way, if the power fails suddenly, many of these programs can then check for any temporary files (such as a 30 page essay you've been working on for the past three hours) and "restore" your work. When you actually tell the program to save the file and give it a file name, the file is then moved to its permanent location.
What are the requirements to be a heavy machine operator?
There are a number of requirements to be a heavy machine operator. Depending on the type of machinery that is being operated, firstly a good level of health is required as the place of work could require some minimal lifting and carrying of smaller goods, in addition to this a college or apprenticship scheme may need to have been completed as will a commercial drivers license test that sometimes has a fee attatched to it.
How do you write a c program for reader writer problem?
/* program to study Reader/Writer problem with Semaphore along with output*/
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
void reader1();
void writer1();
void reader2();
void writer2();
sem_t rsem;
sem_t wsem;
sem_t x,y,z;
int rc=0, wc=0,p=0;
int main()
{
int r,w,i=0,ch=2;
pthread_t t1[5],t2[5];
printf ("\n\nEnter the number of READERS: ");
scanf ("%d", &r);
printf ("\n\nEnter the number of WRITERS: ");
scanf ("%d", &w);
printf ("\n\nMENU\n\n\t1.READERS HAVE PRIORITY");
printf ("\n\n\t2.WRITER HAVE PRIORITY");
printf ("\n\n\tENTER YOUR CHOICE:");
scanf ("%d", &ch);
sem_init(&rsem, 0, 1);
sem_init(&wsem, 0, 1);
sem_init(&x, 0, 1);
sem_init(&y, 0, 1);
sem_init(&z, 0, 1);
if (ch==1)
{
for (i=0;i<r;i++)
pthread_create(&t1[i], NULL, (void *)reader1, (void *)i );
for (i=0; i<w; i++)
pthread_create(&t2[i], NULL, (void *)writer1, (void *)i);
//pthread_create(&t1[w-1], NULL, (void *)reader1, (void *)(w-1) );
//pthread_create(&t2[r-1], NULL, (void *)writer1, (void *)(r-1) );
for (i=0;i<r;i++)
pthread_join(t1[i], NULL);
for (i=0;i<w;i++)
pthread_join(t2[i], NULL);
}
else if (ch==2)
{
//pthread_create(&t2[0], NULL, (void *)writer2, 0);
//pthread_create(&t1[0], NULL, (void *)reader2, 0);
for (i=0;i<r;i++)
pthread_create(&t2[i], NULL, (void *)reader2, (void *)i );
for (i=1;i<w;i++)
pthread_create(&t1[i], NULL, (void *)writer2, (void *)i );
//pthread_create (&t2[r-1], NULL, (void *)writer2, (void *)(r-1) );
//pthread_create (&t1[w-1], NULL, (void *)reader2, (void *)(w-1) );
for(i=0; i<r; i++)
pthread_join(t1[i], NULL);
for(i=0; i<w; i++)
pthread_join(t2[i], NULL);
}
else
printf ("\nINVALID CHOICE....PROGRAM TERMINATED.");
sem_destroy(&rsem);
sem_destroy(&wsem);
return 0;
} // main function close
void reader1 (void *i)
{ int i1;
for(i1=0;i1<10;i1++)
{
sem_wait(&x);
rc++;
if (rc==1)
sem_wait(&wsem);
sem_post(&x);
printf ("\n\t \t \t \tREADER %d IS READING NOW %d", (int)i,p);
sem_wait(&x);
rc--;
if(rc==0)
sem_post(&wsem);
sem_post(&x);
sleep(2);
//printf ("\nREADER %d HAS FINISHED READING\n");
}
}
void writer1(void *i)
{ int i1;
for(i1=0;i1<10;i1++)
{ sem_wait(&wsem);
p++;
printf ("\nWRITER %d IS WRITING NOW %d", (int) i,p);
sem_post(&wsem);
if ((int)i % 2 == 0)sleep(1); else sleep(6);
// printf ("\nWRITER %d HAS FINISHED WRITING\n");
}
}
void reader2(void *i)
{
sem_wait(&z);
sem_wait(&rsem);
sem_wait(&x);
rc++;
if(rc==1)
sem_wait(&wsem);
printf ("\nREADER %d IS READING NOW", (int)i);
sem_post(&x);
sem_post(&rsem);
sem_post(&z);
sleep(1);
sem_wait(&x);
rc--;
if (rc==0)
sem_post(&wsem);
sem_post(&x);
//printf ("\nREADER %d HAS FINISHED READING\n");
}
void writer2(void *i)
{
sem_wait(&y);
wc++;
if (wc==1)
sem_wait(&rsem);
sem_post(&y);
sem_wait(&wsem);
printf ("\nWRITER %d IS WRITING NOW", (int) i);
sem_post(&wsem);
sem_wait(&y);
wc--;
if(wc==0)
sem_post(&rsem);
sem_post(&y);
sleep(1);
//printf ("\nWRITER %d HAS FINISHED WRITING\n");
}
/*
student@student-desktop:~/Documents$ ./rdwrt
Enter the number of READERS: 4
Enter the number of WRITERS: 4
MENU
1.READERS HAVE PRIORITY
2.WRITER HAVE PRIORITY
ENTER YOUR CHOICE:1
READER 0 IS READING NOW
READER 1 IS READING NOW
READER 2 IS READING NOW
READER 1 IS READING NOW
READER 2 IS READING NOW
READER 3 IS READING NOW
READER 1 HAS FINISHED READING
READER 0 HAS FINISHED READING
READER 1 HAS FINISHED READING
READER 2 HAS FINISHED READING
READER 2 HAS FINISHED READING
READER 3 HAS FINISHED READING
WRITER 0 IS WRITING NOW
WRITER 0 HAS FINISHED WRITING
WRITER 1 IS WRITING NOW
WRITER 1 HAS FINISHED WRITING
WRITER 2 IS WRITING NOW
WRITER 2 HAS FINISHED WRITING
WRITER 3 IS WRITING NOW
WRITER 3 HAS FINISHED WRITING
student@student-desktop:~/Documents$ ./rdwrt
Enter the number of READERS: 4
Enter the number of WRITERS: 4
MENU
1.READERS HAVE PRIORITY
2.WRITER HAVE PRIORITY
ENTER YOUR CHOICE:2
WRITER 0 IS WRITING NOW
WRITER 0 HAS FINISHED WRITING
WRITER 1 IS WRITING NOW
WRITER 1 HAS FINISHED WRITING
WRITER 2 IS WRITING NOW
WRITER 2 HAS FINISHED WRITING
WRITER 3 IS WRITING NOW
WRITER 3 HAS FINISHED WRITING
READER 1 IS READING NOW
READER 0 IS READING NOW
READER 2 IS READING NOW
READER 3 IS READING NOW
READER 2 HAS FINISHED READING
READER 1 HAS FINISHED READING
READER 0 HAS FINISHED READING
READER 3 HAS FINISHED READING
*/
Written by: Fabianski Benjamin
Why a for loop is more appropriate here than a while loop?
There is insufficient information in the question to properly answer it. You did not provide the example "here". Please restate the question.
What are the two major types of programming languages in c plus plus?
Object oriented programming and structured programming.
Print your name ten times on the screen using a do-while loop?
#include<stdio.h> #include<conio.h> void main() { clrscr(); int x=0; do { printf("Kaushal"); printf("\n"); x++; }while(x<10); getch(); }
Write a C program to find the sum of all prime numbers in an array?
Borrowing the isPrime function from another answer of mine. Note that this will result in terrible performance for large arrays of numbers. (You should look into one of the sieve algorithms for this)
// returns 1 if n is prime, 0 otherwise
void isPrime(const int n) {
// We know that if n is composite, then we will find a factor in the range [2,sqrt(n)]
// so we compute the square root only once to limit our number of calculations.
const int sqrt_n = sqrt(n);
// Iterate through possible factors
int i;
for( i = 2; i <= sqrt_n; ++i ) {
// If n is divisible by i (n%i==0) then we have a factor
if(!(n % i)) {
return 0;
}
}
// If we get here, we know n has no factors other than itself and 1
return 1;
}
// returns the sum of all prime numbers in nums
int findPrimeSum(const int numsLength, const int[] nums) {
int sum = 0;
// iterate through nums and add up all the primes
int i;
for(i = 0; i < numsLength; ++i) {
if( isPrime(nums[i] ) {
sum += nums[i];
}
}
return sum;
}
How do you display the square root symbol on the output screen using Borland C Plus Plus?
Its Unicode value is 221A according to System tool Character map Advanced view Unicode subrange Math operators.
But I haven't done C in awhile, so I don't know how to or if you can.
ASCII value of root symbol is 251. In C we can print this symbol by printing the character value as below
printf("%c",251);
this will print the root symbol