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

Write a cpp program to convert entered date into words?

#include<iostream>

#include<sstream>

#include<chrono>

#include<ctime>

std::string months[12] { "January", "February", "March", "April", "May", "June", "July",

"August", "September", "October", "November", "December"};

class date

{

public:

date (std::string = "");

date (size_t dd, size_t mm, size_t yyyy);

date (const date& dt): d{dt.d}, m{dt.m}, y{dt.y} {}

date (date&& dt): d{dt.d}, m{dt.m}, y{dt.y} {}

date& operator= (const date& dt) { d = {dt.d}, m = {dt.m}, y = {dt.y}; return *this; }

date& operator= (date&& dt) { d = {dt.d}, m = {dt.m}, y = {dt.y}; return *this; }

static bool is_leap_year (size_t year);

static date today ();

static bool validate (size_t dd, size_t mm, size_t& yyyy);

size_t day() const { return d; }

size_t month() const { return m; }

size_t year() const { return y; }

operator std::string () const;

private:

size_t d;

size_t m;

size_t y;

};

bool date::is_leap_year (size_t year)

{

if (!(year%4)) return false;

if (!(year%100)) return true;

if (!(year%400)) return false;

return true;

}

date date::today()

{

time_t tt = time (nullptr);

struct tm *tm = localtime (&tt);

return date (tm->tm_mday, tm->tm_mon+1, tm->tm_year+1900);

}

bool date::validate (size_t dd, size_t mm, size_t& yyyy)

{

// There was no year zero!

if (!yyyy)

return false;

// There is no day zero!

if (!dd)

return false;

// Check day and month combinations.

switch (mm)

{

case 2:

if (dd > 29 (dd > 28 && !date::is_leap_year (yyyy)))

return false;

break;

case 1: case 3: case 5: case 7: case 8: case 10: case 12:

if (dd > 31)

return false;

break;

case 4: case 6: case 9: case 11:

if (dd > 30)

return false;

break;

default:

return false;

}

// 10 dates were skipped during the switch from Julian to Gregorian

if (yyyy==1582 && mm==10 && dd>4 && dd<15)

return false;

// The date is valid!

return true;

}

date::date (size_t dd, size_t mm, size_t yyyy): d {dd}, m {mm}, y {yyyy}

{

if (!validate (dd, mm, yyyy))

throw std::range_error ("date (size_t,size_t,size_t) - invalid date values!");

}

date::date (std::string ddmmyyyy)

