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

Algorithm for implementing push operation on stack?

1. If TOP = MAXSTK THEN [Stack already filled?]

Print "OVERFLOW"

Go to step 4

Endif

2. TOP = TOP + 1 [Increase TOP by 1]

3. Set ST[STOP] = ITEM [Insert ITEM in new TOP position]

4. End

What is borland c plus plus?

Borland C++ was a C++ development environment originally released by the Borland Software Corporation but is now owned by Embarcadero Technologies who purchased the Borland CodeGear division in 2008.

It is now formally known as Embarcadero C++ Builder, or informally as just C++ Builder. At the time of writing, the latest stable release is C++ Builder XE2.

Write a program in qbasic to print the multiplication table of an input number?

This is a homework assignment. write homework assignments for you because you need to do this yourself or you will not learn the skills that the assignment is trying to teach you.

However if, while trying to do your assignment, you find a specific problem that you need help with, WikiAnswers will help you with these specific questions (e.g is this 'xxxxx' qbasic statement correct).

====

Both the above examples can be adjusted to print out the 15 x tables/or else, the 15 times tables square.

HINT:

A> In the first case the user types in the number 15; then, presses Enter.

B> In the 2nd case you simply change both tablesNo%=/timesNo%= to say 15 instead of 12.

What are the differences between Bresenham's line algorithm and Bresenham's circle algorithm?

These two algorithms are almost completely different. The only real similarity is that they are each designed to use only integer addition/subtraction and multiplication, avoiding expensive division and floating point operations.

What is 2's complement of 10002?

Surprise: it is -10002.

(If you wanted to ask 1000(2), then it is 11111000(2))

How do you write a program to find magic numbers?

#include<stdio.h>

unsigned sum_row (unsigned* sq, const unsigned width, const unsigned row) {

unsigned sum, col;

sum = 0;

for (col=0; col<width; ++col)

sum += sq[row*width+col];

return sum;

}

unsigned sum_col (unsigned* sq, const unsigned width, const unsigned col) {

unsigned sum, row;

sum = 0;

for (row=0; row<width; ++row)

sum += sq[row*width+col];

return sum;

}

unsigned sum_diag (unsigned* sq, const unsigned width) {

unsigned sum, row, col;

sum = 0;

for (row=0, col=0; row<width; ++row, ++col)

sum += sq[row*width+col];

return sum;

}

unsigned sum_anti (unsigned* sq, const unsigned width) {

unsigned sum, row, col;

sum = 0;

for (row=0, col=width-1; row<width; ++row, --col)

sum += sq[row*width+col];

return sum;

}

bool is_magic (unsigned* sq, const unsigned width) {

unsigned magic, row, col;

magic = sum_row (sq, width, 0);

for (row=1; row<width; ++row)

if (magic!=sum_row(sq, width, row))

return false;

for (col=0; col<width; ++col)

if (magic!=sum_col(sq, width, col))

return false;

if (magic!=sum_diag(sq, width))

return false;

if (magic!=sum_anti(sq, width))

return false;

return true;

}

int main () {

const unsigned width = 3;

unsigned a[width][width] {{2,7,6},{9,5,1},{4,3,8}};

unsigned row, col;

printf ("Square:\n\n");

for (row=0; row<width; ++row) {

for (col=0; col<width; ++col) {

printf ("%d ", a[row][col]);

}

printf ("\n");

}

printf ("\n");

if (is_magic((unsigned*)&a, width))

printf ("The square is magic with a magic constant of %d\n", sum_row((unsigned*)&a, 3,0));

else

printf ("The square is not magic\n");

return 0;

}

Binary search using pointer in c?

