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

How do you determine class width?

Not sure what you mean by class width, but if you mean how do you determine the size of a class, the simplest way is to use the sizeof() operator. The value returned may be equal to or greater than the sum of all its member variables, depending on any adjustments made for memory alignment. If the class contains pointers to memory allocated on the heap, this memory will not be included in the total -- only the size of the pointers to those allocations will be considered.

EDIT: Previous answer does not address the question.

#include<stdio.h>

main()

{

int n,m,i,max;

printf("How many numbers(n) you going to enter:");

scanf("%d",&n);

printf("Enter the numbers:");

scanf("%d",&m);

max=m;

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

{

scanf("%d",&m);

if(m>max)

max=m;

}

printf("The Largest Number is %d",max);

}

Output:

How many numbers(n) you going to enter:5

Enter the numbers:

25

410

362

5

56

The Largest Number is 410

How do you execute c program in terminal?

If you have it compiled and linked, then simply:

$ ./myprogram

if you haven't, then compile and link it first:

$ gcc -g -W -Wall -pedantic -o myprogram myprogram.c(other sources, objects, libraries)

$ ./myprogram

Why gets function gives a warning every time you compile a c program?

It is unsafe. In order to use gets() safely, you need to know how many characters you will be reading to ensure your character buffer is large enough:

char buffer[10];

while (gets (buffer) != 0) { ...process buffer... }

The above code has undefined behaviour when the number of characters read is 10 or more (you need one character for the null-terminator). This is because the character buffer, str, decays to a pointer (referencing &str[0]) and the function, gets(), cannot determine the number of characters in a buffer by its pointer alone.

The gets() function was dropped from the C standard in 2011, however some implementations still include it. To avoid the warning, use the fgets() function instead. This allows you to specify the length of your buffer and (when used correctly) prevents buffer overflow.

char buffer[10];

while (fgets (buffer, 10, stdin) != 0) { ...process buffer... }

How do you do a username and password login program using C-language?

For a username, just get the username from the standard input using scanf and check whether this username matches with the username stored. If it doesn't match, then the username is wrong.

For a password, get each character using getch(). Print a * for each character obtained. Now check this password with the stored password. If these don't match, then the password is wrong.

What is size of far pointer?

In real mode far pointers are 32 bit long (segment + offset)

In protected mode 48 bit (16 bit segment + 32 bit offset)