{

if (!ddmmyyyy.size())

{

date dd (today());

d = dd.d;

m = dd.m;

y = dd.y;

return;

}

const std::string error {"date(std::string) - invalid argument!"};

const std::string valid {"0123456789\"};

// check for invalid characters

if (ddmmyyyy.find_first_not_of (valid) != ddmmyyyy.npos)

throw std::range_error (error);

// locate first slash

size_t s1 = ddmmyyyy.find('\\');

if (!s1 s1 ddmmyyyy.npos)

throw std::range_error (error);

// ensure no more slashes

size_t s3 = ddmmyyyy.find ('\\', s2+1);

if (s3 != ddmmyyyy.npos)

throw std::range_error (error);

// parse string

std::string sd = ddmmyyyy.substr (0, s1);

std::string sm = ddmmyyyy.substr (s1+1, s2-s1-1);

std::string sy = ddmmyyyy.substr (s2+1, ddmmyyyy.size()-s2);

std::stringstream ss;

ss << sd << " " << sm << " " << sy;

size_t dd, mm, yyyy;

ss >> dd;

ss >> mm;

ss >> yyyy;

if (!validate(dd,mm,yyyy))

throw std::range_error (error);

d = dd;

m = mm;

y = yyyy;

}

date::operator std::string () const

{

std::stringstream ss;

ss << d << '\\' << m << '\\' << y;

return ss.str();

}

int main()

{

date d;

std::string input;

while (true)

{

std::cout << "Enter a date (dd\\mm\\yyyy): ";

std::cin >> input;

try

{

date t {input};

d = t;

break;

}

catch (std::range_error& e)

{

std::cerr << e.what() << std::endl;

}

}

std::cout << "Date: " << d.day();

switch (d.day() % 10)

{

case (1): std::cout << "st"; break;

case (2): std::cout << "nd"; break;

case (3): std::cout << "rd"; break;

default: std::cout << "th"; break;

}

std::cout << " " << months[d.month()-1] << ", " << d.year() << std::endl;

}

How remove error unable to open the file stdio.h in c?

Reinstall C or update the C library function . stdio.h must be missing

What does allocate means?

Allocation is a set amount of (something), often money or goods. It's something you might get, and I might get, but we all get the same amount of it.

The school set a $250.00 uniform allocation per student.

Why write programs in machine code?

The resulting programs are more efficient, use less processor cycles and memory. Because michine code is the most basic form of language that a processor can understand. It consists of 1's and 0's. It needs no interpreter to interpret to the processor and therefore is the fastest way to tell a processor which commands to run.

C program to check a for loop syntax?

//PROGRAM TO CHECK SYNTAX OF for STATEMENT

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<process.h>

void main()

{

FILE *fp1,*fp2;

char a[100],ch,b[50];

int col=0,i=0,bra1=0,bra2=0,flag=0;

clrscr();

fp1=fopen("c:\\file1.txt","w");

if(fp1==NULL)

{

printf("\n ERROR IN OPENING FILE ... ");

getch();

exit(0);

}

else

{

printf("\n ENTER THE for STATEMENT ... ");

gets(a);

fprintf(fp1,"%s",a);

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

b[i]=a[i];

b[i]=0;

if(strcmp(b,"for")!=0)

{

printf("\n ERROR: UNDEFINED SYMBOL for ");

getch();

exit(0);

}

if(a[i]=='(')

flag=1;

else

flag=0;

fclose(fp1);

fp1=fopen("c:\\file1.txt","r");

while(1)

{

ch=getc(fp1);

if(ch==EOF)

break;

else if(ch=='(')

bra1++;

else if(ch==')')

bra2++;

else if(ch==';')

col++;

}

if(flag==0)

printf("\n ERROR: UNDEFINED SYMBOL for ");

else if(bra1!=1)

printf("\n ERROR: BRACKET ERROR '(' ");

else if(bra2!=1)

printf("\n ERROR: BRACKET ERROR ')' ");

else if(col!=2)

printf("\n ERROR: SEMICOLON ERROR ';' ");

else

printf("\n NO ERROR: ");

}

fclose(fp1);

getch();

}

What is Realloc?

This involves allocating and de-allocating memory at run-time. Look into the new and delete operators for C++ or malloc and free for C.

How can you calculate your age using turbo c?

It will be something like this:

int birthyear = 2001; /* replace it with your birthyear */

int age = 2011 - birthyear;

Example of semantic errors?

scores = [51, 47, 53, 97, 27]

summation = 0.0

for score in scores:

summation *= score # Semantic Error.

average = summation / len(scores)

print average # Isn't average. average == 0

What are unconditional statements?

An unconditional statement is one which would happen always (unconditionally).

Conditional statement:

if(x > 5)

print "Hello!"

Unconditional statement:

print "Hello!"

This term also comes up when speaking of assembly instructions. An unconditional jump would be a line of assembly which always executes the jump. This is in contrast to a conditional jump (also known as a branch) which will execute the jump only if some statement evaluates to true.

Why c is called partially an OOP language?

C is not an OOP language, period. However, while C++ supports OOP it does not rely on it. With C++, you can mix procedural, structured and object-oriented principals by mixing C++ code with C-style code and even raw assembly routines, neither of which are object-oriented.

What is the strcpy function?

The strcpy function is declared in the <string.h> header of the C standard library. The function is used to copy a null-terminated string (a null-terminated array of type char). The function accepts two arguments: a pointer to the memory allocation where the copy will be placed; and a pointer to the first character of the null-terminated string to be copied. The memory allocation where the copy will be made must be large enough to accommodate the string, including the null-terminator.

Example usage:

void f (char* s) {

int len;

char* c;

len = strlen (s) + 1; /* determine length of string (including null-terminator) */

c = malloc (len); /* allocate memory for copy */

strcpy (c, s);

/* ... */

free (c); /* release allocation */

}

What is the difference between int and int32?

The size (and value-range) of int is platform-dependent, whilst that of int32_t is fixed.

C program to check whether a given matrix is orthogonal or not?

#include<iostream>

#include<stdio.h>

#include<conio.h>

using namespace std;

int main()

{

int a[20][20],b[20][20],c[20][20],i,j,k,m,n,f;

cout << "Input row and column of A matrix \n\n";

cin >> n >> m;

cout << "\n\nInput A - matrix \n\n";

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

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

cin >> a[i][j];

cout << "\n\nMatrix A : \n\n";

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

{

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

cout << a[i][j] << " ";

cout << "\n\n";

}

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

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

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

cout << "\n\nTranspose of matrix A is : \n\n";

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

{

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

cout << b[i][j] << " ";

cout << "\n\n";

}

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

{

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

c[i][j]=0;

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

c[i][j]+=a[i][k]*b[k][j];

}

}

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

{

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

{

if((int)c[i][i]==1&&(int)c[i][j]==0)

f=1;

}

}

cout<<"\n\n Matrix A * transpose of A \n\n";

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

{

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

cout << c[i][j];

cout << "\n\n";

}

if(f==1)

cout << "\n\nMatrix A is Orthogonal !!!";

else

cout << "\n\nMatrix A is NOT Orthogonal !!!";

getch();

return 0;

}

