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

Why and operator can not be used in string of c program?

Any character can be used in string, except for \\0.

char example [] = "A&B|C";

You are given a text file named datatxt The file contains a list of integer scores The number of scores is unknown You are required to write a C program to read ALL the scores from the?

#include

#include

#include

using std::cin;

using std::cout;

using std::endl;

using std::ifstream;

//function prototypes

int numberOfElements(ifstream& inData);

void readDataFromFile(ifstream& inData, long* dataFile, int arraySize);

int main()

{

const char fileName[] = "data.txt";

ifstream inData;

inData.open(fileName, std::ios::in);

if (inData.fail())

{

cout << endl << "Could not open the file: " << fileName << endl;

inData.close();

exit(1);

}

int numberOfLines = numberOfElements(inData);

cout << "Number of string is: " << numberOfLines << endl;

long* dataFile = new long[numberOfLines];

readDataFromFile(inData, dataFile, numberOfLines);

for (int index = 0; index < numberOfLines; index++)

{

cout << endl << *(dataFile + index) << endl;

}

delete [] dataFile;

system("PAUSE");

return 0;

} //calculate number of elemenets and return its value

int numberOfElements(ifstream& inData)

{

double data = 0.0;

int counter = 0;

while (inData >> data)

{

counter++;

}

inData.clear();

return counter;

}

//read data from file and save it in array dataFile with size arraySize

void readDataFromFile(ifstream& inData, long* dataFile, int arraySize)

{

for (int index = 0; index < arraySize; index++)

{

inData >> *(dataFile + index);

}

}

What is Min and Max Levels?

'Min' level . . . the lowest it has been since you started watching it,

or the lowest it's expected to be, or the lowest it's allowed to be

'Max' level . . . the highest it has been since you started watching it,

or the highest it's expected to be, or the highest it's allowed to be

What values are expressed in the 1948 declaration?

There were several declarations in 1948. Perhaps you are referring to the "Universal Declaration of Human Rights", from the UN General Assembly on 10 December 1948. If that is what you are looking for, then please see the Related Link below. If you are looking for something else, please re-ask the question and be more specific.

How do you get an answer calculated during a program to be displayed on the graphing screen of my calculator?

