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

Simple c program for mergesort?

#include

#include

#define MAX_ARY 10

void merge_sort(int x[], int end, int start);

int main(void) {

int ary[MAX_ARY];

int j = 0;

printf("\n\nEnter the elements to be sorted: \n");

for(j=0;j scanf("%d",&ary[j]);

/* array before mergesort */

printf("Before :");

for(j = 0; j < MAX_ARY; j++)

printf(" %d", ary[j]);

printf("\n");

merge_sort(ary, 0, MAX_ARY - 1);

/* array after mergesort */

printf("After Merge Sort :");

for(j = 0; j < MAX_ARY; j++)

printf(" %d", ary[j]);

printf("\n");

getch();

}

/* Method to implement Merge Sort*/

void merge_sort(int x[], int end, int start) {

int j = 0;

const int size = start - end + 1;

int mid = 0;

int mrg1 = 0;

int mrg2 = 0;

int executing[MAX_ARY];

if(end == start)

return;

mid = (end + start) / 2;

merge_sort(x, end, mid);

merge_sort(x, mid + 1, start);

for(j = 0; j < size; j++)

executing[j] = x[end + j];

mrg1 = 0;

mrg2 = mid - end + 1;

for(j = 0; j < size; j++) {

if(mrg2 <= start - end)

if(mrg1 <= mid - end)

if(executing[mrg1] > executing[mrg2])

x[j + end] = executing[mrg2++];

else

x[j + end] = executing[mrg1++];

else

x[j + end] = executing[mrg2++];

else

x[j + end] = executing[mrg1++];

}

}

;j++)>

What is the difference between an actual type and declared type variable?

I believe a declared type is the type used when first declaring a variable.

And the actual type is the type that the variable is actually assigned, which

could be the declared type or any subtype of that type. So given

GeometricObject shape = new Triangle();

GeometricObject is the declared type and Triangle is the actual type. To

demonstrate this more clearly the following statements are all correct.

GeometricObject shape; // shape declared to be a GeometricObject

shape = new Triangle(); // actual type assigned = Triangle

shape = new Square(); // now shape refers to a Square object

This is the basis of polymorphism. It allows you to write methods which have

for parameters more general types but are able to handle subtypes differently.

This makes your code more general and reusable.

What function enables the user to input information while the program is being executed in c?

The scanf() function is a commonly used function to input information during the execution of a program. scanf() function has its prototype in the stdio.h header file.

To accept and display a number:

int num;

printf("Enter a number: ");

scanf("%d",&num);

printf("You entered %d",num);

To accept and display a character, replace %d with %c and make num a character variable [char].

Plus: gets, fgets, read, fread, getchar, getc, fgetc, getch...

Is it possible to use a for loop in place of a while loop?

Yes.

while loop consist of only condition statement to make for loop look as while loop we can use syntax shown below:

for(;condition;)

eg:

for(;i<=n;)

What is the difference between string type and character type constants?

A character type constant is a character, a char, while a string type is an array of characters. The character type constant is one character and is delimited by single quotes. The string type constant is zero or more characters and is delimited by double quotes.

Write a program in C plus plus to store 100 element within array and find the particular element if exist?

#include<iostream>

#include<iomanip>

#include<time.h>

template<typename T>

size_t find(T& data, T a[], size_t size)

{

size_t index=0;

do {

if(a[index]==data)

break;

} while(++index<size);

return(index);

}

template<typename T>

void print(T a[], size_t size)

{

using std::cout;

using std::endl;

using std::setw;

size_t index=0;

do{

if(index&&index%20==0)

cout<<endl;

cout<<setw(3)<<a[index];

}while(++index<size);

cout<<endl;

}

int main()

{

srand((unsigned)time(NULL));

const size_t size=100;

unsigned int a[size];

size_t index=0;

do{

unsigned int data=rand()%100;

do{

data=rand()%100;

} while(find(data,a,index)<index);

a[index]=data;

} while(++index<size);

print(a,size);

}