-ALOK

What is an interrupt and how are multiple interrupts dealt with?

An Interrupt is a signal that goes into a microprocessor that tells it something has happened that needs attention. There are generally dedicated pins on the microprocessor, often called "Int" (for Interrupt) and "NMI" (for Non-Maskable Interrupt). For a microprocessor, an interrupt signal is like the bell on a telephone is for you; it's a notice that you should stop what you are doing now and deal with this issue that has come up.

Exact procedures for dealing with an interrupt vary from one microprocessor to another; generally, the microprocessor puts out a signal that says "Where should I go, then?" and a piece of hardware, the Interrupt Controller, then responds with a signal that tells it which condition has happened. The processor then starts processing the indicated piece of code, and that piece of code handles the condition.

The Interrupt Controller often handles setting priority for interrupts, accepting a number of signals (often four), and setting priorities on each. It will trigger another interrupt in the middle of processing one if the new interrupt is a higher priority than the one that is already being processed, or will hold on to the lower priority one until the CPU is finished with a higher-priority one.

The CPU can often "disable interrupts" when it is doing something time-critical. At such times, the only interrupt that can occur is the Non-Maskable Interrupt, which is generally reserved for critical error conditions that have to be dealt with immediately no matter what else is going on.

What are the uses of array?

Array in most programming langauges like C ,C++, Java etc is a static , homogeneous and linear data structure. A data structure itself is a mathematical and logical model to organise data.It clearly specifies the domain of the data that can be stored in the structure and the collection of valid operations (functions) that can be performed on that data. Array is a linear structure in the sense that the data is organised sequentially(one after another in a contineous chunck of memory). It is a homogeneous data structure i.e. it contains elements which are of the same data type.The data type of array can be inbuilt like int , float ,char, double and even user defined that is created using a structure or a class. The main use of an array is to oragnise homogeneous data together as a group to perform operations on data in a collective manner since traversal and retrieval becomes easy.Whenever a homogeneous list(One Dimendional or Multi-Dimensional)has to be implemented we make use of array.The advantage of array lies in the fact that it uses indexed represenattion enabling us to access any member element directly. The example of practical use of array in progarmming will be creation of index lists, pointer arrays,matrix operations(transpose,addition ,subtraction,multiplication,inverse etc.).