answersLogoWhite

0

📱

C Programming

Questions related to the C Computer Programming Language. This ranges all the way from K&R to the most recent ANSI incarnations. C has become one of the most popular languages today, and has been used to write all sorts of things for nearly all of the modern operating systems and applications. It it a good compromise between speed, power, and complexity.

9,649 Questions

What is decision rule for ARR?

If the calculated ARR is greater that the predetermined ARR then accept the project. otherwise reject the project

Where union keyword used?

The UNION keyword is commonly used, as part of a SELECT statement in the SQL language, to combine two tables, which must have identical or at least compatible structures, vertically. That is, records from BOTH tables are placed into a single result table. If, for example, each table has 1000 records, the resulting table would have 2000 records - assuming there are no duplicates.

Does SGID allow users to execute a binary compiled program?

Not, the execute bit does that.

00100 - execute for user

00010 - execute for group

00001 - execute for others

04000 - set-uid

02000 - set-gid

What is the difference between a function and not a function?

The word non-function can mean practically anything, a variable, for example.

int fun (int x) { return x+10; }

int nonfun= 32;

C program to reverse an array elements using dynamic memory allocation?

/* Write a C program to reverse the elements of a 1-D array using pointers. */

#include<stdio.h>

#include<conio.h>

#include<process.h>

void REVERSE(int *ptr1, int *ptr2, int n);

void main()

