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 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.

What is the difference between a recursive and an explicit formula?

a recursive formula is always based on a preceding value and uses A n-1 and the formula must have a start point (an A1) also known as a seed value.

unlike recursion, explicit forms can stand alone and you can put any value into the "n" and one answer does not depend on the answer before it. we assume the "n" starts with 1 then 2 then 3 and so on

arithmetic sequence: an = a1 + d(n-1) this does not depend on a previous value

Calling a function from switch in C?

Yes, you can call a function from within a switch statement in C.

switch (i) {

case 0: function1(i); break;

case 1: function2(i); break;

default: function3(i); break;

}

When the function returns, you will still be in the switch statement.

What is difference between wait and sleep and delay in C plus plus?

It depends on the particular library implementation. It is not a C++ specific question. As far as C++ is concerned, it is just a function call, just like a call to printf or even exit is a function call.

One could be a call to wait for an event.

One could be a call to sleep for a specified period of time.

One could be a call to burn CPU cycles for a specified period of time.

Again, it all depends on the library implementation, and you need to read the documentation for your library to answer this one.

How many nodes are there in the nth of a binary tree?

If N>1, there are (2N-1) - (2N-1-1), otherwise, 1 nodes in the Nth level of a balanced binary tree.

How do you write a c program to determine if a matrix is symmetric or not?

A matrix is symmetric when it is a perfect square and the transpose of the matrix is equal to the original matrix. The implementation is reasonably straightforward and can be done without actually creating the transposed matrix. We simply imagine a dividing line through the main diagonal (top-left to bottom right) and compare the corresponding elements on each side of that diagonal.

bool is_symmetric (int* a, int rows, int cols) {

if (rows!=cols) return false;

for (int row=0, row

for (int col=row+1; col

if (a[row][col] != a[col][row]) return false;

return true;

}

This works because for every element a[row][col] in a square matrix, the corresponding element on the other side of the main diagonal must be a[col][row]. In other words, we simply transpose the subscripts (hence it is called the transpose).

Consider a 4x4 matrix of hexadecimal values:

0 1 2 3

4 5 6 7

8 9 A B

C D E F

The main diagonal line is identified as elements {0, 5, A, F}. Element {7} lies above this line and it corresponds with element {D} below the line. Element {7} is identified with the subscript [1][3] while element {D} is identified with the subscript [3][1].

Note that the inner loop which traverses the columns always starts at col=row+1. This ensures that we always start with the first element after the diagonal element in the current row, and traverse through the remaining elements of that row. In other words, elements in [row][col] are always in the upper-right of the matrix, above the main diagonal. Meanwhile, the corresponding transpose, element [col][row], lies in the lower-left of the matrix, below the main diagonal. There is no need to test the entire array, traversing every column of every row, because there's no point in testing elements on the diagonal itself (we'd be comparing each element to itself and its given that an element is always equal to itself), and there's no point in comparing the lower-left elements to the upper-right elements when we've already performed the reverse comparison. After all, if X==Y then it follows that Y==X because equality is a transitive operation (the result is the same regardless of which side of the operator we place the operands).

Note that in order to replace a square matrix with its transpose, we simply swap the corresponding elements unless the matrix is symmetrical (because, by definition, a symmetrical matrix is equal to its own transpose):

void transpose (int* a, int rows, int cols) {

if (rows!=cols is_symmetric (a, rows, cols)) return;

for (int row=0, row

for (int col=row+1; col

swap (&a[row][col], &a[col][row]);

}

Why is compiling necessary?

Compilers are only necessary for programmers; ordinary users do not require them. A compiler is a program which converts a high-level source input into a lower-level source output, reducing the amount of abstraction. In most cases the output is native machine code and/or an assembly source, however there are some exceptions, such as the Java compiler which outputs Java byte code, and cross-language compilers such as C++ to C compilers.

Machine's do not understand either high-level source code or low-level assembly code, they only understand their own native machine code. Thus conversion to machine code is necessary. Once converted to machine code, no further translation is necessary.

Assembly sources must be converted using an assembler, however assembly is a machine-dependent language (it is non-portable). High-level languages are generally portable and are either compiled or interpreted. Interpretation requires a runtime program to perform the conversion while the source code is executing and performs poorly compared to a native machine code program. Java is both compiled and interpreted. That is; Java source code compiles to Java byte code which is suitable for interpretation by the Java virtual machine.

It is not possible to compile a low-level source to produce a high-level output. For example, you cannot convert C to C++ using a compiler, just as you cannot convert assembly language to C. Compilers can only reduce the amount of abstraction in the source code, not increase it. If you want to add more abstraction, you need to do so manually.