Write a program for addition of to array in c program?
#include
#include
void main()
{ int a[10][10],b[10][10],c[10][10],i,j.r,c;
printf("\nNo. of Rows?");
scanf("%d",&r);
printf("\nNo. of Cols?");
scanf("%d",&c);
printf("\nEnter elements for 1st array:");
for(i=0;i
printf("\nEnter elements for 2nd array:");
for(i=0;i
for(i=0;i
printf("\nDisplaying the result:\n");
for(i=0;i
}
getch();
}
If the ans helps you,plz increase the trust point.
How can you allocate memory dynamically in c?
char* new_string; // could be any type
new_string = (char*) malloc (5120); // allocate memory - typecast is necessary
if (new_string == NULL) ... memory exception ...
... use the data ...
free (new_string); // release memory when done
How do you reverse the order of words in a sentence in c?
#include <string.h>
char * revsentence(char *str)
{
int i,j,len;
char temp;
len=0;
len=strlen(str)
for(i=0;j=len-1;i<j;i++,j--)
{
temp=str[i];
str[j]=str[i];
str[j]=temp;
}
return str;
}
How do you push and pop stack elements?
algorithm of push
if(top==Max-1)
{
cout<<"\n the stack is full";
}
else
top++;
arr[top]=item;
////////////////////////////////////////
algorithm of pop
if(top==-1)
{
cout<<"\n the stack is empty";
}
else
return arr[top];
top--;
}
Write a c program using dynamic memory allocation to transpose a matrix?
Type your answervoid main()
{
int **a,**b,**c;
//int c[3][3];
int a_r,a_c,b_r,b_c;
int i,j,k;
clrscr();
again:
printf("\nenter rows and columns for matrix one:");
scanf("d",&a_r,&a_c);
printf("\nenter rows and columns for matrix two:");
scanf("d",&b_r,&b_c);
if(a_c!=b_r )
{
printf("\ncan not multiply");
goto again;
}
/* allocate memory for matrix one */
a=(int **) malloc(sizeof(int *),a_r);
for( i=0;i {
a[i]=(int *) malloc(sizeof(int*)*a_c);
}
/* allocate memory for matrix two */
b=(int **) malloc(sizeof(int)*b_r);
for( i=0;i {
b[i]=(int *) malloc(sizeof(int*)*b_c);
}
/* allocate memory for sum matrix */
c=(int **) malloc(sizeof(int *)*a_r);
for( i=0;i {
c[i]=(int *) malloc(sizeof(int)*b_c);
}
printf("\n enter matrix one %d by %d\n",a_r,a_c);
for(i=0;i {
for(j=0;j {
scanf("%d",&a[i][j]);
}
}
printf("\n enter matrix two %d by %d\n",b_r,b_c);
for(i=0;i {
for(j=0;j {
scanf("%d",&b[i][j]);
}
}
/*initialize product matrix */
for(i=0;i {
for(j=0;j {
c[i][j]=0;
}
}
/* multiply matrix one and matrix two */
for(i=0;i {
for(j=0;j {
for(k=0;k {
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
/* display result */
printf("\n Product of matrix one and two is\n");
for(i=0;i {
for(j=0;j {
printf("%d\t",c[i][j]);
}
printf("\n");
}
/*free memory*/
for(i=0;i {
free(a[i]);
}
free(a);
for(i=0;i {
free(b[i]);
}
free(b);
for(i=0;i {
free(c[i]);
}
free(c);
printf("\npress any key");
getch();
}
I would suggest you go to
http://code.freefeast.info/matrix-multiplication-using-pointers-in-c-dynamic-matrix-multiplication-in-c/
It has got a well formatted and commented code for this problemWrite a C program to calculate the sum of squares of numbers from 1 to N?
#include <iostream>
using namespace std;
int main()
{
int i,sum; // variables
sum = 0; // initialize sum
/* recursive addition of squares */
for (i = 1; i <= 30; i++)
sum = sum + (i * i);
cout << sum <<" is the sum of the first 30 squares."
<< endl;
return 0;
}
Does every programming language have a compiler?
Operating systems (OSs) are precompiled into low-level executable code that can operate efficiently. An OS distribution might include one or more language compilers, but they aren't needed to run the OS.
High-level programming language intepreted?
EZtrieve and EZtrieve Plus are examples of a high level language that can be either compiled or run interpretive.
What is recursion in c explain with the help of an example?
Recursion is the technique of a function calling itself, rather than iterating through the use of loops. The classic example of a recursive function is the computation of N Factorial...
int nfact (int n) if (n 5, for instance, it will call itself an additional 3 times, so you will have 4 stack frames where the local value of n is 5, 4, 3, and 2. When you get to the last call, you unwind the stack frames, and the multiplication of 2, 3, 4, and 5 takes place.
This example is for illustration only. It fails miserably with even small values of n (13, using 32-bit arithmetic) due to integer overflow, because N Factorial get large quickly.
How does a 'string' type string differ from a c-type string?
A char is a single character. A String is a collection of characters. It may be empty (zero characters), have one character, two character, or many characters - even a fairly long text.
The single quote (') is used to deliniate a character during assignment:
char someChar = 'a';
The double quote (") is used to delineate a string during assignment:
String someString = new String("hello there");
Note that char is a primitive data type in Java, while String is an Object. You CANNOT directly assign a char to a String (or vice versa). There is, however, a Character object that wraps the char primitive type, and Java allows method calls to be made on the char primitive (automagically converting the char to Character before doing so).
i.e. these ALL FAIL:
someString = SomeChar;
someString = new String(someChar);
However, these WILL work:
someString = Character.toString(someChar);
someString = someChar.toString();
Also note that a String is a static memory allocation, while a character's is dynamic. By that, I mean that when a String is created, it is allocated a memory location exactly big enough to fit the assigned string. Any change to that String forces and entirely new memory location to be allocated, the contents of the old String copied in (with the appropriate changes), and the old String object subject to garbage collection. Thus, making changes to a String object are quite inefficient (if you want that kind of behaviour, use StringBuffer instead).
A character is allocated but once; all subsequent changes to a character variable simply overwrite that same memory location, so frequent changes to a character variable incur no real penalty.
Distinguish Between Static and Dynamic variable in C programming?
Global (file scope) variable and static global variables both retain their value for the duration of the program's execution. Static global variables are visible only to functions within the file they are declared, while global variables are visible to all compilation units (files) within the linked load module.
Differences among sequential access direct access?
Let's say you have a set of 100 pieces of data, which are all names. Now, if you want to find a specific name, "Kevin", you can find it in different ways. You could either go through each of the records one after another, or you could randomly generate a number, and check if that record is "Kevin", this is potentially faster than sequential access as "Kevin" could be the last record.
How are pointer different from other variables?
A pointer variable is a variable that contains the memory location of another variable or an array (or anything else in memory). Effectively, it points to another memory location.
For standard variables, you define a type, assign a value to that variable or read the value from it, and you can also read the memory location (&n = memory location of n) of the variable.
For pointers, you can point them to any variable, even another pointer, and you can get the value of the variable it points to (*p), the location of that variable in memory (p), or the address in memory of the pointer itself (&p).
Consider:
long n = 65;
long *p;
p = &n;
Results:
Type | Name | Location | Value
long n 0xA3FF 65
long * p 0x31DF 0xA3FF
So p points to n. Now,
n = 65
&n = 0xA3FF
p = 0xA3FF
*p = 65
&p = 0x31DF
You may find yourself having to use typecasts frequently when using pointers.
Pointers are useful when passing data to functions.
For instance, consider the following function:
void add(int a, int b, int c) { c = a + b; }
The problem here is that the computer copies each variable into a new memory location before passing them to the function as local variables. This function effectively does nothing. However, if you change the function to:
void add(int a, int b, int *c) { c = a + b; }
and call the function by passing in the location of the variable to the function:
add(a,b,&c);
then you can modify the variable itself.
Pointers are also good for working with arrays:
char *c = "Hello World";
int i=0;
while (c[i] != 0x00) { cout << c[i]; c++ } //print one letter at a time.
Is sizeof an operator or function why?
You cannot overload the sizeof() operator because that could introduce uncertainty in its evaluation. The sizeof() operator must always produce an accurate and logically predictable result, thus all user-intervention is completely forbidden.
What is the difference between modular programming and structured programming.?
Modular programming:It is the act of designing and writing programs as interactions among functions that each perform a single well defined function,& which have minimal side effect interaction between them.It is heavily procedural.The focus is entirely on writing code(functions). Data is passive.Any code may access the contents of any data structured passed to it.
Object Oriented programming:it is a programming paradigm using "objects"-data structures consisting of data fields & methods together with their interactions-to design applications and computer programs.programming techniques may include features such as data abstraction,encapsulation,messaging,modularity,polymorphism and inheritance.
Recursive function to find nth number of the Fibonacci series?
STEP1. Set value of count=1, output=1, T1=0, T2=1
STEP2. Read value of n
STEP3. Print output
STEP4. Calculate
output=T1+T2
STEP5. T1=T2 & T2=output
STEP6. Calculate count= count+1
STEP7. If (count<=n>
go to STEP3
else
go to STEP8
STEP8. End
You could also just plug in n into this formula:
F(n) = [φ^n - (1-φ)^n] / sqrt(5)
φ is about 1.618033989 and is the Golden Ratio
[It's also the limit as n approaches infinity of the nth term in the Fibonacci sequence divided by the (n-1)th term]
How do you generate Fibonacci series in c programming language?
#include<stdio.h>
#include<conio.h>
void main()
{
int a=-1,b=1,c=0,i,n;
clrscr()
printf("Enter the limit");
scanf(%d,&n)
printf(the resultant fibonacci sequence is:)
for(i=1;i<=n;i++)
{
c=a+b;
printf(%d, c)
a=b;
b=c;
}
getch();
}
Program for token separation in c language?
/* Write a program to identify and generate the tokens present in the given input */
/* Token Separation */
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<iostream.h>
int key = 0;
char expr[100];
char cont[][20]={"CONTROLS","for","do","while","NULL",};
char cond[][20]={"CONDITION","if","then","NULL"};
char oprt[][20]={"OPERATOR","+","-","*","/","%","<","<=",">",">=","=","(",")","NULL"};
char branch[][20]={"BRANCHING","goto","jump" ,"NULL"};
void checking(char[],char[][20]);
void main()
{
int i,j,l,k,m,n;
char sbexpr[50],txt[3];
clrscr();
cout<<"Enter the expression:";
gets(expr);
for(i=0;expr[i]!=NULL;i++)
{
key=0;
for(j=i,k=0;expr[j]!=32 && expr[j]!=NULL;i++,j++,k++)
sbexpr[k]=expr[j];
sbexpr[k]=NULL;
if(key==0) checking(sbexpr,cond);
if(key==0) checking(sbexpr,cont);
if(key==0) checking(sbexpr,branch);
if(key==0)
{
for(m=0;sbexpr[m]!=NULL;m++)
{
key=0;
txt[0]= sbexpr[m];
txt[1] = NULL;
if(key==0) checking(txt,oprt);
if((key==0) ((sbexpr[m]>=97 && sbexpr[m]<=122) (sbexpr[m]>=65 && sbexpr[m]<=90)))
{
cout<<"\n"<<sbexpr[m]<<"------->"<<"Identifier\n";
key = 1;
}
}
}
if(key == 0)
{
cout<<"\n"<<sbexpr<<"------->"<<"Address\n";
key = 1;
}
}
getch();
}
void checking (char expr[],char check[][20])
{
for(int i=1;strcmp(check[i],"NULL")!=0;i++)
{
if(strcmp(expr,check[i])==0)
{
cout<<expr<<"------>"<<check[0]<<"\n";
key = 1;
}
}
}
In general, parsing is when you take a large chunk of data and break it down into smaller, more useful chunks.
When a compiler or interpreter is turning the source code of a programming language into executable code, it must first parse that source code so it knows what statements the program is trying to use. It can then use that information to translate your source into computer-understandable machine code.
Writing in pseudo code means writing in a natural language, not in any specific programming language, so there is no thing as "pseudo-code used in C" as opposed to "pseudo-code used in Java".
When you write in pseudo-code, you don't have to follow any specific syntactic rules, just to describe the steps you will use in your algorithm.
For example, pseudo-code for bubble sort (taken from wikipedia):
procedure bubbleSort( A : list of sortable items ) do swapped = false for each i in 1 tolength(A) - 1 inclusive do: if A[i-1] > A[i] then swap( A[i-1], A[i] ) swapped = true end ifend for while swapped end procedure
It is not written in any programming language, but it should be easy to implement this in any language after you understand the idea from the pseudo-code.
Application of binary tree in data structure?
1) the complexity of insertion,deletion and searching operation is depend on the height of the tree.
i.e. if height is n(for skew binary tree) then complexity is O(n) .
2) difficult to get the sorted list from the binary tree.which is easy for BST.
Command line arguments are provided at the time of running the program. Example: Suppose that your program needs input name and its value then running it from commandline(DOS prompt) you provide the values after the program name java xyz name JAX(name is name and value is jax)
Write a C program to swap 2 numbers using 3rd variable?
#include<stdio.h>
main()
{
int a,b;
printf("enter the value for a and b\n");
scanf("%d %d",&a,&b);
display(a,b);
}
display(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
printf("%d %d",x,y);
}
How do you write a c plus plus program to find area of a triangle where base4 and height6?
pi x radius x radius the forula for working out the area of a circle if this is what you're asking.
========================================================
Please give the Proper Answer...