{

int X[30], m, i;

clrscr();

printf("\n\n\n\tEnter the size of Array (Less than 30):");

scanf("%d",&m);

if ((m > 30) (m < 2))

{

printf("\n\n\t\tERROR !!!!!!");

printf("\n\t\tENTER SIZE WITHIN GIEVEN FIELDS....TRY

AGAIN.");

getch();

exit(0);

}

clrscr();

printf("\n\n\n\tEnter the elements of Array :");

for(i=0;i<m;i++)

{

printf("\nElement %d:",i+1);

scanf("%d",&X[i]);

}

REVERSE(X,X,m);

printf("\n\n\n\n\tThe Reversed Array is:\n");

for(i=0;i<m;i++)

printf("\n\t\tElement %d: %d",i+1,X[i]);

getch();

}

void REVERSE(int *ptr1, int *ptr2, int n)

{

int *temp=NULL;

int i;

for(i=0;i<n;i++,ptr2++);

for(i=0,ptr2--; i<(n/2); i++,ptr1++,ptr2--)

{

*temp=*ptr1;

*ptr1=*ptr2;

*ptr2=*temp;

}

}

/************ PROGRAM ENDS ************/

What is the C source code for uniform distribution?

The source code for one version of rand(), producing a uniform pseudo-random distribution, is ...

int rand() {

static unsigned seed = 0;

seed = seed * 0x343FD + 0x269EC3;

return seed >> 16 & 0x7FFF;

}

See below for the original answer, including this piece of code...

A uniform distribution is a sequence of events or observations that have equal, i.e. uniform distribution across their probability domain. For example, a fair six sided die has probability 0.167 of having each face show up on a roll. It is, over the long term, uniformly distributed across the discrete probability domain [1,2,3,4,5,6].

As a comparative example, two dice have a triangular distribution for their sum, said sum being in the interval [2-36], with 12 having probability 0.167, 2 and 36 having probability 0.0556, 3 and 37 having probability 0.08333, etc.

There are other distributions, such as gaussian and poisson, but the question asked about uniform, so lets go back there.

You are talking about a random number generator with a uniform, i.e. equal distribution over a certain interval. One way to do this is with a pseudo-random number generator in the run-time library called rand(). When rand() is invoked, it returns an int in the interval [0-RAND_MAX] that is reasonably uniform and random. Each time it is invoked, it returns a different value, although, after a while, the sequence repeats. The sequence of values is always the same from program run to run, but it can be initialized to another sequence by invoking srand(int) to set a new seed, such as based on the clock.

I say reasonably uniform because rand() is usually based on a linear congruential generator, which is very simple, but has defects due to sequential correlation and, if its parameters are not chosen well, limitations on range and spectral purity. Also, it might not be construed as truly random.

Randomness, however, is in the "eye of the beholder", and rand() is perfectly adequate for most applications involving gaming and basic statistical analysis. It can certainly be improved, and it can be replaced.

The source code for one version of rand() is ...

int rand() {

static unsigned seed = 0;

seed = seed * 0x343FD + 0x269EC3;

return seed >> 16 & 0x7FFF;

}

This version is not thread safe. A more practical version would maintain either a per-thread copy of the seed, or pass the address of the seed, said seed being maintained by the caller, but this example serves to show the basic algorithm.

This is a linear congruential generator based on a 32 bit unsigned seed. It has a period of 4,294,967,296, which is maximal for a 32 bit value. Only 15 bits, however, are returned, the 2nd through the 16th, so that sequential correlation issues are minimized. Over the long run, specifically 4,294,967,296 iterations, each possible value will be repeated exactly 8,589,934,591 times, in what appears to be a random sequence. It is, thus, a uniform distribution, over the interval [0-32767].

If you wanted a different interval, such as the simulation of rolling a die, you can convert to a floating interval [0-1], multiply by a number, and truncate. A die version could be ...

int die() {

return int ((double) rand() / RAND_MAX * 6) + 1;

}

This returns a uniform integer in the interval [1, 6];

C program to calculate sum and average of 4 numbers?

/*mycfiles.wordpress.com

To Calculate Sum & Average of 4 no.*/

#include<stdio.h>

#include<conio.h>

void main()

{

float a,b,c,d,sum,avg;

clrscr();

printf("Enter the 4 nos.\n\n");

scanf("%f%f%f%f",&a,&b,&c,&d);

sum=a+b+c+d;

avg=(a+b+c+d)/4;

printf("\nSum is= %f\nAverage is= %f",sum,avg);

getch();

}

Sample code of loops and repetitions?

system.out.println(" print 1-100 numbers");

for(i=0;i<=100;i++)

system.out.println(i);

o/p1

2

3

4

5

.......

C plus plus program that display student name course and grade using arrays of strings?

enum field { name, course, grade }; std::string student[3];

student[name] = "Joe Bloggs";

student[course] = "C++ Programming";

student[grade] = "A+";

Who decides size of data types in a language?

The people who create the language take the liberty of deciding the size of data types in a programming lanauage.

If you (as a programmer) create your own custom data type, for example by defining a class, then you decide what goes into it - for example, in Java, if one of the pieces of data requires an integer, you have the choice of storing it as an int, which uses 4 bytes, or as a long, which uses 8 bytes (and permits larger numbers).

How do you initialize each element of a two-dimensional array alpha to 5?

Two-dimensional arrays are typically iterated through using nested for loops. If you had a 2-D array alpha with ints ROWS and COLS representing the number of rows and columns respectively, and ints row and col as iterators, the loop would look like this:

for (row = 0; row < ROWS; row++){

for (col = 0; col < COLS; col++{

alpha[row][col] = 5;

}

}

Examples of logical errors in c program?

Logical errors :- These errors occur because of logically incorrect instructions in the program. Let us assume that in a 1000 line program, if there should be an instruction, which multiplies two numbers and is wrongly written to perform addition. This logically incorrect instruction may produce wrong results. Detecting such errors are difficult.

Can you get married with str?

Yes, you can get married with a "str," which typically refers to a "string" in programming or can be shorthand for "street." If you meant "same-sex relationships," many places now legally recognize same-sex marriages. However, the specific requirements for marriage can vary by location, so it's essential to check the local laws and regulations regarding marriage.

Write a for loop in C to print the 7 stars row?

...

int i;

for( i = 0; i < 7; ++i ) {

printf("*");

}

printf("\n");

...

Can you overload object pointers?

Overloading refers to defining multiple functions of the same name with different numbers/types of parameters. So no, you cannot overload a pointer.