#include
#include
#include
void main()
{
int a[10]={1,2,3,10,11,13,16,21,46,73};
int mid,lower=0,upper=9,num,flag=1;
clrscr();
printf("enter the number to search");
scanf("%d",&num);
printf("\n the list of the data is");
printf("%-20s","\ndata);
for(num=0;num<=upper;num++)
printf("%3d",a[num]);
printf("%-20s","\nindexno");
for(num=0;num<=upper;num++)
printf("%3d",num);
for(mid=(lower+upper)/2;lower<=upper;mid=(lower+upper)/2)
{
if(a[mid]==num)
{
printf("the number is at position %d in array",mid);
flag=0;
break;
}
if(a[mid]>num)
upper=mid-1;
else
lower=mid+1;
}
if(flag)
printf("element is not present in the array");
getch();
}

What is vector and array processing?

An important part of IDL is the ability to work with data that is organized as vectors and arrays. We will first describe vectors and arrays and then show some tools for constructing them. Finally, we will demonstrate some of their uses. In addition to arrays of numbers, which we will describe here, there are also arrays of strings, structures and objects. You can look up information about them in IDL help if you are eager to find out about them now.

Arrays are especially important in signal image processing. We will find that images are just arrays of values with one value for each pixel. Vectors are important in the representation of one-dimensional functions such as functions of time or one spatial dimension.

Vectors

A vector is a list of data items, and can be constructed with an assignment statement like

A=[1.2, 3.1, 4.6, 5.7]

Note that square brackets are used to contain the list. Try typing a list like that shown above and then using HELP,A to get information about the vector.

Vectors can contain any of the number types that are accepted by IDL. When the assignment is made all of the numbers are converted to one type. The type that is used is determined by the data type hierarchy. Try typing

A=[1, 3B, 4L, 5.7] & HELP,A

IDL responds that A is an array of type FLOAT of length 4. If you were to type

A=[1, 3B, 4L, 5] & HELP,A

you would find that A is now of type LONG. The rule is to convert all of the numbers to the highest number type.

Arrays

We can think of a vector as a list that has one dimension. It is a row of data. An array is a list that is arranged in multiple dimensions. A two-dimensional array is a vector of vectors that are all of the same length.

C=[[1,2,3,4],[5,6,7,8],[7,6,5,4]] & PRINT,C

1 2 3 4

5 6 7 8

7 6 5 4

We see that A has three rows and four columns and that each row is one of the vectors in the list. An array can contain any of the IDL data types. When it is created all of the numbers are converted to the highest number type. We will describe a number of IDL functions for the construction of arrays. By using these functions one can construct arrays of up to eight dimensions.

Indexing

Column and Row Index

It is important to be able to refer to particular elements of an array. An element in column 1 and row 2 can be referred to by C[1,2]. In the above array we would find that C[1,2]=6. In IDL the column index comes before the row index.

Rows and columns are indexed beginning with the number 0. The column indexes of C are 0, 1, 2, 3 and the row indexes are 0, 1, 2. Zero-referenced indexing is a convention that may seem strange at first, but it has programming advantages.

One can refer to an entire row by using an asterisk in the column position. C[*,r] refers to the vector in row r. Try the command PRINT,C[*,2].

One can refer to an entire column by using an asterisk in the row position. C[col,*] refers to the vector in column col. Try the command PRINT,C[3,*].

A part of a row or column can be indexed by providing a list of the elements that are wanted. Try the following

row =[0,1,2] & col = [3,2,1] & PRINT,C[col,row]

Can you explain what is printed?

A contiguous segment of column or row elements can be referred to with the notation a:b. For example, C[0:2,2] refers to the elements in positions 0,1,2 in row 2. Try the command PRINT,C[0:2,2]

Array Index

Every element in an array has an array index. The array index is found by counting each element from the beginning of the array to the end row by row. An array that has four columns and three rows would have the indexes as shown below.

0 1 2 3

4 5 6 7

8 9 10 11

Any element in the array can be referred to by its array index. A list of elements in positions 0, 6 and 8 can be printed by

k=[0,6,8] & C[k]

One can find the value of array index from the row and column indexes. The number of columns, n, has to be known. Then k is simply

k = col + row*n

If k is given then the column and row indexes can be found by integer operations:

row = k/n

col = k - row*n

For an array with n=4 columns, C[6]=C[2,1]. You should try converting in both directions.

Array Creation Tools

IDL provides a number of functions that can be used to create arrays and initialize their values. The list can be found in the section "Array Creation Routines" in IDL Online Help. A subset of these routines are listed below.

BINDGEN Return a byte array with each element set to its subscript.

BYTARRCreate a byte vector or array.

FINDGEN Return a floating-point array with each element set to its subscript.

FLTARRReturn a floating-point vector or array.

INDGEN Return an integer array with each element set to its subscript.

INTARRReturn an integer vector or array.

LINDGEN Return a longword integer array with each element set to its subscript.

LONARRReturn a longword integer vector or array.

IDENTITY Return an identity array.

MAKE_ARRAYGeneral purpose array creation.

REPLICATEForm array of given dimensions filled with a value.

The functions BYTARR, FLTARR, INTARR, LONARR create arrays with of the type indicated by the first letter of the name and with their initial values set to zero. You can then enter data into them by assignment statements. For example, create an integer array of size 3x4 and set the elements of the first row to the value 3.

A=INDGEN(3,4)

A[*,0]=3

PRINT,A

The functions BINDGEN, INDGEN, FINDGEN, LINDGEN create arrays of the type indicated by the first letter of the name and with the initial values set to the array index. For example, create an array of type byte of size 3x4 and set its initial value to the index.

B=BINDGEN(3,4)

PRINT,B

0 1 2

3 4 5

6 7 8

9 10 11

The functions that generate index arrays are very useful in creating a vector of independent variables to be used in calculating and plotting a function. Suppose that you needed to plot the function (1+t2)Cos(5t) over the interval [0,3] in steps of size 0.01. You will then need a vector of 301 values t=[0, .01, .02, ..., 2.99, 3]. You certainly would not want to type all this in. The statements that will do the calculation and plot a graph are given below.

t=FINDGEN(301)/100 ;Generates the desired vector

f=(1+t^2)*cos(5*t) ;Calculates the function

plot,t,f ;Draws the graph

The IDENTITY function returns a square array of type float with the elements on the main diagonal set to 1 and all others set to zero. This function is very useful in many linear algebra computations.

I=IDENTITY(4)

PRINT,I

The REPLICATE function makes an array of the specified size in which a give value placed in all of the positions. To make a 5x2 array in which all of the elements contain the value 3.1 one can use

C=REPLICATE(3.1,5,2)

PRINT,C

The function MAKE_ARRAY is the most general array creation tool. It has many options which are best described by the online help documentation and the reference manual.

When to use inheritance in c plus plus?

You use inheritance whenever you need a more specialised version of an existing class (the base class). Rather than creating a new class entirely from scratch, and therefore duplicating tried and tested code, you simply build upon the existing class, overriding the existing methods and adding more specialised methods, whilst retaining all the generic functionality of the base class.

Complexity of an algorithm in data structure?

* search array => O(1) linked list=> O(n) binary tree=> O(log n) hash=>O(1) * search array => O(1) linked list=> O(n) binary tree=> O(log n) hash=>O(1)

How do you insert a node using doubly linked list in c plus plus?

#include<iostream>

#include<list>

void print_list (std::list<int>& list)

{

for (std::list<int>::const_iterator it=list.begin(); it!=list.end(); ++it)

std::cout << *it << ", ";

std::cout << "\b\b \n";

}

int main()

{

// instantiate a list

std::list<int> list;

// push a new value onto the back of the list:

list.push_back (1);

print_list (list);

// push a new value onto the front of the list:

list.push_front (2);

print_list (list);

// insert a new value at the back of the list

list.insert (list.end(), 3);

print_list (list);

// insert a new value at the front of the list

list.insert (list.begin(), 4);

print_list (list);

// locate value 1.

std::list<int>::const_iterator it=list.begin();

while (it!=list.end() && *it != 1)

++it;

// insert a new value in front of value 1.

list.insert (it, 5);

print_list (list);

}

Output:

1

2, 1

2, 1, 3

4, 2, 1, 3

4, 2, 5, 1, 3

Which is easier 'C' or 'C plus plus'?

Opinion 1:

I think both are easy to use an manipulate although i have never written a c++ program other than hello world but to know c++ you have to know c and c is a standard high level language which is easy to grasp

Opinion 2:

C++ is easier in the end because it is a revised version of the old c language. It is not required to learn C first before learning C++ because you pick up bad habits and when you move to C++, those habits will be hard to get rid of. C++ is a lot more powerful and accurate, it is harder at first but with time it will be a lot easier and more organized than c.

In conclusion, C is easier to learn but not recommended to learn.

I highly recommend people to go straight to C++ to pick up only good habits, then go back to C if you're curious how it works.

What are the advantages of using a foreach loop over a for or while loop for processing list elements?

foreach can simply replace for loop. for ex in perl, if you need to copy display an array, it will take only foreach $var(@arr). then print $var. no need of incrementing the index of array like for loop.

Which of the following data structure is non liner type?

A non-linear data structure is one in which the elements are not arranged or linked in a linear fashion. Trees, graphs, etc. are non-linear data structure since the elements are arranged in a branching manner.

Write a program to find the sum of squares of the given number?

#include<stdio.h>

int main()

{

int count,i,j,k,n,*a,sum=0;

printf("Enter the value of 'n':");

scanf("%d",&n);

a=malloc(n*sizeof(int));

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

{

count=0;

j=1;

while(j<=i)

{

if(i%j==0 && i!=2)

count++;

j++;

}

if(count==2 count==1)

{

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

a[k]=i;

}

}

for(k=0;k<=n;k=k+1)

printf("%d\t",a[k]);

for(k=0;k<=n;k=k+2)

sum+=a[k] * a[k];

printf("The sum of squares of alternative prime numbers is=%d",sum);

getchar();

return 0;

}

Write a program in c to print a mark sheet?

import java.util.*;

import java.lang.String;

import java.io.*;

import java.util.Date;

import java.util.Timer;

class project

{

public static void main(String args[]) throws IOException

{

String sub1,sub2,sub3,sub4,sub5,sub6;

String name,sem;

int m1,m2,m3,m4,m5,m6,m7;

//Date dt = new Date(now.getTime()

int prn_no,seat_no,total;

float per;

float temp;

String msg="*";

long currentTimeInMillis = System.currentTimeMillis();

Date today = new Date( currentTimeInMillis );

Scanner in=new Scanner(System.in);

BufferedReader b=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter name of student=");

name=b.readLine();

System.out.println("Enter prn number =");

prn_no=in.nextInt();

System.out.println("Enter name of semester=");

sem=b.readLine();

System.out.println("Enter Roll number =");

seat_no=in.nextInt();

System.out.println("Enter first subject name =");

sub1=b.readLine();

System.out.println("Enter marks =");

m1=in.nextInt();

System.out.println("Enter second subject name =");

sub2=b.readLine();

System.out.println("Enter marks =");

m2=in.nextInt();

System.out.println("Enter third subject name =");

sub3=b.readLine();

System.out.println("Enter marks =");

m3=in.nextInt();

System.out.println("Enter fourth subject name =");

sub4=b.readLine();

System.out.println("Enter marks =");

m4=in.nextInt();

System.out.println("Enter fifth subject name =");

sub5=b.readLine();

System.out.println("Enter marks =");

m5=in.nextInt();

System.out.println("\t\t\t\t\t\t DR.BABASAHEB AMBEDKAR MARATHWADA UNIVERSITY");

System.out.println("\t\t\t\t\t\t\tAurangabad-431004");

System.out.println("STUDENT NAME:"+name+"\t\t\tprn :"+prn_no+"\t\t\tSEAT NO:"+seat_no+"\t\t\tSEMESTER:"+sem);

System.out.println("------------------------------------------------------------------------------------");

System.out.println("\n\n| SUBJECTS"+"\t\t\t\t|\tMAXIMUM MARKS"+"\t|\t min marks"+"\t|\tOBTAINED MARKS");

System.out.println("------------------------------------------------------------------------------------");

if(m1>40)

msg="";

else

msg="*";

System.out.println("|"+sub1+"\t\t\t\t\t\t|\t100\t\t\t\t|\t40\t\t\t|\t"+m1);

if(m2>40)

msg="";

else

msg="*";

System.out.println("|"+sub2+"\t\t\t\t\t\t|\t100\t\t\t\t|\t40\t\t\t|\t"+m2);

//System.out.println(+sub2+"\t\t\t"+"|"+"\t\t100"+"\t\t\t"+"|"+"\t\t40"+"\t\t\t\t"+"|"+"\t\t"m2+msg+"\t|");

if(m3>40)

msg="";

else

msg="*";

System.out.println("|"+sub3+"\t\t\t\t\t\t|\t100\t\t\t\t|\t40\t\t\t|\t"+m3);

//System.out.println(+sub3+"\t\t\t"+"|"+"\t\t100"+"\t\t\t"+"|"+"\t\t40"+"\t\t\t\t"+"|"+"\t\t"m3+msg+"\t|");

if(m4>40)

msg="";

else

msg="*";

System.out.println("|"+sub4+"\t\t\t\t\t\t|\t100\t\t\t\t|\t40\t\t\t|\t"+m4);

//System.out.println(+sub4+"\t\t\t"+"|"+"\t\t100"+"\t\t\t"+"|"+"\t\t40"+"\t\t\t\t"+"|"+"\t\t"m4+msg+"\t|");

if(m5>40)

msg="";

else

msg="*";

System.out.println("|"+sub5+"\t\t\t\t\t\t|\t100\t\t\t\t|\t40\t\t\t|\t"+m5);

//System.out.println(+sub5+"\t\t\t"+"|"+"\t\t100"+"\t\t\t"+"|"+"\t\t40"+"\t\t\t\t"+"|"+"\t\t"m5+msg+"\t|");

System.out.println("------------------------------------------------------------------------------------");

total=m1+m2+m3+m4+m5;

System.out.println("\t\t\t\t\t\t\t\t\t\t\t\t\t"+"|"+"\t\t TOTAL ::"+total+"/500");

temp=(float)total/500;

per=temp*100;

System.out.println("\t\t\t\t\t\t\t\t\t\t\t\t\t"+"|"+"\t\t PERCENTAGE ::"+per+"%");

System.out.println("------------------------------------------------------------------------------------");

if(m1<40&&m2<40&&m3<40&&m4<40&&m5<40)

{

System.out.println("\t\t\t\t\t\t\t\t\t\t\t\t\t"+"|"+"\t\t RESULT ::DROPPED");

System.out.println("------------------------------------------------------------------------------------");

}

else if(m1<40&&m2<40m3<40&&m4<40&&m5<40)

{

System.out.println("\t\t\t\t\t\t\t\t\t\t\t\t\t"+"|"+"\t\t RESULT ::A.T.K.T.");

}

else if(per<35)

{

System.out.println("\t\t\t\t\t\t\t\t\t\t\t\t\t"+"|"+"\t\t RESULT ::FAILED");

}

else if(per>=35&&per<40)

{

System.out.println("\t\t\t\t\t\t\t\t\t\t\t\t\t"+"|"+"\t\t RESULT ::passed");

}

else if(per>=40&&per<59)

{

System.out.println("\t\t\t\t\t\t\t\t\t\t\t\t\t"+"|"+"\t\t RESULT :::passed with SECOND CLASS");

}

else if(per>=60&&per<75)

{

System.out.println("\t\t\t\t\t\t\t\t\t\t\t\t\t"+"|"+"\t\t RESULT :passed with first class");

}

else if(per>=75)

{

System.out.println("\t\t\t\t\t\t\t\t\t\t\t\t\t"+"|"+"\t\t RESULT :passed with DISTINCTION class");

}

System.out.println( today );

}

}

What is the relationship between high level language code and low level language code?

All types of programming languages have one property in common: all languages, ultimately and in some manner, lead to machine instructions upon which the processor operates.

Some higher programming languages, especially those of a traditional design such as the C programming language, might generate code in a low level language (i.e. assembly language) as an intermediate step during compilation. However, most modern designs do not implement or expose this as an explicit step, and transcoding of higher to lower level languages is certainly not required.

The fact that all programming languages lead to executed machine code, however, does not mean that all language lead to a translation which results in executable code; interpreted languages, which include popular modern languages such as Java and the .NET family of languages, will generally execute machine code which interprets the language instructions, while compiled languages (which includes most forms of C) generally generate directly executable machine code.

Program to find sum of two numbers in VB.NET language?

Option Explicit

Dim a as integer

Dim b as integer

Dim c as integer

Dim result as integer

a=val(text1.text)

b=val(text2.text)

c=val(text3.text)

result=a+b+c

'now we want to push the result to another text box ,below code insert into the button click event

text4.text=result

text1.text=""

text2.text=""

text3.text=""

Rename the cmd button

As sum.

And run the program by pressing F5 key.

Static variable in c plus plus?

There are two uses for a static variable in C++. When declared outside of a class, a variable is regarded as being global. However a static variable is deemed local to the file in which it is declared. That is, the variable is scoped to the file, and cannot be accessed by code outside of that file. This aspect was inherited from C.

C++ also allows static variables to be declared inside a class. In this case, the variable is local to the class. By contrast, instance variables (non-static member variables) are local to each instance of the class. With static variables, there is only one instance of each variable which can be shared by all instances of the class. It is not unlike a global but it is scoped to the class.

Since all static variables are instantiated at compile time, they exist for the entire duration a program runs. Even if they fall from scope, they never lose their value. Static variables defined within a class are also available even when no instances of the class are instantiated. Their visibility outside of the class is dependent upon whether they are declared public, protected or private.