Can an extern be paid?

Yes, an extern can be paid, though it often depends on the specific externship program and the industry. Some externships, particularly in fields like healthcare, engineering, or technology, may offer compensation to attract talent and provide valuable experience. However, many externships are unpaid, focusing instead on providing educational experiences and networking opportunities. It's essential for externs to clarify payment arrangements before beginning their positions.

Full form of basic?

The full form of computer is computer;

BASIC is (or can be seen as) short for 'Beginner's All-purpose Symbolic Instruction Code'.

How many steps are involved in writing a computer program?

It depends on the type of computer program you desire. At the bare minimum you need 3 steps in order to make a simple program. The first that is learning you compiler's language (example: JavaScript, C++, C# ect.) and next is to learn how to avoid syntax errors and then create the program that you want. For example, to make a simple execution program using command prompt you will have to learn the commands of a .bat file and then order the commands correctly in order for command prompt to execute and then let it run. Most of programming is trial and error thought the more you get acquainted with your compiler the easier and better you'll become a making your program.

How would you define Continue statement?

A continue statement is used to "branch" to the end of a loop without exiting the loop. If we wish to exit the loop entirely, use a break or return statement.

for (int i=1; i<10; ++i) {

if (i==5) continue;

printf (%d ", i);

}

printf ("\n");

The above loop will print the following:

1 2 3 4 6 7 8 9

What is the fastest way to compute the factorial of 123456 on a processor of speed 1.4 Ghz 512 mb ram and 40 gb hard disk?

i have solved this problem.....!! i dont know that my Program is fastest than other traditional algos like prime swing etc. MY computer computed factorial of 123456! in appx 14-15 mins. The output was of 5,74,965 digits. actually no computer in this world can support this big answer.

Actually i saved my ans into an array. My program is in C. If any body really interested to see the result & program logic, then contact me at ankitsingh.05@gmail.com.

Regards,

Ankit Singh

Pune

How you get a code of Radix sort?

#include<stdio.h>

void radix(int a[],int n)

{

int i,j,k=1,l,temp,b[10][n],cnt;

int max=a[0],c=0;

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

if(a[i]>max)

max=a[i];

while(max%10)

{

c++;

max/=10;

}

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

for(j=0;j<n;j++)

b[i][j]=-999;

for(l=0;l<c;l++)

{

for(j=0;j<n;j++)

{

temp=(a[j]/k)%10;

b[temp][j]=a[j];

}

k=k*10; cnt=0;

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

for(j=0;j<n;j++)

if(b[i][j]!=-999)

a[cnt++]=b[i][j];

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

for(j=0;j<n;j++)

b[i][j]=-999;

}

}

main()

{

int i,n;

printf("Enter array capacity \t: ");

scanf("%d",&n);

int a[n];

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

a[i]=rand()%100;

printf("\nArray Before Sorting : ");

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

printf("%4d",a[i]);

radix(a,n);

printf("\n\nArray After Sorting : ");

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

printf("%4d",a[i]);

printf("\n");

}

Should you capitalize the letters PS and the end of a letter?

yes you should because they stand for two words. they are not a word by them selves

What does machine language consists of?

A series of 1's and 0's which represent Instructions.

Assembly Language is a 1-to-1 representation of machine code in human readable context. Assembly Language can also contain comments and label names which get filtered out when converting assembly to machine code (compiling).

If you want to read machine language, you use a disassembler to convert machine code into assembly. Otherwise programs such as notepad or nano will show the contents of the file as gobbly-gook (random characters with no meaning).

32-bit processors parse the file into 32-bit 1's and 0's segments. Each segment gets ran and processed individually. And not all segments are meant to be ran. Some are data and get skipped over, and other instructions load and use that data for their intended purpose.

Can you list morphographs?

You can make a list of them.. of course!