(In 64-bit mode 80 bit (16 nit segment + 64 bit offset) but it's not so useful)

Determine the highest of the three input numbers using flowchart?

(start)

/a=0 c=0\

\b=0 /

/input a/

/input b/

/input c/

/a>b\ no /b>c\ yes /display b/ -> (a)

\ / \ /

yes no

/a>c\ no /display c/ -> (a)

\ /

yes

/display a/

<- (a)

(end)

Is missing statement an empty statement that does nothing?

empty statement does nothing, 'missing statement' is an error-message from the compiler, eg: { if (x==2) }

corrected version: { if (x==2); }

How do you convert a List to a Tuple?

It makes no sense to convert a list to a tuple given that tuples are specifically intended for collections of heterogeneous types whilst a list is a collection of homogeneous types. If the intent is simply to reduce memory consumption, then converting the list to an array would achieve the exact same result as converting to a tuple.

What is code migration?

Code migration is the movement of programming code from one system to another. There are three distinct levels of code migration with increasing complexity, cost and risk. Simple migration involves the movement from language to a newer version. A second, more complicated level of migration involves moving to a different programming language. Migrating to an entirely new platform or operating system is the most complex type of migration.

How to write a program that accepts two file names as command line arguments?

int main(int argc, char** argv) { char *argument1, *argument2; if (argc < 2) ...{exception}... argument1 = argv[1]; argument2 = argv[2]; } Note: It is up to you to decide if the arguments actually refer to valid filenames. Also, if the filenames contain spaces, this example will not work - you would need to write code to parse (or use library routines) and support quoted filenames.

Print 1-10 numbers using while loop?

public static void main(String args){

int counter = 0; //initialize the counter variable to 0

while (counter < 10){ //while the counter variable is less than 10...

counter ++; //increase the counter variable by 1

System.out.println(counter); //print the counter variable

}

}

How can you write a c program to find maximum element in row and minimum element in column of a matrix?

#include <stdio.h>

main()

{

int m, n, c, d, A[10][10],temp=0;

printf("\nEnter the number of rows and columns for matrix A:\n");

scanf("%d%d", &m, &n);

printf("\nEnter the elements of matrix A:\n");

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

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

scanf("%d", &A[c][d]);

printf("\nMatrix entered is:\n");

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

{

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

printf("%d\t", A[c][d]);

printf("\n");

}

printf("\n\nMaximum element of each row is:\n");

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

{

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

{

if(A[c][d]>temp)

temp=A[c][d];

}

printf("\n\t\tRow %d: %d",c+1,temp); temp=0;

}

temp=A[0][0];

printf("\n\nMinimum element of each coloumn is:\n");

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

{

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

{

if(A[d][c]<temp)

temp=A[d][c];

}

printf("\n\t\tColoumn %d: %d",c+1,temp); temp=A[d][c] ;

}

return 0;

}

What is mini project?

mini project is a self made ideal project by using any language.

C plus plus program of periodic table?

The Periodic Table Of Elements has a well defined order. The layout can be emulated easily since the arrangement is quite logical.

The first step would be to define a struct containing the following members as a minimum:

- atomic number (int - number of protons in its nucleus)

- symbol (char* - short form of the element name)

- name (char* - long form of the element name)

i.e.:

struct elementinfo {

int atomicnumber;

char *symbol, *name;

};

Other members can be added as your program develops.

The next step is to arrange the table itself. If you're using Win32 or another graphical system, it's a matter of drawing a box (Win32 would require a MoveToEx() call and four LineTo() calls) and TextOut() (or a related function) for the atomic number and element symbol (centered horizontally and aligned top and bottom respectively).

Including conio.h or curses.h would give you the ability to position the cursor and even change the text color, allowing for an alternate "graphical" method.

To keep things simple, storing the elements in an array would require something akin to the following:

struct elementinfo elementlist[]={

{1, "H", "Helium"}, {0, NULL, NULL}, {0, NULL, NULL},

{0, NULL, NULL}, {0, NULL, NULL}, {0, NULL, NULL},

{0, NULL, NULL}, {0, NULL, NULL}, {0, NULL, NULL},

{0, NULL, NULL}, {0, NULL, NULL}, {0, NULL, NULL},

{0, NULL, NULL}, {0, NULL, NULL}, {0, NULL, NULL},

{0, NULL, NULL}, {0, NULL, NULL}, {2, "He", "Helium"},

{-1, NULL, NULL},

{3, "Li", "Lithium"}, {4, "Be", "Beryllium"}, {0, NULL, NULL},

{0, NULL, NULL}, {0, NULL, NULL}, {0, NULL, NULL},

{0, NULL, NULL}, {0, NULL, NULL}, {0, NULL, NULL},

{0, NULL, NULL}, {0, NULL, NULL}, {0, NULL, NULL},

{5, "B", "Boron"}, {6, "C", "Carbon"}, {7, "N", "Nitrogen"},

{8, "O", "Oxygen"}, {9, "F", "Fluorine"}, {10, "Ne", "Neon"},

{-1, NULL, NULL},

...

{-2, NULL, NULL}

};

In the above array, {0, NULL, NULL} represents a blank displayed for that particular cell, and {-1, NULL, NULL} represents a newline. The {-2, NULL, NULL} signifies the end of the table. The following for() loop would wrap around your display code like so:

for (count=0; elementlist[count].atomicnumber!=-2; count++) {

if elementlist[count].atomicnumber==-1) {

// jump to next line of elements

}

else {

// display current element

}

}

Drawing this graphically, you'd have to keep track of the current cursor (X, Y) position.

If you are sending this to stdout or another text stream (i.e. text file), you could draw each line, referencing the array of elements as you go. The list of elements would have to be stored in a nested array: the outermost array contains each line of elements in an array. This would do away with the {-1, NULL, NULL} terminating each line of elements.

Extending this code to use classes would be relatively simple, but might only make sense if you were drawing this graphically storing each element as an object.

The Lanthanides and Actinides, since they're displayed separately from the main table, would probably have to be stored separately for sake of convenience.

Also, as laboratories continue to synthesize (or, on the rare chance, discover) new elements, the layout of the table may change (even drastically) to suit. Thus, the code would have to be altered accordingly.

See the related links below for more ideas on how to design a program that displays the Periodic Table of Elements.

(Note: Code originally posted was copyrighted. Added to related links.)

Examples of while loop?

// Infinite while loop

while(true) {

System.out.println("Still looping...");

}

// More useful while loop to move through all elements in a Queue

Object currentItem;

while((currentItem = queue.poll()) != null) {

System.out.println(currentItem);

}

Write an interactive c plus plus program to create a linear linked list of customer names their telephone numbers?

#include<list>

#include<string>

struct Customer

{

std::string m_name;

std::string m_phone;

Customer (std::string name, std::string phone): m_name(name), m_phone(phone) {}

// ...

};

int main()

{

std::list<Customer> customers;

customers.push_back ("Joe Bloggs", "555 7465");

customers.push_back ("Dan Mann", "555 3458");

// ...

}

What is the lifetime of local variables in a function?

If the variable is local to the function it exists until the function returns.