It depends on the calculator. On a TI-84 or TI-83, you can print text to the graphing screen using :Text(#,#,"jibberish",var,"jibberish",var,var) You can actually switch between constant text and displaying the value of a variable within the same "Text" function. Anything within quotations will show up exactly as you type it between the quotation marks. Separate every switch with a comma like I showed. Anything outside of the quotation marks will display the value of that variable as long as syntax is followed properly.

How do you create c program for matrix using pointer without array?

An array offers the most efficient method of implementing a matrix (an n-dimensional array). All arrays implicitly convert to pointers, so a pointer can be treated as an array even when it doesn't actually refer to one (so long as you don't access memory that doesn't actually belong to your program, of course).

If you don't want to use an array, use linked lists instead. Each node is a member of two or more lists, so each node requires at least one node pointer per list. If you require bi-directional traversal, each node requires at least two node pointers per list. For a two-dimensional, bi-directional matrix, each node requires up, down, left and right node pointers. For a three-dimensional, bi-directional matrix, each node requires forward and backward node pointers. For a generalised n-dimensional, bi-directional matrix, each node requires an array of n pairs of pointers, one pair for each dimension.

There are very few reasons to resort to linked lists to implement a matrix. For any given number of dimensions and elements, arrays will use far less memory and are much quicker to navigate than a linked list. One of the few exceptions is when you need to slice the matrix (to insert or extract rows or columns, for instance). In these cases it is generally more efficient to break or create a sequence of links than it is to move elements in order to remove or create gaps in the array, not to mention the reallocation of the entire matrix whenever an insert is required. However, this can be largely alleviated by implementing the matrix as separately allocated arrays. E.g., for a three-dimensional array of type T, use a T*** to refer to an array of type T**, where each pointer in that array refers to separately allocated arrays of type T*, where each pointer in those arrays refers to separately allocated arrays of type T. The pointer arithmetic is exactly the same as it would be if the matrix were allocate contiguously as a single block.

Another reason we might prefer a linked list implementation is when the array is sparse and largely contains no data. In this case, each node pointer refers to the nearest neighbouring node that holds data -- the non-data nodes in between can be omitted completely, thus reducing memory consumption and speeding up traversal.

Note that space and time complexities only give us a general sense of an algorithm's performance. Only a real-world performance test will tell us which algorithm is most suitable for a given task. There is no one-size-fits-all solution, hence C does not provide a built-in matrix type.

What is the exact meaning of mixed signal array?

A mixed signal array refers to a collection of electronic components or circuits that can process both analog and digital signals within a single system. These arrays are commonly used in applications like data conversion, where signals need to be converted from analog to digital formats and vice versa. The integration of mixed signal capabilities allows for more compact designs and improved performance in various electronic devices, such as sensors, communication systems, and audio equipment.

How do you convert a prefix expression to postfix using recursion?

struct stack { char ele; struct stack *next; }; void push(int); int pop(); int precedence(char); struct stack *top = NULL; int main() { char infix[20], postfix[20]; int i=0,j=0; printf("ENTER INFIX EXPRESSION: "); gets(infix); while(infix[i]!='\0') { if(isalnum(infix[i])) postfix[j++]=infix[i]; else { if(top==NULL) push(infix[i]); else { while(top!=NULL && (precedence(top->ele)>=precedence(infix[i]))) postfix[j++]=pop(); push(infix[i]); } } ++i; } while(top!=NULL) postfix[j++]=pop(); postfix[j]='\0'; puts(postfix); getchar(); return 0; } int precedence(char x) { switch(x) { case '^': return 4; case '*': case '/': return 3; case '+': case '-': return 2; default: return 0; } } void push(int x) { int item; struct stack *tmp; if(top==NULL) { top=(struct stack *)malloc(sizeof(struct stack)); top->ele=x; top->next=NULL; } else { tmp=top; top->ele=x; top->next=tmp; } } int pop() { struct stack *tmp; int item; if(top==NULL) puts("EMPTY STACK"); else if(top->next==NULL) { tmp=top; item=top->ele; top=NULL; free(tmp); } else { tmp=top; item=top->ele; top=top->next; free(tmp); } return item; }

C code to implement the the first fit best fit worst fit algorithm?

#include<stdio.h>

#include<malloc.h>

/*

filename:firstfit.c

description:this program implements first fit algorithm

Author:Anupam Biswas,NIT Allahabad,India

Date:29.08.2011

*/

#define maxp 10 //maxp:maximum no of processes

#define maxmsz 100 //maxmsz:maximum memory size

struct memory{

int bsz;//bsz:block size

int bs;//bs:block start

int be;//be:block end

int ap;//ap:allocated process

int flag;//for allocated or not

struct memory*link;

}*start,*trav,*prev;

int pq[maxp];//pq:process queue

int f=-1,r=-1;

void enqueue(int ps)

{

/*ps:process size*/

if((r+1)%maxp==f)

{

printf("\nqueue is full, no new process can be loaded");

}

else

{

r=(r+1)%maxp;

pq[r]=ps;

}

}

int dequeue()

{

int ps;

if(f==r)

{

printf("queue empty,no process left\n");

return -1;

}

else

{

f=(f+1)%maxp;

ps=pq[f];

return ps;

}

}

void first_fit(int ps,int pid)

{

/*pid:process id*/

struct memory*block;

block=(struct memory*)malloc(sizeof(struct memory));

trav=start;

/*search the memory block large enough*/

while(trav!=NULL)

{

if(trav->flag==0 && trav->bsz>=ps)

{

break;

}

prev=trav;

trav=trav->link;

}

if(trav==NULL)

{

printf("No large enough block is found\n");

}

else /*if enough memory is found*/

{

dequeue();//remove process from queue

block->bsz=ps;

block->bs=trav->bs;

block->be=trav->bs+ps-1; //anus size defininition

block->ap=pid;

block->flag=1;

block->link=trav;

trav->bsz=trav->bsz-ps;

trav->bs=block->be+1;

if(trav==start)/*if first block is large enough*/

{

start=block;

}

else

{

prev->link=block;

}

}

}

void dealocate_memory(int pid)

{

trav=start;

while(trav!=NULL && trav->ap!=pid)

{

trav=trav->link;

}

if(trav==NULL)

{

printf("This process is not in memory at all\n");

}

else

{

if(trav->link->flag==0)

{

trav->bsz=trav->bsz+trav->link->bsz; //anus link definition

trav->be=trav->link->be;

prev=trav->link;

trav->link=prev->link;

prev->link=NULL;

free(prev);

}

trav->ap=-1;

trav->flag=0;

}

}

void show_mem_status()

{

int c=1,free=0,aloc=0;

trav=start;

printf("Memory Status is::\n");

printf("Block BSZ BS BE Flag process\n");

while(trav!=NULL)

{

printf("%d %d %d %d %d %d\n",c++,trav->bsz,trav->bs,trav->be,trav->flag,trav->ap);

if(trav->flag==0)

{

free=free+trav->bsz;

}

else

{

aloc=aloc+trav->bsz;

}

trav=trav->link;

}

printf("Free memory= %d\n",free);

printf("Allocated memory= %d\n",aloc);

}

int main()

{

int ch,ps;

char cc;

/*the size of the total memory size is defined i.e. largest block or hole*/

/*......................................................................*/

start=(struct memory*)malloc(sizeof(struct memory));

start->bsz=maxmsz;

start->bs=0;

start->be=maxmsz;

start->ap=-1;

start->flag=0;

start->link=NULL;

/*......................................................................*/

while(1)

{

printf("\n\t1. Enter process in queue\n");

printf("\t2. Allocate memory to process from queue\n");

printf("\t3. Show memory staus\n");

printf("\t4. Deallocate memory of processor\n");

printf("\t5. Exit\n");

printf("Enter your choice: ");

scanf("%d",&ch);

switch(ch)

{

case 1:

do

{

printf("Enter the size of the process: ");

scanf("%d",&ps);

enqueue(ps);

printf("Do you want to enter another process(y/n)?: ");

scanf("%c",&cc);

scanf("%c",&cc);

}

while(cc=='y');

break;

case 2:

do

{

ps=pq[f+1];

if(ps!=-1)

{

first_fit(ps,f+1);

show_mem_status();

}

printf("Do you want to allocate mem to another process(y/n)?: ");

scanf("%c",&cc);

scanf("%c",&cc);

}

while(cc=='y');

break;

case 3:

show_mem_status();

break;

case 4:

do

{

printf("Enter the process Id: ");

scanf("%d",&ps);

dealocate_memory(ps);

show_mem_status();

printf("Do you want to enter another process(y/n)?: ");

scanf("%c",&cc);

scanf("%c",&cc);

}

while(cc=='y');

break;

case 5:

break;

default:

printf("\nEnter valid choice!!\n");

}

if(ch==5)

break;

}

}

Even number program?

bool is_even(long int num)

{

return !(num & 1); //when the number is even(divisible by two),

//its least significant bit is 0

}

What is the function of an accumulator?

An accumulator is a register that is a part of a processor. It has more/faster instructions than other registers. Examples:

/360: no accumulator

8080: A

6800: A and B

8086: AX

80386: EAX

x86-64: RAX
The accumulator in an automatic transmission softens the shift between gears.

Write a program to find the factorial number using function?

//C program to find the factorial of a given number using functions

#include<stdio.h>

#include<conio.h>

int fact(int);

void main()

{

int f,t;

clrscr();

printf("\nEnter any number:");

scanf("%d",&f);

t=fact(f);

printf("1=%d",t);

getch();

}

int fact(int fa)

{

int i,fac=1,t;

for(i=fa;i>=2;i--)

{

// TO print the series

printf("%dx",i);

fac=i*fac;

}

return fac;

}

What if a for loop without any executable statements in it?

Such loops are usually referred to as "null" or "empty" loops.

What are nested structures in C?

Structures can contain other structures as members; in other words, structures can nest. Consider the following two structure types:

struct first_structure_type { int member_of_1; }; struct second_structure_type { double double_member; struct first_structure_type first_struct_member; }; The first structure type is incorporated as a member of the second structure type. You can initialize a variable of the second type as follows:

struct second_structure_type second_struct_member; second_struct_member.double_member = 12345.6789; second_struct_member.first_struct_member.integer_member_of_1 = 5; The member operator . is used to access members of structures that are themselves members of a larger structure. No parentheses are needed to force a special order of evaluation; a member operator expression is simply evaluated from left to right. In principle, structures can be nested indefinitely. Ref: http://www.crasseux.com/books/ctutorial/Nested-structures.html

How do you delete a word in a file using c?

in c++ the source code is as follows........... .

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

#include<string.h>

#include<stdio.h>

void main()

{

clrscr();

int f=0;

char s[30],c[10];

ofstream of("old.txt");

cout<<"enter string\n";

cin.getline(s,20);

of<<s;

of.close();

ifstream fi("old.txt");

ofstream fo("new.txt");

while(!fi.eof())

{

fi>>c;

if(strcmp(c,"the")==0)

{

f++;

}

else

fo<<c<<" ";

}

cout<<"occ:"<<f;

remove("old.txt") ;

rename("new.txt","old.txt");

fo.close();

fi.close();

ifstream n("old.txt");

n.seekg(0,ios::beg);

cout<<"\nnew file\n";

while(!n.eof())

{

n.getline(s,20);

cout<<s;

}

n.close();

getch();

}